graph_model.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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, project_id,model_id, system_name, domain):
  48. self.project_id=project_id
  49. self.model_id = model_id
  50. self.system_name = system_name
  51. self.domain = domain
  52. floor_connectors = systemdatautils.get_connectors_data(model_id, system_name, domain)
  53. floor_connectors = systemutils.sort_connectors(floor_connectors)
  54. floor_connectors = list(map(systemutils.add_attr_is_used, floor_connectors))
  55. self.connector_data = (floor_connectors)
  56. self.element_data = systemutils.list_to_dict(systemdatautils.get_element_data(model_id))
  57. self.system_graphs = []
  58. def get_floor_graphs(self):
  59. """Get current floor graph.
  60. system_name,domain
  61. """
  62. for connector in self.connector_data:
  63. system_graph = graph_model.SystemGraph()
  64. graph_next_connectors = [connector]
  65. # dept-first
  66. for connector in graph_next_connectors:
  67. self.get_path(connector, system_graph, graph_next_connectors)
  68. if len(system_graph.system_edges) != 0:
  69. self.system_graphs.append(system_graph)
  70. print("Next loop:",connector.source_id)
  71. return self.system_graphs
  72. def get_path(self, connector, system_graph, graph_next_connectors):
  73. """
  74. Get Graph_Edge start with connector,end with triple ,cross,equipment.
  75. :param connector:
  76. :return:Graph_Edge
  77. """
  78. system_edge = None
  79. if (connector.is_used):
  80. return system_edge
  81. try:
  82. #Add other side to be Spreaded
  83. graph_next_connectors.extend(self.get_other_connectors(connector))
  84. edge_elements = [self.get_element(connector.belong)]
  85. current_connector = connector
  86. while (current_connector is not None):
  87. current_connector.is_used = True
  88. connecteds = self.get_connecteds(current_connector.connect_id_json)
  89. if len(connecteds) > 1:
  90. raise Exception("connected to many.connector_id:", current_connector.connect_id_json)
  91. #Open Connector
  92. if len(connecteds) == 0:
  93. if current_connector != connector:
  94. system_edge = system_graph.add_edge(connector, current_connector, edge_elements)
  95. break
  96. next_connector = connecteds[0]
  97. if next_connector is not None and next_connector.domain == connector.domain and next_connector.mep_system_type == connector.mep_system_type:
  98. next_element = self.get_element(next_connector.belong)
  99. edge_elements.append(next_element)
  100. other_connectors = self.get_other_connectors(next_connector)
  101. if systemutils.is_break_condition(next_element) or len(other_connectors) != 1:
  102. print(next_connector.source_id)
  103. next_connector.is_used = True
  104. if(len(other_connectors) != 1):
  105. graph_next_connectors.extend(other_connectors)
  106. system_edge = system_graph.add_edge(connector, next_connector, edge_elements)
  107. break
  108. if len(other_connectors)==1:
  109. next_connector.is_used = True
  110. current_connector = other_connectors[0]
  111. continue
  112. current_connector = None
  113. except Exception as e:
  114. print(e)
  115. return system_edge
  116. def get_other_connectors(self, use_connector):
  117. """
  118. Get the connector owner other connectors.
  119. belong,domian,mep_system_type same.
  120. is_used==False.
  121. :param use_connector:
  122. :return:
  123. """
  124. connectors = [connector for connector in self.connector_data if
  125. 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]
  126. connectors = [connector for connector in connectors if bool(1 - connector.is_used)]
  127. return connectors
  128. def get_connecteds(self, connetor_id):
  129. """
  130. Get Ref Connector. may have two.like system logic connector
  131. :param connetor_id:
  132. :return:
  133. """
  134. connectors = list(self.get_connector(id) for id in connetor_id)
  135. return connectors
  136. def get_connector(self, connector_id):
  137. """
  138. Get Connector by Id.
  139. :param connector_id:
  140. :return:
  141. """
  142. cons = list(c for c in self.connector_data if c.id == connector_id)
  143. return cons[0]
  144. def get_element(self, element_id):
  145. """
  146. Get Element by Id.
  147. :param eleemnt_id:
  148. :return:
  149. """
  150. return self.element_data[element_id]
  151. class SystemFlowDirection(Enum):
  152. """Edge Flow Direction.
  153. """
  154. flow_in = 1
  155. flow_out = -1
  156. flow_undefine = 0
  157. class SystemVertexType(Enum):
  158. """Vertex Type.
  159. """
  160. real_point = 0
  161. imaginary_point = 1
  162. hollow_dot = 2