conformance_scd.alc 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. result = is_nominal_subtype(metamodel, new_superclass, superclass)
  27. if (result):
  28. return True
  29. return False
  30. Boolean function is_structural_instance(model : Element, instance : Element, type : Element):
  31. return is_structural_subtype(dict_read_node(model["type_mapping"], instance), type)
  32. Boolean function is_structural_subtype(subtype : Element, supertype : Element):
  33. // Determine whether it is just the exact type or not
  34. if (element_eq(subtype, supertype)):
  35. return True
  36. // Find all links that are required (name and type) from the specified type
  37. Element required_keys
  38. required_keys = dict_keys(supertype)
  39. Integer required_keys_len
  40. required_keys_len = dict_len(required_keys)
  41. String key
  42. Element equivalent
  43. Integer i
  44. i = 0
  45. // Go over all keys that we require
  46. while (i < required_keys_len):
  47. key = set_pop(required_keys)
  48. // Check whether they exist in the instance
  49. if (dict_in(subtype, key)):
  50. // Normally, we should still check whether they don't violate the constraints imposed on the class (i.e., are actually present)
  51. // For now, we ignore this and simply require that it is always there in the metamodel (not necessarily in the instance)
  52. // TODO
  53. // Still check whether the types match
  54. if (bool_not(is_structural_subtype(subtype[key], supertype[key]))):
  55. return False
  56. // All clear, so pass on to the next attribute
  57. i = i + 1
  58. else:
  59. return False
  60. // No violations found, so OK
  61. return True
  62. String function conformance_scd(model : Element):
  63. // Initialization
  64. Element keys
  65. Element metamodel
  66. Element typing
  67. String model_name
  68. Element src_model
  69. Element dst_model
  70. Element src_metamodel
  71. Element dst_metamodel
  72. Element element
  73. Element check_list
  74. String check_type
  75. Element cardinalities
  76. Element scd
  77. // Load in variables
  78. scd = import_node("models/SimpleClassDiagrams")
  79. metamodel = model["metamodel"]
  80. typing = model["type_mapping"]
  81. cardinalities = create_node()
  82. // Create dictionary with all associations and allowed cardinalities
  83. if (list_len(model["model"]) > 0):
  84. keys = dict_keys(metamodel["model"])
  85. Integer slc
  86. Integer suc
  87. Integer tlc
  88. Integer tuc
  89. String key
  90. Element tmp_dict
  91. while (0 < list_len(keys)):
  92. key = set_pop(keys)
  93. if (is_nominal_instance(metamodel, metamodel["model"][key], scd["model"]["Association"])):
  94. tmp_dict = create_node()
  95. slc = read_attribute(metamodel, key, "source_lower_cardinality")
  96. suc = read_attribute(metamodel, key, "source_upper_cardinality")
  97. tlc = read_attribute(metamodel, key, "target_lower_cardinality")
  98. tuc = read_attribute(metamodel, key, "target_upper_cardinality")
  99. if (element_neq(slc, read_root())):
  100. dict_add(tmp_dict, "slc", slc)
  101. if (element_neq(suc, read_root())):
  102. dict_add(tmp_dict, "suc", suc)
  103. if (element_neq(tlc, read_root())):
  104. dict_add(tmp_dict, "tlc", tlc)
  105. if (element_neq(tuc, read_root())):
  106. dict_add(tmp_dict, "tuc", tuc)
  107. if (list_len(tmp_dict) > 0):
  108. dict_add(cardinalities, key, tmp_dict)
  109. // Iterate over each element of the model, finding out whether everything is fine
  110. keys = dict_keys(model["model"])
  111. while (0 < list_len(keys)):
  112. model_name = set_pop(keys)
  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. check_list = selectPossibleOutgoing(model, model_name, dict_keys(cardinalities))
  139. while (0 < list_len(check_list)):
  140. check_type = set_pop(check_list)
  141. if (dict_in(cardinalities, check_type)):
  142. // Cardinalities defined for this association, so check them
  143. lower_val = cardinalities[check_type]["tlc"]
  144. upper_val = cardinalities[check_type]["tuc"]
  145. instances = list_len(allOutgoingAssociationInstances(model, model_name, check_type))
  146. if (dict_in(cardinalities[check_type], "tlc")):
  147. // A lower cardinality was defined at the target
  148. if (integer_gt(cardinalities[check_type]["tlc"], instances)):
  149. return "Lower cardinality violation for outgoing edge at " + model_name
  150. if (dict_in(cardinalities[check_type], "tuc")):
  151. // An upper cardinality was defined at the target
  152. if (integer_lt(cardinalities[check_type]["tuc"], instances)):
  153. return "Upper cardinality violation for outgoing edge at " + model_name
  154. // Identical, but for outgoing, and thus for A in the figure
  155. check_list = selectPossibleIncoming(model, model_name, dict_keys(cardinalities))
  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. lower_val = cardinalities[check_type]["slc"]
  161. upper_val = cardinalities[check_type]["suc"]
  162. instances = list_len(allIncomingAssociationInstances(model, model_name, check_type))
  163. if (dict_in(cardinalities[check_type], "slc")):
  164. // A lower cardinality was defined at the source
  165. if (integer_gt(cardinalities[check_type]["slc"], instances)):
  166. return "Lower cardinality violation for incoming edge at " + model_name
  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. return "Upper cardinality violation for incoming edge at " + model_name
  171. // Check multiplicities, if they are defined (optional)
  172. Element metamodel_keys
  173. String metamodel_element
  174. Integer attr_value
  175. metamodel_keys = dict_keys(metamodel["model"])
  176. while (0 < list_len(metamodel_keys)):
  177. metamodel_element = set_pop(metamodel_keys)
  178. // Lower multiplicities
  179. attr_value = read_attribute(metamodel, metamodel_element, "lower_cardinality")
  180. if (element_neq(attr_value, read_root())):
  181. // We have defined a lower cardinality, so check number of instances!
  182. if (attr_value > list_len(allInstances(model, metamodel_element))):
  183. return "Lower cardinality violated for class: " + metamodel_element
  184. // Upper multiplicities
  185. attr_value = read_attribute(metamodel, metamodel_element, "upper_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 "Upper cardinality violated for class: " + metamodel_element
  190. // Structure seems fine, now do static semantics
  191. if (dict_in(metamodel, "constraints")):
  192. Element constraint_function
  193. constraint_function = metamodel["constraints"]
  194. return constraint_function(model)
  195. else:
  196. return "OK"
  197. Element function set_model_constraints(model : Element, func : Element):
  198. if (dict_in(model, "constraints")):
  199. dict_delete(model, "constraints")
  200. dict_add(model, "constraints", func)
  201. return model
  202. Element function generate_bottom_type_mapping(model : Element):
  203. Element mm
  204. mm = model["metamodel"]["model"]
  205. dict_delete(model, "type_mapping")
  206. Element tm
  207. tm = create_node()
  208. dict_add(model, "type_mapping", tm)
  209. // Iterate over every element
  210. Element elem_keys
  211. Element elem
  212. elem_keys = dict_keys(model["model"])
  213. while (0 < read_nr_out(elem_keys)):
  214. elem = model["model"][set_pop(elem_keys)]
  215. if (is_edge(elem)):
  216. dict_add(tm, elem, mm["Edge"])
  217. else:
  218. dict_add(tm, elem, mm["Node"])
  219. return model