conformance_scd.alc 7.8 KB

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