attribute_ops.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. import sys
  2. sys.path.append("../wrappers")
  3. from wrappers import modelverse as mv
  4. import commons
  5. class AttributeAdd(object):
  6. def __init__(self):
  7. self._key = ""
  8. self._value = ""
  9. self._node_id = ""
  10. self._node_type = ""
  11. def execute(self, model, node_id, key, value, local):
  12. """
  13. Add a new attribute (key, value) to node with node_id in model.
  14. """
  15. self._key = key
  16. self._value = value
  17. self._node_id = node_id
  18. if not self._node_type:
  19. self._node_type = commons.get_node_type(model, node_id)
  20. if local:
  21. mv.transformation_execute_MANUAL("graph_ops/add_attribute", {"gm":model}, {"gm":model},
  22. callback=self._callback)
  23. # TODO: local add can make attribute mandatory and break conformance relationship of instance models
  24. else:
  25. for m in commons.all_models():
  26. nodes = commons.all_nodes_with_type(m, self._node_type)
  27. for nid in nodes:
  28. self.execute(m, nid, key, value, local=True)
  29. def _callback(self, model):
  30. attr_id = mv.instantiate(model, "gm/Attribute")
  31. mv.attr_assign(model, attr_id, "key", self._key)
  32. mv.attr_assign(model, attr_id, "value", self._value)
  33. mv.instantiate(model, "gm/NodeAttribute", ("gm/"+self._node_id, attr_id))
  34. class AttributeDelete(object):
  35. def __init__(self):
  36. self._key = ""
  37. self._node_id = ""
  38. self._node_type = ""
  39. def execute(self, model, node_id, key, local, check_if_last=False):
  40. """
  41. Deletes an attribute identified by its key from node_id in model.
  42. """
  43. self._key = key
  44. self._node_id = node_id
  45. if not self._node_type:
  46. self._node_type = commons.get_node_type(model, node_id)
  47. if local:
  48. mv.transformation_execute_MANUAL("graph_ops/del_attribute", {"gm":model}, {"gm":model},
  49. callback=self._callback)
  50. if check_if_last:
  51. # a local attribute delete can break the conformance for instance models if the attribute was the last in
  52. # all example models -> check and correct this if necessary
  53. for exm in commons.all_example_models():
  54. all_attrs = commons.get_all_attributes_of_type(exm, self._node_type)
  55. for attr in all_attrs:
  56. if attr.key == key:
  57. # wasn't the last, we're done here
  58. return
  59. # it was the last -> delete this attribute in all instance models to maintain conformance
  60. print("Attribute {} was the last, deleting it from all instance models to maintain conformance ...".format(key))
  61. for im in commons.all_instance_models():
  62. nodes = commons.get_nodes_with_attribute(im, key, self._node_type)
  63. for node in nodes:
  64. self.execute(im, node, key, local=True, check_if_last=False)
  65. else:
  66. # delete all attributes with key
  67. for m in commons.all_models():
  68. nodes = commons.all_nodes_with_type(m, self._node_type)
  69. for nid in nodes:
  70. self.execute(m, nid, key, local=True)
  71. def _callback(self, model):
  72. outgoings = mv.read_outgoing(model, "gm/"+self._node_id, "gm/NodeAttribute")
  73. for edge in outgoings:
  74. attr = mv.read_association_destination(model, edge)[0]
  75. attr_key = mv.read_attrs(model, attr)["key"]
  76. if attr_key == self._key:
  77. mv.delete_element(model, attr)
  78. break
  79. class AttributeChange(object):
  80. """
  81. Remark: This is currently not needed since the UI does not have the functionality to update an attribute
  82. key in-place. Instead, an attribute must be deleted and added with the new key to effectively change the key.
  83. Therefore, this is only here for the sake of completeness.
  84. """
  85. def __init__(self):
  86. self._node_id = ""
  87. self._old_key = ""
  88. self._new_key = ""
  89. self._node_type = ""
  90. def execute(self, model, node_id, old_key, new_key, local):
  91. """
  92. Update the attribute key old_key of node_id in model to new_key.
  93. """
  94. self._node_id = node_id
  95. self._old_key = old_key
  96. self._new_key = new_key
  97. if not self._node_type:
  98. self._node_type = commons.get_node_type(model, node_id)
  99. if local:
  100. mv.transformation_execute_MANUAL("graph_ops/change_attribute", {"gm":model}, {"gm":model},
  101. callback=self._callback)
  102. else:
  103. for m in commons.all_models():
  104. nodes = commons.all_nodes_with_type(m, self._node_type)
  105. for nid in nodes:
  106. self.execute(m, nid, old_key, new_key, local=True)
  107. def _callback(self, model):
  108. outgoings = mv.read_outgoing(model, "gm/"+self._node_id, "gm/NodeAttribute")
  109. for link in outgoings:
  110. attr = mv.read_association_destination(model, link)[0]
  111. attr_key = mv.read_attrs(model, attr)["key"]
  112. if attr_key == self._old_key:
  113. mv.attr_assign(model, attr, "key", self._new_key)
  114. break