commons.py 3.1 KB

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