conformance_scd.alc 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. return is_nominal_subtype(model["metamodel"], dict_read_node(model["type_mapping"], instance), type)
  11. Boolean function is_nominal_subtype(metamodel : Element, subclass : Element, superclass : Element):
  12. if (element_eq(subclass, superclass)):
  13. return True
  14. Element superclasses
  15. Element new_superclass
  16. Element result
  17. log((("Nominal subtype? " + cast_e2s(subclass)) + " --|> ") + cast_e2s(superclass))
  18. superclasses = get_superclasses(metamodel, subclass)
  19. while (0 < list_len(superclasses)):
  20. new_superclass = set_pop(superclasses)
  21. result = is_nominal_subtype(metamodel, new_superclass, superclass)
  22. if (result):
  23. return True
  24. return False
  25. Boolean function is_structural_instance(model : Element, instance : Element, type : Element):
  26. return is_structural_subtype(dict_read_node(model["type_mapping"], instance), type)
  27. Boolean function is_structural_subtype(subtype : Element, supertype : Element):
  28. // Determine whether it is just the exact type or not
  29. if (element_eq(subtype, supertype)):
  30. return True
  31. // Find all links that are required (name and type) from the specified type
  32. Element required_keys
  33. required_keys = dict_keys(supertype)
  34. Integer required_keys_len
  35. required_keys_len = dict_len(required_keys)
  36. String key
  37. Element equivalent
  38. Integer i
  39. i = 0
  40. // Go over all keys that we require
  41. while (i < required_keys_len):
  42. key = set_pop(required_keys)
  43. // Check whether they exist in the instance
  44. if (dict_in(subtype, key)):
  45. // Normally, we should still check whether they don't violate the constraints imposed on the class (i.e., are actually present)
  46. // For now, we ignore this and simply require that it is always there in the metamodel (not necessarily in the instance)
  47. // TODO
  48. // Still check whether the types match
  49. if (bool_not(is_structural_subtype(subtype[key], supertype[key]))):
  50. return False
  51. // All clear, so pass on to the next attribute
  52. i = i + 1
  53. else:
  54. return False
  55. // No violations found, so OK
  56. return True
  57. String function conformance_scd(model : Element):
  58. // Initialization
  59. Element keys
  60. Element metamodel
  61. Element typing
  62. String model_name
  63. Element src_model
  64. Element dst_model
  65. Element src_metamodel
  66. Element dst_metamodel
  67. Element element
  68. Element check_list
  69. Element check_type
  70. // Load in variables
  71. keys = dict_keys(model["model"])
  72. metamodel = model["metamodel"]
  73. typing = model["type_mapping"]
  74. // Iterate over each element of the model, finding out whether everything is fine
  75. while (0 < list_len(keys)):
  76. model_name = set_pop(keys)
  77. element = model["model"][model_name]
  78. if (bool_not(dict_in_node(typing, element))):
  79. return "Model has no type specified: " + model_name
  80. if (bool_not(set_in_node(metamodel["model"], dict_read_node(typing, element)))):
  81. return "Type of element not in specified metamodel: " + model_name
  82. if (bool_not(is_nominal_instance(model, element, dict_read_node(typing, element)))):
  83. return "Element is not an instance of its specified type: " + model_name
  84. if (is_edge(element)):
  85. src_model = read_edge_src(element)
  86. dst_model = read_edge_dst(element)
  87. src_metamodel = read_edge_src(dict_read_node(typing, element))
  88. dst_metamodel = read_edge_dst(dict_read_node(typing, element))
  89. if (bool_not(is_nominal_instance(model, src_model, src_metamodel))):
  90. return "Source of model edge not typed by source of type: " + model_name
  91. if (bool_not(is_nominal_instance(model, dst_model, dst_metamodel))):
  92. return "Destination of model edge not typed by source of type: " + model_name
  93. // Check cardinality for all of our edges
  94. //
  95. // SLC..SUC TLC..TUC
  96. // A ---------------------> B
  97. //
  98. // First the incoming, so we are at B in the above figure
  99. check_list = allPossibleIncoming(model, model_name)
  100. while (0 < list_len(check_list)):
  101. Integer instances
  102. Integer lower_val
  103. Integer upper_val
  104. check_type = set_pop(check_list)
  105. log("Lookup!")
  106. log(cast_e2s(check_type))
  107. log(cast_e2s(reverseNameLookup(model, check_type)))
  108. log(cast_e2s(reverseNameLookup(metamodel, check_type)))
  109. lower_val = read_attribute(metamodel, reverseNameLookup(metamodel, check_type), "target_lower_cardinality")
  110. upper_val = read_attribute(metamodel, reverseNameLookup(metamodel, check_type), "target_upper_cardinality")
  111. log("Look up " + cast_e2s(element))
  112. log(" " + cast_e2s(check_type))
  113. instances = list_len(allIncomingAssociationInstances(model, element, check_type))
  114. if (element_neq(lower_val, read_root())):
  115. // A lower multiplicity was defined at the target
  116. if (lower_val > instances):
  117. return "Lower cardinality violation for incoming edge at " + model_name
  118. if (element_neq(upper_val, read_root())):
  119. // A lower multiplicity was defined at the target
  120. if (upper_val < instances):
  121. return "Upper cardinality violation for incoming edge at " + model_name
  122. // Identical, but for outgoing, and thus for A in the figure
  123. check_list = allPossibleOutgoing(model, model_name)
  124. while (0 < list_len(check_list)):
  125. Integer instances
  126. Integer lower_val
  127. Integer upper_val
  128. check_type = set_pop(check_list)
  129. lower_val = read_attribute(metamodel, reverseNameLookup(metamodel, check_type), "source_lower_cardinality")
  130. upper_val = read_attribute(metamodel, reverseNameLookup(metamodel, check_type), "source_upper_cardinality")
  131. instances = list_len(allOutgoingAssociationInstances(model, element, check_type))
  132. if (element_neq(lower_val, read_root())):
  133. // A lower multiplicity was defined at the source
  134. if (lower_val > instances):
  135. return "Lower cardinality violation for incoming edge at " + model_name
  136. if (element_neq(upper_val, read_root())):
  137. // A lower multiplicity was defined at the source
  138. if (upper_val < instances):
  139. return "Upper cardinality violation for incoming edge at " + model_name
  140. // Check multiplicities, if they are defined (optional)
  141. Element metamodel_keys
  142. String metamodel_element
  143. Integer attr_value
  144. metamodel_keys = dict_keys(metamodel["model"])
  145. while (0 < list_len(metamodel_keys)):
  146. metamodel_element = set_pop(metamodel_keys)
  147. // Lower multiplicities
  148. attr_value = read_attribute(metamodel, metamodel_element, "lower_multiplicity")
  149. if (element_neq(attr_value, read_root())):
  150. // We have defined a lower multiplicity, so check number of instances!
  151. if (attr_value > list_len(allInstances(model, metamodel["model"][metamodel_element]))):
  152. return "Lower multiplicity violated for class: " + metamodel_element
  153. // Upper multiplicities
  154. attr_value = read_attribute(metamodel, metamodel_element, "upper_multiplicity")
  155. if (element_neq(attr_value, read_root())):
  156. // We have defined a lower multiplicity, so check number of instances!
  157. if (attr_value < list_len(allInstances(model, metamodel["model"][metamodel_element]))):
  158. return "Upper multiplicity violated for class: " + metamodel_element
  159. // Structure seems fine, now do static semantics
  160. if (dict_in(metamodel, "constraints")):
  161. Element constraint_function
  162. constraint_function = metamodel["constraints"]
  163. return constraint_function(model)
  164. else:
  165. return "OK"
  166. Element function set_model_constraints(model : Element, func : Element):
  167. if (dict_in(model, "constraints")):
  168. dict_delete(model, "constraints")
  169. dict_add(model, "constraints", func)
  170. return model
  171. Element function generate_bottom_type_mapping(model : Element):
  172. Element mm
  173. mm = model["metamodel"]["model"]
  174. dict_delete(model, "type_mapping")
  175. Element tm
  176. tm = create_node()
  177. dict_add(model, "type_mapping", tm)
  178. // Iterate over every element
  179. Element elem_keys
  180. Element elem
  181. elem_keys = dict_keys(model["model"])
  182. while (0 < read_nr_out(elem_keys)):
  183. elem = model["model"][set_pop(elem_keys)]
  184. if (is_edge(elem)):
  185. dict_add(tm, elem, mm["Edge"])
  186. else:
  187. dict_add(tm, elem, mm["Node"])
  188. return model