transform.alc 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. include "primitives.alh"
  2. include "object_operations.alh"
  3. include "modelling.alh"
  4. Element function make_matching_schedule(LHS_model : Element):
  5. Element schedule
  6. Element workset
  7. Element all_elements
  8. Element full_all_elements
  9. Integer required_size
  10. String new_element
  11. Integer counter
  12. String next
  13. // Initialize
  14. schedule = create_node()
  15. workset = create_node()
  16. all_elements = allInstances(LHS_model, "Pre_Element")
  17. full_all_elements = set_copy(all_elements)
  18. required_size = read_nr_out(all_elements)
  19. // Need to keep adding to the schedule
  20. while (read_nr_out(schedule) < required_size):
  21. // workset is empty, but we still need to add to the list
  22. // Therefore, we pick a random, unbound node, and add it to the workset
  23. new_element = set_pop(all_elements)
  24. while (bool_or(set_in(schedule, new_element), is_edge(LHS_model["model"][new_element]))):
  25. // Element is not usable, so pick another one
  26. new_element = set_pop(all_elements)
  27. set_add(workset, new_element)
  28. // Handle the workset
  29. while (read_nr_out(workset) > 0):
  30. // Still elements in the workset, so pop from these first
  31. next = set_pop(workset)
  32. // Check if element might not be already used somewhere
  33. if (bool_not(set_in(schedule, next))):
  34. if (set_in(full_all_elements, next)):
  35. list_append(schedule, next)
  36. // If it is an edge, we should also add the target and source
  37. if (is_edge(LHS_model["model"][next])):
  38. // Add the target/source to the schedule
  39. set_add(workset, reverseKeyLookup(LHS_model["model"], read_edge_src(LHS_model["model"][next])))
  40. set_add(workset, reverseKeyLookup(LHS_model["model"], read_edge_dst(LHS_model["model"][next])))
  41. // Also add all outgoing links
  42. counter = read_nr_out(LHS_model["model"][next])
  43. while (counter > 0):
  44. counter = counter - 1
  45. if (set_in_node(LHS_model["model"], read_out(LHS_model["model"][next], counter))):
  46. set_add(workset, reverseKeyLookup(LHS_model["model"], read_out(LHS_model["model"][next], counter)))
  47. return schedule!
  48. Element function get_possible_bindings(host_model : Element, LHS_model : Element, current_element : String, map : Element):
  49. Element options
  50. String src_label
  51. String dst_label
  52. String typename
  53. String original_typename
  54. options = create_node()
  55. typename = reverseKeyLookup(LHS_model["metamodel"]["model"], dict_read_node(LHS_model["type_mapping"], LHS_model["model"][current_element]))
  56. original_typename = string_substr(typename, 4, string_len(typename))
  57. if (is_edge(LHS_model["model"][current_element])):
  58. // Is an edge, so check for already bound source/target
  59. src_label = read_attribute(LHS_model, reverseKeyLookup(LHS_model["model"], read_edge_src(LHS_model["model"][current_element])), "label")
  60. dst_label = read_attribute(LHS_model, reverseKeyLookup(LHS_model["model"], read_edge_dst(LHS_model["model"][current_element])), "label")
  61. if (bool_and(set_in(dict_keys(map), src_label), set_in(dict_keys(map), dst_label))):
  62. // Source and destination are bound
  63. options = allOutgoingAssociationInstances(host_model, map[src_label], original_typename)
  64. options = set_overlap(options, allIncomingAssociationInstances(host_model, map[dst_label], original_typename))
  65. elif (set_in(dict_keys(map), src_label)):
  66. // Source is bound
  67. options = allOutgoingAssociationInstances(host_model, map[src_label], original_typename)
  68. elif (set_in(dict_keys(map), dst_label)):
  69. // Destination is bound
  70. options = allIncomingAssociationInstances(host_model, map[dst_label], original_typename)
  71. else:
  72. // Neither is bound, so just get all of them
  73. log("ERROR: unbound source/target for association!")
  74. return create_node()!
  75. else:
  76. // Is a node, so check for already bound incoming/outgoing
  77. options = allInstances(host_model, original_typename)
  78. // Filter options further
  79. Element filtered_options
  80. String option
  81. filtered_options = create_node()
  82. while (read_nr_out(options) > 0):
  83. option = set_pop(options)
  84. // Check for detecting same element twice
  85. if (bool_not(set_in(map, option))):
  86. // Option is already present with another label, so skip this!
  87. // Check for local constraints of element
  88. if (element_eq(read_attribute(LHS_model, current_element, "constraint"), read_root())):
  89. // No local constraints, so all is well
  90. set_add(filtered_options, option)
  91. else:
  92. // Check local constraints and add only if positive
  93. Element constraint_function
  94. constraint_function = read_attribute(LHS_model, current_element, "constraint")
  95. Boolean result
  96. result = constraint_function(host_model, option)
  97. if (result):
  98. set_add(filtered_options, option)
  99. return filtered_options!
  100. Element function match(host_model : Element, LHS_model : Element):
  101. // Match the LHS_model to the host_model, returning all possible mappings from LHS_model elements to host_model elements
  102. // Make the schedule first
  103. Element schedule
  104. schedule = make_matching_schedule(LHS_model)
  105. // Now follow the schedule, incrementally building all mappings
  106. Element mappings
  107. Element new_mappings
  108. Element new_map
  109. String current_element
  110. Element map
  111. String option
  112. Element options
  113. mappings = create_node()
  114. set_add(mappings, create_node())
  115. while (bool_and(read_nr_out(schedule) > 0, read_nr_out(mappings) > 0)):
  116. current_element = list_pop(schedule, 0)
  117. new_mappings = create_node()
  118. while (read_nr_out(mappings) > 0):
  119. map = set_pop(mappings)
  120. options = get_possible_bindings(host_model, LHS_model, current_element, map)
  121. while (read_nr_out(options) > 0):
  122. option = set_pop(options)
  123. new_map = dict_copy(map)
  124. dict_add(new_map, read_attribute(LHS_model, current_element, "label"), option)
  125. set_add(new_mappings, new_map)
  126. mappings = new_mappings
  127. return mappings!
  128. Void function rewrite(host_model : Element, RHS_model : Element, mapping : Element):
  129. // Rewrite the host model based on the mapping combined with the RHS
  130. Element LHS_labels
  131. Element RHS_labels
  132. Element RHS_elements
  133. Element remaining
  134. String elem
  135. String label
  136. Element labels_to_remove
  137. Element labels_to_add
  138. String typename
  139. String original_typename
  140. String src
  141. String dst
  142. Element new_mapping
  143. String new_name
  144. LHS_labels = dict_keys(mapping)
  145. RHS_labels = create_node()
  146. RHS_elements = allInstances(RHS_model, "Post_Element")
  147. while (read_nr_out(RHS_elements) > 0):
  148. set_add(RHS_labels, read_attribute(RHS_model, set_pop(RHS_elements), "label"))
  149. remaining = set_overlap(LHS_labels, RHS_labels)
  150. while (read_nr_out(remaining) > 0):
  151. elem = set_pop(remaining)
  152. set_remove(LHS_labels, elem)
  153. set_remove(RHS_labels, elem)
  154. log("LHS labels: " + set_to_string(LHS_labels))
  155. log("RHS labels: " + set_to_string(RHS_labels))
  156. labels_to_remove = LHS_labels
  157. labels_to_add = set_to_list(RHS_labels)
  158. log("Labels to add: " + dict_to_string(labels_to_add))
  159. log("Labels to remove: " + set_to_string(labels_to_remove))
  160. new_mapping = dict_copy(mapping)
  161. while (read_nr_out(labels_to_remove) > 0):
  162. // Remove the elements linked to these labels
  163. label = set_pop(labels_to_remove)
  164. log("Remove element linked to label " + label)
  165. log(" --> " + cast_v2s(mapping[label]))
  166. model_delete_element(host_model, mapping[label])
  167. dict_delete(new_mapping, label)
  168. while (read_nr_out(labels_to_add) > 0):
  169. // Add the elementsl inked to these labels
  170. label = list_pop(labels_to_add, 0)
  171. log("Add element linked to label " + label)
  172. if (is_edge(RHS_model["model"][mapping[label]])):
  173. // Edge
  174. src = read_attribute(RHS_model, reverseKeyLookup(RHS_model["model"], read_edge_src(RHS_model["model"][mapping[label]])), "label")
  175. dst = read_attribute(RHS_model, reverseKeyLookup(RHS_model["model"], read_edge_dst(RHS_model["model"][mapping[label]])), "label")
  176. // First check whether both source and destination are already created
  177. if (bool_and(dict_in(new_mapping, src), dict_in(new_mapping, dst))):
  178. // Both are present, so we can make the link
  179. typename = reverseKeyLookup(RHS_model["metamodel"]["model"], dict_read_node(RHS_model["type_mapping"], RHS_model["model"][mapping[label]]))
  180. original_typename = string_substr(typename, 5, string_len(typename))
  181. new_name = instantiate_link(host_model, original_typename, "", new_mapping[src], new_mapping[dst])
  182. dict_add(new_mapping, label, new_name)
  183. else:
  184. // Delay this a bit, until all are bound
  185. list_append(labels_to_add, label)
  186. else:
  187. // Node
  188. // Create the node and add it
  189. typename = reverseKeyLookup(RHS_model["metamodel"]["model"], dict_read_node(RHS_model["type_mapping"], RHS_model["model"][mapping[label]]))
  190. original_typename = string_substr(typename, 5, string_len(typename))
  191. new_name = instantiate_node(host_model, original_typename, "")
  192. dict_add(new_mapping, label, new_name)
  193. return!
  194. Void function transform(host_model : Element, LHS_model : Element, RHS_model : Element):
  195. Element mapping
  196. Element mappings
  197. // Get all possible mappings
  198. mappings = match(host_model, LHS_model)
  199. log("Found total mappings: " + cast_v2s(read_nr_out(mappings)))
  200. // Select one such mapping and rewrite it
  201. if (read_nr_out(mappings) > 0):
  202. // Mapping found, so can rewrite it
  203. mapping = set_pop(mappings)
  204. log("Found example mapping " + dict_to_string(mapping))
  205. rewrite(host_model, RHS_model, mapping)
  206. else:
  207. output("No mapping found!")
  208. return!