conformance_scd.alc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. include "primitives.alh"
  2. include "library.alh"
  3. include "object_operations.alh"
  4. include "constructors.alh"
  5. include "modelling.alh"
  6. Boolean function wrap_conformance(model : Element):
  7. String result
  8. result = conformance_scd(model)
  9. output("Success: " + result)
  10. if (result == "OK"):
  11. return True!
  12. else:
  13. return False!
  14. Boolean function is_direct_instance(model : Element, instance : String, type : String):
  15. // Just check whether or not the type mapping specifies the type as the type of the instance
  16. return value_eq(model["type_mapping"][instance], type)!
  17. Boolean function is_nominal_instance(model : Element, instance : String, type : String):
  18. if (bool_not(dict_in(model["metamodel"]["model"], type))):
  19. // type is not even in the specified metamodel, so this will never work
  20. return False!
  21. if (bool_and(is_edge(model["metamodel"]["model"][type]), bool_not(is_edge(model["model"][instance])))):
  22. // type is an edge, but we aren't
  23. return False!
  24. if (bool_not(dict_in(model["type_mapping"], instance))):
  25. // doesn't even have a type
  26. return False!
  27. if (value_eq(model["type_mapping"][instance], type)):
  28. return True!
  29. return is_nominal_subtype(model["metamodel"], model["type_mapping"][instance], type)!
  30. Boolean function is_nominal_subtype(metamodel : Element, subclass : String, superclass : String):
  31. if (element_eq(metamodel["model"][subclass], metamodel["model"][superclass])):
  32. return True!
  33. if (bool_not(dict_in(metamodel["model"], subclass))):
  34. return False!
  35. if (bool_not(dict_in(metamodel["model"], superclass))):
  36. return False!
  37. return set_in(get_superclasses(metamodel, subclass), superclass)!
  38. Element function precompute_cardinalities(model : Element):
  39. Integer slc
  40. Integer suc
  41. Integer tlc
  42. Integer tuc
  43. String key
  44. Element tmp_dict
  45. Element cardinalities
  46. Element keys
  47. Element metamodel
  48. Boolean optional
  49. metamodel = model["metamodel"]
  50. keys = allInstances(metamodel, "Association")
  51. cardinalities = dict_create()
  52. while (0 < list_len(keys)):
  53. key = set_pop(keys)
  54. tmp_dict = dict_create()
  55. slc = read_attribute(metamodel, key, "source_lower_cardinality")
  56. suc = read_attribute(metamodel, key, "source_upper_cardinality")
  57. tlc = read_attribute(metamodel, key, "target_lower_cardinality")
  58. tuc = read_attribute(metamodel, key, "target_upper_cardinality")
  59. if (element_neq(slc, read_root())):
  60. dict_add_fast(tmp_dict, "slc", slc)
  61. if (element_neq(suc, read_root())):
  62. dict_add_fast(tmp_dict, "suc", suc)
  63. if (element_neq(tlc, read_root())):
  64. dict_add_fast(tmp_dict, "tlc", tlc)
  65. if (element_neq(tuc, read_root())):
  66. dict_add_fast(tmp_dict, "tuc", tuc)
  67. if (list_len(tmp_dict) > 0):
  68. dict_add_fast(cardinalities, key, tmp_dict)
  69. keys = allInstances(metamodel, "AttributeLink")
  70. while (0 < list_len(keys)):
  71. key = set_pop(keys)
  72. tmp_dict = dict_create()
  73. // Attributes always have 1 max
  74. dict_add_fast(tmp_dict, "tuc", 1)
  75. // Depending on whether it is optional or not, the cardinality is changed
  76. optional = read_attribute(metamodel, key, "optional")
  77. if (optional):
  78. dict_add_fast(tmp_dict, "tlc", 0)
  79. else:
  80. dict_add_fast(tmp_dict, "tlc", 1)
  81. if (list_len(tmp_dict) > 0):
  82. dict_add_fast(cardinalities, key, tmp_dict)
  83. return cardinalities!
  84. String function conformance_scd(model : Element):
  85. // Initialization
  86. Element keys
  87. Element metamodel
  88. Element typing
  89. String model_name
  90. String src_model
  91. String dst_model
  92. String src_metamodel
  93. String dst_metamodel
  94. Element element
  95. Element check_list
  96. String check_type
  97. Element cardinalities
  98. Element scd
  99. Integer instances
  100. String type_name
  101. Element spo_cache
  102. Element spi_cache
  103. Element constraint_function
  104. Element reverse_m
  105. Element reverse_mm
  106. reverse_m = make_reverse_dictionary(model["model"])
  107. reverse_mm = make_reverse_dictionary(model["metamodel"]["model"])
  108. spo_cache = dict_create()
  109. spi_cache = dict_create()
  110. // Load in variables
  111. scd = import_node("models/SimpleClassDiagrams")
  112. metamodel = model["metamodel"]
  113. typing = model["type_mapping"]
  114. // Create dictionary with all associations and allowed cardinalities
  115. if (list_len(model["model"]) > 0):
  116. cardinalities = precompute_cardinalities(model)
  117. // Iterate over each element of the model, finding out whether everything is fine
  118. keys = dict_keys(model["model"])
  119. while (0 < list_len(keys)):
  120. model_name = set_pop(keys)
  121. type_name = read_type(model, model_name)
  122. element = model["model"][model_name]
  123. log("Check " + model_name)
  124. log(" : " + type_name)
  125. if (bool_not(dict_in(typing, model_name))):
  126. return "Model has no type specified: " + model_info(model, model_name)!
  127. if (bool_not(dict_in(metamodel["model"], typing[model_name]))):
  128. return "Type of element not in specified metamodel: " + model_info(model, model_name)!
  129. if (is_edge(element)):
  130. src_model = reverse_m[cast_id2s(read_edge_src(element))]
  131. dst_model = reverse_m[cast_id2s(read_edge_dst(element))]
  132. src_metamodel = reverse_mm[cast_id2s(read_edge_src(metamodel["model"][typing[model_name]]))]
  133. dst_metamodel = reverse_mm[cast_id2s(read_edge_dst(metamodel["model"][typing[model_name]]))]
  134. if (bool_not(is_nominal_instance(model, src_model, src_metamodel))):
  135. log("got: " + src_model)
  136. log("expected: " + src_metamodel)
  137. return "Source of model edge not typed by source of type: " + model_info(model, model_name)!
  138. if (bool_not(is_nominal_instance(model, dst_model, dst_metamodel))):
  139. log("got: " + dst_model)
  140. log("expected: " + dst_metamodel)
  141. return "Destination of model edge not typed by destination of type: " + model_info(model, model_name)!
  142. // Check cardinality for all of our edges
  143. //
  144. // SLC..SUC TLC..TUC
  145. // A ---------------------> B
  146. //
  147. // First the incoming, so we are at B in the above figure
  148. if (bool_not(dict_in(spo_cache, type_name))):
  149. dict_add_fast(spo_cache, type_name, selectPossibleOutgoing(metamodel, type_name, dict_keys(cardinalities)))
  150. check_list = set_copy(spo_cache[type_name])
  151. while (0 < list_len(check_list)):
  152. check_type = set_pop(check_list)
  153. if (dict_in(cardinalities, check_type)):
  154. // Cardinalities defined for this association, so check them
  155. if (bool_or(dict_in(cardinalities[check_type], "tlc"), dict_in(cardinalities[check_type], "tuc"))):
  156. instances = list_len(allOutgoingAssociationInstances(model, model_name, check_type))
  157. if (dict_in(cardinalities[check_type], "tlc")):
  158. // A lower cardinality was defined at the target
  159. if (integer_gt(cardinalities[check_type]["tlc"], instances)):
  160. String error
  161. error = (("Lower cardinality violation for outgoing edge of type " + check_type) + " at ") + model_info(model, model_name)
  162. return error!
  163. if (dict_in(cardinalities[check_type], "tuc")):
  164. // An upper cardinality was defined at the target
  165. if (integer_lt(cardinalities[check_type]["tuc"], instances)):
  166. String error
  167. error = (("Upper cardinality violation for outgoing edge of type " + check_type) + " at ") + model_info(model, model_name)
  168. return error!
  169. // Identical, but for outgoing, and thus for A in the figure
  170. if (bool_not(dict_in(spi_cache, type_name))):
  171. dict_add_fast(spi_cache, type_name, selectPossibleIncoming(metamodel, type_name, dict_keys(cardinalities)))
  172. check_list = set_copy(spi_cache[type_name])
  173. while (0 < list_len(check_list)):
  174. check_type = set_pop(check_list)
  175. if (dict_in(cardinalities, check_type)):
  176. // Cardinalities defined for this association, so check them
  177. if (bool_or(dict_in(cardinalities[check_type], "slc"), dict_in(cardinalities[check_type], "suc"))):
  178. instances = list_len(allIncomingAssociationInstances(model, model_name, check_type))
  179. if (dict_in(cardinalities[check_type], "slc")):
  180. // A lower cardinality was defined at the source
  181. if (integer_gt(cardinalities[check_type]["slc"], instances)):
  182. String error
  183. error = (("Lower cardinality violation for incoming edge of type " + check_type) + " at ") + model_info(model, model_name)
  184. return error!
  185. if (dict_in(cardinalities[check_type], "suc")):
  186. // An upper cardinality was defined at the source
  187. if (integer_lt(cardinalities[check_type]["suc"], instances)):
  188. String error
  189. error = (("Upper cardinality violation for incoming edge of type " + check_type) + " at ") + model_info(model, model_name)
  190. return error!
  191. constraint_function = read_attribute(metamodel, typing[model_name], "constraint")
  192. if (element_neq(constraint_function, read_root())):
  193. String result
  194. Element func
  195. func = get_func_AL_model(import_node(constraint_function))
  196. result = func(model, model_name)
  197. if (result != "OK"):
  198. return result!
  199. // Check multiplicities, if they are defined (optional)
  200. Element metamodel_keys
  201. String metamodel_element
  202. Element mm_model
  203. Integer attr_value
  204. mm_model = metamodel["model"]
  205. metamodel_keys = dict_keys(mm_model)
  206. while (read_nr_out(metamodel_keys) > 0):
  207. metamodel_element = set_pop(metamodel_keys)
  208. // Lower multiplicities
  209. attr_value = read_attribute(metamodel, metamodel_element, "lower_cardinality")
  210. if (element_neq(attr_value, read_root())):
  211. // We have defined a lower cardinality, so check number of instances!
  212. if (attr_value > list_len(allInstances(model, metamodel_element))):
  213. return "Lower cardinality violated for class: " + metamodel_element!
  214. // Upper multiplicities
  215. attr_value = read_attribute(metamodel, metamodel_element, "upper_cardinality")
  216. if (element_neq(attr_value, read_root())):
  217. // We have defined a lower cardinality, so check number of instances!
  218. if (attr_value < list_len(allInstances(model, metamodel_element))):
  219. return "Upper cardinality violated for class: " + metamodel_element!
  220. // Check all ComplexAttributes recursively
  221. Element all_complex_types
  222. Element complex_instances
  223. String complex_instance
  224. String complex_type
  225. String result
  226. all_complex_types = allInstances(model["metamodel"]["metamodel"], "ComplexAttribute")
  227. while (read_nr_out(all_complex_types) > 0):
  228. complex_type = set_pop(all_complex_types)
  229. complex_instances = allInstances(model, complex_type)
  230. while (read_nr_out(complex_instances) > 0):
  231. complex_instance = set_pop(complex_instances)
  232. complex_type = read_attribute(model["metamodel"], read_type(model, complex_instance), "type")
  233. result = check_location_conformance(model["model"][complex_instance], complex_type)
  234. if (result != "OK"):
  235. return ((("Complex attribute doesn't match for: " + complex_instance) + "\n Message: ") + result)!
  236. // Structure seems fine, now do global constraints
  237. Element global_constraints
  238. String constraint
  239. Element func
  240. global_constraints = allInstances(model, "GlobalConstraint")
  241. while (read_nr_out(global_constraints) > 0):
  242. constraint = set_pop(global_constraints)
  243. func = get_func_AL_model(import_node(read_attribute(model, constraint, "global_constraint")))
  244. result = func(model)
  245. if (result != "OK"):
  246. return result!
  247. return "OK"!
  248. String function check_location_conformance(instance_location : String, type_location : String):
  249. // Check whether the instance is typed by the type
  250. Element instance
  251. Element type
  252. instance = import_node(instance_location)
  253. type = import_node(type_location)
  254. if (element_neq(instance["metamodel"], type)):
  255. return "Instance not statically typed by specified metamodel"!
  256. return conformance_scd(instance)!
  257. Element function set_model_constraints(model : Element, func : Element):
  258. if (dict_in(model, "constraints")):
  259. dict_delete(model, "constraints")
  260. dict_add_fast(model, "constraints", func)
  261. return model!
  262. String function model_info(model : Element, name : String):
  263. return name!
  264. Element function generate_bottom_type_mapping(model_dict : Element):
  265. // The model is only a dictionary of elements, being the usual model["model"] value.
  266. // This function returns the type mapping dictionary
  267. // There are only two concepts that we can link to: "Node" and "Edge".
  268. // Iterate over each element in the model
  269. Element result
  270. Element elements
  271. String element
  272. elements = dict_keys(model_dict)
  273. result = dict_create()
  274. while (set_len(elements) > 0):
  275. element = set_pop(elements)
  276. if (is_edge(model_dict[element])):
  277. dict_add_fast(result, element, "Edge")
  278. else:
  279. dict_add_fast(result, element, "Node")
  280. return result!