conformance_scd.alc 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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. if (bool_not(dict_in_node(model["type_mapping"], instance))):
  14. // Doesn't even have a type
  15. return False
  16. return is_nominal_subtype(model["metamodel"], dict_read_node(model["type_mapping"], instance), type)
  17. Boolean function is_nominal_subtype(metamodel : Element, subclass : Element, superclass : Element):
  18. if (element_eq(subclass, superclass)):
  19. return True
  20. Element superclasses
  21. Element new_superclass
  22. Element result
  23. superclasses = get_superclasses(metamodel, subclass)
  24. while (0 < list_len(superclasses)):
  25. new_superclass = set_pop(superclasses)
  26. if (is_nominal_subtype(metamodel, new_superclass, superclass)):
  27. return True
  28. return False
  29. Boolean function is_structural_instance(model : Element, instance : Element, type : Element):
  30. return is_structural_subtype(dict_read_node(model["type_mapping"], instance), type)
  31. Boolean function is_structural_subtype(subtype : Element, supertype : Element):
  32. // Determine whether it is just the exact type or not
  33. if (element_eq(subtype, supertype)):
  34. return True
  35. // Find all links that are required (name and type) from the specified type
  36. Element required_keys
  37. required_keys = dict_keys(supertype)
  38. Integer required_keys_len
  39. required_keys_len = dict_len(required_keys)
  40. String key
  41. Element equivalent
  42. Integer i
  43. i = 0
  44. // Go over all keys that we require
  45. while (i < required_keys_len):
  46. key = set_pop(required_keys)
  47. // Check whether they exist in the instance
  48. if (dict_in(subtype, key)):
  49. // Normally, we should still check whether they don't violate the constraints imposed on the class (i.e., are actually present)
  50. // For now, we ignore this and simply require that it is always there in the metamodel (not necessarily in the instance)
  51. // TODO
  52. // Still check whether the types match
  53. if (bool_not(is_structural_subtype(subtype[key], supertype[key]))):
  54. return False
  55. // All clear, so pass on to the next attribute
  56. i = i + 1
  57. else:
  58. return False
  59. // No violations found, so OK
  60. return True
  61. String function conformance_scd(model : Element):
  62. // Initialization
  63. Element keys
  64. Element metamodel
  65. Element typing
  66. String model_name
  67. Element src_model
  68. Element dst_model
  69. Element src_metamodel
  70. Element dst_metamodel
  71. Element element
  72. Element check_list
  73. String check_type
  74. Element cardinalities
  75. Element scd
  76. // Load in variables
  77. scd = import_node("models/SimpleClassDiagrams")
  78. metamodel = model["metamodel"]
  79. typing = model["type_mapping"]
  80. cardinalities = create_node()
  81. // Create dictionary with all associations and allowed cardinalities
  82. if (list_len(model["model"]) > 0):
  83. keys = dict_keys(metamodel["model"])
  84. Integer slc
  85. Integer suc
  86. Integer tlc
  87. Integer tuc
  88. String key
  89. Element tmp_dict
  90. while (0 < list_len(keys)):
  91. key = set_pop(keys)
  92. if (is_nominal_instance(metamodel, metamodel["model"][key], scd["model"]["Association"])):
  93. tmp_dict = create_node()
  94. slc = read_attribute(metamodel, key, "source_lower_cardinality")
  95. suc = read_attribute(metamodel, key, "source_upper_cardinality")
  96. tlc = read_attribute(metamodel, key, "target_lower_cardinality")
  97. tuc = read_attribute(metamodel, key, "target_upper_cardinality")
  98. if (element_neq(slc, read_root())):
  99. dict_add(tmp_dict, "slc", slc)
  100. if (element_neq(suc, read_root())):
  101. dict_add(tmp_dict, "suc", suc)
  102. if (element_neq(tlc, read_root())):
  103. dict_add(tmp_dict, "tlc", tlc)
  104. if (element_neq(tuc, read_root())):
  105. dict_add(tmp_dict, "tuc", tuc)
  106. if (list_len(tmp_dict) > 0):
  107. dict_add(cardinalities, key, tmp_dict)
  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. log("Check element " + model_name)
  113. element = model["model"][model_name]
  114. if (bool_not(dict_in_node(typing, element))):
  115. return "Model has no type specified: " + model_name
  116. if (bool_not(set_in_node(metamodel["model"], dict_read_node(typing, element)))):
  117. return "Type of element not in specified metamodel: " + model_name
  118. if (bool_not(is_nominal_instance(model, element, dict_read_node(typing, element)))):
  119. return "Element is not an instance of its specified type: " + model_name
  120. if (is_edge(element)):
  121. src_model = read_edge_src(element)
  122. dst_model = read_edge_dst(element)
  123. src_metamodel = read_edge_src(dict_read_node(typing, element))
  124. dst_metamodel = read_edge_dst(dict_read_node(typing, element))
  125. if (bool_not(is_nominal_instance(model, src_model, src_metamodel))):
  126. return "Source of model edge not typed by source of type: " + model_name
  127. if (bool_not(is_nominal_instance(model, dst_model, dst_metamodel))):
  128. return "Destination of model edge not typed by source of type: " + model_name
  129. // Check cardinality for all of our edges
  130. //
  131. // SLC..SUC TLC..TUC
  132. // A ---------------------> B
  133. //
  134. // First the incoming, so we are at B in the above figure
  135. Integer lower_val
  136. Integer upper_val
  137. Integer instances
  138. log("Check cardinalities")
  139. check_list = selectPossibleOutgoing(model, model_name, dict_keys(cardinalities))
  140. log("Got outgoing")
  141. while (0 < list_len(check_list)):
  142. log("Loop")
  143. check_type = set_pop(check_list)
  144. if (dict_in(cardinalities, check_type)):
  145. // Cardinalities defined for this association, so check them
  146. lower_val = cardinalities[check_type]["tlc"]
  147. upper_val = cardinalities[check_type]["tuc"]
  148. instances = list_len(allOutgoingAssociationInstances(model, model_name, check_type))
  149. if (dict_in(cardinalities[check_type], "tlc")):
  150. // A lower cardinality was defined at the target
  151. if (integer_gt(cardinalities[check_type]["tlc"], instances)):
  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. return "Upper cardinality violation for outgoing edge at " + model_name
  157. // Identical, but for outgoing, and thus for A in the figure
  158. check_list = selectPossibleIncoming(model, model_name, dict_keys(cardinalities))
  159. log("Got incoming")
  160. while (0 < list_len(check_list)):
  161. log("Loop")
  162. check_type = set_pop(check_list)
  163. if (dict_in(cardinalities, check_type)):
  164. // Cardinalities defined for this association, so check them
  165. lower_val = cardinalities[check_type]["slc"]
  166. upper_val = cardinalities[check_type]["suc"]
  167. instances = list_len(allIncomingAssociationInstances(model, model_name, check_type))
  168. if (dict_in(cardinalities[check_type], "slc")):
  169. // A lower cardinality was defined at the source
  170. if (integer_gt(cardinalities[check_type]["slc"], instances)):
  171. return "Lower cardinality violation for incoming edge at " + model_name
  172. if (dict_in(cardinalities[check_type], "suc")):
  173. // An upper cardinality was defined at the source
  174. if (integer_lt(cardinalities[check_type]["suc"], instances)):
  175. return "Upper cardinality violation for incoming edge at " + model_name
  176. log("Finished, check multiplicities")
  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