conformance_scd.alc 11 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(dict_read(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(dict_read(model, "type_mapping"), instance), dict_read(dict_read(model, "metamodel"), "type_mapping"), dict_read(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(dict_read(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(dict_read(subtype, key), dict_read(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(dict_read(model, "model"))
  89. Element typing
  90. typing = dict_read(model, "type_mapping")
  91. metamodels = set_copy(dict_read(dict_read(model, "metamodel"), "model"))
  92. Element inheritance
  93. inheritance = dict_read(dict_read(model, "metamodel"), "inheritance")
  94. Element metamodel_typing
  95. metamodel_typing = dict_read(dict_read(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. // Basic check: type of the value agrees with the actual type
  106. // this is always checked, as it falls back to a sane default for non-values
  107. if (bool_not(type_eq(dict_read_node(typing, work_node), typeof(work_node)))):
  108. return "Primitive type does not agree with actual type: " + getName(model, work_node)
  109. // For edges only: check whether the source is typed according to the metamodel
  110. if (is_edge(work_node)):
  111. model_src = read_edge_src(work_node)
  112. metamodel_src = read_edge_src(dict_read_node(typing, work_node))
  113. if (bool_not(is_nominal_instance(model, model_src, metamodel_src))):
  114. return "Source of model edge not typed by source of type: " + getName(model, work_node)
  115. // For edges only: check whether the destination is typed according to the metamodel
  116. if (is_edge(work_node)):
  117. model_dst = read_edge_dst(work_node)
  118. metamodel_dst = read_edge_dst(dict_read_node(typing, work_node))
  119. if (bool_not(is_nominal_instance(model, model_dst, metamodel_dst))):
  120. return "Destination of model edge not typed by destination of type: " + getName(model, work_node)
  121. // Structure seems fine, now do static semantics
  122. if (dict_in(dict_read(model, "metamodel"), "constraints")):
  123. Element constraint_function
  124. constraint_function = dict_read(dict_read(model, "metamodel"), "constraints")
  125. return constraint_function(model)
  126. else:
  127. return "OK"
  128. Element function retype(model : Element, metamodel : Element, inheritance : Element, mapping : Element):
  129. if (dict_in(model, "type_mapping")):
  130. // Remove previous type mappings
  131. dict_delete(model, "type_mapping")
  132. if (dict_in(model, "metamodel")):
  133. // Remove the previous metamodel too, as this might change too
  134. dict_delete(model, "metamodel")
  135. if (dict_in(model, "inheritance")):
  136. // Remove the inheritance link too, as, yet again, this can vary
  137. dict_delete(model, "inheritance")
  138. // Start the new configuration of the metamodel and inheritance link, as well as set the new mapping relation
  139. dict_add(model, "metamodel", metamodel)
  140. dict_add(model, "inheritance", inheritance)
  141. dict_add(model, "type_mapping", mapping)
  142. return model
  143. Element function add_to_model(model : Element, name : String, element : Element):
  144. if (name == ""):
  145. // No name desired
  146. dict_add(dict_read(model, "model"), "__" + cast_id2s(element), element)
  147. else:
  148. dict_add(dict_read(model, "model"), name, element)
  149. return element
  150. Element function instantiate_bottom_node(model : Element, name : String):
  151. Element new_element
  152. new_element = create_node()
  153. return add_to_model(model, name, new_element)
  154. Element function instantiate_bottom_value(model : Element, name : String, value : Element):
  155. Element new_element
  156. new_element = create_value(value)
  157. return add_to_model(model, name, new_element)
  158. Element function instantiate_bottom_edge(model : Element, name : String, source : Element, target : Element):
  159. Element new_element
  160. new_element = create_edge(source, target)
  161. return add_to_model(model, name, new_element)
  162. Element function set_model_constraints(model : Element, func : Element):
  163. if (dict_in(model, "constraints")):
  164. dict_delete(model, "constraints")
  165. dict_add(model, "constraints", func)
  166. return model
  167. Element function instantiate_model_lib(model : Element, type : Element, name : String, optionals : Element, attribute_types : Element, attribute_instances : Element):
  168. Element new_element
  169. if (is_edge(type)):
  170. // Create a new edge from "optionals[0]" to "optionals[1]"
  171. new_element = instantiate_bottom_edge(model, name, list_read(optionals, 0), list_read(optionals, 1))
  172. else:
  173. if (typeof(type) == Type):
  174. new_element = instantiate_bottom_value(model, name, list_read(optionals, 0))
  175. else:
  176. new_element = instantiate_bottom_node(model, name)
  177. // Add it to the type mapping
  178. dict_add(dict_read(model, "type_mapping"), new_element, type)
  179. // Add all attribute types at this level
  180. Integer counter
  181. Integer max
  182. Element keys
  183. keys = dict_keys(attribute_types)
  184. counter = 0
  185. max = list_len(keys)
  186. Element attr_name
  187. Element attr_type
  188. Element created_attr
  189. Element created_edge
  190. Element metamodel
  191. metamodel = dict_read(dict_read(model, "metamodel"), "model")
  192. // For all new attributes
  193. while (counter < max):
  194. attr_name = set_pop(keys)
  195. attr_type = dict_read(attribute_types, attr_name)
  196. created_attr = create_edge(new_element, attr_type)
  197. created_edge = create_edge(created_attr, attr_name)
  198. // Add it to the model
  199. dict_add(dict_read(model, "model"), "__" + cast_id2s(attr_name), attr_name)
  200. dict_add(dict_read(model, "model"), "__" + cast_id2s(attr_type), attr_type)
  201. dict_add(dict_read(model, "model"), "__" + cast_id2s(created_attr), created_attr)
  202. dict_add(dict_read(model, "model"), "__" + cast_id2s(created_edge), created_edge)
  203. // And add the typing
  204. dict_add(dict_read(model, "type_mapping"), attr_name, dict_read(metamodel, "__String"))
  205. dict_add(dict_read(model, "type_mapping"), attr_type, dict_read(metamodel, "Type"))
  206. dict_add(dict_read(model, "type_mapping"), created_attr, dict_read(metamodel, "Attribute"))
  207. dict_add(dict_read(model, "type_mapping"), created_edge, dict_read(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 = dict_read(attribute_instances, attr_name)
  222. attr_definer_class = find_attribute(type, attr_name, dict_read(dict_read(model, "metamodel"), "type_mapping"), dict_read(model, "inheritance"))
  223. attr_type = dict_read(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(dict_read(model, "model"), "__" + cast_id2s(attr_value), attr_value)
  228. dict_add(dict_read(model, "model"), "__" + cast_id2s(attr_edge), attr_edge)
  229. // Type the new elements
  230. dict_add(dict_read(model, "type_mapping"), attr_value, attr_type)
  231. dict_add(dict_read(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 = dict_read(dict_read(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(dict_read(model, "model"))
  253. while (0 < read_nr_out(elem_keys)):
  254. elem = dict_read(dict_read(model, "model"), set_pop(elem_keys))
  255. if (is_edge(elem)):
  256. dict_add(tm, elem, dict_read(mm, "Edge"))
  257. else:
  258. if (cast_v2s(elem) != "None"):
  259. dict_add(tm, elem, dict_read(mm, cast_v2s(typeof(elem))))
  260. else:
  261. dict_add(tm, elem, dict_read(mm, "Node"))
  262. return model