conformance_scd.alc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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 : Element, type : Element):
  7. // Just check whether or not the type mapping specifies the type as the type of the instance
  8. return element_eq(dict_read_node(model["type_mapping"], instance), type)
  9. Boolean function is_nominal_instance(model : Element, instance : Element, type : Element):
  10. if (bool_not(set_in_node(model["metamodel"]["model"], type))):
  11. // type is not even in the specified metamodel, so this will never work
  12. return False
  13. return is_nominal_subtype(model["metamodel"], dict_read_node(model["type_mapping"], instance), type)
  14. Boolean function is_nominal_subtype(metamodel : Element, subclass : Element, superclass : Element):
  15. if (element_eq(subclass, superclass)):
  16. return True
  17. Element superclasses
  18. Element new_superclass
  19. Element result
  20. superclasses = get_superclasses(metamodel, subclass)
  21. while (0 < list_len(superclasses)):
  22. new_superclass = set_pop(superclasses)
  23. result = is_nominal_subtype(metamodel, new_superclass, superclass)
  24. if (result):
  25. return True
  26. return False
  27. Boolean function is_structural_instance(model : Element, instance : Element, type : Element):
  28. return is_structural_subtype(dict_read_node(model["type_mapping"], instance), type)
  29. Boolean function is_structural_subtype(subtype : Element, supertype : Element):
  30. // Determine whether it is just the exact type or not
  31. if (element_eq(subtype, supertype)):
  32. return True
  33. // Find all links that are required (name and type) from the specified type
  34. Element required_keys
  35. required_keys = dict_keys(supertype)
  36. Integer required_keys_len
  37. required_keys_len = dict_len(required_keys)
  38. String key
  39. Element equivalent
  40. Integer i
  41. i = 0
  42. // Go over all keys that we require
  43. while (i < required_keys_len):
  44. key = set_pop(required_keys)
  45. // Check whether they exist in the instance
  46. if (dict_in(subtype, key)):
  47. // Normally, we should still check whether they don't violate the constraints imposed on the class (i.e., are actually present)
  48. // For now, we ignore this and simply require that it is always there in the metamodel (not necessarily in the instance)
  49. // TODO
  50. // Still check whether the types match
  51. if (bool_not(is_structural_subtype(subtype[key], supertype[key]))):
  52. return False
  53. // All clear, so pass on to the next attribute
  54. i = i + 1
  55. else:
  56. return False
  57. // No violations found, so OK
  58. return True
  59. String function conformance_scd(model : Element):
  60. // Initialization
  61. Element keys
  62. Element metamodel
  63. Element typing
  64. String model_name
  65. Element src_model
  66. Element dst_model
  67. Element src_metamodel
  68. Element dst_metamodel
  69. Element element
  70. Element check_list
  71. String check_type
  72. Element cardinalities
  73. Element scd
  74. // Load in variables
  75. scd = import_node("models/SimpleClassDiagrams")
  76. metamodel = model["metamodel"]
  77. typing = model["type_mapping"]
  78. cardinalities = create_node()
  79. // Create dictionary with all associations and allowed cardinalities
  80. if (list_len(model["model"]) > 0):
  81. keys = dict_keys(metamodel["model"])
  82. Integer slc
  83. Integer suc
  84. Integer tlc
  85. Integer tuc
  86. String key
  87. Element tmp_dict
  88. while (0 < list_len(keys)):
  89. key = set_pop(keys)
  90. if (is_nominal_instance(metamodel, metamodel["model"][key], scd["model"]["Association"])):
  91. tmp_dict = create_node()
  92. slc = read_attribute(metamodel, key, "source_lower_cardinality")
  93. suc = read_attribute(metamodel, key, "source_upper_cardinality")
  94. tlc = read_attribute(metamodel, key, "target_lower_cardinality")
  95. tuc = read_attribute(metamodel, key, "target_upper_cardinality")
  96. if (element_neq(slc, read_root())):
  97. dict_add(tmp_dict, "slc", slc)
  98. if (element_neq(suc, read_root())):
  99. dict_add(tmp_dict, "suc", suc)
  100. if (element_neq(tlc, read_root())):
  101. dict_add(tmp_dict, "tlc", tlc)
  102. if (element_neq(tuc, read_root())):
  103. dict_add(tmp_dict, "tuc", tuc)
  104. if (list_len(tmp_dict) > 0):
  105. log("Found cardinalities for " + key)
  106. dict_add(cardinalities, key, tmp_dict)
  107. // Iterate over each element of the model, finding out whether everything is fine
  108. keys = dict_keys(model["model"])
  109. while (0 < list_len(keys)):
  110. model_name = set_pop(keys)
  111. element = model["model"][model_name]
  112. if (bool_not(dict_in_node(typing, element))):
  113. return "Model has no type specified: " + model_name
  114. if (bool_not(set_in_node(metamodel["model"], dict_read_node(typing, element)))):
  115. return "Type of element not in specified metamodel: " + model_name
  116. if (bool_not(is_nominal_instance(model, element, dict_read_node(typing, element)))):
  117. return "Element is not an instance of its specified type: " + model_name
  118. if (is_edge(element)):
  119. src_model = read_edge_src(element)
  120. dst_model = read_edge_dst(element)
  121. src_metamodel = read_edge_src(dict_read_node(typing, element))
  122. dst_metamodel = read_edge_dst(dict_read_node(typing, element))
  123. if (bool_not(is_nominal_instance(model, src_model, src_metamodel))):
  124. return "Source of model edge not typed by source of type: " + model_name
  125. if (bool_not(is_nominal_instance(model, dst_model, dst_metamodel))):
  126. return "Destination of model edge not typed by source of type: " + model_name
  127. // Check cardinality for all of our edges
  128. //
  129. // SLC..SUC TLC..TUC
  130. // A ---------------------> B
  131. //
  132. // First the incoming, so we are at B in the above figure
  133. Integer lower_val
  134. Integer upper_val
  135. Integer instances
  136. check_list = selectPossibleOutgoing(model, model_name, dict_keys(cardinalities))
  137. while (0 < list_len(check_list)):
  138. log("Check cardinalities for " + model_name)
  139. check_type = set_pop(check_list)
  140. if (dict_in(cardinalities, check_type)):
  141. log("Target Got some cardinalities to check!")
  142. // Cardinalities defined for this association, so check them
  143. lower_val = cardinalities[check_type]["tlc"]
  144. upper_val = cardinalities[check_type]["tuc"]
  145. log("Read instances for " + cast_e2s(model_name))
  146. log(" of " + cast_e2s(check_type))
  147. instances = list_len(allOutgoingAssociationInstances(model, model_name, check_type))
  148. log("Instances: " + cast_i2s(instances))
  149. if (dict_in(cardinalities[check_type], "tlc")):
  150. // A lower cardinality was defined at the target
  151. log("TLC Compare with " + cast_i2s(instances))
  152. if (integer_gt(cardinalities[check_type]["tlc"], instances)):
  153. return "Lower cardinality violation for outgoing edge at " + model_name
  154. if (dict_in(cardinalities[check_type], "tuc")):
  155. // An upper cardinality was defined at the target
  156. log("TUC Compare with " + cast_i2s(instances))
  157. if (integer_gt(cardinalities[check_type]["tuc"], instances)):
  158. return "Upper cardinality violation for outgoing edge at " + model_name
  159. // Identical, but for outgoing, and thus for A in the figure
  160. check_list = selectPossibleIncoming(model, model_name, dict_keys(cardinalities))
  161. while (0 < list_len(check_list)):
  162. log("Check cardinalities for " + model_name)
  163. check_type = set_pop(check_list)
  164. log(cast_e2s(check_type))
  165. if (dict_in(cardinalities, check_type)):
  166. log("Source Got some cardinalities to check!")
  167. // Cardinalities defined for this association, so check them
  168. lower_val = cardinalities[check_type]["slc"]
  169. upper_val = cardinalities[check_type]["suc"]
  170. instances = list_len(allIncomingAssociationInstances(model, model_name, check_type))
  171. log("Instances: " + cast_i2s(list_len(instances)))
  172. if (dict_in(cardinalities[check_type], "slc")):
  173. // A lower cardinality was defined at the source
  174. log("SLC Compare with " + cast_i2s(instances))
  175. if (integer_gt(cardinalities[check_type]["slc"], instances)):
  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. log("SUC Compare with " + cast_i2s(instances))
  180. if (integer_gt(cardinalities[check_type]["suc"], instances)):
  181. return "Upper cardinality violation for incoming edge at " + model_name
  182. log("Done for " + model_name)
  183. // Check multiplicities, if they are defined (optional)
  184. Element metamodel_keys
  185. String metamodel_element
  186. Integer attr_value
  187. metamodel_keys = dict_keys(metamodel["model"])
  188. while (0 < list_len(metamodel_keys)):
  189. metamodel_element = set_pop(metamodel_keys)
  190. // Lower multiplicities
  191. attr_value = read_attribute(metamodel, metamodel_element, "lower_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["model"][metamodel_element]))):
  195. return "Lower cardinality violated for class: " + metamodel_element
  196. // Upper multiplicities
  197. attr_value = read_attribute(metamodel, metamodel_element, "upper_cardinality")
  198. if (element_neq(attr_value, read_root())):
  199. // We have defined a lower cardinality, so check number of instances!
  200. if (attr_value < list_len(allInstances(model, metamodel["model"][metamodel_element]))):
  201. return "Upper cardinality violated for class: " + metamodel_element
  202. // Structure seems fine, now do static semantics
  203. if (dict_in(metamodel, "constraints")):
  204. Element constraint_function
  205. constraint_function = metamodel["constraints"]
  206. return constraint_function(model)
  207. else:
  208. return "OK"
  209. Element function set_model_constraints(model : Element, func : Element):
  210. if (dict_in(model, "constraints")):
  211. dict_delete(model, "constraints")
  212. dict_add(model, "constraints", func)
  213. return model
  214. Element function generate_bottom_type_mapping(model : Element):
  215. Element mm
  216. mm = model["metamodel"]["model"]
  217. dict_delete(model, "type_mapping")
  218. Element tm
  219. tm = create_node()
  220. dict_add(model, "type_mapping", tm)
  221. // Iterate over every element
  222. Element elem_keys
  223. Element elem
  224. elem_keys = dict_keys(model["model"])
  225. while (0 < read_nr_out(elem_keys)):
  226. elem = model["model"][set_pop(elem_keys)]
  227. if (is_edge(elem)):
  228. dict_add(tm, elem, mm["Edge"])
  229. else:
  230. dict_add(tm, elem, mm["Node"])
  231. return model