node_ops.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import sys
  2. sys.path.append("../wrappers")
  3. from wrappers import modelverse as mv
  4. import commons
  5. class NodeAdd(object):
  6. def __init__(self):
  7. self._node_type = ""
  8. def execute(self, model, node_type, local):
  9. """
  10. Add a new node with type node_type to model.
  11. If local is true, the node is only added to the model.
  12. If local is false, the node will be added to all example models.
  13. """
  14. self._node_type = node_type
  15. if local:
  16. mv.transformation_execute_MANUAL("graph_ops/add_node", {"gm":model}, {"gm":model},
  17. callback=self._callback)
  18. else:
  19. for m in commons.all_example_models():
  20. self.execute(m, node_type, local=True)
  21. def _callback(self, model):
  22. node_id = mv.instantiate(model, "gm/Node")
  23. mv.attr_assign(model, node_id, "typeID", self._node_type)
  24. def repair(self):
  25. """
  26. Check if the node add invalidated any instance models by making a type mandatory.
  27. If yes, repair by adding a node of the same type to it.
  28. """
  29. if commons.is_type_mandatory(self._node_type):
  30. for im in commons.all_instance_models():
  31. if not commons.all_nodes_with_type(im, self._node_type):
  32. print("Adding mandatory type {} to model {}".format(self._node_type, im))
  33. self.execute(im, self._node_type, local=True)
  34. class NodeDelete(object):
  35. def __init__(self):
  36. self._node = ""
  37. self._node_type = ""
  38. self.was_last = False
  39. def execute(self, model, node, local):
  40. self._node = node
  41. self._node_type = commons.get_node_type(model, node)
  42. if local:
  43. mv.transformation_execute_MANUAL("graph_ops/del_node", {"gm":model}, {"gm":model},
  44. callback=self._callback)
  45. else:
  46. for m in commons.all_example_models():
  47. nodes_to_delete = commons.all_nodes_with_type(m, self._node_type)
  48. for node in nodes_to_delete:
  49. self.execute(m, node, local=True)
  50. def _callback(self, model):
  51. mv.delete_element(model, "gm/"+self._node)
  52. def repair(self):
  53. """
  54. Check if this operation invalidated any instance model by leaving a node untyped.
  55. If yes, delete untyped node as well.
  56. """
  57. remaining_instances = 0
  58. for exm in commons.all_example_models():
  59. remaining_instances += len(commons.all_nodes_with_type(exm, self._node_type))
  60. if remaining_instances == 0:
  61. self.was_last = True
  62. print("Type {} became unavailable, deleting from all instance models ...".format(self._node_type))
  63. for im in commons.all_instance_models():
  64. nodes_to_delete = commons.all_nodes_with_type(im, self._node_type)
  65. for node in nodes_to_delete:
  66. self.execute(im, node, local=True)
  67. class NodeRetype(object):
  68. def __init__(self):
  69. self._node = ""
  70. self._old_type = ""
  71. self._new_type = ""
  72. self._scope = ""
  73. def execute(self, model, node, new_type, local):
  74. """
  75. Retype the node in model to new_type.
  76. If local is true, new_type must already exist and the change only affects the given node.
  77. If local is false, all nodes in all example models are renamed to a new type (which must not exist yet).
  78. """
  79. self._node = node
  80. self._new_type = new_type
  81. self._old_type = commons.get_node_type(model, node)
  82. if local:
  83. self._scope = "Local"
  84. else:
  85. self._scope = "Global"
  86. if local:
  87. mv.transformation_execute_MANUAL("graph_ops/retype_node", {"gm":model}, {"gm":model},
  88. callback=self._callback)
  89. else:
  90. for exm in commons.all_example_models():
  91. for node in commons.all_nodes_with_type(exm, self._old_type):
  92. print("NodeRetype: retyping {} in {}".format(node, exm))
  93. self.execute(exm, node, new_type, local=True)
  94. def _callback(self, model):
  95. mv.attr_assign(model, "gm/"+self._node, "typeID", self._new_type)
  96. def repair(self):
  97. """
  98. Check if this operation invalidated any instance models.
  99. Depends on the scope: if local, need to retype all instance model nodes with the old type as well
  100. """
  101. if self._scope == "Global":
  102. for im in commons.all_instance_models():
  103. for node in commons.all_nodes_with_type(im, self._old_type):
  104. self.execute(im, node, self._new_type, local=True)
  105. else:
  106. pass # TODO implement