commons.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import re
  2. from wrappers import modelverse as mv
  3. def all_models():
  4. """ Returns a list of paths of all example- and instance models """
  5. return all_instance_models() + all_example_models()
  6. def all_instance_models():
  7. """ Returns a list of paths of all instance models """
  8. try:
  9. instance_models = mv.model_list("models/instance")
  10. except mv.UnknownLocation:
  11. # no instance models
  12. return []
  13. instance_models_full = ["models/instance/"+m for m in instance_models]
  14. return instance_models_full
  15. def all_example_models():
  16. """ Returns a list of paths of all example models """
  17. example_models = mv.model_list("models/example")
  18. example_models_full = ["models/example/"+exm for exm in example_models]
  19. return example_models_full
  20. def all_nodes_with_type(model, typ):
  21. """ Returns a list of nodes in model model with type typ """
  22. all_nodes = mv.all_instances(model, "Node")
  23. ret = [node for node in all_nodes if mv.read_attrs(model, node)["typeID"] == typ]
  24. return ret
  25. def get_node_type(model, node):
  26. """ Returns the type attribute of node in model as string"""
  27. return mv.read_attrs(model, node)["typeID"]
  28. def get_associations_between(model, node1, node2):
  29. """ Returns a list of association IDs between the nodes node1 and node2 """
  30. edges_n1 = mv.read_outgoing(model, node1, "Edge")
  31. edges_n1.update(mv.read_incoming(model, node1, "Edge"))
  32. edges_n2 = mv.read_outgoing(model, node2, "Edge")
  33. edges_n2.update(mv.read_incoming(model, node2, "Edge"))
  34. return list(edges_n1.intersection(edges_n2))
  35. def new_instance_model():
  36. """
  37. Adds a new, empty instance model to the Modelverse.
  38. Returns the name of the new model
  39. """
  40. existing_models = all_instance_models()
  41. idx = 1
  42. nums = []
  43. for model in existing_models:
  44. m = model.split("/")[-1]
  45. try:
  46. idx = int(re.search(r'\d+', m).group())
  47. nums.append(idx)
  48. except AttributeError:
  49. pass
  50. if nums:
  51. idx = sorted(nums)[-1] + 1
  52. im = "models/instance/im{}".format(idx)
  53. print("Adding new instance model {}".format(im))
  54. mv.model_add(im, "formalisms/graphMM")
  55. mid = mv.instantiate(im, "Model")
  56. mv.attr_assign(im, mid, "is_example", False)
  57. mv.attr_assign(im, mid, "descr", "")
  58. return im
  59. def new_example_model():
  60. """
  61. Adds a new, empty example model to the Modelverse.
  62. Returns the name of the new model
  63. """
  64. existing_models = all_example_models()
  65. idx = 1
  66. nums = []
  67. for model in existing_models:
  68. m = model.split("/")[-1]
  69. try:
  70. idx = int(re.search(r'\d+', m).group())
  71. nums.append(idx)
  72. except AttributeError:
  73. pass
  74. if nums:
  75. idx = sorted(nums)[-1] + 1
  76. exm = "models/instance/ex{}".format(idx)
  77. print("Adding new example model {}".format(exm))
  78. mv.model_add(exm, "formalisms/graphMM")
  79. mid = mv.instantiate(exm, "Model")
  80. mv.attr_assign(exm, mid, "is_example", True)
  81. mv.attr_assign(exm, mid, "descr", "")
  82. return exm