conformance_scd.alc 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. include "primitives.alh"
  2. include "library.alh"
  3. include "object_operations.alh"
  4. include "constructors.alh"
  5. Boolean function is_direct_instance(model : Element, instance : Element, type : Element):
  6. // Just check whether or not the type mapping specifies the type as the type of the instance
  7. return element_eq(dict_read_node(model["type_mapping"], instance), type)
  8. Boolean function is_nominal_instance(model : Element, instance : Element, type : Element):
  9. return is_nominal_subtype(model["metamodel"], dict_read_node(model["type_mapping"], instance), type)
  10. Boolean function is_nominal_subtype(metamodel : Element, subclass : Element, superclass : Element):
  11. if (element_eq(subclass, superclass)):
  12. return True
  13. Element superclasses
  14. Element new_superclass
  15. Element result
  16. superclasses = get_superclasses(metamodel, subclass)
  17. while (0 < list_len(superclasses)):
  18. new_superclass = set_pop(superclasses)
  19. result = is_nominal_subtype(metamodel, new_superclass, superclass)
  20. if (result):
  21. return True
  22. return False
  23. Boolean function is_structural_instance(model : Element, instance : Element, type : Element):
  24. return is_structural_subtype(dict_read_node(model["type_mapping"], instance), type)
  25. Boolean function is_structural_subtype(subtype : Element, supertype : Element):
  26. // Determine whether it is just the exact type or not
  27. if (element_eq(subtype, supertype)):
  28. return True
  29. // Find all links that are required (name and type) from the specified type
  30. Element required_keys
  31. required_keys = dict_keys(supertype)
  32. Integer required_keys_len
  33. required_keys_len = dict_len(required_keys)
  34. String key
  35. Element equivalent
  36. Integer i
  37. i = 0
  38. // Go over all keys that we require
  39. while (i < required_keys_len):
  40. key = set_pop(required_keys)
  41. // Check whether they exist in the instance
  42. if (dict_in(subtype, key)):
  43. // Normally, we should still check whether they don't violate the constraints imposed on the class (i.e., are actually present)
  44. // For now, we ignore this and simply require that it is always there in the metamodel (not necessarily in the instance)
  45. // TODO
  46. // Still check whether the types match
  47. if (bool_not(is_structural_subtype(subtype[key], supertype[key]))):
  48. return False
  49. // All clear, so pass on to the next attribute
  50. i = i + 1
  51. else:
  52. return False
  53. // No violations found, so OK
  54. return True
  55. String function conformance_scd(model : Element):
  56. // Initialization
  57. Element keys
  58. Element metamodel
  59. Element typing
  60. String model_name
  61. Element src_model
  62. Element dst_model
  63. Element src_metamodel
  64. Element dst_metamodel
  65. Element element
  66. // Load in variables
  67. keys = dict_keys(model["model"])
  68. metamodel = model["metamodel"]
  69. typing = model["type_mapping"]
  70. // Iterate over each element of the model, finding out whether everything is fine
  71. while (0 < list_len(keys)):
  72. model_name = set_pop(keys)
  73. element = model["model"][model_name]
  74. if (bool_not(dict_in_node(typing, element))):
  75. return "Model has no type specified: " + model_name
  76. if (bool_not(set_in_node(metamodel["model"], dict_read_node(typing, element)))):
  77. return "Type of element not in specified metamodel: " + model_name
  78. if (bool_not(is_nominal_instance(model, element, dict_read_node(typing, element)))):
  79. return "Element is not an instance of its specified type: " + model_name
  80. if (is_edge(element)):
  81. src_model = read_edge_src(element)
  82. dst_model = read_edge_dst(element)
  83. src_metamodel = read_edge_src(dict_read_node(typing, element))
  84. dst_metamodel = read_edge_dst(dict_read_node(typing, element))
  85. if (bool_not(is_nominal_instance(model, src_model, src_metamodel))):
  86. return "Source of model edge not typed by source of type: " + model_name
  87. if (bool_not(is_nominal_instance(model, dst_model, dst_metamodel))):
  88. return "Destination of model edge not typed by source of type: " + model_name
  89. // Structure seems fine, now do static semantics
  90. if (dict_in(metamodel, "constraints")):
  91. Element constraint_function
  92. constraint_function = metamodel["constraints"]
  93. return constraint_function(model)
  94. else:
  95. return "OK"
  96. Element function set_model_constraints(model : Element, func : Element):
  97. if (dict_in(model, "constraints")):
  98. dict_delete(model, "constraints")
  99. dict_add(model, "constraints", func)
  100. return model
  101. Element function generate_bottom_type_mapping(model : Element):
  102. Element mm
  103. mm = model["metamodel"]["model"]
  104. dict_delete(model, "type_mapping")
  105. Element tm
  106. tm = create_node()
  107. dict_add(model, "type_mapping", tm)
  108. // Iterate over every element
  109. Element elem_keys
  110. Element elem
  111. elem_keys = dict_keys(model["model"])
  112. while (0 < read_nr_out(elem_keys)):
  113. elem = model["model"][set_pop(elem_keys)]
  114. if (is_edge(elem)):
  115. dict_add(tm, elem, mm["Edge"])
  116. else:
  117. dict_add(tm, elem, mm["Node"])
  118. return model