conformance_scd.alc 12 KB

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