node_ops.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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, check_if_last=False):
  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. # Local add can make a type mandatory and therefore break conformance
  19. if check_if_last:
  20. if commons.is_type_mandatory(node_type):
  21. print("Type {} became mandatory, adding it to instance models ...".format(node_type))
  22. # local add made type mandatory, so need to add to instance models
  23. for im in commons.all_instance_models():
  24. self.execute(im, node_type, local=True, check_if_last=False)
  25. else:
  26. for m in commons.all_models():
  27. self.execute(m, node_type, local=True)
  28. def _callback(self, model):
  29. node_id = mv.instantiate(model, "gm/Node")
  30. mv.attr_assign(model, node_id, "typeID", self._node_type)
  31. class NodeDelete(object):
  32. def __init__(self):
  33. self._node = ""
  34. self._node_type = ""
  35. self._was_last = False
  36. def execute(self, model, node, local, check_if_last=False):
  37. self._node = node
  38. self._node_type = commons.get_node_type(model, node)
  39. if local:
  40. mv.transformation_execute_MANUAL("graph_ops/del_node", {"gm":model}, {"gm":model},
  41. callback=self._callback)
  42. if check_if_last:
  43. # check if we just deleted the last instance of the node in all example models
  44. # if yes, delete it in instance models as well to preserve their validity
  45. remaining_instances = 0
  46. for m in commons.all_example_models():
  47. remaining_instances += len(commons.all_nodes_with_type(m, self._node_type))
  48. if remaining_instances == 0:
  49. # it was indeed the last one, delete from instance models
  50. self._was_last = True
  51. for m in commons.all_instance_models():
  52. nodes_to_delete = commons.all_nodes_with_type(m, self._node_type)
  53. for node in nodes_to_delete:
  54. self.execute(m, node, local=True)
  55. else:
  56. for m in commons.all_models():
  57. nodes_to_delete = commons.all_nodes_with_type(m, self._node_type)
  58. for node in nodes_to_delete:
  59. self.execute(m, node, local=True)
  60. def was_last(self):
  61. return self._was_last
  62. def _callback(self, model):
  63. mv.delete_element(model, "gm/"+self._node)
  64. class NodeRetype(object):
  65. def __init__(self):
  66. self._node = ""
  67. self._new_type = ""
  68. def execute(self, model, node, new_type, local):
  69. """
  70. Retype the node in model to new_type.
  71. If local is true, the change only affects the single node.
  72. If local is false, the change affects all nodes in all models.
  73. """
  74. self._node = node
  75. self._new_type = new_type
  76. if local:
  77. mv.transformation_execute_MANUAL("graph_ops/retype_node", {"gm":model}, {"gm":model},
  78. callback=self._callback)
  79. else:
  80. node_type = commons.get_node_type(model, node)
  81. for m in commons.all_models():
  82. for node in commons.all_nodes_with_type(m, node_type):
  83. self.execute(m, node, new_type, local=True)
  84. def _callback(self, model):
  85. mv.attr_assign(model, "gm/"+self._node, "typeID", self._new_type)