object_operations.alc 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. include "primitives.alh"
  2. include "conformance_scd.alh"
  3. include "constructors.alh"
  4. Element function allInstances(model : Element, type_name : String):
  5. Element type_mapping
  6. Element result
  7. Element type
  8. type = model["metamodel"]["model"][type_name]
  9. type_mapping = model["type_mapping"]
  10. result = create_node()
  11. Integer counter
  12. counter = 0
  13. Integer length
  14. length = read_nr_out(type_mapping)
  15. Element edge
  16. while (counter < length):
  17. edge = read_out(type_mapping, counter)
  18. if (element_eq(read_edge_dst(edge), type)):
  19. // Found an element of the specified type
  20. set_add(result, getName(model, read_edge_dst(read_out(edge, 0))))
  21. counter = counter + 1
  22. return result
  23. Element function selectPossibleIncoming(model : Element, target : String, limit_set : Element):
  24. // Find all possible incoming link types for the target model
  25. // Should also include those specified on the superclass(es)
  26. String type
  27. Element metamodel
  28. Element elem
  29. Element result
  30. result = create_node()
  31. metamodel = model["metamodel"]
  32. while (0 < list_len(limit_set)):
  33. type = set_pop(limit_set)
  34. elem = metamodel["model"][type]
  35. if (is_edge(elem)):
  36. if (is_nominal_instance(model, model["model"][target], read_edge_dst(elem))):
  37. set_add(result, type)
  38. return result
  39. Element function selectPossibleOutgoing(model : Element, source : String, limit_set : Element):
  40. // Find all possible outgoing link types for the source model
  41. // Should also include those specified on the superclass(es)
  42. String type
  43. Element metamodel
  44. Element elem
  45. Element result
  46. result = create_node()
  47. metamodel = model["metamodel"]
  48. while (0 < list_len(limit_set)):
  49. type = set_pop(limit_set)
  50. elem = metamodel["model"][type]
  51. if (is_edge(elem)):
  52. if (is_nominal_instance(model, model["model"][source], read_edge_src(elem))):
  53. set_add(result, type)
  54. return result
  55. Element function allOutgoingAssociationInstances(model : Element, source_name : String, assoc_name : String):
  56. // Read out all outgoing edges of the model and select those that are typed by the specified association
  57. // TODO for some reason this crashes if allInstances is used!
  58. Integer nr_out
  59. Integer i
  60. Element out
  61. String out_name
  62. Element result
  63. result = create_node()
  64. nr_out = read_nr_out(model["model"][source_name])
  65. i = 0
  66. while (i < nr_out):
  67. out = read_out(model["model"][source_name], i)
  68. if (set_in_node(model["model"], out)):
  69. out_name = reverseKeyLookup(model["model"], out)
  70. if (is_nominal_instance(model, out, model["metamodel"]["model"][assoc_name])):
  71. set_add(result, out_name)
  72. i = i + 1
  73. return result
  74. Element function allIncomingAssociationInstances(model : Element, target_name : String, assoc_name : String):
  75. // Read out all outgoing edges of the model and select those that are typed by the specified association
  76. Integer nr_in
  77. Integer i
  78. Element in
  79. String in_name
  80. Element result
  81. result = create_node()
  82. nr_in = read_nr_in(model["model"][target_name])
  83. i = 0
  84. while (i < nr_in):
  85. in = read_in(model["model"][target_name], i)
  86. if (set_in_node(model["model"], in)):
  87. in_name = reverseKeyLookup(model["model"], in)
  88. if (is_nominal_instance(model, in, model["metamodel"]["model"][assoc_name])):
  89. set_add(result, in_name)
  90. i = i + 1
  91. return result
  92. Element function getAttributeList(model : Element, element : String):
  93. Element result
  94. Element keys
  95. Element type
  96. Element attr_name
  97. String attr_type
  98. result = create_node()
  99. type = dict_read_node(model["type_mapping"], model["model"][element])
  100. keys = dict_keys(type)
  101. // Add our own attributes
  102. while (0 < list_len(keys)):
  103. attr_name = set_pop(keys)
  104. if (is_physical_string(attr_name)):
  105. attr_type = getName(model["metamodel"], type[attr_name])
  106. dict_add(result, attr_name, attr_type)
  107. // Go on to the metalevel
  108. // TODO
  109. return result
  110. Element function getInstantiatableAttributes(model : Element, element : String):
  111. Element result
  112. result = create_node()
  113. // Get all outgoing "dictionary" links
  114. Element set_own
  115. Element elem
  116. elem = model["model"][element]
  117. set_own = dict_keys(element)
  118. // Filter them
  119. Element e
  120. while (0 < read_nr_out(set_own)):
  121. e = set_pop(set_own)
  122. if (is_physical_string(e)):
  123. dict_add(result, e, getName(model, element[e]))
  124. return result
  125. String function getName(model : Element, element : Element):
  126. return reverseKeyLookup(model["model"], element)
  127. // TODO Utility functions!
  128. String function reverseKeyLookup(dict : Element, element : Element):
  129. Element elements
  130. String name
  131. elements = dict_keys(dict)
  132. while (0 < list_len(elements)):
  133. name = set_pop(elements)
  134. if (element_eq(dict[name], element)):
  135. return name
  136. return string_join(string_join("(unknown: ", cast_e2s(element)), " )")
  137. String function print_dict(dict : Element):
  138. Element keys
  139. Element key
  140. String result
  141. keys = dict_keys(dict)
  142. result = ""
  143. while (0 < list_len(keys)):
  144. key = set_pop(keys)
  145. result = result + cast_v2s(key)
  146. result = result + ": "
  147. result = result + cast_v2s(dict[key])
  148. result = result + "\n"
  149. return result