conformance_scd.alc 13 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 counter_copy
  8. Integer max
  9. result = create_node()
  10. // Expand the provided list by including all elements that need to be checked
  11. counter_copy = 0
  12. max = read_nr_out(elem_to_copy)
  13. while (integer_lt(counter_copy, max)):
  14. set_add(result, read_edge_dst(read_out(elem_to_copy, counter_copy)))
  15. counter_copy = integer_addition(counter_copy, 1)
  16. return result
  17. Boolean function is_direct_instance(model_idi : Element, instance_idi : Element, type_idi : Element):
  18. // Just check whether or not the type mapping specifies the type as the type of the instance
  19. return element_eq(dict_read_node(dict_read(model_idi, "type_mapping"), instance_idi), type_idi)
  20. Boolean function is_nominal_instance(model_ini : Element, instance_ini : Element, type_ini : Element):
  21. return is_nominal_subtype(type_ini, dict_read_node(dict_read(model_ini, "type_mapping"), instance_ini), dict_read(dict_read(model_ini, "metamodel"), "type_mapping"), dict_read(model_ini, "inheritance"))
  22. Boolean function is_nominal_subtype(superclass : Element, subclass : Element, types : Element, inheritance_link : Element):
  23. Integer counter_iso
  24. Integer i_iso
  25. Element edge_iso
  26. Element destination_iso
  27. // End of recursion
  28. if (element_eq(superclass, subclass)):
  29. return True
  30. // Iterate over all superclasses of the found class
  31. counter_iso = read_nr_out(subclass)
  32. i_iso = 0
  33. while (integer_lt(i_iso, counter_iso)):
  34. edge_iso = read_out(subclass, i_iso)
  35. // Check if it even has a type (to prevent errors)
  36. if (dict_in_node(types, edge_iso)):
  37. // Check whether it is an inheritance edge, as there is no other distinction between them
  38. if (element_eq(dict_read_node(types, edge_iso), inheritance_link)):
  39. // It is an inheritance edge, so follow it to its destination
  40. destination_iso = read_edge_dst(edge_iso)
  41. // Found a new superclass to test
  42. if (is_nominal_subtype(superclass, destination_iso, types, inheritance_link)):
  43. return True
  44. i_iso = integer_addition(i_iso, 1)
  45. // No link seems to have been found, so it is False
  46. return False
  47. Boolean function is_structural_instance(model_isi : Element, instance_isi : Element, type_isi : Element):
  48. return is_structural_subtype(dict_read_node(dict_read(model_isi, "type_mapping"), instance_isi), type_isi)
  49. Boolean function is_structural_subtype(subtype_isi : Element, supertype_isi : Element):
  50. // Determine whether it is just the exact type or not
  51. if (element_eq(subtype_isi, supertype_isi)):
  52. return True
  53. // Find all links that are required (name and type) from the specified type
  54. Element required_keys_isi
  55. required_keys_isi = dict_keys(supertype_isi)
  56. Integer required_keys_len_isi
  57. required_keys_len_isi = dict_len(required_keys_isi)
  58. String key_isi
  59. Element equivalent_isi
  60. Integer i_isi
  61. i_isi = 0
  62. // Go over all keys that we require
  63. while (integer_lt(i_isi, required_keys_len_isi)):
  64. key_isi = set_pop(required_keys_isi)
  65. // Check whether they exist in the instance
  66. if (dict_in(subtype_isi, key_isi)):
  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_isi, key_isi), dict_read(supertype_isi, key_isi)))):
  72. return False
  73. // All clear, so pass on to the next attribute
  74. i_isi = integer_addition(i_isi, 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_conf
  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 (integer_gt(dict_len(models), 0)):
  98. work_conf = set_pop(models)
  99. // Basic check: does the element have a type
  100. if (bool_not(dict_in_node(typing, work_conf))):
  101. return string_join("Model has no type specified: ", getName(model, work_conf))
  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_conf)))):
  104. return string_join("Type of element not in specified metamodel: ", getName(model, work_conf))
  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_conf), typeof(work_conf)))):
  108. return string_join("Primitive type does not agree with actual type: ", getName(model, work_conf))
  109. // For edges only: check whether the source is typed according to the metamodel
  110. if (is_edge(work_conf)):
  111. model_src = read_edge_src(work_conf)
  112. metamodel_src = read_edge_src(dict_read_node(typing, work_conf))
  113. if (bool_not(is_nominal_instance(model, model_src, metamodel_src))):
  114. return string_join("Source of model edge not typed by source of type: ", getName(model, work_conf))
  115. // For edges only: check whether the destination is typed according to the metamodel
  116. if (is_edge(work_conf)):
  117. model_dst = read_edge_dst(work_conf)
  118. metamodel_dst = read_edge_dst(dict_read_node(typing, work_conf))
  119. if (bool_not(is_nominal_instance(model, model_dst, metamodel_dst))):
  120. return string_join("Destination of model edge not typed by destination of type: ", getName(model, work_conf))
  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_rt : Element, metamodel_rt : Element, inheritance_rt : Element, mapping_rt : Element):
  129. if (dict_in(model_rt, "type_mapping")):
  130. // Remove previous type mappings
  131. dict_delete(model_rt, "type_mapping")
  132. if (dict_in(model_rt, "metamodel")):
  133. // Remove the previous metamodel too, as this might change too
  134. dict_delete(model_rt, "metamodel")
  135. if (dict_in(model_rt, "inheritance")):
  136. // Remove the inheritance link too, as, yet again, this can vary
  137. dict_delete(model_rt, "inheritance")
  138. // Start the new configuration of the metamodel and inheritance link, as well as set the new mapping relation
  139. dict_add(model_rt, "metamodel", metamodel_rt)
  140. dict_add(model_rt, "inheritance", inheritance_rt)
  141. dict_add(model_rt, "type_mapping", mapping_rt)
  142. return model_rt
  143. Element function add_to_model(model_atm : Element, name_atm : String, element_atm : Element):
  144. if (string_eq(name_atm, "")):
  145. // No name desired
  146. dict_add(dict_read(model_atm, "model"), string_join("__", cast_id2s(element_atm)), element_atm)
  147. else:
  148. dict_add(dict_read(model_atm, "model"), name_atm, element_atm)
  149. return element_atm
  150. Element function instantiate_bottom_node(model_bn : Element, name_bn : String):
  151. Element new_element_bn
  152. new_element_bn = create_node()
  153. return add_to_model(model_bn, name_bn, new_element_bn)
  154. Element function instantiate_bottom_value(model_bv : Element, name_bv : String, value_bv : Element):
  155. Element new_element_bv
  156. new_element_bv = create_value(value_bv)
  157. return add_to_model(model_bv, name_bv, new_element_bv)
  158. Element function instantiate_bottom_edge(model_be : Element, name_be : String, source_be : Element, target_be : Element):
  159. Element new_element_be
  160. new_element_be = create_edge(source_be, target_be)
  161. return add_to_model(model_be, name_be, new_element_be)
  162. Element function set_model_constraints(model_con : Element, func_con : Element):
  163. if (dict_in(model_con, "constraints")):
  164. dict_delete(model_con, "constraints")
  165. dict_add(model_con, "constraints", func_con)
  166. return model_con
  167. Element function instantiate_model_lib(model_mo : Element, type_mo : Element, name_mo : String, optionals : Element, attribute_types : Element, attribute_instances : Element):
  168. Element new_element_mo
  169. if (is_edge(type_mo)):
  170. // Create a new edge from "optionals[0]" to "optionals[1]"
  171. new_element_mo = instantiate_bottom_edge(model_mo, name_mo, list_read(optionals, 0), list_read(optionals, 1))
  172. else:
  173. if (type_eq(typeof(type_mo), Type)):
  174. new_element_mo = instantiate_bottom_value(model_mo, name_mo, list_read(optionals, 0))
  175. else:
  176. new_element_mo = instantiate_bottom_node(model_mo, name_mo)
  177. // Add it to the type mapping
  178. dict_add(dict_read(model_mo, "type_mapping"), new_element_mo, type_mo)
  179. // Add all attribute types at this level
  180. Integer counter_mo
  181. Integer max_mo
  182. Element keys_mo
  183. keys_mo = dict_keys(attribute_types)
  184. counter_mo = 0
  185. max_mo = list_len(keys_mo)
  186. Element attr_name_mo
  187. Element attr_type_mo
  188. Element created_attr_mo
  189. Element created_edge_mo
  190. Element metamodel_mo
  191. metamodel_mo = dict_read(dict_read(model_mo, "metamodel"), "model")
  192. // For all new attributes
  193. while (integer_lt(counter_mo, max_mo)):
  194. attr_name_mo = set_pop(keys_mo)
  195. attr_type_mo = dict_read(attribute_types, attr_name_mo)
  196. created_attr_mo = create_edge(new_element_mo, attr_type_mo)
  197. created_edge_mo = create_edge(created_attr_mo, attr_name_mo)
  198. // Add it to the model
  199. dict_add(dict_read(model_mo, "model"), string_join("__", cast_id2s(attr_name_mo)), attr_name_mo)
  200. dict_add(dict_read(model_mo, "model"), string_join("__", cast_id2s(attr_type_mo)), attr_type_mo)
  201. dict_add(dict_read(model_mo, "model"), string_join("__", cast_id2s(created_attr_mo)), created_attr_mo)
  202. dict_add(dict_read(model_mo, "model"), string_join("__", cast_id2s(created_edge_mo)), created_edge_mo)
  203. // And add the typing
  204. dict_add(dict_read(model_mo, "type_mapping"), attr_name_mo, dict_read(metamodel_mo, "__String"))
  205. dict_add(dict_read(model_mo, "type_mapping"), attr_type_mo, dict_read(metamodel_mo, "Type"))
  206. dict_add(dict_read(model_mo, "type_mapping"), created_attr_mo, dict_read(metamodel_mo, "Attribute"))
  207. dict_add(dict_read(model_mo, "type_mapping"), created_edge_mo, dict_read(metamodel_mo, "__Name"))
  208. // Increase while loop counter
  209. counter_mo = integer_addition(counter_mo, 1)
  210. // Similarly for instantiated attributes
  211. counter_mo = 0
  212. keys_mo = dict_keys(attribute_instances)
  213. max_mo = list_len(keys_mo)
  214. Element attr_definer_class_mo
  215. Element attr_type_edge_mo
  216. Element attr_value_mo
  217. Element attr_edge_mo
  218. while (integer_lt(counter_mo, max_mo)):
  219. // Look it up
  220. attr_name_mo = set_pop(keys_mo)
  221. attr_value_mo = dict_read(attribute_instances, attr_name_mo)
  222. attr_definer_class_mo = find_attribute(type_mo, attr_name_mo, dict_read(dict_read(model_mo, "metamodel"), "type_mapping"), dict_read(model_mo, "inheritance"))
  223. attr_type_mo = dict_read(attr_definer_class_mo, attr_name_mo)
  224. attr_type_edge_mo = dict_read_edge(attr_definer_class_mo, attr_name_mo)
  225. attr_edge_mo = create_edge(new_element_mo, attr_value_mo)
  226. // Add to model
  227. dict_add(dict_read(model_mo, "model"), string_join("__", cast_id2s(attr_value_mo)), attr_value_mo)
  228. dict_add(dict_read(model_mo, "model"), string_join("__", cast_id2s(attr_edge_mo)), attr_edge_mo)
  229. // Type the new elements
  230. dict_add(dict_read(model_mo, "type_mapping"), attr_value_mo, attr_type_mo)
  231. dict_add(dict_read(model_mo, "type_mapping"), attr_edge_mo, attr_type_edge_mo)
  232. counter_mo = integer_addition(counter_mo, 1)
  233. return new_element_mo
  234. Element function instantiate_new_model(metamodel_inm : Element, inheritance_inm : Element):
  235. Element model_inm
  236. model_inm = create_node()
  237. dict_add(model_inm, "model", create_node())
  238. dict_add(model_inm, "type_mapping", create_node())
  239. dict_add(model_inm, "metamodel", metamodel_inm)
  240. dict_add(model_inm, "inheritance", inheritance_inm)
  241. return model_inm
  242. Element function generate_bottom_type_mapping(model_tm : Element):
  243. Element mm_tm
  244. mm_tm = dict_read(dict_read(model_tm, "metamodel"), "model")
  245. dict_delete(model_tm, "type_mapping")
  246. Element tm_tm
  247. tm_tm = create_node()
  248. dict_add(model_tm, "type_mapping", tm_tm)
  249. // Iterate over every element
  250. Element elem_keys_tm
  251. Element elem_tm
  252. elem_keys_tm = dict_keys(dict_read(model_tm, "model"))
  253. while (integer_lt(0, read_nr_out(elem_keys_tm))):
  254. elem_tm = dict_read(dict_read(model_tm, "model"), set_pop(elem_keys_tm))
  255. if (is_edge(elem_tm)):
  256. dict_add(tm_tm, elem_tm, dict_read(mm_tm, "Edge"))
  257. else:
  258. if (string_neq(cast_v2s(elem_tm), "None")):
  259. dict_add(tm_tm, elem_tm, dict_read(mm_tm, cast_v2s(typeof(elem_tm))))
  260. else:
  261. dict_add(tm_tm, elem_tm, dict_read(mm_tm, "Node"))
  262. return model_tm