mvops.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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):
  35. # type: (str, IconType) -> str
  36. """
  37. Add a new concrete syntax model for type "type_id" and representation type "icon_type" to the modelverse.
  38. Returns the path of the newly created model.
  39. """
  40. csm = "models/consyn/" + type_id
  41. try:
  42. mv.model_add(csm, "formalisms/consynMM")
  43. except mv.ModelExists:
  44. return ""
  45. icon = mv.instantiate(csm, "Icon")
  46. mv.attr_assign(csm, icon, "typeID", type_id)
  47. if icon_type == IconType.PRIMITIVE:
  48. mv.attr_assign(csm, icon, "is_primitive", True)
  49. else:
  50. mv.attr_assign(csm, icon, "is_primitive", False)
  51. return csm
  52. def del_concrete_syntax_model(type_id):
  53. """
  54. Delete a concrete syntax model. Returns True if success, False otherwise.
  55. """
  56. csm = "models/consyn/" + type_id
  57. if csm not in commons.all_consyn_models():
  58. return False
  59. mv.model_delete(csm)
  60. return True
  61. def add_rect_to_cs(csm, rect):
  62. # type: (str, QRect) -> None
  63. rect_id = mv.instantiate(csm, "Rectangle")
  64. mv.attr_assign(csm, rect_id, "x", rect.topLeft().x())
  65. mv.attr_assign(csm, rect_id, "y", rect.topLeft().y())
  66. mv.attr_assign(csm, rect_id, "width", rect.width())
  67. mv.attr_assign(csm, rect_id, "height", rect.height())
  68. def add_ellipse_to_cs(csm, rect):
  69. # type: (str, QRect) -> None
  70. elp_id = mv.instantiate(csm, "Ellipse")
  71. mv.attr_assign(csm, elp_id, "x", rect.topLeft().x())
  72. mv.attr_assign(csm, elp_id, "y", rect.topLeft().y())
  73. mv.attr_assign(csm, elp_id, "width", rect.width())
  74. mv.attr_assign(csm, elp_id, "height", rect.height())
  75. def add_line_to_cs(csm, p1, p2):
  76. # type: (str, QPoint, QPoint) -> None
  77. line_id = mv.instantiate(csm, "Line")
  78. mv.attr_assign(csm, line_id, "start_x", p1.x())
  79. mv.attr_assign(csm, line_id, "start_y", p1.y())
  80. mv.attr_assign(csm, line_id, "end_x", p2.x())
  81. mv.attr_assign(csm, line_id, "end_y", p2.y())
  82. def add_attribute(model, node_id, key, val):
  83. """
  84. Adds an attribute to node_id in model with key and value
  85. """
  86. attr_id = mv.instantiate(model, "Attribute")
  87. mv.attr_assign(model, attr_id, "key", key)
  88. mv.attr_assign(model, attr_id, "value", val)
  89. mv.instantiate(model, "NodeAttribute", (node_id, attr_id))
  90. def update_attribute_val(model, node_id, key, new_val):
  91. """
  92. Update the attribute identified by its key of node node_id in model to new_val
  93. """
  94. outgoings = mv.read_outgoing(model, node_id, "NodeAttribute")
  95. for link in outgoings:
  96. attr = mv.read_association_destination(model, link)[0]
  97. attr_key = mv.read_attrs(model, attr)["key"]
  98. if attr_key == key:
  99. mv.attr_assign(model, attr, "value", new_val)
  100. break
  101. def delete_attribute_from_node(model, node_id, key):
  102. """
  103. Deletes the attribute identified by its key of node node_id in model
  104. """
  105. outgoings = mv.read_outgoing(model, node_id, "NodeAttribute")
  106. for link in outgoings:
  107. attr = mv.read_association_destination(model, link)[0]
  108. attr_key = mv.read_attrs(model, attr)["key"]
  109. if attr_key == key:
  110. mv.delete_element(model, attr)
  111. break
  112. def delete_instance_model(model):
  113. """
  114. Deletes the instance model "model". Returns True on success, False otherwise.
  115. """
  116. if model not in commons.all_instance_models():
  117. return False
  118. mv.model_delete(model)
  119. return True
  120. def delete_example_model(model):
  121. """
  122. Deletes the example model "model". Should be empty since no evolution handling is done here.
  123. Returns True on success, False otherwise.
  124. """
  125. if model not in commons.all_example_models():
  126. return False
  127. mv.model_delete(model)
  128. return True