edge_ops.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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):
  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. else:
  22. for m in commons.all_models():
  23. nodes_from = commons.all_nodes_with_type(m, self._from_type)
  24. nodes_to = commons.all_nodes_with_type(m, self._to_type)
  25. for nf in nodes_from:
  26. for nt in nodes_to:
  27. self.execute(m, nf, nt, local=True)
  28. def _callback(self, model):
  29. mv.instantiate(model, "gm/Edge", ("gm/"+self._from_node, "gm/"+self._to_node))
  30. def repair(self):
  31. """
  32. Check if the operation invalidated any instance models by making an edge mandatory.
  33. If so, repair it by adding the edge there.
  34. """
  35. mandatory_edges = commons.get_mandtory_edges()
  36. this_edge = commons.Edge(self._from_type, self._to_type)
  37. if this_edge in mandatory_edges:
  38. print("Edge {} became mandatory, adding to instance models ...".format(this_edge))
  39. for im in commons.all_instance_models():
  40. nodes_from = commons.all_nodes_with_type(im, self._from_type)
  41. nodes_to = commons.all_nodes_with_type(im, self._to_type)
  42. for nf in nodes_from:
  43. for nt in nodes_to:
  44. self.execute(im, nf, nt, local=True)
  45. class EdgeDel(object):
  46. def __init__(self):
  47. self._edge = ""
  48. self._from_node = ""
  49. self._to_node = ""
  50. self._from_type = ""
  51. self._to_type = ""
  52. def execute(self, model, edge, local):
  53. self._edge = edge
  54. self._from_node = mv.read_association_source(model, edge)[0]
  55. self._to_node = mv.read_association_destination(model, edge)[0]
  56. if not self._from_type:
  57. self._from_type = commons.get_node_type(model, self._from_node)
  58. if not self._to_type:
  59. self._to_type = commons.get_node_type(model, self._to_node)
  60. if local:
  61. mv.transformation_execute_MANUAL("graph_ops/del_edge", {"gm":model}, {"gm":model},
  62. callback=self._callback)
  63. else:
  64. for m in commons.all_models():
  65. nodes_from = commons.all_nodes_with_type(m, self._from_type)
  66. nodes_to = commons.all_nodes_with_type(m, self._to_type)
  67. for nf in nodes_from:
  68. for nt in nodes_to:
  69. edges = commons.get_associations_between(m, nf, nt)
  70. for edge in edges:
  71. self.execute(m, edge, local=True)
  72. def _callback(self, model):
  73. mv.delete_element(model, "gm/"+self._edge)
  74. def repair(self):
  75. """
  76. Check if this operation invalidated any instance models by leaving an edge untyped.
  77. If so, delete untyped edges.
  78. """
  79. if not commons.is_edge_supported(self._from_type, self._to_type):
  80. print("Edge between {} and {} not supported anymore, deleting from all instance models ...".format(
  81. self._from_type, self._to_type))
  82. for im in commons.all_instance_models():
  83. nodes_from = commons.all_nodes_with_type(im, self._from_type)
  84. nodes_to = commons.all_nodes_with_type(im, self._to_type)
  85. for nf in nodes_from:
  86. for nt in nodes_to:
  87. edges = commons.get_associations_between(im, nf, nt)
  88. for edge in edges:
  89. self.execute(im, edge, local=True)