object_operations.alc 5.1 KB

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