edge_ops.py 3.2 KB

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