object_operations.alc 5.0 KB

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