transform.alc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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. // Finished, so try the global constraint
  128. Element LHS
  129. Element constraint
  130. new_mappings = create_node()
  131. LHS = allInstances(LHS_model, "LHS")
  132. LHS = set_pop(LHS)
  133. constraint = read_attribute(LHS_model, LHS, "constraint")
  134. if (element_neq(constraint, read_root())):
  135. while (read_nr_out(mappings) > 0):
  136. map = set_pop(mappings)
  137. if (constraint(host_model, map)):
  138. set_add(new_mappings, map)
  139. mappings = new_mappings
  140. return mappings!
  141. Void function rewrite(host_model : Element, RHS_model : Element, mapping : Element):
  142. // Rewrite the host model based on the mapping combined with the RHS
  143. Element LHS_labels
  144. Element RHS_labels
  145. Element RHS_elements
  146. Element remaining
  147. String elem
  148. String label
  149. Element labels_to_remove
  150. Element labels_to_add
  151. String typename
  152. String original_typename
  153. String src
  154. String dst
  155. Element new_mapping
  156. String new_name
  157. Element RHS_map
  158. String tmp
  159. Element value
  160. Element value_function
  161. Element action
  162. Element original_RHS_labels
  163. LHS_labels = dict_keys(mapping)
  164. RHS_labels = create_node()
  165. RHS_map = create_node()
  166. RHS_elements = allInstances(RHS_model, "Post_Element")
  167. while (read_nr_out(RHS_elements) > 0):
  168. tmp = set_pop(RHS_elements)
  169. label = read_attribute(RHS_model, tmp, "label")
  170. set_add(RHS_labels, label)
  171. dict_add(RHS_map, label, tmp)
  172. remaining = set_overlap(LHS_labels, RHS_labels)
  173. original_RHS_labels = set_copy(RHS_labels)
  174. while (read_nr_out(remaining) > 0):
  175. elem = set_pop(remaining)
  176. set_remove(LHS_labels, elem)
  177. set_remove(RHS_labels, elem)
  178. labels_to_remove = LHS_labels
  179. labels_to_add = set_to_list(RHS_labels)
  180. new_mapping = dict_copy(mapping)
  181. while (read_nr_out(labels_to_add) > 0):
  182. // Add the elements linked to these labels
  183. label = list_pop(labels_to_add, 0)
  184. if (element_neq(read_attribute(RHS_model, RHS_map[label], "value"), read_root())):
  185. // There is a value associated with this node
  186. value_function = read_attribute(RHS_model, RHS_map[label], "value")
  187. value = value_function(host_model, mapping)
  188. typename = reverseKeyLookup(RHS_model["metamodel"]["model"], dict_read_node(RHS_model["type_mapping"], RHS_model["model"][RHS_map[label]]))
  189. original_typename = string_substr(typename, 5, string_len(typename))
  190. new_name = instantiate_value(host_model, original_typename, "", value)
  191. dict_add(new_mapping, label, new_name)
  192. elif (is_edge(RHS_model["model"][RHS_map[label]])):
  193. // Edge
  194. src = read_attribute(RHS_model, reverseKeyLookup(RHS_model["model"], read_edge_src(RHS_model["model"][RHS_map[label]])), "label")
  195. dst = read_attribute(RHS_model, reverseKeyLookup(RHS_model["model"], read_edge_dst(RHS_model["model"][RHS_map[label]])), "label")
  196. // First check whether both source and destination are already created
  197. if (bool_and(dict_in(new_mapping, src), dict_in(new_mapping, dst))):
  198. // Both are present, so we can make the link
  199. typename = reverseKeyLookup(RHS_model["metamodel"]["model"], dict_read_node(RHS_model["type_mapping"], RHS_model["model"][RHS_map[label]]))
  200. original_typename = string_substr(typename, 5, string_len(typename))
  201. new_name = instantiate_link(host_model, original_typename, "", new_mapping[src], new_mapping[dst])
  202. dict_add(new_mapping, label, new_name)
  203. else:
  204. // Delay this a bit, until all are bound
  205. list_append(labels_to_add, label)
  206. else:
  207. // Node
  208. // Create the node and add it
  209. typename = reverseKeyLookup(RHS_model["metamodel"]["model"], dict_read_node(RHS_model["type_mapping"], RHS_model["model"][RHS_map[label]]))
  210. original_typename = string_substr(typename, 5, string_len(typename))
  211. new_name = instantiate_node(host_model, original_typename, "")
  212. dict_add(new_mapping, label, new_name)
  213. while (read_nr_out(original_RHS_labels) > 0):
  214. label = set_pop(original_RHS_labels)
  215. action = read_attribute(RHS_model, RHS_map[label], "action")
  216. if (element_neq(action, read_root())):
  217. action(host_model, new_mapping[label], mapping)
  218. while (read_nr_out(labels_to_remove) > 0):
  219. // Remove the elements linked to these labels
  220. label = set_pop(labels_to_remove)
  221. model_delete_element(host_model, mapping[label])
  222. dict_delete(new_mapping, label)
  223. // Execute global action (whatever it may be)
  224. Element RHS
  225. RHS = allInstances(RHS_model, "RHS")
  226. RHS = set_pop(RHS)
  227. action = read_attribute(RHS_model, RHS, "action")
  228. if (element_neq(action, read_root())):
  229. action(host_model, new_mapping)
  230. return!
  231. Void function transform(host_model : Element, LHS_model : Element, RHS_model : Element):
  232. Element mapping
  233. Element mappings
  234. // Get all possible mappings
  235. mappings = match(host_model, LHS_model)
  236. // Select one such mapping and rewrite it
  237. if (read_nr_out(mappings) > 0):
  238. // Mapping found, so can rewrite it
  239. mapping = set_pop(mappings)
  240. rewrite(host_model, RHS_model, mapping)
  241. else:
  242. output("No mapping found!")
  243. return!