conformance_scd.alc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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. spo_cache = create_node()
  97. spi_cache = create_node()
  98. // Load in variables
  99. scd = import_node("models/SimpleClassDiagrams")
  100. metamodel = model["metamodel"]
  101. typing = model["type_mapping"]
  102. // Create dictionary with all associations and allowed cardinalities
  103. if (list_len(model["model"]) > 0):
  104. cardinalities = precompute_cardinalities(model)
  105. // Iterate over each element of the model, finding out whether everything is fine
  106. keys = dict_keys(model["model"])
  107. while (0 < list_len(keys)):
  108. model_name = set_pop(keys)
  109. type_name = read_type(model, model_name)
  110. element = model["model"][model_name]
  111. log("Check " + model_name)
  112. log(" : " + type_name)
  113. if (bool_not(dict_in(typing, model_name))):
  114. return "Model has no type specified: " + model_info(model, model_name)!
  115. if (bool_not(dict_in(metamodel["model"], typing[model_name]))):
  116. return "Type of element not in specified metamodel: " + model_info(model, model_name)!
  117. if (is_edge(element)):
  118. src_model = reverseKeyLookup(model["model"], read_edge_src(element))
  119. dst_model = reverseKeyLookup(model["model"], read_edge_dst(element))
  120. src_metamodel = reverseKeyLookup(metamodel["model"], read_edge_src(metamodel["model"][typing[model_name]]))
  121. dst_metamodel = reverseKeyLookup(metamodel["model"], read_edge_dst(metamodel["model"][typing[model_name]]))
  122. if (bool_not(is_nominal_instance(model, src_model, src_metamodel))):
  123. log("got: " + src_model)
  124. log("expected: " + src_metamodel)
  125. return "Source of model edge not typed by source of type: " + model_info(model, model_name)!
  126. if (bool_not(is_nominal_instance(model, dst_model, dst_metamodel))):
  127. log("got: " + dst_model)
  128. log("expected: " + dst_metamodel)
  129. return "Destination of model edge not typed by destination of type: " + model_info(model, model_name)!
  130. // Check cardinality for all of our edges
  131. //
  132. // SLC..SUC TLC..TUC
  133. // A ---------------------> B
  134. //
  135. // First the incoming, so we are at B in the above figure
  136. if (bool_not(dict_in(spo_cache, type_name))):
  137. dict_add_fast(spo_cache, type_name, selectPossibleOutgoing(metamodel, type_name, dict_keys(cardinalities)))
  138. check_list = set_copy(spo_cache[type_name])
  139. while (0 < list_len(check_list)):
  140. check_type = set_pop(check_list)
  141. if (dict_in(cardinalities, check_type)):
  142. // Cardinalities defined for this association, so check them
  143. if (bool_or(dict_in(cardinalities[check_type], "tlc"), dict_in(cardinalities[check_type], "tuc"))):
  144. instances = list_len(allOutgoingAssociationInstances(model, model_name, check_type))
  145. if (dict_in(cardinalities[check_type], "tlc")):
  146. // A lower cardinality was defined at the target
  147. if (integer_gt(cardinalities[check_type]["tlc"], instances)):
  148. String error
  149. error = (("Lower cardinality violation for outgoing edge of type " + check_type) + " at ") + model_info(model, model_name)
  150. return error!
  151. if (dict_in(cardinalities[check_type], "tuc")):
  152. // An upper cardinality was defined at the target
  153. if (integer_lt(cardinalities[check_type]["tuc"], instances)):
  154. String error
  155. error = (("Upper cardinality violation for outgoing edge of type " + check_type) + " at ") + model_info(model, model_name)
  156. return error!
  157. // Identical, but for outgoing, and thus for A in the figure
  158. if (bool_not(dict_in(spi_cache, type_name))):
  159. dict_add_fast(spi_cache, type_name, selectPossibleIncoming(metamodel, type_name, dict_keys(cardinalities)))
  160. check_list = set_copy(spi_cache[type_name])
  161. while (0 < list_len(check_list)):
  162. check_type = set_pop(check_list)
  163. if (dict_in(cardinalities, check_type)):
  164. // Cardinalities defined for this association, so check them
  165. if (bool_or(dict_in(cardinalities[check_type], "slc"), dict_in(cardinalities[check_type], "suc"))):
  166. instances = list_len(allIncomingAssociationInstances(model, model_name, check_type))
  167. if (dict_in(cardinalities[check_type], "slc")):
  168. // A lower cardinality was defined at the source
  169. if (integer_gt(cardinalities[check_type]["slc"], instances)):
  170. String error
  171. error = (("Lower cardinality violation for incoming edge of type " + check_type) + " at ") + model_info(model, model_name)
  172. return error!
  173. if (dict_in(cardinalities[check_type], "suc")):
  174. // An upper cardinality was defined at the source
  175. if (integer_lt(cardinalities[check_type]["suc"], instances)):
  176. String error
  177. error = (("Upper cardinality violation for incoming edge of type " + check_type) + " at ") + model_info(model, model_name)
  178. return error!
  179. constraint_function = read_attribute(metamodel, typing[model_name], "constraint")
  180. if (element_neq(constraint_function, read_root())):
  181. String result
  182. Element func
  183. func = get_func_AL_model(import_node(constraint_function))
  184. result = func(model, model_name)
  185. if (result != "OK"):
  186. return result!
  187. // Check multiplicities, if they are defined (optional)
  188. Element metamodel_keys
  189. String metamodel_element
  190. Element mm_model
  191. Integer attr_value
  192. mm_model = metamodel["model"]
  193. metamodel_keys = dict_keys(mm_model)
  194. while (read_nr_out(metamodel_keys) > 0):
  195. metamodel_element = set_pop(metamodel_keys)
  196. // Lower multiplicities
  197. attr_value = read_attribute(metamodel, metamodel_element, "lower_cardinality")
  198. if (element_neq(attr_value, read_root())):
  199. // We have defined a lower cardinality, so check number of instances!
  200. if (attr_value > list_len(allInstances(model, metamodel_element))):
  201. return "Lower cardinality violated for class: " + metamodel_element!
  202. // Upper multiplicities
  203. attr_value = read_attribute(metamodel, metamodel_element, "upper_cardinality")
  204. if (element_neq(attr_value, read_root())):
  205. // We have defined a lower cardinality, so check number of instances!
  206. if (attr_value < list_len(allInstances(model, metamodel_element))):
  207. return "Upper cardinality violated for class: " + metamodel_element!
  208. // Check all ComplexAttributes recursively
  209. Element all_complex_types
  210. Element complex_instances
  211. String complex_instance
  212. String complex_type
  213. String result
  214. all_complex_types = allInstances(model["metamodel"]["metamodel"], "ComplexAttribute")
  215. while (read_nr_out(all_complex_types) > 0):
  216. complex_type = set_pop(all_complex_types)
  217. complex_instances = allInstances(model, complex_type)
  218. while (read_nr_out(complex_instances) > 0):
  219. complex_instance = set_pop(complex_instances)
  220. complex_type = read_attribute(model["metamodel"], read_type(model, complex_instance), "type")
  221. result = check_location_conformance(model["model"][complex_instance], complex_type)
  222. if (result != "OK"):
  223. return ((("Complex attribute doesn't match for: " + complex_instance) + "\n Message: ") + result)!
  224. // Structure seems fine, now do global constraints
  225. Element global_constraints
  226. String constraint
  227. Element func
  228. global_constraints = allInstances(model, "GlobalConstraints")
  229. while (read_nr_out(global_constraints) > 0):
  230. constraint = set_pop(global_constraints)
  231. func = get_func_AL_model(import_node(read_attribute(model, constraint, "global_constraint")))
  232. result = func(model)
  233. if (result != "OK"):
  234. return result!
  235. return "OK"!
  236. String function check_location_conformance(instance_location : String, type_location : String):
  237. // Check whether the instance is typed by the type
  238. Element instance
  239. Element type
  240. instance = import_node(instance_location)
  241. type = import_node(type_location)
  242. if (element_neq(instance["metamodel"], type)):
  243. return "Instance not statically typed by specified metamodel"!
  244. return conformance_scd(instance)!
  245. Element function set_model_constraints(model : Element, func : Element):
  246. if (dict_in(model, "constraints")):
  247. dict_delete(model, "constraints")
  248. dict_add_fast(model, "constraints", func)
  249. return model!
  250. Element function generate_bottom_type_mapping(model : Element):
  251. Element mm
  252. mm = model["metamodel"]["model"]
  253. dict_delete(model, "type_mapping")
  254. Element tm
  255. tm = create_node()
  256. dict_add_fast(model, "type_mapping", tm)
  257. // Iterate over every element
  258. Element elem_keys
  259. String elem
  260. elem_keys = dict_keys(model["model"])
  261. while (read_nr_out(elem_keys) > 0):
  262. elem = set_pop(elem_keys)
  263. if (is_edge(model["model"][elem])):
  264. dict_add_fast(tm, elem, "Edge")
  265. else:
  266. dict_add_fast(tm, elem, "Node")
  267. return model!
  268. String function model_info(model : Element, name : String):
  269. return name!
  270. // For more detailed information
  271. String result
  272. result = ""
  273. result = (result + "\nModel name: ") + name
  274. result = (result + "\nType: ") + cast_v2s(model["type_mapping"][name])
  275. result = (result + "\nValue: ") + cast_v2s(model["model"][name])
  276. result = (result + "\nSource: ") + cast_v2s(reverseKeyLookup(model["model"], read_edge_src(model["model"][name])))
  277. result = (result + "\nDestination: ") + cast_v2s(reverseKeyLookup(model["model"], read_edge_dst(model["model"][name])))
  278. return result!