mvops.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. from PyQt5.Qt import QRect, QPoint
  10. def add_node(model, node_type):
  11. """ Adds new node to model "model" with type attribute "node_type" """
  12. node_id = mv.instantiate(model, "Node")
  13. mv.attr_assign(model, node_id, "typeID", node_type)
  14. return node_id
  15. def add_edge(model, from_id, to_id, directed=False):
  16. """ Adds an edge to model "model" between the nodes "from_id" and "to_id" """
  17. edge_id = mv.instantiate(model, "Edge", (from_id, to_id))
  18. mv.attr_assign(model, edge_id, "directed", directed)
  19. return edge_id
  20. def delete_node(model, node_id):
  21. mv.delete_element(model, node_id)
  22. def delete_edge(model, edge_id):
  23. mv.delete_element(model, edge_id)
  24. def get_consyn_of(node_type):
  25. """
  26. Queries the modelverse for a concrete syntax of elements of type "node_type".
  27. Returns the list of model elements by mv.element_list_nice or an empty list if no
  28. concrete syntax exists.
  29. """
  30. for csm in commons.all_consyn_models():
  31. if csm.split("/")[-1] == node_type:
  32. return mv.element_list_nice(csm)
  33. return []
  34. def new_concrete_syntax_model(type_id, icon_type, overwrite=False):
  35. # type: (str, IconType, bool) -> str
  36. """
  37. Add a new concrete syntax model for type "type_id" and representation type "icon_type" to the modelverse.
  38. If overwrite, an already existing CS model for the same type is deleted.
  39. Returns the path of the newly created model.
  40. """
  41. csm = "models/consyn/" + type_id
  42. try:
  43. mv.model_add(csm, "formalisms/consynMM")
  44. except mv.ModelExists:
  45. if not overwrite:
  46. return ""
  47. else:
  48. mv.model_delete(csm)
  49. mv.model_add(csm, "formalisms/consynMM")
  50. icon = mv.instantiate(csm, "Icon")
  51. mv.attr_assign(csm, icon, "typeID", type_id)
  52. if icon_type == IconType.PRIMITIVE:
  53. mv.attr_assign(csm, icon, "is_primitive", True)
  54. else:
  55. mv.attr_assign(csm, icon, "is_primitive", False)
  56. return csm
  57. def del_concrete_syntax_model(type_id):
  58. """
  59. Delete a concrete syntax model. Returns True if success, False otherwise.
  60. """
  61. csm = "models/consyn/" + type_id
  62. if csm not in commons.all_consyn_models():
  63. return False
  64. mv.model_delete(csm)
  65. return True
  66. def add_rect_to_cs(csm, rect):
  67. # type: (str, QRect) -> None
  68. rect_id = mv.instantiate(csm, "Rectangle")
  69. mv.attr_assign(csm, rect_id, "x", rect.topLeft().x())
  70. mv.attr_assign(csm, rect_id, "y", rect.topLeft().y())
  71. mv.attr_assign(csm, rect_id, "width", rect.width())
  72. mv.attr_assign(csm, rect_id, "height", rect.height())
  73. def add_ellipse_to_cs(csm, rect):
  74. # type: (str, QRect) -> None
  75. elp_id = mv.instantiate(csm, "Ellipse")
  76. mv.attr_assign(csm, elp_id, "x", rect.topLeft().x())
  77. mv.attr_assign(csm, elp_id, "y", rect.topLeft().y())
  78. mv.attr_assign(csm, elp_id, "width", rect.width())
  79. mv.attr_assign(csm, elp_id, "height", rect.height())
  80. def add_line_to_cs(csm, p1, p2):
  81. # type: (str, QPoint, QPoint) -> None
  82. line_id = mv.instantiate(csm, "Line")
  83. mv.attr_assign(csm, line_id, "start_x", p1.x())
  84. mv.attr_assign(csm, line_id, "start_y", p1.y())
  85. mv.attr_assign(csm, line_id, "end_x", p2.x())
  86. mv.attr_assign(csm, line_id, "end_y", p2.y())
  87. def add_attribute(model, node_id, key, val):
  88. """
  89. Adds an attribute to node_id in model with key and value
  90. """
  91. attr_id = mv.instantiate(model, "Attribute")
  92. mv.attr_assign(model, attr_id, "key", key)
  93. mv.attr_assign(model, attr_id, "value", val)
  94. mv.instantiate(model, "NodeAttribute", (node_id, attr_id))
  95. def update_attribute_val(model, node_id, key, new_val):
  96. """
  97. Update the attribute identified by its key of node node_id in model to new_val
  98. """
  99. outgoings = mv.read_outgoing(model, node_id, "NodeAttribute")
  100. for link in outgoings:
  101. attr = mv.read_association_destination(model, link)[0]
  102. attr_key = mv.read_attrs(model, attr)["key"]
  103. if attr_key == key:
  104. mv.attr_assign(model, attr, "value", new_val)
  105. break
  106. def delete_attribute_from_node(model, node_id, key):
  107. """
  108. Deletes the attribute identified by its key of node node_id in model
  109. """
  110. outgoings = mv.read_outgoing(model, node_id, "NodeAttribute")
  111. for link in outgoings:
  112. attr = mv.read_association_destination(model, link)[0]
  113. attr_key = mv.read_attrs(model, attr)["key"]
  114. if attr_key == key:
  115. mv.delete_element(model, attr)
  116. break
  117. def delete_instance_model(model):
  118. """
  119. Deletes the instance model "model". Returns True on success, False otherwise.
  120. """
  121. if model not in commons.all_instance_models():
  122. return False
  123. mv.model_delete(model)
  124. return True
  125. def delete_example_model(model):
  126. """
  127. Deletes the example model "model". Should be empty since no evolution handling is done here.
  128. Returns True on success, False otherwise.
  129. """
  130. if model not in commons.all_example_models():
  131. return False
  132. mv.model_delete(model)
  133. return True