transform.alc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. include "primitives.alh"
  2. include "object_operations.alh"
  3. include "modelling.alh"
  4. include "random.alh"
  5. include "conformance_scd.alh"
  6. include "model_management.alh"
  7. Element function make_matching_schedule(schedule_model : Element, LHS : Element):
  8. Element schedule
  9. Element workset
  10. Element all_elements
  11. Element full_all_elements
  12. Integer required_size
  13. String new_element
  14. Integer counter
  15. String next
  16. Element tmp
  17. // Initialize
  18. schedule = create_node()
  19. workset = create_node()
  20. all_elements = allAssociationDestinations(schedule_model, LHS, "LHS_contains")
  21. full_all_elements = set_copy(all_elements)
  22. required_size = read_nr_out(all_elements)
  23. // Need to keep adding to the schedule
  24. while (read_nr_out(schedule) < required_size):
  25. // workset is empty, but we still need to add to the list
  26. // Therefore, we pick a random, unbound node, and add it to the workset
  27. new_element = set_pop(all_elements)
  28. while (bool_or(set_in(schedule, new_element), is_edge(schedule_model["model"][new_element]))):
  29. // Element is not usable, so pick another one
  30. new_element = set_pop(all_elements)
  31. set_add(workset, new_element)
  32. // Handle the workset
  33. while (read_nr_out(workset) > 0):
  34. // Still elements in the workset, so pop from these first
  35. next = set_pop(workset)
  36. // Check if element might not be already used somewhere
  37. if (bool_not(set_in(schedule, next))):
  38. if (set_in(full_all_elements, next)):
  39. list_append(schedule, next)
  40. // If it is an edge, we should also add the target and source
  41. if (is_edge(schedule_model["model"][next])):
  42. // Add the target/source to the schedule
  43. set_add(workset, reverseKeyLookup(schedule_model["model"], read_edge_src(schedule_model["model"][next])))
  44. set_add(workset, reverseKeyLookup(schedule_model["model"], read_edge_dst(schedule_model["model"][next])))
  45. // Also add all outgoing links
  46. counter = read_nr_out(schedule_model["model"][next])
  47. while (counter > 0):
  48. counter = counter - 1
  49. if (set_in_node(schedule_model["model"], read_out(schedule_model["model"][next], counter))):
  50. set_add(workset, reverseKeyLookup(schedule_model["model"], read_out(schedule_model["model"][next], counter)))
  51. return schedule!
  52. Element function get_possible_bindings(host_model : Element, schedule_model : Element, current_element : String, map : Element):
  53. Element options
  54. String src_label
  55. String dst_label
  56. String typename
  57. String original_typename
  58. options = create_node()
  59. typename = reverseKeyLookup(schedule_model["metamodel"]["model"], dict_read_node(schedule_model["type_mapping"], schedule_model["model"][current_element]))
  60. original_typename = string_substr(typename, 4, string_len(typename))
  61. if (is_edge(schedule_model["model"][current_element])):
  62. // Is an edge, so check for already bound source/target
  63. src_label = read_attribute(schedule_model, reverseKeyLookup(schedule_model["model"], read_edge_src(schedule_model["model"][current_element])), "label")
  64. dst_label = read_attribute(schedule_model, reverseKeyLookup(schedule_model["model"], read_edge_dst(schedule_model["model"][current_element])), "label")
  65. if (bool_and(set_in(dict_keys(map), src_label), set_in(dict_keys(map), dst_label))):
  66. // Source and destination are bound
  67. options = allOutgoingAssociationInstances(host_model, map[src_label], original_typename)
  68. options = set_overlap(options, allIncomingAssociationInstances(host_model, map[dst_label], original_typename))
  69. elif (set_in(dict_keys(map), src_label)):
  70. // Source is bound
  71. options = allOutgoingAssociationInstances(host_model, map[src_label], original_typename)
  72. elif (set_in(dict_keys(map), dst_label)):
  73. // Destination is bound
  74. options = allIncomingAssociationInstances(host_model, map[dst_label], original_typename)
  75. else:
  76. // Neither is bound, so just get all of them
  77. log("ERROR: unbound source/target for association!")
  78. return create_node()!
  79. else:
  80. // Is a node, so check for already bound incoming/outgoing
  81. options = allInstances(host_model, original_typename)
  82. // Filter options further
  83. Element filtered_options
  84. String option
  85. filtered_options = create_node()
  86. while (read_nr_out(options) > 0):
  87. option = set_pop(options)
  88. // Check for detecting same element twice
  89. if (bool_not(set_in(map, option))):
  90. // Option is already present with another label, so skip this!
  91. // Check for local constraints of element
  92. if (element_eq(read_attribute(schedule_model, current_element, "constraint"), read_root())):
  93. // No local constraints, so all is well
  94. set_add(filtered_options, option)
  95. else:
  96. // Check local constraints and add only if positive
  97. Element constraint_function
  98. constraint_function = read_attribute(schedule_model, current_element, "constraint")
  99. Boolean result
  100. result = constraint_function(host_model, option)
  101. if (result):
  102. set_add(filtered_options, option)
  103. return filtered_options!
  104. Element function match(host_model : Element, schedule_model : Element, LHS : Element):
  105. // Match the schedule_model to the host_model, returning all possible mappings from schedule_model elements to host_model elements
  106. // Make the schedule first
  107. Element schedule
  108. schedule = make_matching_schedule(schedule_model, LHS)
  109. // Now follow the schedule, incrementally building all mappings
  110. Element mappings
  111. Element new_mappings
  112. Element new_map
  113. String current_element
  114. Element map
  115. String option
  116. Element options
  117. mappings = create_node()
  118. set_add(mappings, create_node())
  119. while (bool_and(read_nr_out(schedule) > 0, read_nr_out(mappings) > 0)):
  120. current_element = list_pop(schedule, 0)
  121. new_mappings = create_node()
  122. while (read_nr_out(mappings) > 0):
  123. map = set_pop(mappings)
  124. options = get_possible_bindings(host_model, schedule_model, current_element, map)
  125. while (read_nr_out(options) > 0):
  126. option = set_pop(options)
  127. new_map = dict_copy(map)
  128. dict_add(new_map, read_attribute(schedule_model, current_element, "label"), option)
  129. set_add(new_mappings, new_map)
  130. mappings = new_mappings
  131. // Finished, so try the global constraint
  132. Element constraint
  133. new_mappings = create_node()
  134. constraint = read_attribute(schedule_model, LHS, "constraint")
  135. if (element_neq(constraint, read_root())):
  136. while (read_nr_out(mappings) > 0):
  137. map = set_pop(mappings)
  138. if (constraint(host_model, map)):
  139. set_add(new_mappings, map)
  140. mappings = new_mappings
  141. return mappings!
  142. Void function rewrite(host_model : Element, schedule_model : Element, RHS : String, mapping : Element):
  143. // Rewrite the host model based on the mapping combined with the RHS
  144. Element LHS_labels
  145. Element RHS_labels
  146. Element RHS_elements
  147. Element remaining
  148. String elem
  149. String label
  150. Element labels_to_remove
  151. Element labels_to_add
  152. String typename
  153. String original_typename
  154. String src
  155. String dst
  156. Element new_mapping
  157. String new_name
  158. Element RHS_map
  159. String tmp
  160. Element value
  161. Element value_function
  162. Element action
  163. Element original_RHS_labels
  164. LHS_labels = dict_keys(mapping)
  165. RHS_labels = create_node()
  166. RHS_map = create_node()
  167. RHS_elements = allAssociationDestinations(schedule_model, RHS, "RHS_contains")
  168. while (read_nr_out(RHS_elements) > 0):
  169. tmp = set_pop(RHS_elements)
  170. label = read_attribute(schedule_model, tmp, "label")
  171. set_add(RHS_labels, label)
  172. dict_add(RHS_map, label, tmp)
  173. remaining = set_overlap(LHS_labels, RHS_labels)
  174. original_RHS_labels = set_copy(RHS_labels)
  175. while (read_nr_out(remaining) > 0):
  176. elem = set_pop(remaining)
  177. set_remove(LHS_labels, elem)
  178. set_remove(RHS_labels, elem)
  179. labels_to_remove = LHS_labels
  180. labels_to_add = set_to_list(RHS_labels)
  181. new_mapping = dict_copy(mapping)
  182. while (read_nr_out(labels_to_add) > 0):
  183. // Add the elements linked to these labels
  184. label = list_pop(labels_to_add, 0)
  185. if (element_neq(read_attribute(schedule_model, RHS_map[label], "value"), read_root())):
  186. // There is a value associated with this node
  187. value_function = read_attribute(schedule_model, RHS_map[label], "value")
  188. value = value_function(host_model, mapping)
  189. typename = reverseKeyLookup(schedule_model["metamodel"]["model"], dict_read_node(schedule_model["type_mapping"], schedule_model["model"][RHS_map[label]]))
  190. original_typename = string_substr(typename, 5, string_len(typename))
  191. new_name = instantiate_value(host_model, original_typename, "", value)
  192. dict_add(new_mapping, label, new_name)
  193. elif (is_edge(schedule_model["model"][RHS_map[label]])):
  194. // Edge
  195. src = read_attribute(schedule_model, reverseKeyLookup(schedule_model["model"], read_edge_src(schedule_model["model"][RHS_map[label]])), "label")
  196. dst = read_attribute(schedule_model, reverseKeyLookup(schedule_model["model"], read_edge_dst(schedule_model["model"][RHS_map[label]])), "label")
  197. // First check whether both source and destination are already created
  198. if (bool_and(dict_in(new_mapping, src), dict_in(new_mapping, dst))):
  199. // Both are present, so we can make the link
  200. typename = reverseKeyLookup(schedule_model["metamodel"]["model"], dict_read_node(schedule_model["type_mapping"], schedule_model["model"][RHS_map[label]]))
  201. original_typename = string_substr(typename, 5, string_len(typename))
  202. new_name = instantiate_link(host_model, original_typename, "", new_mapping[src], new_mapping[dst])
  203. dict_add(new_mapping, label, new_name)
  204. else:
  205. // Delay this a bit, until all are bound
  206. list_append(labels_to_add, label)
  207. else:
  208. // Node
  209. // Create the node and add it
  210. typename = reverseKeyLookup(schedule_model["metamodel"]["model"], dict_read_node(schedule_model["type_mapping"], schedule_model["model"][RHS_map[label]]))
  211. original_typename = string_substr(typename, 5, string_len(typename))
  212. new_name = instantiate_node(host_model, original_typename, "")
  213. dict_add(new_mapping, label, new_name)
  214. while (read_nr_out(original_RHS_labels) > 0):
  215. label = set_pop(original_RHS_labels)
  216. action = read_attribute(schedule_model, RHS_map[label], "action")
  217. if (element_neq(action, read_root())):
  218. action(host_model, new_mapping[label], mapping)
  219. while (read_nr_out(labels_to_remove) > 0):
  220. // Remove the elements linked to these labels
  221. label = set_pop(labels_to_remove)
  222. model_delete_element(host_model, mapping[label])
  223. dict_delete(new_mapping, label)
  224. // Execute global action (whatever it may be)
  225. action = read_attribute(schedule_model, RHS, "action")
  226. if (element_neq(action, read_root())):
  227. action(host_model, new_mapping)
  228. return!
  229. Element function transform(host_model : Element, schedule_model : Element):
  230. // Find initial model
  231. Element all_composites
  232. String composite
  233. String current
  234. Element merged
  235. Element original_mm
  236. // Now start transforming for real
  237. all_composites = allInstances(schedule_model, "Composite")
  238. if (read_nr_out(all_composites) == 1):
  239. // Only one, so it is easy
  240. current = set_pop(all_composites)
  241. else:
  242. // Filter out those that are themselves contained
  243. while (read_nr_out(all_composites) > 0):
  244. composite = set_pop(all_composites)
  245. if (read_nr_out(allIncomingAssociationInstances(schedule_model, composite, "Contains")) == 0):
  246. // Isn't contained in any, so this is the root model!
  247. current = composite
  248. if (transform_composite(host_model, schedule_model, current)):
  249. // Success, so return True if it is in-place, or the new model if it is out-place
  250. log("Transform success!")
  251. return True!
  252. else:
  253. log("Transform failed!")
  254. return False!
  255. Boolean function transform_composite(host_model : Element, schedule_model : Element, composite : String):
  256. String current
  257. String typename
  258. Boolean result
  259. current = set_pop(allAssociationDestinations(schedule_model, composite, "Initial"))
  260. while (is_nominal_instance(schedule_model, current, "Rule")):
  261. // Still a rule that we must execute
  262. typename = reverseKeyLookup(schedule_model["metamodel"]["model"], dict_read_node(schedule_model["type_mapping"], schedule_model["model"][current]))
  263. if (typename == "Atomic"):
  264. result = transform_atomic(host_model, schedule_model, current)
  265. elif (typename == "Query"):
  266. result = transform_query(host_model, schedule_model, current)
  267. elif (typename == "Composite"):
  268. result = transform_composite(host_model, schedule_model, current)
  269. elif (typename == "ForAll"):
  270. result = transform_forall(host_model, schedule_model, current)
  271. if (result):
  272. current = set_pop(allAssociationDestinations(schedule_model, current, "OnSuccess"))
  273. else:
  274. current = set_pop(allAssociationDestinations(schedule_model, current, "OnFailure"))
  275. // No longer a rule, so it is either success or failure
  276. if (is_nominal_instance(schedule_model, current, "Success")):
  277. return True!
  278. else:
  279. return False!
  280. Boolean function transform_atomic(host_model : Element, schedule_model : Element, current : String):
  281. // Execute the atomic transformation
  282. Element mappings
  283. String LHS
  284. Element mapping
  285. LHS = set_pop(allAssociationDestinations(schedule_model, current, "AtomicLHS"))
  286. mappings = match(host_model, schedule_model, LHS)
  287. if (read_nr_out(mappings) > 0):
  288. // Pick one!
  289. mapping = random_choice(set_to_list(mappings))
  290. String RHS
  291. RHS = set_pop(allAssociationDestinations(schedule_model, current, "AtomicRHS"))
  292. rewrite(host_model, schedule_model, RHS, mapping)
  293. return True!
  294. else:
  295. return False!
  296. Boolean function transform_forall(host_model : Element, schedule_model : Element, current : String):
  297. // Execute the atomic transformation
  298. Element mappings
  299. String LHS
  300. String RHS
  301. Element mapping
  302. Boolean result
  303. LHS = set_pop(allAssociationDestinations(schedule_model, current, "AtomicLHS"))
  304. mappings = match(host_model, schedule_model, LHS)
  305. if (read_nr_out(mappings) > 0):
  306. result = True
  307. else:
  308. result = False
  309. while (read_nr_out(mappings) > 0):
  310. mapping = set_pop(mappings)
  311. // TODO check if there are actually no deletions happening in the meantime of other matched elements...
  312. RHS = set_pop(allAssociationDestinations(schedule_model, current, "AtomicRHS"))
  313. rewrite(host_model, schedule_model, RHS, mapping)
  314. return result!
  315. Boolean function transform_query(host_model : Element, schedule_model : Element, current : String):
  316. // Execute the transformation
  317. String LHS
  318. Element mappings
  319. Element mapping
  320. LHS = set_pop(allAssociationDestinations(schedule_model, current, "QueryLHS"))
  321. mappings = match(host_model, schedule_model, LHS)
  322. if (read_nr_out(mappings) > 0):
  323. return True!
  324. else:
  325. return False!