conformance_scd.alc 12 KB

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