conformance_scd.alc 9.5 KB

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