conformance_scd.alc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. include "primitives.alh"
  2. include "library.alh"
  3. include "object_operations.alh"
  4. include "constructors.alh"
  5. include "modelling.alh"
  6. Element function set_copy(a : Element):
  7. Element b
  8. Integer i
  9. Integer count
  10. b = create_node()
  11. i = 0
  12. count = read_nr_out(a)
  13. while (i < count):
  14. set_add(b, read_edge_dst(read_out(a, i)))
  15. i = i + 1
  16. return b
  17. Boolean function is_direct_instance(model : Element, instance : String, type : String):
  18. // Just check whether or not the type mapping specifies the type as the type of the instance
  19. return element_eq(dict_read_node(model["type_mapping"], model["model"][instance]), model["metamodel"]["model"][type])
  20. Boolean function is_nominal_instance(model : Element, instance : String, type : String):
  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 (bool_not(dict_in_node(model["type_mapping"], model["model"][instance]))):
  28. // doesn't even have a type
  29. return False
  30. Boolean result
  31. result = is_nominal_subtype(model["metamodel"], reverseKeyLookup(model["metamodel"]["model"], dict_read_node(model["type_mapping"], model["model"][instance])), type)
  32. return result
  33. Boolean function is_nominal_subtype(metamodel : Element, subclass : String, superclass : String):
  34. if (element_eq(metamodel["model"][subclass], metamodel["model"][superclass])):
  35. return True
  36. if (bool_not(dict_in(metamodel["model"], subclass))):
  37. return False
  38. if (bool_not(dict_in(metamodel["model"], superclass))):
  39. return False
  40. Element superclasses
  41. Element new_superclass
  42. Element result
  43. superclasses = get_superclasses(metamodel, subclass)
  44. while (0 < list_len(superclasses)):
  45. new_superclass = set_pop(superclasses)
  46. if (is_nominal_subtype(metamodel, new_superclass, superclass)):
  47. return True
  48. return False
  49. Element function precompute_cardinalities(model : Element):
  50. Integer slc
  51. Integer suc
  52. Integer tlc
  53. Integer tuc
  54. String key
  55. Element tmp_dict
  56. Element cardinalities
  57. Element keys
  58. Element metamodel
  59. metamodel = model["metamodel"]
  60. keys = allInstances(metamodel, "Association")
  61. cardinalities = create_node()
  62. while (0 < list_len(keys)):
  63. key = set_pop(keys)
  64. tmp_dict = create_node()
  65. slc = read_attribute(metamodel, key, "source_lower_cardinality")
  66. suc = read_attribute(metamodel, key, "source_upper_cardinality")
  67. tlc = read_attribute(metamodel, key, "target_lower_cardinality")
  68. tuc = read_attribute(metamodel, key, "target_upper_cardinality")
  69. if (element_neq(slc, read_root())):
  70. dict_add(tmp_dict, "slc", slc)
  71. if (element_neq(suc, read_root())):
  72. dict_add(tmp_dict, "suc", suc)
  73. if (element_neq(tlc, read_root())):
  74. dict_add(tmp_dict, "tlc", tlc)
  75. if (element_neq(tuc, read_root())):
  76. dict_add(tmp_dict, "tuc", tuc)
  77. if (list_len(tmp_dict) > 0):
  78. dict_add(cardinalities, key, tmp_dict)
  79. return cardinalities
  80. String function conformance_scd(model : Element):
  81. // Initialization
  82. Element keys
  83. Element metamodel
  84. Element typing
  85. String model_name
  86. String src_model
  87. String dst_model
  88. String src_metamodel
  89. String dst_metamodel
  90. Element element
  91. Element check_list
  92. String check_type
  93. Element cardinalities
  94. Element scd
  95. Integer instances
  96. String type_name
  97. Element spo_cache
  98. Element spi_cache
  99. spo_cache = create_node()
  100. spi_cache = create_node()
  101. // Load in variables
  102. scd = import_node("models/SimpleClassDiagrams")
  103. metamodel = model["metamodel"]
  104. typing = model["type_mapping"]
  105. // Create dictionary with all associations and allowed cardinalities
  106. if (list_len(model["model"]) > 0):
  107. cardinalities = precompute_cardinalities(model)
  108. // Iterate over each element of the model, finding out whether everything is fine
  109. keys = dict_keys(model["model"])
  110. while (0 < list_len(keys)):
  111. model_name = set_pop(keys)
  112. element = model["model"][model_name]
  113. type_name = reverseKeyLookup(metamodel["model"], dict_read_node(typing, element))
  114. log((("Check " + model_name) + " : ") + type_name)
  115. if (bool_not(dict_in_node(typing, element))):
  116. return "Model has no type specified: " + model_name
  117. if (bool_not(set_in_node(metamodel["model"], dict_read_node(typing, element)))):
  118. return "Type of element not in specified metamodel: " + model_name
  119. // This is true by definition of is_nominal_instance
  120. //if (bool_not(is_nominal_instance(model, model_name, type_name))):
  121. // return "Element is not an instance of its specified type: " + model_name
  122. if (is_edge(element)):
  123. src_model = reverseKeyLookup(model["model"], read_edge_src(element))
  124. dst_model = reverseKeyLookup(model["model"], read_edge_dst(element))
  125. src_metamodel = reverseKeyLookup(metamodel["model"], read_edge_src(dict_read_node(typing, element)))
  126. dst_metamodel = reverseKeyLookup(metamodel["model"], read_edge_dst(dict_read_node(typing, element)))
  127. if (bool_not(is_nominal_instance(model, src_model, src_metamodel))):
  128. return "Source of model edge not typed by source of type: " + model_name
  129. if (bool_not(is_nominal_instance(model, dst_model, dst_metamodel))):
  130. return "Destination of model edge not typed by source of type: " + model_name
  131. // Check cardinality for all of our edges
  132. //
  133. // SLC..SUC TLC..TUC
  134. // A ---------------------> B
  135. //
  136. // First the incoming, so we are at B in the above figure
  137. if (bool_not(dict_in(spo_cache, type_name))):
  138. dict_add(spo_cache, type_name, selectPossibleOutgoing(metamodel, type_name, dict_keys(cardinalities)))
  139. check_list = set_copy(spo_cache[type_name])
  140. while (0 < list_len(check_list)):
  141. check_type = set_pop(check_list)
  142. if (dict_in(cardinalities, check_type)):
  143. // Cardinalities defined for this association, so check them
  144. if (bool_or(dict_in(cardinalities[check_type], "tlc"), dict_in(cardinalities[check_type], "tuc"))):
  145. instances = list_len(allOutgoingAssociationInstances(model, model_name, check_type))
  146. if (dict_in(cardinalities[check_type], "tlc")):
  147. // A lower cardinality was defined at the target
  148. if (integer_gt(cardinalities[check_type]["tlc"], instances)):
  149. log("Instances: " + cast_i2s(instances))
  150. log("Cardinalities: " + cast_i2s(cardinalities[check_type]["tuc"]))
  151. log("Type: " + check_type)
  152. return "Lower cardinality violation for outgoing edge at " + model_name
  153. if (dict_in(cardinalities[check_type], "tuc")):
  154. // An upper cardinality was defined at the target
  155. if (integer_lt(cardinalities[check_type]["tuc"], instances)):
  156. log("Instances: " + cast_i2s(instances))
  157. log("Cardinalities: " + cast_i2s(cardinalities[check_type]["tuc"]))
  158. log("Type: " + check_type)
  159. return "Upper cardinality violation for outgoing edge at " + model_name
  160. // Identical, but for outgoing, and thus for A in the figure
  161. if (bool_not(dict_in(spi_cache, type_name))):
  162. dict_add(spi_cache, type_name, selectPossibleIncoming(metamodel, type_name, dict_keys(cardinalities)))
  163. check_list = set_copy(spi_cache[type_name])
  164. while (0 < list_len(check_list)):
  165. check_type = set_pop(check_list)
  166. if (dict_in(cardinalities, check_type)):
  167. // Cardinalities defined for this association, so check them
  168. if (bool_or(dict_in(cardinalities[check_type], "slc"), dict_in(cardinalities[check_type], "suc"))):
  169. instances = list_len(allIncomingAssociationInstances(model, model_name, check_type))
  170. if (dict_in(cardinalities[check_type], "slc")):
  171. // A lower cardinality was defined at the source
  172. if (integer_gt(cardinalities[check_type]["slc"], instances)):
  173. log("Instances: " + cast_i2s(instances))
  174. log("Cardinalities: " + cast_i2s(cardinalities[check_type]["tuc"]))
  175. log("Type: " + check_type)
  176. return "Lower cardinality violation for incoming edge at " + model_name
  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. log("Instances: " + cast_i2s(instances))
  181. log("Cardinalities: " + cast_i2s(cardinalities[check_type]["tuc"]))
  182. log("Type: " + check_type)
  183. return "Upper cardinality violation for incoming edge at " + model_name
  184. Element constraint_function
  185. constraint_function = read_attribute(metamodel, reverseKeyLookup(metamodel["model"], dict_read_node(typing, element)), "constraint")
  186. if (element_neq(constraint_function, read_root())):
  187. String result
  188. result = constraint_function(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. Integer attr_value
  195. metamodel_keys = dict_keys(metamodel["model"])
  196. while (0 < list_len(metamodel_keys)):
  197. metamodel_element = set_pop(metamodel_keys)
  198. // Lower multiplicities
  199. attr_value = read_attribute(metamodel, metamodel_element, "lower_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 "Lower cardinality violated for class: " + metamodel_element
  204. // Upper multiplicities
  205. attr_value = read_attribute(metamodel, metamodel_element, "upper_cardinality")
  206. if (element_neq(attr_value, read_root())):
  207. // We have defined a lower cardinality, so check number of instances!
  208. if (attr_value < list_len(allInstances(model, metamodel_element))):
  209. return "Upper cardinality violated for class: " + metamodel_element
  210. // Structure seems fine, now do static semantics
  211. if (dict_in(metamodel, "constraints")):
  212. Element constraint_function
  213. constraint_function = metamodel["constraints"]
  214. return constraint_function(model)
  215. else:
  216. return "OK"
  217. Element function set_model_constraints(model : Element, func : Element):
  218. if (dict_in(model, "constraints")):
  219. dict_delete(model, "constraints")
  220. dict_add(model, "constraints", func)
  221. return model
  222. Element function generate_bottom_type_mapping(model : Element):
  223. Element mm
  224. mm = model["metamodel"]["model"]
  225. dict_delete(model, "type_mapping")
  226. Element tm
  227. tm = create_node()
  228. dict_add(model, "type_mapping", tm)
  229. // Iterate over every element
  230. Element elem_keys
  231. Element elem
  232. elem_keys = dict_keys(model["model"])
  233. while (0 < read_nr_out(elem_keys)):
  234. elem = model["model"][set_pop(elem_keys)]
  235. if (is_edge(elem)):
  236. dict_add(tm, elem, mm["Edge"])
  237. else:
  238. dict_add(tm, elem, mm["Node"])
  239. return model