attribute_ops.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. else:
  24. for m in commons.all_models():
  25. nodes = commons.all_nodes_with_type(m, self._node_type)
  26. for nid in nodes:
  27. self.execute(m, nid, key, value, local=True)
  28. def _callback(self, model):
  29. attr_id = mv.instantiate(model, "gm/Attribute")
  30. mv.attr_assign(model, attr_id, "key", self._key)
  31. mv.attr_assign(model, attr_id, "value", self._value)
  32. mv.instantiate(model, "gm/NodeAttribute", ("gm/"+self._node_id, attr_id))
  33. class AttributeDelete(object):
  34. def __init__(self):
  35. self._key = ""
  36. self._node_id = ""
  37. self._node_type = ""
  38. def execute(self, model, node_id, key, local):
  39. """
  40. Deletes an attribute identified by its key from node_id in model.
  41. """
  42. self._key = key
  43. self._node_id = node_id
  44. if not self._node_type:
  45. self._node_type = commons.get_node_type(model, node_id)
  46. if local:
  47. mv.transformation_execute_MANUAL("graph_ops/del_attribute", {"gm":model}, {"gm":model},
  48. callback=self._callback)
  49. else:
  50. # delete all attributes with key
  51. for m in commons.all_models():
  52. nodes = commons.all_nodes_with_type(m, self._node_type)
  53. for nid in nodes:
  54. self.execute(m, nid, key, local=True)
  55. def _callback(self, model):
  56. outgoings = mv.read_outgoing(model, "gm/"+self._node_id, "gm/NodeAttribute")
  57. for edge in outgoings:
  58. attr = mv.read_association_destination(model, edge)[0]
  59. attr_key = mv.read_attrs(model, attr)["key"]
  60. if attr_key == self._key:
  61. mv.delete_element(model, attr)
  62. break
  63. class AttributeChange(object):
  64. """
  65. Remark: This is currently not needed since the UI does not have the functionality to update an attribute
  66. key in-place. Instead, an attribute must be deleted and added with the new key to effectively change the key.
  67. Therefore, this is only here for the sake of completeness.
  68. """
  69. def __init__(self):
  70. self._node_id = ""
  71. self._old_key = ""
  72. self._new_key = ""
  73. self._node_type = ""
  74. def execute(self, model, node_id, old_key, new_key, local):
  75. """
  76. Update the attribute key old_key of node_id in model to new_key.
  77. """
  78. self._node_id = node_id
  79. self._old_key = old_key
  80. self._new_key = new_key
  81. if not self._node_type:
  82. self._node_type = commons.get_node_type(model, node_id)
  83. if local:
  84. mv.transformation_execute_MANUAL("graph_ops/change_attribute", {"gm":model}, {"gm":model},
  85. callback=self._callback)
  86. else:
  87. for m in commons.all_models():
  88. nodes = commons.all_nodes_with_type(m, self._node_type)
  89. for nid in nodes:
  90. self.execute(m, nid, old_key, new_key, local=True)
  91. def _callback(self, model):
  92. outgoings = mv.read_outgoing(model, "gm/"+self._node_id, "gm/NodeAttribute")
  93. for link in outgoings:
  94. attr = mv.read_association_destination(model, link)[0]
  95. attr_key = mv.read_attrs(model, attr)["key"]
  96. if attr_key == self._old_key:
  97. mv.attr_assign(model, attr, "key", self._new_key)
  98. break