mvops.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 get_available_types():
  10. """ Returns a list of all types of nodes in all example models """
  11. types = []
  12. for m in commons.all_example_models():
  13. nodes = mv.all_instances(m, "Node")
  14. for node in nodes:
  15. typ = commons.get_node_type(m, node)
  16. types.append(typ)
  17. return list(set(types))
  18. def is_edge_supported(from_type, to_type):
  19. """
  20. True if an association between from_type and to_type exists in any example model
  21. False otherwise
  22. """
  23. for m in commons.all_example_models():
  24. nodes_from = commons.all_nodes_with_type(m, from_type)
  25. nodes_to = commons.all_nodes_with_type(m, to_type)
  26. for nf in nodes_from:
  27. for nt in nodes_to:
  28. assocs = commons.get_associations_between(m, nf, nt)
  29. if assocs:
  30. # somewhere in an example model, there is such an association
  31. return True
  32. return False
  33. def add_node(model, node_type):
  34. """ Adds new node to model "model" with type attribute "node_type" """
  35. node_id = mv.instantiate(model, "Node")
  36. mv.attr_assign(model, node_id, "typeID", node_type)
  37. return node_id
  38. def add_edge(model, from_id, to_id, directed=False):
  39. """ Adds an edge to model "model" between the nodes "from_id" and "to_id" """
  40. edge_id = mv.instantiate(model, "Edge", (from_id, to_id))
  41. mv.attr_assign(model, edge_id, "directed", directed)
  42. return edge_id
  43. def delete_node(model, node_id):
  44. mv.delete_element(model, node_id)
  45. def get_consyn_of(node_type):
  46. """
  47. Queries the modelverse for a concrete syntax of elements of type "node_type".
  48. Returns the list of model elements by mv.element_list_nice or an empty list if no
  49. concrete syntax exists.
  50. """
  51. for csm in commons.all_consyn_models():
  52. if csm.split("/")[-1] == node_type:
  53. return mv.element_list_nice(csm)
  54. return []
  55. def new_concrete_syntax(type_id, icon_type):
  56. # type: (str, IconType) -> str
  57. """
  58. Add a new concrete syntax model for type "type_id" and representation type "icon_type" to the modelverse
  59. """
  60. csm = "models/consyn/" + type_id
  61. try:
  62. mv.model_add(csm, "formalisms/consynMM")
  63. except mv.ModelExists:
  64. return ""
  65. icon = mv.instantiate(csm, "Icon")
  66. if icon_type == IconType.PRIMITIVE:
  67. mv.attr_assign(csm, icon, "is_primitive", True)
  68. mv.instantiate(csm, "PrimitiveGroup")
  69. else:
  70. mv.attr_assign(csm, icon, "is_primitive", False)
  71. return csm
  72. def add_attribute(model, node_id, key, val):
  73. """
  74. Adds an attribute to node_id in model with key and value
  75. """
  76. attr_id = mv.instantiate(model, "Attribute")
  77. mv.attr_assign(model, attr_id, "key", key)
  78. mv.attr_assign(model, attr_id, "value", val)
  79. mv.instantiate(model, "NodeAttribute", (node_id, attr_id))
  80. def is_attributing_allowed(node_type):
  81. """
  82. Check if attributing is allowed for a node of type node_type.
  83. Goes through all example models and checks if they have attributes.
  84. """
  85. for exm in commons.all_example_models():
  86. if commons.get_all_attributes_of_type(exm, node_type):
  87. return True
  88. return False
  89. def update_attribute_val(model, node_id, key, new_val):
  90. """
  91. Update the attribute identified by its key of node node_id in model to new_val
  92. """
  93. outgoings = mv.read_outgoing(model, node_id, "NodeAttribute")
  94. for link in outgoings:
  95. attr = mv.read_association_destination(model, link)[0]
  96. attr_key = mv.read_attrs(model, attr)["key"]
  97. if attr_key == key:
  98. mv.attr_assign(model, attr, "value", new_val)
  99. break
  100. def delete_attribute_from_node(model, node_id, key):
  101. """
  102. Deletes the attribute identified by its key of node node_id in model
  103. """
  104. outgoings = mv.read_outgoing(model, node_id, "NodeAttribute")
  105. for link in outgoings:
  106. attr = mv.read_association_destination(model, link)[0]
  107. attr_key = mv.read_attrs(model, attr)["key"]
  108. if attr_key == key:
  109. mv.delete_element(model, attr)
  110. break