conformance_scd.alc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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, "Attribute")
  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. return "Source of model edge not typed by source of type: " + model_info(model, model_name)!
  122. if (bool_not(is_nominal_instance(model, dst_model, dst_metamodel))):
  123. return "Destination of model edge not typed by source of type: " + model_info(model, model_name)!
  124. // Check cardinality for all of our edges
  125. //
  126. // SLC..SUC TLC..TUC
  127. // A ---------------------> B
  128. //
  129. // First the incoming, so we are at B in the above figure
  130. if (bool_not(dict_in(spo_cache, type_name))):
  131. dict_add(spo_cache, type_name, selectPossibleOutgoing(metamodel, type_name, dict_keys(cardinalities)))
  132. check_list = set_copy(spo_cache[type_name])
  133. while (0 < list_len(check_list)):
  134. check_type = set_pop(check_list)
  135. if (dict_in(cardinalities, check_type)):
  136. // Cardinalities defined for this association, so check them
  137. if (bool_or(dict_in(cardinalities[check_type], "tlc"), dict_in(cardinalities[check_type], "tuc"))):
  138. instances = list_len(allOutgoingAssociationInstances(model, model_name, check_type))
  139. if (dict_in(cardinalities[check_type], "tlc")):
  140. // A lower cardinality was defined at the target
  141. if (integer_gt(cardinalities[check_type]["tlc"], instances)):
  142. String error
  143. error = (("Lower cardinality violation for outgoing edge of type " + check_type) + " at ") + model_info(model, model_name)
  144. log("NAME: " + cast_e2s(read_attribute(model, check_type, "name")))
  145. return error!
  146. if (dict_in(cardinalities[check_type], "tuc")):
  147. // An upper cardinality was defined at the target
  148. if (integer_lt(cardinalities[check_type]["tuc"], instances)):
  149. String error
  150. error = (("Upper cardinality violation for outgoing edge of type " + check_type) + " at ") + model_info(model, model_name)
  151. return error!
  152. // Identical, but for outgoing, and thus for A in the figure
  153. if (bool_not(dict_in(spi_cache, type_name))):
  154. dict_add(spi_cache, type_name, selectPossibleIncoming(metamodel, type_name, dict_keys(cardinalities)))
  155. check_list = set_copy(spi_cache[type_name])
  156. while (0 < list_len(check_list)):
  157. check_type = set_pop(check_list)
  158. if (dict_in(cardinalities, check_type)):
  159. // Cardinalities defined for this association, so check them
  160. if (bool_or(dict_in(cardinalities[check_type], "slc"), dict_in(cardinalities[check_type], "suc"))):
  161. instances = list_len(allIncomingAssociationInstances(model, model_name, check_type))
  162. if (dict_in(cardinalities[check_type], "slc")):
  163. // A lower cardinality was defined at the source
  164. if (integer_gt(cardinalities[check_type]["slc"], instances)):
  165. String error
  166. error = (("Lower cardinality violation for incoming edge of type " + check_type) + " at ") + model_info(model, model_name)
  167. return error!
  168. if (dict_in(cardinalities[check_type], "suc")):
  169. // An upper cardinality was defined at the source
  170. if (integer_lt(cardinalities[check_type]["suc"], instances)):
  171. String error
  172. error = (("Upper cardinality violation for incoming edge of type " + check_type) + " at ") + model_info(model, model_name)
  173. return error!
  174. constraint_function = read_attribute(metamodel, typing[model_name], "constraint")
  175. if (element_neq(constraint_function, read_root())):
  176. String result
  177. result = constraint_function(model, model_name)
  178. if (result != "OK"):
  179. return result!
  180. // Check multiplicities, if they are defined (optional)
  181. Element metamodel_keys
  182. String metamodel_element
  183. Integer attr_value
  184. metamodel_keys = dict_keys(metamodel["model"])
  185. while (0 < list_len(metamodel_keys)):
  186. metamodel_element = set_pop(metamodel_keys)
  187. // Lower multiplicities
  188. attr_value = read_attribute(metamodel, metamodel_element, "lower_cardinality")
  189. if (element_neq(attr_value, read_root())):
  190. // We have defined a lower cardinality, so check number of instances!
  191. if (attr_value > list_len(allInstances(model, metamodel_element))):
  192. return "Lower cardinality violated for class: " + metamodel_element!
  193. // Upper multiplicities
  194. attr_value = read_attribute(metamodel, metamodel_element, "upper_cardinality")
  195. if (element_neq(attr_value, read_root())):
  196. // We have defined a lower cardinality, so check number of instances!
  197. if (attr_value < list_len(allInstances(model, metamodel_element))):
  198. return "Upper cardinality violated for class: " + metamodel_element!
  199. // Structure seems fine, now do static semantics
  200. if (dict_in(metamodel, "constraints")):
  201. Element constraint_function
  202. constraint_function = metamodel["constraints"]
  203. return constraint_function(model)!
  204. else:
  205. return "OK"!
  206. Element function set_model_constraints(model : Element, func : Element):
  207. if (dict_in(model, "constraints")):
  208. dict_delete(model, "constraints")
  209. dict_add(model, "constraints", func)
  210. return model!
  211. Element function generate_bottom_type_mapping(model : Element):
  212. Element mm
  213. mm = model["metamodel"]["model"]
  214. dict_delete(model, "type_mapping")
  215. Element tm
  216. tm = create_node()
  217. dict_add(model, "type_mapping", tm)
  218. // Iterate over every element
  219. Element elem_keys
  220. Element elem
  221. elem_keys = dict_keys(model["model"])
  222. while (0 < read_nr_out(elem_keys)):
  223. elem = set_pop(elem_keys)
  224. if (is_edge(model["model"][elem])):
  225. dict_add(tm, elem, "Edge")
  226. else:
  227. dict_add(tm, elem, "Node")
  228. return model!
  229. String function model_info(model : Element, name : String):
  230. return name!
  231. // For more detailed information
  232. String result
  233. result = ""
  234. result = (result + "\nModel name: ") + name
  235. result = (result + "\nType: ") + cast_v2s(model["type_mapping"][name])
  236. result = (result + "\nValue: ") + cast_v2s(model["model"][name])
  237. result = (result + "\nSource: ") + cast_v2s(reverseKeyLookup(model["model"], read_edge_src(model["model"][name])))
  238. result = (result + "\nDestination: ") + cast_v2s(reverseKeyLookup(model["model"], read_edge_dst(model["model"][name])))
  239. return result!