conformance_scd.alc 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. log((("Nominal subtype? " + reverseKeyLookup(metamodel["model"], subclass)) + " --|> ") + reverseKeyLookup(metamodel["model"], superclass))
  21. superclasses = get_superclasses(metamodel, subclass)
  22. while (0 < list_len(superclasses)):
  23. new_superclass = set_pop(superclasses)
  24. result = is_nominal_subtype(metamodel, new_superclass, superclass)
  25. if (result):
  26. return True
  27. return False
  28. Boolean function is_structural_instance(model : Element, instance : Element, type : Element):
  29. return is_structural_subtype(dict_read_node(model["type_mapping"], instance), type)
  30. Boolean function is_structural_subtype(subtype : Element, supertype : Element):
  31. // Determine whether it is just the exact type or not
  32. if (element_eq(subtype, supertype)):
  33. return True
  34. // Find all links that are required (name and type) from the specified type
  35. Element required_keys
  36. required_keys = dict_keys(supertype)
  37. Integer required_keys_len
  38. required_keys_len = dict_len(required_keys)
  39. String key
  40. Element equivalent
  41. Integer i
  42. i = 0
  43. // Go over all keys that we require
  44. while (i < required_keys_len):
  45. key = set_pop(required_keys)
  46. // Check whether they exist in the instance
  47. if (dict_in(subtype, key)):
  48. // Normally, we should still check whether they don't violate the constraints imposed on the class (i.e., are actually present)
  49. // For now, we ignore this and simply require that it is always there in the metamodel (not necessarily in the instance)
  50. // TODO
  51. // Still check whether the types match
  52. if (bool_not(is_structural_subtype(subtype[key], supertype[key]))):
  53. return False
  54. // All clear, so pass on to the next attribute
  55. i = i + 1
  56. else:
  57. return False
  58. // No violations found, so OK
  59. return True
  60. String function conformance_scd(model : Element):
  61. // Initialization
  62. Element keys
  63. Element metamodel
  64. Element typing
  65. String model_name
  66. Element src_model
  67. Element dst_model
  68. Element src_metamodel
  69. Element dst_metamodel
  70. Element element
  71. Element check_list
  72. Element check_type
  73. Element cardinalities
  74. Element scd
  75. // Load in variables
  76. scd = import_node("models/SimpleClassDiagrams")
  77. metamodel = model["metamodel"]
  78. typing = model["type_mapping"]
  79. cardinalities = create_node()
  80. // Create dictionary with all associations and allowed cardinalities
  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. dict_add(cardinalities, key, tmp_dict)
  106. else:
  107. log("Not checking cardinalities of " + key)
  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. element = model["model"][model_name]
  113. if (bool_not(dict_in_node(typing, element))):
  114. return "Model has no type specified: " + model_name
  115. if (bool_not(set_in_node(metamodel["model"], dict_read_node(typing, element)))):
  116. return "Type of element not in specified metamodel: " + model_name
  117. if (bool_not(is_nominal_instance(model, element, dict_read_node(typing, element)))):
  118. return "Element is not an instance of its specified type: " + model_name
  119. if (is_edge(element)):
  120. src_model = read_edge_src(element)
  121. dst_model = read_edge_dst(element)
  122. src_metamodel = read_edge_src(dict_read_node(typing, element))
  123. dst_metamodel = read_edge_dst(dict_read_node(typing, element))
  124. if (bool_not(is_nominal_instance(model, src_model, src_metamodel))):
  125. return "Source of model edge not typed by source of type: " + model_name
  126. if (bool_not(is_nominal_instance(model, dst_model, dst_metamodel))):
  127. return "Destination of model edge not typed by source of type: " + model_name
  128. // Check cardinality for all of our edges
  129. //
  130. // SLC..SUC TLC..TUC
  131. // A ---------------------> B
  132. //
  133. // First the incoming, so we are at B in the above figure
  134. log("At element " + model_name)
  135. Integer lower_val
  136. Integer upper_val
  137. Integer instances
  138. check_list = selectPossibleIncoming(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, reverseKeyLookup(metamodel["model"], check_type))):
  142. // Cardinalities defined for this association, so check them
  143. log(" check " + reverseKeyLookup(metamodel["model"], check_type))
  144. lower_val = cardinalities[check_type]["tlc"]
  145. upper_val = cardinalities[check_type]["tlc"]
  146. instances = list_len(allIncomingAssociationInstances(model, element, check_type))
  147. if (dict_in(cardinalities[check_type], "tlc")):
  148. // A lower cardinality was defined at the target
  149. if (integer_gt(cardinalities[check_type]["tlc"], instances)):
  150. return "Lower cardinality violation for incoming edge at " + model_name
  151. if (dict_in(cardinalities[check_type], "tuc")):
  152. // An upper cardinality was defined at the target
  153. if (integer_gt(cardinalities[check_type]["tuc"], instances)):
  154. return "Upper cardinality violation for incoming edge at " + model_name
  155. // Identical, but for outgoing, and thus for A in the figure
  156. check_list = selectPossibleOutgoing(model, model_name, dict_keys(cardinalities))
  157. while (0 < list_len(check_list)):
  158. check_type = set_pop(check_list)
  159. if (dict_in(cardinalities, reverseKeyLookup(metamodel["model"], check_type))):
  160. // Cardinalities defined for this association, so check them
  161. log(" check " + reverseKeyLookup(metamodel["model"], check_type))
  162. lower_val = cardinalities[check_type]["slc"]
  163. upper_val = cardinalities[check_type]["slc"]
  164. instances = list_len(allOutgoingAssociationInstances(model, element, check_type))
  165. if (dict_in(cardinalities[check_type], "slc")):
  166. // A lower cardinality was defined at the source
  167. if (integer_gt(cardinalities[check_type]["slc"], instances)):
  168. return "Lower cardinality violation for outgoing edge at " + model_name
  169. if (dict_in(cardinalities[check_type], "suc")):
  170. // An upper cardinality was defined at the source
  171. if (integer_gt(cardinalities[check_type]["suc"], instances)):
  172. return "Upper cardinality violation for outgoing edge at " + model_name
  173. // Check multiplicities, if they are defined (optional)
  174. Element metamodel_keys
  175. String metamodel_element
  176. Integer attr_value
  177. metamodel_keys = dict_keys(metamodel["model"])
  178. while (0 < list_len(metamodel_keys)):
  179. metamodel_element = set_pop(metamodel_keys)
  180. // Lower multiplicities
  181. attr_value = read_attribute(metamodel, metamodel_element, "lower_cardinality")
  182. if (element_neq(attr_value, read_root())):
  183. // We have defined a lower cardinality, so check number of instances!
  184. if (attr_value > list_len(allInstances(model, metamodel["model"][metamodel_element]))):
  185. return "Lower cardinality violated for class: " + metamodel_element
  186. // Upper multiplicities
  187. attr_value = read_attribute(metamodel, metamodel_element, "upper_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["model"][metamodel_element]))):
  191. return "Upper cardinality violated for class: " + metamodel_element
  192. // Structure seems fine, now do static semantics
  193. if (dict_in(metamodel, "constraints")):
  194. Element constraint_function
  195. constraint_function = metamodel["constraints"]
  196. return constraint_function(model)
  197. else:
  198. return "OK"
  199. Element function set_model_constraints(model : Element, func : Element):
  200. if (dict_in(model, "constraints")):
  201. dict_delete(model, "constraints")
  202. dict_add(model, "constraints", func)
  203. return model
  204. Element function generate_bottom_type_mapping(model : Element):
  205. Element mm
  206. mm = model["metamodel"]["model"]
  207. dict_delete(model, "type_mapping")
  208. Element tm
  209. tm = create_node()
  210. dict_add(model, "type_mapping", tm)
  211. // Iterate over every element
  212. Element elem_keys
  213. Element elem
  214. elem_keys = dict_keys(model["model"])
  215. while (0 < read_nr_out(elem_keys)):
  216. elem = model["model"][set_pop(elem_keys)]
  217. if (is_edge(elem)):
  218. dict_add(tm, elem, mm["Edge"])
  219. else:
  220. dict_add(tm, elem, mm["Node"])
  221. return model