conformance_scd.alc 10 KB

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