mvops.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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