node_ops.py 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. self._node_id = None
  9. def execute(self, model, node_type, local):
  10. """
  11. Add a new node with type node_type to model model.
  12. If local is true, the node is only added to the model.
  13. If local is false, the node will be added to all models.
  14. """
  15. self._node_type = node_type
  16. if local:
  17. mv.transformation_execute_MANUAL("graph_ops/add_node", {"gm":model}, {"gm":model},
  18. callback=self._callback)
  19. else:
  20. for m in all_models():
  21. self.execute(m, node_type, local=True)
  22. def get_node_id(self):
  23. # attention: in case of global execution, this method returns only the last added node in the last model
  24. return self._node_id
  25. def _callback(self, model):
  26. node_id = mv.instantiate(model, "gm/Node")
  27. mv.attr_assign(model, node_id, "typeID", self._node_type)
  28. self._node_id = node_id
  29. class NodeDelete(object):
  30. def __init__(self):
  31. self._node = ""
  32. self._node_type = ""
  33. def execute(self, model, node, local, check_if_last=False):
  34. self._node = node
  35. self._node_type = get_node_type(model, node)
  36. if local:
  37. mv.transformation_execute_MANUAL("graph_ops/del_node", {"gm":model}, {"gm":model},
  38. callback=self._callback)
  39. if check_if_last:
  40. # check if we just deleted the last instance of the node in all example models
  41. # if yes, delete it in instance models as well to preserve their validity
  42. remaining_instances = 0
  43. for m in all_example_models():
  44. remaining_instances += len(all_nodes_with_type(m, self._node_type))
  45. if remaining_instances == 0:
  46. # it was indeed the last one, delete from instance models
  47. for m in all_instance_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. else:
  52. for m in all_models():
  53. nodes_to_delete = all_nodes_with_type(m, self._node_type)
  54. for node in nodes_to_delete:
  55. self.execute(m, node, local=True)
  56. def _callback(self, model):
  57. mv.delete_element(model, "gm/"+self._node)
  58. class NodeRetype(object):
  59. def __init__(self):
  60. self._node = ""
  61. self._new_type = ""
  62. def execute(self, model, node, new_type, local):
  63. """
  64. Retype the node in model to new_type.
  65. If local is true, the change only affects the single node.
  66. If local is false, the change affects all nodes in all models.
  67. """
  68. self._node = node
  69. self._new_type = new_type
  70. if local:
  71. mv.transformation_execute_MANUAL("graph_ops/retype_node", {"gm":model}, {"gm":model},
  72. callback=self._callback)
  73. else:
  74. node_type = get_node_type(model, node)
  75. for m in all_models():
  76. for node in all_nodes_with_type(m, node_type):
  77. self.execute(m, node, new_type, local=True)
  78. def _callback(self, model):
  79. mv.attr_assign(model, "gm/"+self._node, "typeID", self._new_type)