conformance_scd.alc 12 KB

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