node_ops.py 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import sys
  2. sys.path.append("../wrappers")
  3. from wrappers import modelverse as mv
  4. from commons import *
  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 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 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 all_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. class NodeDelete(object):
  25. def __init__(self):
  26. self._node = ""
  27. self._node_type = ""
  28. def execute(self, model, node, local, check_if_last=False):
  29. self._node = node
  30. self._node_type = get_node_type(model, node)
  31. if local:
  32. mv.transformation_execute_MANUAL("graph_ops/del_node", {"gm":model}, {"gm":model},
  33. callback=self._callback)
  34. if check_if_last:
  35. # check if we just deleted the last instance of the node in all example models
  36. # if yes, delete it in instance models as well to preserve their validity
  37. remaining_instances = 0
  38. for m in all_example_models():
  39. remaining_instances += len(all_nodes_with_type(m, self._node_type))
  40. if remaining_instances == 0:
  41. # it was indeed the last one, delete from instance models
  42. for m in all_instance_models():
  43. nodes_to_delete = all_nodes_with_type(m, self._node_type)
  44. for node in nodes_to_delete:
  45. self.execute(m, node, local=True)
  46. else:
  47. for m in all_models():
  48. nodes_to_delete = all_nodes_with_type(m, self._node_type)
  49. for node in nodes_to_delete:
  50. self.execute(m, node, local=True)
  51. def _callback(self, model):
  52. mv.delete_element(model, "gm/"+self._node)
  53. class NodeRetype(object):
  54. def __init__(self):
  55. self._node = ""
  56. self._new_type = ""
  57. def execute(self, model, node, new_type, local):
  58. """
  59. Retype the node in model to new_type.
  60. If local is true, the change only affects the single node.
  61. If local is false, the change affects all nodes in all models.
  62. """
  63. self._node = node
  64. self._new_type = new_type
  65. if local:
  66. mv.transformation_execute_MANUAL("graph_ops/retype_node", {"gm":model}, {"gm":model},
  67. callback=self._callback)
  68. else:
  69. node_type = get_node_type(model, node)
  70. for m in all_models():
  71. for node in all_nodes_with_type(m, node_type):
  72. self.execute(m, node, new_type, local=True)
  73. def _callback(self, model):
  74. mv.attr_assign(model, "gm/"+self._node, "typeID", self._new_type)