mvops.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. def get_available_types():
  9. """ Returns a list of all types of nodes in all example models """
  10. types = []
  11. for m in commons.all_example_models():
  12. nodes = mv.all_instances(m, "Node")
  13. for node in nodes:
  14. typ = commons.get_node_type(m, node)
  15. types.append(typ)
  16. return list(set(types))
  17. def is_edge_supported(from_type, to_type):
  18. """
  19. True if an association between from_type and to_type exists in any example model
  20. False otherwise
  21. """
  22. for m in commons.all_example_models():
  23. nodes_from = commons.all_nodes_with_type(m, from_type)
  24. nodes_to = commons.all_nodes_with_type(m, to_type)
  25. for nf in nodes_from:
  26. for nt in nodes_to:
  27. assocs = commons.get_associations_between(m, nf, nt)
  28. if assocs:
  29. # somewhere in an example model, there is such an association
  30. return True
  31. return False
  32. def add_node(model, node_type):
  33. """ Adds new node to model "model" with type attribute "node_type" """
  34. node_id = mv.instantiate(model, "Node")
  35. mv.attr_assign(model, node_id, "typeID", node_type)
  36. print("Added node with id {} and type {} to model {}".format(node_id, node_type, model))
  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. print("Added edge between {} and {} to model".format(from_id, to_id))
  43. return edge_id