conformance_finding.alc 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. include "primitives.alh"
  2. include "object_operations.alh"
  3. include "typing.alh"
  4. Boolean function find_type_mapping(model : Element):
  5. // Finds a correct type mapping for the provided model (with partial type mapping)
  6. // We go through several phases:
  7. // 1) remove elements from type mapping that are not in the model or metamodel
  8. // 2) find a mapping based on the current partial mapping
  9. // 3) (optional) verify that the mapping is correct with conformance checking
  10. // Returns True if the type mapping was altered
  11. // Start of with some initialization
  12. Element tm
  13. Element elems
  14. String elem
  15. tm = get_type_mapping_as_dict(model)
  16. // 1) remove elements from type mapping that are not in the model or metamodel
  17. elems = dict_keys(tm)
  18. while (set_len(elems) > 0):
  19. elem = set_pop(elems)
  20. if (bool_not(dict_in(model["model"], elem))):
  21. // Remove the key, as the model does not contain the element anymore
  22. dict_delete(tm, elem)
  23. else:
  24. if (bool_not(dict_in(model["metamodel"]["model"], tm[elem]))):
  25. // Remove the key, as the metamodel does not contain the type anymore
  26. dict_delete(tm, elem)
  27. // 2) find a mapping based on the current partial mapping, but only if it is not yet complete
  28. // TODO this must be expanded for other things than trivial metamodels!
  29. if (dict_len(model["model"]) > dict_len(tm)):
  30. // TODO for now, this only returns something for a simple case, where the MM has one edge, and one node
  31. // and it makes the assumption that SCD is the M3 level...
  32. // First find the name of the edge and node elements
  33. Element elems
  34. String elem
  35. Element nodes
  36. Element edges
  37. nodes = allInstances(model["metamodel"], "Class")
  38. edges = allInstances(model["metamodel"], "Association")
  39. // Filter out classes from the associations
  40. Element new_nodes
  41. String node
  42. new_nodes = set_create()
  43. while (set_len(nodes) > 0):
  44. node = set_pop(nodes)
  45. if (bool_not(is_edge(model["metamodel"]["model"][node]))):
  46. set_add(new_nodes, node)
  47. nodes = new_nodes
  48. log("Nodes: " + set_to_string(nodes))
  49. log("Edges: " + set_to_string(edges))
  50. // Simple allocation: this seems like conformance bottom
  51. if (bool_and(set_len(edges) == 1, set_len(nodes) == 1)):
  52. String node_source_element
  53. String edge_element
  54. node_source_element = set_pop(nodes)
  55. edge_element = set_pop(edges)
  56. elems = dict_keys(model["model"])
  57. while (set_len(elems) > 0):
  58. elem = set_pop(elems)
  59. if (is_edge(model["model"][elem])):
  60. retype(model, elem, edge_element)
  61. else:
  62. retype(model, elem, node_source_element)
  63. return True!
  64. // Simple allocation: this seems like a traceability model or so
  65. // Make sure to check that the edge goes from one node to the other!
  66. if (bool_and(set_len(edges) == 1, set_len(nodes) == 2)):
  67. String node_target_element
  68. String node_source_element
  69. String edge_element
  70. edge_element = set_pop(edges)
  71. node_source_element = readAssociationSource(model["metamodel"], edge_element)
  72. node_target_element = readAssociationDestination(model["metamodel"], edge_element)
  73. if (value_eq(node_source_element, node_target_element)):
  74. log("Could not automatically deduce mapping in a trivial way!")
  75. return False!
  76. elems = dict_keys(model["model"])
  77. while (set_len(elems) > 0):
  78. elem = set_pop(elems)
  79. if (bool_and(is_edge(model["model"][elem]), read_nr_out(model["model"][elem]) == 0)):
  80. // An edge, and there is always exactly one, so type
  81. retype(model, elem, edge_element)
  82. // The source and target are ALWAYS typed as well!
  83. retype(model, readAssociationSource(model, elem), node_source_element)
  84. retype(model, readAssociationDestination(model, elem), node_target_element)
  85. return True!
  86. // Simple allocation: this seems like a dictionary model or so
  87. if (bool_and(set_len(edges) == 2, set_len(nodes) == 3)):
  88. String main_edge
  89. String small_edge
  90. main_edge = set_pop(edges)
  91. small_edge = set_pop(edges)
  92. if (readAssociationSource(model["metamodel"], main_edge) == small_edge):
  93. // Switch both
  94. String tmp
  95. tmp = main_edge
  96. main_edge = small_edge
  97. small_edge = tmp
  98. if (readAssociationSource(model["metamodel"], small_edge) == main_edge):
  99. String origin
  100. String value
  101. String key
  102. String middle_edge
  103. origin = readAssociationSource(model["metamodel"], main_edge)
  104. value = readAssociationDestination(model["metamodel"], main_edge)
  105. key = readAssociationDestination(model["metamodel"], small_edge)
  106. if (bool_and(bool_and(origin != value, origin != key), value != key)):
  107. // All three nodes are different, meaning that we are complete and have identified a simple mapping!
  108. elems = dict_keys(model["model"])
  109. while (set_len(elems) > 0):
  110. elem = set_pop(elems)
  111. if (bool_and(is_edge(model["model"][elem]), read_nr_out(model["model"][elem]) == 0)):
  112. // An edge with no outgoing links, meaning that it is the small_edge
  113. retype(model, elem, small_edge)
  114. // Source is main_edge and target is key
  115. middle_edge = readAssociationSource(model, elem)
  116. retype(model, middle_edge, main_edge)
  117. retype(model, readAssociationDestination(model, elem), key)
  118. // The main_edge goes from root to the value
  119. retype(model, readAssociationSource(model, middle_edge), origin)
  120. retype(model, readAssociationDestination(model, middle_edge), value)
  121. return True!
  122. log("Could not automatically deduce mapping in a trivial way!")
  123. log("Model: " + set_to_string(dict_keys(model["model"])))
  124. log("TM: " + set_to_string(dict_keys(tm)))
  125. log("DIFF: " + set_to_string(set_difference(dict_keys(model["model"]), dict_keys(tm))))
  126. return False!
  127. return True!