conformance_scd.alc 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. // Load in variables
  68. keys = dict_keys(model["model"])
  69. metamodel = model["metamodel"]
  70. typing = model["type_mapping"]
  71. // Iterate over each element of the model, finding out whether everything is fine
  72. while (0 < list_len(keys)):
  73. model_name = set_pop(keys)
  74. element = model["model"][model_name]
  75. if (bool_not(dict_in_node(typing, element))):
  76. return "Model has no type specified: " + model_name
  77. if (bool_not(set_in_node(metamodel["model"], dict_read_node(typing, element)))):
  78. return "Type of element not in specified metamodel: " + model_name
  79. if (bool_not(is_nominal_instance(model, element, dict_read_node(typing, element)))):
  80. return "Element is not an instance of its specified type: " + model_name
  81. if (is_edge(element)):
  82. src_model = read_edge_src(element)
  83. dst_model = read_edge_dst(element)
  84. src_metamodel = read_edge_src(dict_read_node(typing, element))
  85. dst_metamodel = read_edge_dst(dict_read_node(typing, element))
  86. if (bool_not(is_nominal_instance(model, src_model, src_metamodel))):
  87. return "Source of model edge not typed by source of type: " + model_name
  88. if (bool_not(is_nominal_instance(model, dst_model, dst_metamodel))):
  89. return "Destination of model edge not typed by source of type: " + model_name
  90. // Structure seems fine, now do static semantics
  91. if (dict_in(metamodel, "constraints")):
  92. Element constraint_function
  93. constraint_function = metamodel["constraints"]
  94. return constraint_function(model)
  95. else:
  96. return "OK"
  97. Element function set_model_constraints(model : Element, func : Element):
  98. if (dict_in(model, "constraints")):
  99. dict_delete(model, "constraints")
  100. dict_add(model, "constraints", func)
  101. return model
  102. Element function generate_bottom_type_mapping(model : Element):
  103. Element mm
  104. mm = model["metamodel"]["model"]
  105. dict_delete(model, "type_mapping")
  106. Element tm
  107. tm = create_node()
  108. dict_add(model, "type_mapping", tm)
  109. // Iterate over every element
  110. Element elem_keys
  111. Element elem
  112. elem_keys = dict_keys(model["model"])
  113. while (0 < read_nr_out(elem_keys)):
  114. elem = model["model"][set_pop(elem_keys)]
  115. if (is_edge(elem)):
  116. dict_add(tm, elem, mm["Edge"])
  117. else:
  118. dict_add(tm, elem, mm["Node"])
  119. return model