conformance_scd.alc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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. return is_nominal_subtype(model["metamodel"], reverseKeyLookup(model["metamodel"]["model"], dict_read_node(model["type_mapping"], model["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. metamodel = model["metamodel"]
  50. keys = allInstances(metamodel, "Association")
  51. cardinalities = create_node()
  52. while (0 < list_len(keys)):
  53. key = set_pop(keys)
  54. tmp_dict = create_node()
  55. slc = read_attribute(metamodel, key, "source_lower_cardinality")
  56. suc = read_attribute(metamodel, key, "source_upper_cardinality")
  57. tlc = read_attribute(metamodel, key, "target_lower_cardinality")
  58. tuc = read_attribute(metamodel, key, "target_upper_cardinality")
  59. if (element_neq(slc, read_root())):
  60. dict_add(tmp_dict, "slc", slc)
  61. if (element_neq(suc, read_root())):
  62. dict_add(tmp_dict, "suc", suc)
  63. if (element_neq(tlc, read_root())):
  64. dict_add(tmp_dict, "tlc", tlc)
  65. if (element_neq(tuc, read_root())):
  66. dict_add(tmp_dict, "tuc", tuc)
  67. if (list_len(tmp_dict) > 0):
  68. dict_add(cardinalities, key, tmp_dict)
  69. return cardinalities!
  70. String function conformance_scd(model : Element):
  71. // Initialization
  72. Element keys
  73. Element metamodel
  74. Element typing
  75. String model_name
  76. String src_model
  77. String dst_model
  78. String src_metamodel
  79. String dst_metamodel
  80. Element element
  81. Element check_list
  82. String check_type
  83. Element cardinalities
  84. Element scd
  85. Integer instances
  86. String type_name
  87. Element spo_cache
  88. Element spi_cache
  89. Element constraint_function
  90. spo_cache = create_node()
  91. spi_cache = create_node()
  92. // Load in variables
  93. scd = import_node("models/SimpleClassDiagrams")
  94. metamodel = model["metamodel"]
  95. typing = model["type_mapping"]
  96. // Create dictionary with all associations and allowed cardinalities
  97. if (list_len(model["model"]) > 0):
  98. cardinalities = precompute_cardinalities(model)
  99. // Iterate over each element of the model, finding out whether everything is fine
  100. keys = dict_keys(model["model"])
  101. while (0 < list_len(keys)):
  102. model_name = set_pop(keys)
  103. element = model["model"][model_name]
  104. type_name = reverseKeyLookup(metamodel["model"], dict_read_node(typing, element))
  105. log((("Check " + model_name) + " : ") + type_name)
  106. if (bool_not(dict_in_node(typing, element))):
  107. return "Model has no type specified: " + model_info(model, model_name)!
  108. if (bool_not(set_in_node(metamodel["model"], dict_read_node(typing, element)))):
  109. return "Type of element not in specified metamodel: " + model_info(model, model_name)!
  110. // This is true by definition of is_nominal_instance
  111. //if (bool_not(is_nominal_instance(model, model_name, type_name))):
  112. // return "Element is not an instance of its specified type: " + model_info(model, model_name)!
  113. if (is_edge(element)):
  114. src_model = reverseKeyLookup(model["model"], read_edge_src(element))
  115. dst_model = reverseKeyLookup(model["model"], read_edge_dst(element))
  116. src_metamodel = reverseKeyLookup(metamodel["model"], read_edge_src(dict_read_node(typing, element)))
  117. dst_metamodel = reverseKeyLookup(metamodel["model"], read_edge_dst(dict_read_node(typing, element)))
  118. if (bool_not(is_nominal_instance(model, src_model, src_metamodel))):
  119. return "Source of model edge not typed by source of type: " + model_info(model, model_name)!
  120. if (bool_not(is_nominal_instance(model, dst_model, dst_metamodel))):
  121. return "Destination of model edge not typed by source of type: " + model_info(model, model_name)!
  122. // Check cardinality for all of our edges
  123. //
  124. // SLC..SUC TLC..TUC
  125. // A ---------------------> B
  126. //
  127. // First the incoming, so we are at B in the above figure
  128. if (bool_not(dict_in(spo_cache, type_name))):
  129. dict_add(spo_cache, type_name, selectPossibleOutgoing(metamodel, type_name, dict_keys(cardinalities)))
  130. check_list = set_copy(spo_cache[type_name])
  131. while (0 < list_len(check_list)):
  132. check_type = set_pop(check_list)
  133. if (dict_in(cardinalities, check_type)):
  134. // Cardinalities defined for this association, so check them
  135. if (bool_or(dict_in(cardinalities[check_type], "tlc"), dict_in(cardinalities[check_type], "tuc"))):
  136. instances = list_len(allOutgoingAssociationInstances(model, model_name, check_type))
  137. if (dict_in(cardinalities[check_type], "tlc")):
  138. // A lower cardinality was defined at the target
  139. if (integer_gt(cardinalities[check_type]["tlc"], instances)):
  140. String error
  141. error = (("Lower cardinality violation for outgoing edge of type " + check_type) + " at ") + model_info(model, model_name)
  142. return error!
  143. if (dict_in(cardinalities[check_type], "tuc")):
  144. // An upper cardinality was defined at the target
  145. if (integer_lt(cardinalities[check_type]["tuc"], instances)):
  146. String error
  147. error = (("Upper cardinality violation for outgoing edge of type " + check_type) + " at ") + model_info(model, model_name)
  148. return error!
  149. // Identical, but for outgoing, and thus for A in the figure
  150. if (bool_not(dict_in(spi_cache, type_name))):
  151. dict_add(spi_cache, type_name, selectPossibleIncoming(metamodel, type_name, dict_keys(cardinalities)))
  152. check_list = set_copy(spi_cache[type_name])
  153. while (0 < list_len(check_list)):
  154. check_type = set_pop(check_list)
  155. if (dict_in(cardinalities, check_type)):
  156. // Cardinalities defined for this association, so check them
  157. if (bool_or(dict_in(cardinalities[check_type], "slc"), dict_in(cardinalities[check_type], "suc"))):
  158. instances = list_len(allIncomingAssociationInstances(model, model_name, check_type))
  159. if (dict_in(cardinalities[check_type], "slc")):
  160. // A lower cardinality was defined at the source
  161. if (integer_gt(cardinalities[check_type]["slc"], instances)):
  162. String error
  163. error = (("Lower cardinality violation for incoming edge of type " + check_type) + " at ") + model_info(model, model_name)
  164. return error!
  165. if (dict_in(cardinalities[check_type], "suc")):
  166. // An upper cardinality was defined at the source
  167. if (integer_lt(cardinalities[check_type]["suc"], instances)):
  168. String error
  169. error = (("Upper cardinality violation for incoming edge of type " + check_type) + " at ") + model_info(model, model_name)
  170. return error!
  171. constraint_function = read_attribute(metamodel, reverseKeyLookup(metamodel["model"], dict_read_node(typing, element)), "constraint")
  172. if (element_neq(constraint_function, read_root())):
  173. String result
  174. result = constraint_function(model, model_name)
  175. if (result != "OK"):
  176. return result!
  177. // Check multiplicities, if they are defined (optional)
  178. Element metamodel_keys
  179. String metamodel_element
  180. Integer attr_value
  181. metamodel_keys = dict_keys(metamodel["model"])
  182. while (0 < list_len(metamodel_keys)):
  183. metamodel_element = set_pop(metamodel_keys)
  184. // Lower multiplicities
  185. attr_value = read_attribute(metamodel, metamodel_element, "lower_cardinality")
  186. if (element_neq(attr_value, read_root())):
  187. // We have defined a lower cardinality, so check number of instances!
  188. if (attr_value > list_len(allInstances(model, metamodel_element))):
  189. return "Lower cardinality violated for class: " + metamodel_element!
  190. // Upper multiplicities
  191. attr_value = read_attribute(metamodel, metamodel_element, "upper_cardinality")
  192. if (element_neq(attr_value, read_root())):
  193. // We have defined a lower cardinality, so check number of instances!
  194. if (attr_value < list_len(allInstances(model, metamodel_element))):
  195. return "Upper cardinality violated for class: " + metamodel_element!
  196. // Structure seems fine, now do static semantics
  197. if (dict_in(metamodel, "constraints")):
  198. Element constraint_function
  199. constraint_function = metamodel["constraints"]
  200. return constraint_function(model)!
  201. else:
  202. return "OK"!
  203. Element function set_model_constraints(model : Element, func : Element):
  204. if (dict_in(model, "constraints")):
  205. dict_delete(model, "constraints")
  206. dict_add(model, "constraints", func)
  207. return model!
  208. Element function generate_bottom_type_mapping(model : Element):
  209. Element mm
  210. mm = model["metamodel"]["model"]
  211. dict_delete(model, "type_mapping")
  212. Element tm
  213. tm = create_node()
  214. dict_add(model, "type_mapping", tm)
  215. // Iterate over every element
  216. Element elem_keys
  217. Element elem
  218. elem_keys = dict_keys(model["model"])
  219. while (0 < read_nr_out(elem_keys)):
  220. elem = model["model"][set_pop(elem_keys)]
  221. if (is_edge(elem)):
  222. dict_add(tm, elem, mm["Edge"])
  223. else:
  224. dict_add(tm, elem, mm["Node"])
  225. return model!
  226. String function model_info(model : Element, name : String):
  227. return name!
  228. // For more detailed information
  229. String result
  230. result = ""
  231. result = (result + "\nModel name: ") + name
  232. result = (result + "\nType: ") + cast_v2s(reverseKeyLookup(model["metamodel"]["model"], dict_read_node(model["type_mapping"], model["model"][name])))
  233. result = (result + "\nValue: ") + cast_v2s(model["model"][name])
  234. result = (result + "\nSource: ") + cast_v2s(reverseKeyLookup(model["model"], read_edge_src(model["model"][name])))
  235. result = (result + "\nDestination: ") + cast_v2s(reverseKeyLookup(model["model"], read_edge_dst(model["model"][name])))
  236. return result!