conformance_scd.alc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. include "primitives.alh"
  2. include "library.alh"
  3. include "object_operations.alh"
  4. include "constructors.alh"
  5. Element function set_copy(elem_to_copy : Element):
  6. Element result
  7. Integer i
  8. Integer max
  9. result = create_node()
  10. // Expand the provided list by including all elements that need to be checked
  11. i = 0
  12. max = read_nr_out(elem_to_copy)
  13. while (i < max):
  14. set_add(result, read_edge_dst(read_out(elem_to_copy, i)))
  15. i = i + 1
  16. return result
  17. Boolean function is_direct_instance(model : Element, instance : Element, type : Element):
  18. // Just check whether or not the type mapping specifies the type as the type of the instance
  19. return dict_read_node(model["type_mapping"], instance) == type
  20. Boolean function is_nominal_instance(model : Element, instance : Element, type : Element):
  21. return is_nominal_subtype(type, dict_read_node(model["type_mapping"], instance), model["metamodel"]["type_mapping"], model["inheritance"])
  22. Boolean function is_nominal_subtype(superclass : Element, subclass : Element, types : Element, inheritance_link : Element):
  23. Integer counter
  24. Integer i
  25. Element edge
  26. Element destination
  27. // End of recursion
  28. if (superclass == subclass):
  29. return True
  30. // Iterate over all superclasses of the found class
  31. counter = read_nr_out(subclass)
  32. i = 0
  33. while (i < counter):
  34. edge = read_out(subclass, i)
  35. // Check if it even has a type (to prevent errors)
  36. if (dict_in_node(types, edge)):
  37. // Check whether it is an inheritance edge, as there is no other distinction between them
  38. if (dict_read_node(types, edge) == inheritance_link):
  39. // It is an inheritance edge, so follow it to its destination
  40. destination = read_edge_dst(edge)
  41. // Found a new superclass to test
  42. if (is_nominal_subtype(superclass, destination, types, inheritance_link)):
  43. return True
  44. i = i + 1
  45. // No link seems to have been found, so it is False
  46. return False
  47. Boolean function is_structural_instance(model : Element, instance : Element, type : Element):
  48. return is_structural_subtype(dict_read_node(model["type_mapping"], instance), type)
  49. Boolean function is_structural_subtype(subtype : Element, supertype : Element):
  50. // Determine whether it is just the exact type or not
  51. if (subtype == supertype):
  52. return True
  53. // Find all links that are required (name and type) from the specified type
  54. Element required_keys
  55. required_keys = dict_keys(supertype)
  56. Integer required_keys_len
  57. required_keys_len = dict_len(required_keys)
  58. String key
  59. Element equivalent
  60. Integer i
  61. i = 0
  62. // Go over all keys that we require
  63. while (i < required_keys_len):
  64. key = set_pop(required_keys)
  65. // Check whether they exist in the instance
  66. if (dict_in(subtype, key)):
  67. // Normally, we should still check whether they don't violate the constraints imposed on the class (i.e., are actually present)
  68. // For now, we ignore this and simply require that it is always there in the metamodel (not necessarily in the instance)
  69. // TODO
  70. // Still check whether the types match
  71. if (bool_not(is_structural_subtype(subtype[key], supertype[key]))):
  72. return False
  73. // All clear, so pass on to the next attribute
  74. i = i + 1
  75. else:
  76. return False
  77. // No violations found, so OK
  78. return True
  79. String function conformance_scd(model : Element):
  80. // Initialization
  81. Element work_node
  82. Element model_src
  83. Element metamodel_src
  84. Element model_dst
  85. Element metamodel_dst
  86. Element models
  87. Element metamodels
  88. models = set_copy(model["model"])
  89. Element typing
  90. typing = model["type_mapping"]
  91. metamodels = set_copy(model["metamodel"]["model"])
  92. Element inheritance
  93. inheritance = model["metamodel"]["inheritance"]
  94. Element metamodel_typing
  95. metamodel_typing = model["metamodel"]["type_mapping"]
  96. // Iterate over all model elements and check if they are typed (in "typing") and their type is in the metamodel
  97. while (dict_len(models) > 0):
  98. work_node = set_pop(models)
  99. // Basic check: does the element have a type
  100. if (bool_not(dict_in_node(typing, work_node))):
  101. return "Model has no type specified: " + getName(model, work_node)
  102. // Basic check: is the type of the element part of the metamodel
  103. if (bool_not(set_in_node(metamodels, dict_read_node(typing, work_node)))):
  104. return "Type of element not in specified metamodel: " + getName(model, work_node)
  105. // For edges only: check whether the source is typed according to the metamodel
  106. if (is_edge(work_node)):
  107. model_src = read_edge_src(work_node)
  108. metamodel_src = read_edge_src(dict_read_node(typing, work_node))
  109. if (bool_not(is_nominal_instance(model, model_src, metamodel_src))):
  110. return "Source of model edge not typed by source of type: " + getName(model, work_node)
  111. // For edges only: check whether the destination is typed according to the metamodel
  112. if (is_edge(work_node)):
  113. model_dst = read_edge_dst(work_node)
  114. metamodel_dst = read_edge_dst(dict_read_node(typing, work_node))
  115. if (bool_not(is_nominal_instance(model, model_dst, metamodel_dst))):
  116. return "Destination of model edge not typed by destination of type: " + getName(model, work_node)
  117. // Structure seems fine, now do static semantics
  118. if (dict_in(model["metamodel"], "constraints")):
  119. Element constraint_function
  120. constraint_function = model["metamodel"]["constraints"]
  121. return constraint_function(model)
  122. else:
  123. return "OK"
  124. Element function retype(model : Element, metamodel : Element, inheritance : Element, mapping : Element):
  125. if (dict_in(model, "type_mapping")):
  126. // Remove previous type mappings
  127. dict_delete(model, "type_mapping")
  128. if (dict_in(model, "metamodel")):
  129. // Remove the previous metamodel too, as this might change too
  130. dict_delete(model, "metamodel")
  131. if (dict_in(model, "inheritance")):
  132. // Remove the inheritance link too, as, yet again, this can vary
  133. dict_delete(model, "inheritance")
  134. // Start the new configuration of the metamodel and inheritance link, as well as set the new mapping relation
  135. dict_add(model, "metamodel", metamodel)
  136. dict_add(model, "inheritance", inheritance)
  137. dict_add(model, "type_mapping", mapping)
  138. return model
  139. Element function add_to_model(model : Element, name : String, element : Element):
  140. if (name == ""):
  141. // No name desired
  142. dict_add(model["model"], "__" + cast_id2s(element), element)
  143. else:
  144. dict_add(model["model"], name, element)
  145. return element
  146. Element function instantiate_bottom_node(model : Element, name : String):
  147. Element new_element
  148. new_element = create_node()
  149. return add_to_model(model, name, new_element)
  150. Element function instantiate_bottom_value(model : Element, name : String, value : Element):
  151. Element new_element
  152. new_element = create_value(value)
  153. return add_to_model(model, name, new_element)
  154. Element function instantiate_bottom_edge(model : Element, name : String, source : Element, target : Element):
  155. Element new_element
  156. new_element = create_edge(source, target)
  157. return add_to_model(model, name, new_element)
  158. Element function set_model_constraints(model : Element, func : Element):
  159. if (dict_in(model, "constraints")):
  160. dict_delete(model, "constraints")
  161. dict_add(model, "constraints", func)
  162. return model
  163. Element function instantiate_model_lib(model : Element, type : Element, name : String, optionals : Element, attribute_types : Element, attribute_instances : Element):
  164. Element new_element
  165. if (is_edge(type)):
  166. // Create a new edge from "optionals[0]" to "optionals[1]"
  167. new_element = instantiate_bottom_edge(model, name, list_read(optionals, 0), list_read(optionals, 1))
  168. else:
  169. if (typeof(type) == Type):
  170. new_element = instantiate_bottom_value(model, name, list_read(optionals, 0))
  171. else:
  172. new_element = instantiate_bottom_node(model, name)
  173. // Add it to the type mapping
  174. dict_add(model["type_mapping"], new_element, type)
  175. // Add all attribute types at this level
  176. Integer counter
  177. Integer max
  178. Element keys
  179. keys = dict_keys(attribute_types)
  180. counter = 0
  181. max = list_len(keys)
  182. Element attr_name
  183. Element attr_type
  184. Element created_attr
  185. Element created_edge
  186. Element metamodel
  187. metamodel = model["metamodel"]["model"]
  188. // For all new attributes
  189. while (counter < max):
  190. attr_name = set_pop(keys)
  191. attr_type = attribute_types[attr_name]
  192. created_attr = create_edge(new_element, attr_type)
  193. created_edge = create_edge(created_attr, attr_name)
  194. Element m
  195. Element tm
  196. m = model["model"]
  197. tm = model["type_mapping"]
  198. // Add it to the model
  199. dict_add(m, "__" + cast_id2s(attr_name), attr_name)
  200. dict_add(m, "__" + cast_id2s(attr_type), attr_type)
  201. dict_add(m, "__" + cast_id2s(created_attr), created_attr)
  202. dict_add(m, "__" + cast_id2s(created_edge), created_edge)
  203. // And add the typing
  204. dict_add(tm, attr_name, metamodel["__String"])
  205. dict_add(tm, attr_type, metamodel["Type"])
  206. dict_add(tm, created_attr, metamodel["Attribute"])
  207. dict_add(tm, created_edge, metamodel["__Name"])
  208. // Increase while loop counter
  209. counter = counter + 1
  210. // Similarly for instantiated attributes
  211. counter = 0
  212. keys = dict_keys(attribute_instances)
  213. max = list_len(keys)
  214. Element attr_definer_class
  215. Element attr_type_edge
  216. Element attr_value
  217. Element attr_edge
  218. while (counter < max):
  219. // Look it up
  220. attr_name = set_pop(keys)
  221. attr_value = attribute_instances[attr_name]
  222. attr_definer_class = find_attribute(type, attr_name, model["metamodel"]["type_mapping"], model["inheritance"])
  223. attr_type = attr_definer_class[attr_name]
  224. attr_type_edge = dict_read_edge(attr_definer_class, attr_name)
  225. attr_edge = create_edge(new_element, attr_value)
  226. // Add to model
  227. dict_add(model["model"], "__" + cast_id2s(attr_value), attr_value)
  228. dict_add(model["model"], "__" + cast_id2s(attr_edge), attr_edge)
  229. // Type the new elements
  230. dict_add(model["type_mapping"], attr_value, attr_type)
  231. dict_add(model["type_mapping"], attr_edge, attr_type_edge)
  232. counter = counter + 1
  233. return new_element
  234. Element function instantiate_new_model(metamodel : Element, inheritance : Element):
  235. Element model
  236. model = create_node()
  237. dict_add(model, "model", create_node())
  238. dict_add(model, "type_mapping", create_node())
  239. dict_add(model, "metamodel", metamodel)
  240. dict_add(model, "inheritance", inheritance)
  241. return model
  242. Element function generate_bottom_type_mapping(model : Element):
  243. Element mm
  244. mm = model["metamodel"]["model"]
  245. dict_delete(model, "type_mapping")
  246. Element tm
  247. tm = create_node()
  248. dict_add(model, "type_mapping", tm)
  249. // Iterate over every element
  250. Element elem_keys
  251. Element elem
  252. elem_keys = dict_keys(model["model"])
  253. while (0 < read_nr_out(elem_keys)):
  254. elem = model["model"][set_pop(elem_keys)]
  255. if (is_edge(elem)):
  256. dict_add(tm, elem, mm["Edge"])
  257. else:
  258. if (cast_v2s(elem) != "None"):
  259. dict_add(tm, elem, mm[cast_v2s(typeof(elem))])
  260. else:
  261. dict_add(tm, elem, mm["Node"])
  262. return model