object_operations.alc 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. include "primitives.alh"
  2. include "conformance_scd.alh"
  3. include "constructors.alh"
  4. include "modelling.alh"
  5. include "typing.alh"
  6. Element function allInstances(model : Element, type_name : String):
  7. if (dict_in(model["metamodel"]["model"], type_name)):
  8. Element result
  9. Element accepted
  10. Element keys
  11. Element tm
  12. String key
  13. String class
  14. Element results
  15. result = set_create()
  16. accepted = get_subclasses(model["metamodel"], type_name)
  17. while (set_len(accepted) > 0):
  18. class = set_pop(accepted)
  19. set_merge(result, elements_typed_by(model, class))
  20. return result!
  21. else:
  22. log("No such type in the metamodel: " + type_name)
  23. return set_create()!
  24. Element function selectPossibleIncoming(model : Element, target : String, limit_set : Element):
  25. // Find all possible incoming link types for the target model
  26. // Should also include those specified on the superclass(es)
  27. String type
  28. Element model_dict
  29. Element elem
  30. Element result
  31. Element target_element
  32. result = set_create()
  33. model_dict = model["model"]
  34. while (list_len(limit_set) > 0):
  35. type = set_pop(limit_set)
  36. elem = model_dict[type]
  37. if (is_nominal_subtype(model, target, reverseKeyLookup(model_dict, read_edge_dst(elem)))):
  38. set_add(result, type)
  39. return result!
  40. Element function selectPossibleOutgoing(model : Element, source : String, limit_set : Element):
  41. // Find all possible outgoing link types for the source model
  42. // Should also include those specified on the superclass(es)
  43. String type
  44. Element model_dict
  45. Element elem
  46. Element result
  47. Element source_element
  48. result = set_create()
  49. model_dict = model["model"]
  50. while (list_len(limit_set) > 0):
  51. type = set_pop(limit_set)
  52. elem = model_dict[type]
  53. if (is_nominal_subtype(model, source, reverseKeyLookup(model_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. Element result
  59. Element source
  60. String option
  61. Integer all_out
  62. Integer i
  63. result = set_create()
  64. source = model["model"][source_name]
  65. all_out = read_nr_out(source)
  66. i = 0
  67. while (i < all_out):
  68. option = reverseKeyLookup(model["model"], read_out(source, i))
  69. if (assoc_name == ""):
  70. set_add(result, option)
  71. elif (is_nominal_instance(model, option, assoc_name)):
  72. set_add(result, option)
  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. Element result
  78. Element source
  79. String option
  80. Integer all_out
  81. Integer i
  82. result = set_create()
  83. source = model["model"][target_name]
  84. all_out = read_nr_in(source)
  85. i = 0
  86. while (i < all_out):
  87. option = reverseKeyLookup(model["model"], read_in(source, i))
  88. if (assoc_name == ""):
  89. set_add(result, option)
  90. elif (is_nominal_instance(model, option, assoc_name)):
  91. set_add(result, option)
  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. Element types
  100. Element mm
  101. String attr_type
  102. result = dict_create()
  103. mm = model["metamodel"]["model"]
  104. types = get_superclasses(model["metamodel"], read_type(model, element))
  105. while (set_len(types) > 0):
  106. type = set_pop(types)
  107. // Add our own attributes
  108. keys = dict_keys(mm[type])
  109. while (set_len(keys) > 0):
  110. attr_name = set_pop(keys)
  111. if (is_physical_string(attr_name)):
  112. if (read_type(model["metamodel"], reverseKeyLookup(mm, dict_read_edge(mm[type], attr_name))) == "AttributeLink"):
  113. attr_type = reverseKeyLookup(mm, mm[type][attr_name])
  114. // WARNING: do not change this to dict_add_fast, as this crashes random code...
  115. dict_add(result, attr_name, attr_type)
  116. return result!
  117. Element function getAttributes(model : Element, element : String):
  118. Element result
  119. Element keys
  120. Element type
  121. Element attr_name
  122. Element types
  123. Element mm
  124. result = dict_create()
  125. mm = model["metamodel"]["model"]
  126. types = get_superclasses(model["metamodel"], read_type(model, element))
  127. while (set_len(types) > 0):
  128. type = set_pop(types)
  129. // Add our own attributes
  130. keys = dict_keys(mm[type])
  131. while (set_len(keys) > 0):
  132. attr_name = set_pop(keys)
  133. if (is_physical_string(attr_name)):
  134. // WARNING: do not change this to dict_add_fast, as this crashes random code...
  135. dict_add(result, attr_name, read_attribute(model, element, attr_name))
  136. return result!
  137. Element function getInstantiatableAttributes(model : Element, element : String, type : String):
  138. Element all_links
  139. Element result
  140. String link
  141. result = dict_create()
  142. all_links = allOutgoingAssociationInstances(model, element, type)
  143. while (set_len(all_links) > 0):
  144. link = set_pop(all_links)
  145. // WARNING: do not change this to dict_add_fast, as this crashes random code...
  146. dict_add(result, read_attribute(model, link, "name"), readAssociationDestination(model, link))
  147. return result!
  148. String function print_dict(dict : Element):
  149. Element keys
  150. Element key
  151. String result
  152. keys = dict_keys(dict)
  153. result = ""
  154. while (0 < list_len(keys)):
  155. key = set_pop(keys)
  156. result = result + cast_value(key)
  157. result = result + ": "
  158. result = result + cast_value(dict[key])
  159. result = result + "\n"
  160. return result!
  161. String function readAssociationSource(model : Element, name : String):
  162. return reverseKeyLookup(model["model"], read_edge_src(model["model"][name]))!
  163. String function readAssociationDestination(model : Element, name : String):
  164. return reverseKeyLookup(model["model"], read_edge_dst(model["model"][name]))!
  165. Element function allAssociationDestinations(model : Element, name : String, association_type : String):
  166. Element tmp
  167. Element result
  168. result = set_create()
  169. tmp = allOutgoingAssociationInstances(model, name, association_type)
  170. while (set_len(tmp) > 0):
  171. set_add(result, readAssociationDestination(model, set_pop(tmp)))
  172. return result!
  173. Element function allAssociationOrigins(model : Element, name : String, association_type : String):
  174. Element tmp
  175. Element result
  176. result = set_create()
  177. tmp = allIncomingAssociationInstances(model, name, association_type)
  178. while (set_len(tmp) > 0):
  179. set_add(result, readAssociationSource(model, set_pop(tmp)))
  180. return result!
  181. Element function allowedAssociationsBetween(model : Element, src : String, dst : String):
  182. // Go to the type and find all possibilities
  183. String type
  184. Element all_types
  185. Integer nr_edges
  186. Integer i
  187. Element result
  188. Element edge
  189. String edge_name
  190. String dst_name
  191. result = set_create()
  192. type = read_type(model, src)
  193. all_types = get_superclasses(model["metamodel"], type)
  194. while (set_len(all_types) > 0):
  195. type = set_pop(all_types)
  196. nr_edges = read_nr_out(model["metamodel"]["model"][type])
  197. i = 0
  198. while (i < nr_edges):
  199. edge = read_out(model["metamodel"]["model"][type], i)
  200. edge_name = reverseKeyLookup(model["metamodel"]["model"], edge)
  201. if (dict_in(model["metamodel"]["model"], edge_name)):
  202. // Find destination
  203. dst_name = reverseKeyLookup(model["metamodel"]["model"], read_edge_dst(edge))
  204. if (is_nominal_instance(model, dst, dst_name)):
  205. // Find out whether our dst is an instance of the found destination type
  206. if (is_nominal_instance(model["metamodel"], edge_name, "Association")):
  207. set_add(result, edge_name)
  208. i = i + 1
  209. return result!