graph_model.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. """Graph Model.
  2. """
  3. from enum import Enum
  4. from systemrelation import graph_model
  5. from systemrelation import systemdatautils
  6. from systemrelation import systemutils
  7. class SystemEdge:
  8. """Graph Edge."""
  9. # edge data.contain all edge element
  10. flow_direction = 0
  11. def __init__(self, system_data):
  12. self.system_data = system_data
  13. # @property
  14. # def start_vertex(self):
  15. # return self.start_vertex
  16. #
  17. # @property
  18. # def end_vertex(self):
  19. # return self.end_vertex
  20. class SystemVertex:
  21. """Graph Vertex.
  22. """
  23. vertex_type = 0
  24. def __init__(self, system_data):
  25. self.system_data = system_data
  26. def __str__(self):
  27. return str(self.system_data.source_id)
  28. class SystemGraph:
  29. """System Graph
  30. """
  31. # system edge array.
  32. system_graph_id = ''
  33. # system type Name
  34. system_name = ""
  35. def __init__(self):
  36. self.system_edges = []
  37. def add_edge(self, c1, c2, elements):
  38. edge = SystemEdge(elements)
  39. edge.start_vertex = SystemVertex(elements[0])
  40. edge.end_vertex = SystemVertex(elements[-1])
  41. self.system_edges.append(edge)
  42. return edge
  43. class FloorGraph:
  44. """FLoor Graph.
  45. May contain one or more SystemGraph.
  46. """
  47. def __init__(self, floor_id, system_name, domain):
  48. self.floor_id = floor_id
  49. self.system_name = system_name
  50. self.domain = domain
  51. floor_connectors = systemdatautils.get_connectors_data(floor_id, system_name, domain)
  52. floor_connectors = systemutils.sort_connectors(floor_connectors)
  53. floor_connectors = list(map(systemutils.add_attr_is_used, floor_connectors))
  54. self.connector_data = (floor_connectors)
  55. self.element_data = systemutils.list_to_dict(systemdatautils.get_element_data(floor_id))
  56. self.system_graphs = []
  57. def get_floor_graphs(self):
  58. """Get current floor graph.
  59. system_name,domain
  60. """
  61. for connector in self.connector_data:
  62. system_graph = graph_model.SystemGraph()
  63. graph_next_connectors = [connector]
  64. # dept-first
  65. for connector in graph_next_connectors:
  66. self.get_path(connector, system_graph, graph_next_connectors)
  67. if len(system_graph.system_edges) != 0:
  68. self.system_graphs.append(system_graph)
  69. print("Next loop:",connector.source_id)
  70. return self.system_graphs
  71. def get_path(self, connector, system_graph, graph_next_connectors):
  72. """
  73. Get Graph_Edge start with connector,end with triple ,cross,equipment.
  74. :param connector:
  75. :return:Graph_Edge
  76. """
  77. system_edge = None
  78. if (connector.is_used):
  79. return system_edge
  80. try:
  81. #Add other side to be Spreaded
  82. graph_next_connectors.extend(self.get_other_connectors(connector))
  83. edge_elements = [self.get_element(connector.belong)]
  84. current_connector = connector
  85. while (current_connector is not None):
  86. current_connector.is_used = True
  87. connecteds = self.get_connecteds(current_connector.connect_id_json)
  88. if len(connecteds) > 1:
  89. raise Exception("connected to many.connector_id:", current_connector.connect_id_json)
  90. #Open Connector
  91. if len(connecteds) == 0:
  92. if current_connector != connector:
  93. system_edge = system_graph.add_edge(connector, current_connector, edge_elements)
  94. break
  95. next_connector = connecteds[0]
  96. if next_connector is not None and next_connector.domain == connector.domain and next_connector.mep_system_type == connector.mep_system_type:
  97. next_element = self.get_element(next_connector.belong)
  98. edge_elements.append(next_element)
  99. other_connectors = self.get_other_connectors(next_connector)
  100. if systemutils.is_break_condition(next_element) or len(other_connectors) != 1:
  101. print(next_connector.source_id)
  102. next_connector.is_used = True
  103. if(len(other_connectors) != 1):
  104. graph_next_connectors.extend(other_connectors)
  105. system_edge = system_graph.add_edge(connector, next_connector, edge_elements)
  106. break
  107. if len(other_connectors)==1:
  108. next_connector.is_used = True
  109. current_connector = other_connectors[0]
  110. continue
  111. current_connector = None
  112. except Exception as e:
  113. print(e)
  114. return system_edge
  115. def get_other_connectors(self, use_connector):
  116. """
  117. Get the connector owner other connectors.
  118. belong,domian,mep_system_type same.
  119. is_used==False.
  120. :param use_connector:
  121. :return:
  122. """
  123. connectors = [connector for connector in self.connector_data if
  124. connector != use_connector and connector.belong == use_connector.belong and connector.domain == use_connector.domain and connector.mep_system_type == use_connector.mep_system_type]
  125. connectors = [connector for connector in connectors if bool(1 - connector.is_used)]
  126. return connectors
  127. def get_connecteds(self, connetor_id):
  128. """
  129. Get Ref Connector. may have two.like system logic connector
  130. :param connetor_id:
  131. :return:
  132. """
  133. connectors = list(self.get_connector(id) for id in connetor_id)
  134. return connectors
  135. def get_connector(self, connector_id):
  136. """
  137. Get Connector by Id.
  138. :param connector_id:
  139. :return:
  140. """
  141. cons = list(c for c in self.connector_data if c.id == connector_id)
  142. return cons[0]
  143. def get_element(self, element_id):
  144. """
  145. Get Element by Id.
  146. :param eleemnt_id:
  147. :return:
  148. """
  149. return self.element_data[element_id]
  150. class SystemFlowDirection(Enum):
  151. """Edge Flow Direction.
  152. """
  153. flow_in = 1
  154. flow_out = -1
  155. flow_undefine = 0
  156. class SystemVertexType(Enum):
  157. """Vertex Type.
  158. """
  159. real_point = 0
  160. imaginary_point = 1
  161. hollow_dot = 2