node_ops.py 3.4 KB

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