edge_ops.py 4.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import sys
  2. sys.path.append("../wrappers")
  3. from wrappers import modelverse as mv
  4. import commons
  5. class EdgeAdd(object):
  6. def __init__(self):
  7. self._from_node = ""
  8. self._to_node = ""
  9. self._from_type = ""
  10. self._to_type = ""
  11. def execute(self, model, from_node, to_node, local, check_if_last=False):
  12. self._from_node = from_node
  13. self._to_node = to_node
  14. if not self._from_type:
  15. self._from_type = commons.get_node_type(model, from_node)
  16. if not self._to_type:
  17. self._to_type = commons.get_node_type(model, to_node)
  18. if local:
  19. mv.transformation_execute_MANUAL("graph_ops/add_edge", {"gm":model}, {"gm":model},
  20. callback=self._callback)
  21. if check_if_last:
  22. mandatory_edges = commons.get_mandtory_edges()
  23. this_edge = commons.Edge(self._from_type, self._to_type)
  24. if this_edge in mandatory_edges:
  25. print("Edge {} became mandatory, adding to instance models ...".format(this_edge))
  26. for im in commons.all_instance_models():
  27. nodes_from = commons.all_nodes_with_type(im, self._from_type)
  28. nodes_to = commons.all_nodes_with_type(im, self._to_type)
  29. for nf in nodes_from:
  30. for nt in nodes_to:
  31. self.execute(im, nf, nt, local=True, check_if_last=False)
  32. else:
  33. for m in commons.all_models():
  34. nodes_from = commons.all_nodes_with_type(m, self._from_type)
  35. nodes_to = commons.all_nodes_with_type(m, self._to_type)
  36. for nf in nodes_from:
  37. for nt in nodes_to:
  38. self.execute(m, nf, nt, local=True, check_if_last=False)
  39. def _callback(self, model):
  40. mv.instantiate(model, "gm/Edge", ("gm/"+self._from_node, "gm/"+self._to_node))
  41. class EdgeDel(object):
  42. def __init__(self):
  43. self._edge = ""
  44. def execute(self, model, edge, local, check_if_last=False):
  45. self._edge = edge
  46. from_node = mv.read_association_source(model, edge)[0]
  47. to_node = mv.read_association_destination(model, edge)[0]
  48. from_type = commons.get_node_type(model, from_node)
  49. to_type = commons.get_node_type(model, to_node)
  50. if local:
  51. mv.transformation_execute_MANUAL("graph_ops/del_edge", {"gm":model}, {"gm":model},
  52. callback=self._callback)
  53. if check_if_last:
  54. # check if this association was the last one in all example models
  55. # if yes, delete it from all instance models as well to preserve their validity
  56. remaining_instances = 0
  57. for m in commons.all_example_models():
  58. nodes_from = commons.all_nodes_with_type(m, from_type)
  59. nodes_to = commons.all_nodes_with_type(m, to_type)
  60. for nf in nodes_from:
  61. for nt in nodes_to:
  62. edges = commons.get_associations_between(m, nf, nt)
  63. remaining_instances += len(edges)
  64. if remaining_instances == 0:
  65. for m in commons.all_instance_models():
  66. nodes_from = commons.all_nodes_with_type(m, from_type)
  67. nodes_to = commons.all_nodes_with_type(m, to_type)
  68. for nf in nodes_from:
  69. for nt in nodes_to:
  70. edges = commons.get_associations_between(m, nf, nt)
  71. for edge in edges:
  72. self.execute(m, edge, local=True, check_if_last=False)
  73. else:
  74. for m in commons.all_models():
  75. nodes_from = commons.all_nodes_with_type(m, from_type)
  76. nodes_to = commons.all_nodes_with_type(m, to_type)
  77. for nf in nodes_from:
  78. for nt in nodes_to:
  79. edges = commons.get_associations_between(m, nf, nt)
  80. for edge in edges:
  81. self.execute(m, edge, local=True, check_if_last=False)
  82. def _callback(self, model):
  83. mv.delete_element(model, "gm/"+self._edge)