conformance_scd.alc 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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 : String, type : String):
  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"], model["model"][instance]), model["metamodel"]["model"][type])
  9. Boolean function is_nominal_instance(model : Element, instance : String, type : String):
  10. if (bool_not(dict_in(model["metamodel"]["model"], type))):
  11. // type is not even in the specified metamodel, so this will never work
  12. return False
  13. if (bool_and(is_edge(model["metamodel"]["model"][type]), bool_not(is_edge(model["model"][instance])))):
  14. // type is an edge, but we aren't
  15. return False
  16. if (bool_not(dict_in_node(model["type_mapping"], model["model"][instance]))):
  17. // doesn't even have a type
  18. return False
  19. return is_nominal_subtype(model["metamodel"], reverseKeyLookup(model["metamodel"]["model"], dict_read_node(model["type_mapping"], model["model"][instance])), type)
  20. Boolean function is_nominal_subtype(metamodel : Element, subclass : String, superclass : String):
  21. if (element_eq(metamodel["model"][subclass], metamodel["model"][superclass])):
  22. return True
  23. if (bool_not(dict_in(metamodel["model"], subclass))):
  24. return False
  25. if (bool_not(dict_in(metamodel["model"], superclass))):
  26. return False
  27. Element superclasses
  28. Element new_superclass
  29. Element result
  30. superclasses = get_superclasses(metamodel, subclass)
  31. while (0 < list_len(superclasses)):
  32. new_superclass = set_pop(superclasses)
  33. if (is_nominal_subtype(metamodel, new_superclass, superclass)):
  34. return True
  35. return False
  36. String function conformance_scd(model : Element):
  37. // Initialization
  38. Element keys
  39. Element metamodel
  40. Element typing
  41. String model_name
  42. String src_model
  43. String dst_model
  44. String src_metamodel
  45. String dst_metamodel
  46. Element element
  47. Element check_list
  48. String check_type
  49. Element cardinalities
  50. Element scd
  51. Integer instances
  52. // Load in variables
  53. scd = import_node("models/SimpleClassDiagrams")
  54. metamodel = model["metamodel"]
  55. typing = model["type_mapping"]
  56. cardinalities = create_node()
  57. // Create dictionary with all associations and allowed cardinalities
  58. if (list_len(model["model"]) > 0):
  59. keys = dict_keys(metamodel["model"])
  60. Integer slc
  61. Integer suc
  62. Integer tlc
  63. Integer tuc
  64. String key
  65. Element tmp_dict
  66. log("Start attribute read")
  67. while (0 < list_len(keys)):
  68. key = set_pop(keys)
  69. if (is_nominal_instance(metamodel, key, "Association")):
  70. tmp_dict = create_node()
  71. slc = read_attribute(metamodel, key, "source_lower_cardinality")
  72. suc = read_attribute(metamodel, key, "source_upper_cardinality")
  73. tlc = read_attribute(metamodel, key, "target_lower_cardinality")
  74. tuc = read_attribute(metamodel, key, "target_upper_cardinality")
  75. if (element_neq(slc, read_root())):
  76. dict_add(tmp_dict, "slc", slc)
  77. if (element_neq(suc, read_root())):
  78. dict_add(tmp_dict, "suc", suc)
  79. if (element_neq(tlc, read_root())):
  80. dict_add(tmp_dict, "tlc", tlc)
  81. if (element_neq(tuc, read_root())):
  82. dict_add(tmp_dict, "tuc", tuc)
  83. if (list_len(tmp_dict) > 0):
  84. dict_add(cardinalities, key, tmp_dict)
  85. log("Finish attribute read")
  86. // Iterate over each element of the model, finding out whether everything is fine
  87. keys = dict_keys(model["model"])
  88. while (0 < list_len(keys)):
  89. model_name = set_pop(keys)
  90. log("Check " + model_name)
  91. element = model["model"][model_name]
  92. if (bool_not(dict_in_node(typing, element))):
  93. return "Model has no type specified: " + model_name
  94. if (bool_not(set_in_node(metamodel["model"], dict_read_node(typing, element)))):
  95. return "Type of element not in specified metamodel: " + model_name
  96. if (bool_not(is_nominal_instance(model, model_name, reverseKeyLookup(metamodel["model"], dict_read_node(typing, element))))):
  97. return "Element is not an instance of its specified type: " + model_name
  98. if (is_edge(element)):
  99. src_model = reverseKeyLookup(model["model"], read_edge_src(element))
  100. dst_model = reverseKeyLookup(model["model"], read_edge_dst(element))
  101. src_metamodel = reverseKeyLookup(metamodel["model"], read_edge_src(dict_read_node(typing, element)))
  102. dst_metamodel = reverseKeyLookup(metamodel["model"], read_edge_dst(dict_read_node(typing, element)))
  103. if (bool_not(is_nominal_instance(model, src_model, src_metamodel))):
  104. return "Source of model edge not typed by source of type: " + model_name
  105. if (bool_not(is_nominal_instance(model, dst_model, dst_metamodel))):
  106. return "Destination of model edge not typed by source of type: " + model_name
  107. // Check cardinality for all of our edges
  108. //
  109. // SLC..SUC TLC..TUC
  110. // A ---------------------> B
  111. //
  112. // First the incoming, so we are at B in the above figure
  113. check_list = selectPossibleOutgoing(model, model_name, dict_keys(cardinalities))
  114. while (0 < list_len(check_list)):
  115. check_type = set_pop(check_list)
  116. if (dict_in(cardinalities, check_type)):
  117. // Cardinalities defined for this association, so check them
  118. if (bool_or(dict_in(cardinalities[check_type], "tlc"), dict_in(cardinalities[check_type], "tuc"))):
  119. instances = list_len(allOutgoingAssociationInstances(model, model_name, check_type))
  120. if (dict_in(cardinalities[check_type], "tlc")):
  121. // A lower cardinality was defined at the target
  122. if (integer_gt(cardinalities[check_type]["tlc"], instances)):
  123. return "Lower cardinality violation for outgoing edge at " + model_name
  124. if (dict_in(cardinalities[check_type], "tuc")):
  125. // An upper cardinality was defined at the target
  126. if (integer_lt(cardinalities[check_type]["tuc"], instances)):
  127. return "Upper cardinality violation for outgoing edge at " + model_name
  128. // Identical, but for outgoing, and thus for A in the figure
  129. check_list = selectPossibleIncoming(model, model_name, dict_keys(cardinalities))
  130. while (0 < list_len(check_list)):
  131. check_type = set_pop(check_list)
  132. if (dict_in(cardinalities, check_type)):
  133. // Cardinalities defined for this association, so check them
  134. if (bool_or(dict_in(cardinalities[check_type], "slc"), dict_in(cardinalities[check_type], "suc"))):
  135. instances = list_len(allIncomingAssociationInstances(model, model_name, check_type))
  136. if (dict_in(cardinalities[check_type], "slc")):
  137. // A lower cardinality was defined at the source
  138. if (integer_gt(cardinalities[check_type]["slc"], instances)):
  139. return "Lower cardinality violation for incoming edge at " + model_name
  140. if (dict_in(cardinalities[check_type], "suc")):
  141. // An upper cardinality was defined at the source
  142. if (integer_lt(cardinalities[check_type]["suc"], instances)):
  143. return "Upper cardinality violation for incoming edge at " + model_name
  144. Element constraint_function
  145. constraint_function = read_attribute(metamodel, reverseKeyLookup(metamodel["model"], dict_read_node(typing, element)), "constraint")
  146. if (element_neq(constraint_function, read_root())):
  147. String result
  148. result = constraint_function(model, model_name)
  149. if (result != "OK"):
  150. return result
  151. log("Start multiplicities")
  152. // Check multiplicities, if they are defined (optional)
  153. Element metamodel_keys
  154. String metamodel_element
  155. Integer attr_value
  156. metamodel_keys = dict_keys(metamodel["model"])
  157. while (0 < list_len(metamodel_keys)):
  158. metamodel_element = set_pop(metamodel_keys)
  159. // Lower multiplicities
  160. attr_value = read_attribute(metamodel, metamodel_element, "lower_cardinality")
  161. if (element_neq(attr_value, read_root())):
  162. // We have defined a lower cardinality, so check number of instances!
  163. if (attr_value > list_len(allInstances(model, metamodel_element))):
  164. return "Lower cardinality violated for class: " + metamodel_element
  165. // Upper multiplicities
  166. attr_value = read_attribute(metamodel, metamodel_element, "upper_cardinality")
  167. if (element_neq(attr_value, read_root())):
  168. // We have defined a lower cardinality, so check number of instances!
  169. if (attr_value < list_len(allInstances(model, metamodel_element))):
  170. return "Upper cardinality violated for class: " + metamodel_element
  171. log("Finished multiplicities")
  172. // Structure seems fine, now do static semantics
  173. if (dict_in(metamodel, "constraints")):
  174. Element constraint_function
  175. constraint_function = metamodel["constraints"]
  176. return constraint_function(model)
  177. else:
  178. return "OK"
  179. Element function set_model_constraints(model : Element, func : Element):
  180. if (dict_in(model, "constraints")):
  181. dict_delete(model, "constraints")
  182. dict_add(model, "constraints", func)
  183. return model
  184. Element function generate_bottom_type_mapping(model : Element):
  185. Element mm
  186. mm = model["metamodel"]["model"]
  187. dict_delete(model, "type_mapping")
  188. Element tm
  189. tm = create_node()
  190. dict_add(model, "type_mapping", tm)
  191. // Iterate over every element
  192. Element elem_keys
  193. Element elem
  194. elem_keys = dict_keys(model["model"])
  195. while (0 < read_nr_out(elem_keys)):
  196. elem = model["model"][set_pop(elem_keys)]
  197. if (is_edge(elem)):
  198. dict_add(tm, elem, mm["Edge"])
  199. else:
  200. dict_add(tm, elem, mm["Node"])
  201. return model