conformance_scd.alc 10 KB

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