mvops.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. """
  2. Modelverse operations used by the UI
  3. """
  4. import sys
  5. sys.path.append("..")
  6. import wrappers.modelverse as mv
  7. import commons
  8. from sketchUI.graphics_node_item import IconType
  9. def add_node(model, node_type):
  10. """ Adds new node to model "model" with type attribute "node_type" """
  11. node_id = mv.instantiate(model, "Node")
  12. mv.attr_assign(model, node_id, "typeID", node_type)
  13. return node_id
  14. def add_edge(model, from_id, to_id, directed=False):
  15. """ Adds an edge to model "model" between the nodes "from_id" and "to_id" """
  16. edge_id = mv.instantiate(model, "Edge", (from_id, to_id))
  17. mv.attr_assign(model, edge_id, "directed", directed)
  18. return edge_id
  19. def delete_node(model, node_id):
  20. mv.delete_element(model, node_id)
  21. def delete_edge(model, edge_id):
  22. mv.delete_element(model, edge_id)
  23. def get_consyn_of(node_type):
  24. """
  25. Queries the modelverse for a concrete syntax of elements of type "node_type".
  26. Returns the list of model elements by mv.element_list_nice or an empty list if no
  27. concrete syntax exists.
  28. """
  29. for csm in commons.all_consyn_models():
  30. if csm.split("/")[-1] == node_type:
  31. return mv.element_list_nice(csm)
  32. return []
  33. def new_concrete_syntax(type_id, icon_type):
  34. # type: (str, IconType) -> str
  35. """
  36. Add a new concrete syntax model for type "type_id" and representation type "icon_type" to the modelverse
  37. """
  38. csm = "models/consyn/" + type_id
  39. try:
  40. mv.model_add(csm, "formalisms/consynMM")
  41. except mv.ModelExists:
  42. return ""
  43. icon = mv.instantiate(csm, "Icon")
  44. if icon_type == IconType.PRIMITIVE:
  45. mv.attr_assign(csm, icon, "is_primitive", True)
  46. mv.instantiate(csm, "PrimitiveGroup")
  47. else:
  48. mv.attr_assign(csm, icon, "is_primitive", False)
  49. return csm
  50. def add_attribute(model, node_id, key, val):
  51. """
  52. Adds an attribute to node_id in model with key and value
  53. """
  54. attr_id = mv.instantiate(model, "Attribute")
  55. mv.attr_assign(model, attr_id, "key", key)
  56. mv.attr_assign(model, attr_id, "value", val)
  57. mv.instantiate(model, "NodeAttribute", (node_id, attr_id))
  58. def is_attributing_allowed(node_type):
  59. """
  60. Check if attributing is allowed for a node of type node_type.
  61. Goes through all example models and checks if they have attributes.
  62. """
  63. for exm in commons.all_example_models():
  64. if commons.get_all_attributes_of_type(exm, node_type):
  65. return True
  66. return False
  67. def update_attribute_val(model, node_id, key, new_val):
  68. """
  69. Update the attribute identified by its key of node node_id in model to new_val
  70. """
  71. outgoings = mv.read_outgoing(model, node_id, "NodeAttribute")
  72. for link in outgoings:
  73. attr = mv.read_association_destination(model, link)[0]
  74. attr_key = mv.read_attrs(model, attr)["key"]
  75. if attr_key == key:
  76. mv.attr_assign(model, attr, "value", new_val)
  77. break
  78. def delete_attribute_from_node(model, node_id, key):
  79. """
  80. Deletes the attribute identified by its key of node node_id in model
  81. """
  82. outgoings = mv.read_outgoing(model, node_id, "NodeAttribute")
  83. for link in outgoings:
  84. attr = mv.read_association_destination(model, link)[0]
  85. attr_key = mv.read_attrs(model, attr)["key"]
  86. if attr_key == key:
  87. mv.delete_element(model, attr)
  88. break