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