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