commons.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import re
  2. from wrappers import modelverse as mv
  3. class Attribute(object):
  4. """
  5. Helper class for attribute representation as key value pair
  6. """
  7. def __init__(self, key, val):
  8. self.key = key
  9. self.val = val
  10. def __eq__(self, other):
  11. if other.key == self.key and other.val == self.val:
  12. return True
  13. return False
  14. def __repr__(self):
  15. return "{}={}".format(self.key, self.val)
  16. def all_models():
  17. """ Returns a list of paths of all example- and instance models """
  18. return all_instance_models() + all_example_models()
  19. def all_instance_models():
  20. """ Returns a list of paths of all instance models """
  21. try:
  22. instance_models = mv.model_list("models/instance")
  23. except mv.UnknownLocation:
  24. # no instance models
  25. return []
  26. instance_models_full = ["models/instance/"+m for m in instance_models]
  27. return instance_models_full
  28. def all_example_models():
  29. """ Returns a list of paths of all example models """
  30. try:
  31. example_models = mv.model_list("models/example")
  32. except mv.UnknownLocation:
  33. # no example models
  34. return []
  35. example_models_full = ["models/example/"+exm for exm in example_models]
  36. return example_models_full
  37. def all_consyn_models():
  38. """ Returns a list of paths of all concrete syntax models """
  39. try:
  40. consyn_models = mv.model_list("models/consyn")
  41. except mv.UnknownLocation:
  42. return []
  43. consyn_models_full = ["models/consyn/"+csm for csm in consyn_models]
  44. return consyn_models_full
  45. def all_nodes_with_type(model, typ):
  46. """ Returns a list of nodes in model model with type typ """
  47. all_nodes = mv.all_instances(model, "Node")
  48. ret = [node for node in all_nodes if mv.read_attrs(model, node)["typeID"] == typ]
  49. return ret
  50. def get_node_type(model, node):
  51. """ Returns the type attribute of node in model as string"""
  52. return mv.read_attrs(model, node)["typeID"]
  53. def get_associations_between(model, node1, node2):
  54. """ Returns a list of association IDs between the nodes node1 and node2 """
  55. edges_n1 = mv.read_outgoing(model, node1, "Edge")
  56. edges_n1.update(mv.read_incoming(model, node1, "Edge"))
  57. edges_n2 = mv.read_outgoing(model, node2, "Edge")
  58. edges_n2.update(mv.read_incoming(model, node2, "Edge"))
  59. return list(edges_n1.intersection(edges_n2))
  60. def get_attributes_of(model, node_type):
  61. # type: (str, str) -> list(Attribute)
  62. """ returns a list of attributes of a node with type node_type in a model """
  63. ret = []
  64. node_attribute_links = mv.all_instances(model, "NodeAttribute")
  65. for link in node_attribute_links:
  66. src_node = mv.read_association_source(model, link)[0]
  67. src_typ = mv.read_attrs(model, src_node)["typeID"]
  68. if src_typ != node_type:
  69. continue
  70. attr_node_name = mv.read_association_destination(model, link)[0]
  71. attr_dict = mv.read_attrs(model, attr_node_name)
  72. attr = Attribute(attr_dict["key"], attr_dict["value"])
  73. ret.append(attr)
  74. return ret
  75. def new_instance_model():
  76. """
  77. Adds a new, empty instance model to the Modelverse.
  78. Returns the name of the new model
  79. """
  80. existing_models = all_instance_models()
  81. idx = 1
  82. nums = []
  83. for model in existing_models:
  84. m = model.split("/")[-1]
  85. try:
  86. idx = int(re.search(r'\d+', m).group())
  87. nums.append(idx)
  88. except AttributeError:
  89. pass
  90. if nums:
  91. idx = sorted(nums)[-1] + 1
  92. im = "models/instance/im{}".format(idx)
  93. print("Adding new instance model {}".format(im))
  94. mv.model_add(im, "formalisms/graphMM")
  95. mid = mv.instantiate(im, "Model")
  96. mv.attr_assign(im, mid, "is_example", False)
  97. mv.attr_assign(im, mid, "descr", "")
  98. return im
  99. def new_example_model():
  100. """
  101. Adds a new, empty example model to the Modelverse.
  102. Returns the name of the new model
  103. """
  104. existing_models = all_example_models()
  105. idx = 1
  106. nums = []
  107. for model in existing_models:
  108. m = model.split("/")[-1]
  109. try:
  110. idx = int(re.search(r'\d+', m).group())
  111. nums.append(idx)
  112. except AttributeError:
  113. pass
  114. if nums:
  115. idx = sorted(nums)[-1] + 1
  116. exm = "models/example/ex{}".format(idx)
  117. print("Adding new example model {}".format(exm))
  118. mv.model_add(exm, "formalisms/graphMM")
  119. mid = mv.instantiate(exm, "Model")
  120. mv.attr_assign(exm, mid, "is_example", True)
  121. mv.attr_assign(exm, mid, "descr", "")
  122. return exm