conformance_finding.alc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. log("Model is incompletely typed!")
  31. log("Model has: " + set_to_string(dict_keys(model["model"])))
  32. log("Type mapping has: " + set_to_string(dict_keys(tm)))
  33. log("Difference: " + set_to_string(set_difference(dict_keys(model["model"]), dict_keys(tm))))
  34. // TODO for now, this only returns something for a simple case, where the MM has one edge, and one node
  35. // and it makes the assumption that SCD is the M3 level...
  36. // First find the name of the edge and node elements
  37. Element elems
  38. String elem
  39. String node_source_element
  40. String node_target_element
  41. String edge_element
  42. Element nodes
  43. Element edges
  44. nodes = allInstances(model["metamodel"], "Class")
  45. edges = allInstances(model["metamodel"], "Association")
  46. log("Searching for type...")
  47. if (bool_and(set_len(edges) == 1, set_len(nodes) == 1)):
  48. // Simple allocation: this seems like conformance bottom
  49. node_source_element = set_pop(nodes)
  50. node_target_element = node_source_element
  51. edge_element = set_pop(edges)
  52. log("Found node: " + node_source_element)
  53. log("Found edge: " + edge_element)
  54. elif (bool_and(set_len(edges) == 1, set_len(nodes) == 2)):
  55. // Simple allocation: this seems like a type mapping
  56. // Make sure to check that the edge goes from one node to the other!
  57. edge_element = set_pop(edges)
  58. node_source_element = readAssociationSource(model["metamodel"], edge_element)
  59. node_target_element = readAssociationDestination(model["metamodel"], edge_element)
  60. if (value_eq(node_source_element, node_target_element)):
  61. log("Source and target are the same, meaning that the second node is unknown")
  62. return False!
  63. log("Found edge: " + edge_element)
  64. log("Found source node: " + node_source_element)
  65. log("Found target node: " + node_target_element)
  66. else:
  67. log("Could not automatically deduce mapping in a trivial way!")
  68. return False!
  69. // Now we have both an edge_element and node_element of the metamodel
  70. // Now just trivially bind all elements!
  71. log("Searching for type...")
  72. elems = dict_keys(model["model"])
  73. while (set_len(elems) > 0):
  74. elem = set_pop(elems)
  75. if (bool_and(is_edge(model["model"][elem]), read_nr_out(model["model"][elem]) == 0)):
  76. // An edge, and there is always exactly one, so type
  77. retype(model, elem, edge_element)
  78. log("Type " + elem + " as " + edge_element)
  79. // The source and target are ALWAYS typed as well!
  80. retype(model, readAssociationSource(model, elem), node_source_element)
  81. log("Type " + readAssociationSource(model, elem) + " as " + node_source_element)
  82. retype(model, readAssociationDestination(model, elem), node_target_element)
  83. log("Type " + readAssociationDestination(model, elem) + " as " + node_target_element)
  84. elif (node_source_element == node_target_element):
  85. // A node, and we are sure that there is only one
  86. retype(model, elem, node_source_element)
  87. // 3) (optional) verify that the mapping is correct with conformance checking
  88. // TODO
  89. return True!