commons.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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_all_attributes_of_type(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 get_attributes_of_node(model, node_id):
  76. # type: (str, str) -> list(Attribute)
  77. """ Returns a list of attributes of a specific node with id node_id """
  78. ret = []
  79. outgoings = mv.read_outgoing(model, node_id, "NodeAttribute")
  80. if not outgoings:
  81. return []
  82. for link in outgoings:
  83. dest = mv.read_association_destination(model, link)[0]
  84. attr_dict = mv.read_attrs(model, dest)
  85. attr = Attribute(attr_dict["key"], attr_dict["value"])
  86. ret.append(attr)
  87. return ret
  88. def new_instance_model():
  89. """
  90. Adds a new, empty instance model to the Modelverse.
  91. Returns the name of the new model
  92. """
  93. existing_models = all_instance_models()
  94. idx = 1
  95. nums = []
  96. for model in existing_models:
  97. m = model.split("/")[-1]
  98. try:
  99. idx = int(re.search(r'\d+', m).group())
  100. nums.append(idx)
  101. except AttributeError:
  102. pass
  103. if nums:
  104. idx = sorted(nums)[-1] + 1
  105. im = "models/instance/im{}".format(idx)
  106. print("Adding new instance model {}".format(im))
  107. mv.model_add(im, "formalisms/graphMM")
  108. mid = mv.instantiate(im, "Model")
  109. mv.attr_assign(im, mid, "is_example", False)
  110. mv.attr_assign(im, mid, "descr", "")
  111. return im
  112. def new_example_model():
  113. """
  114. Adds a new, empty example model to the Modelverse.
  115. Returns the name of the new model
  116. """
  117. existing_models = all_example_models()
  118. idx = 1
  119. nums = []
  120. for model in existing_models:
  121. m = model.split("/")[-1]
  122. try:
  123. idx = int(re.search(r'\d+', m).group())
  124. nums.append(idx)
  125. except AttributeError:
  126. pass
  127. if nums:
  128. idx = sorted(nums)[-1] + 1
  129. exm = "models/example/ex{}".format(idx)
  130. print("Adding new example model {}".format(exm))
  131. mv.model_add(exm, "formalisms/graphMM")
  132. mid = mv.instantiate(exm, "Model")
  133. mv.attr_assign(exm, mid, "is_example", True)
  134. mv.attr_assign(exm, mid, "descr", "")
  135. return exm