transform.alc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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. //TODO should actually return all
  79. return create_node()!
  80. else:
  81. // Is a node, so find whether or not there are some connections that are already resolved
  82. Element ic
  83. Element oc
  84. String poll
  85. ic = allIncomingAssociationInstances(schedule_model, current_element, "Pre_Element")
  86. oc = allOutgoingAssociationInstances(schedule_model, current_element, "Pre_Element")
  87. while (bool_and(read_nr_out(ic) > 0, read_nr_out(options) == 0)):
  88. poll = set_pop(ic)
  89. if (dict_in(map, read_attribute(schedule_model, poll, "label"))):
  90. // This incoming link is already defined, so we just have one option: the destination of the link we matched
  91. set_add(options, readAssociationDestination(host_model, map[read_attribute(schedule_model, poll, "label")]))
  92. while (bool_and(read_nr_out(oc) > 0, read_nr_out(options) == 0)):
  93. poll = set_pop(oc)
  94. if (dict_in(map, read_attribute(schedule_model, poll, "label"))):
  95. // This incoming link is already defined, so we just have one option: the destination of the link we matched
  96. set_add(options, readAssociationSource(host_model, map[read_attribute(schedule_model, poll, "label")]))
  97. if (read_nr_out(options) == 0):
  98. // Is a node and no connections, so we just pick all options
  99. options = allInstances(host_model, original_typename)
  100. elif (read_nr_out(options) > 1):
  101. // Multiple "only" options, which will not work out: no options!
  102. return create_node()!
  103. // Filter options further
  104. Element filtered_options
  105. String option
  106. filtered_options = create_node()
  107. while (read_nr_out(options) > 0):
  108. option = set_pop(options)
  109. // Check for detecting same element twice
  110. if (bool_not(set_in(map, option))):
  111. // Option is already present with another label, so skip this!
  112. // Check for local constraints of element
  113. if (element_eq(read_attribute(schedule_model, current_element, "constraint"), read_root())):
  114. // No local constraints, so all is well
  115. set_add(filtered_options, option)
  116. else:
  117. // Check local constraints and add only if positive
  118. Element constraint_function
  119. constraint_function = read_attribute(schedule_model, current_element, "constraint")
  120. Boolean result
  121. result = constraint_function(host_model, option)
  122. if (result):
  123. set_add(filtered_options, option)
  124. return filtered_options!
  125. Element function match(host_model : Element, schedule_model : Element, LHS : Element):
  126. // Match the schedule_model to the host_model, returning all possible mappings from schedule_model elements to host_model elements
  127. // Make the schedule first
  128. Element schedule
  129. schedule = make_matching_schedule(schedule_model, LHS)
  130. // Now follow the schedule, incrementally building all mappings
  131. Element mappings
  132. Element new_mappings
  133. Element new_map
  134. String current_element
  135. Element map
  136. String option
  137. Element options
  138. mappings = create_node()
  139. set_add(mappings, create_node())
  140. while (bool_and(read_nr_out(schedule) > 0, read_nr_out(mappings) > 0)):
  141. current_element = list_pop(schedule, 0)
  142. new_mappings = create_node()
  143. while (read_nr_out(mappings) > 0):
  144. map = set_pop(mappings)
  145. options = get_possible_bindings(host_model, schedule_model, current_element, map)
  146. while (read_nr_out(options) > 0):
  147. option = set_pop(options)
  148. new_map = dict_copy(map)
  149. dict_add(new_map, read_attribute(schedule_model, current_element, "label"), option)
  150. set_add(new_mappings, new_map)
  151. mappings = new_mappings
  152. // Finished, so try the global constraint
  153. Element constraint
  154. new_mappings = create_node()
  155. constraint = read_attribute(schedule_model, LHS, "constraint")
  156. if (element_neq(constraint, read_root())):
  157. while (read_nr_out(mappings) > 0):
  158. map = set_pop(mappings)
  159. if (constraint(host_model, map)):
  160. set_add(new_mappings, map)
  161. mappings = new_mappings
  162. return mappings!
  163. Void function rewrite(host_model : Element, schedule_model : Element, RHS : String, mapping : Element):
  164. // Rewrite the host model based on the mapping combined with the RHS
  165. Element LHS_labels
  166. Element RHS_labels
  167. Element RHS_elements
  168. Element remaining
  169. String elem
  170. String label
  171. Element labels_to_remove
  172. Element labels_to_add
  173. String typename
  174. String original_typename
  175. String src
  176. String dst
  177. Element new_mapping
  178. String new_name
  179. Element RHS_map
  180. String tmp
  181. Element value
  182. Element value_function
  183. Element action
  184. Element original_RHS_labels
  185. LHS_labels = dict_keys(mapping)
  186. RHS_labels = create_node()
  187. RHS_map = create_node()
  188. RHS_elements = allAssociationDestinations(schedule_model, RHS, "RHS_contains")
  189. while (read_nr_out(RHS_elements) > 0):
  190. tmp = set_pop(RHS_elements)
  191. label = read_attribute(schedule_model, tmp, "label")
  192. set_add(RHS_labels, label)
  193. dict_add(RHS_map, label, tmp)
  194. remaining = set_overlap(LHS_labels, RHS_labels)
  195. original_RHS_labels = set_copy(RHS_labels)
  196. while (read_nr_out(remaining) > 0):
  197. elem = set_pop(remaining)
  198. set_remove(LHS_labels, elem)
  199. set_remove(RHS_labels, elem)
  200. labels_to_remove = LHS_labels
  201. labels_to_add = set_to_list(RHS_labels)
  202. new_mapping = dict_copy(mapping)
  203. while (read_nr_out(labels_to_add) > 0):
  204. // Add the elements linked to these labels
  205. label = list_pop(labels_to_add, 0)
  206. if (element_neq(read_attribute(schedule_model, RHS_map[label], "value"), read_root())):
  207. // There is a value associated with this node
  208. value_function = read_attribute(schedule_model, RHS_map[label], "value")
  209. value = value_function(host_model, mapping)
  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_value(host_model, original_typename, "", value)
  213. dict_add(new_mapping, label, new_name)
  214. elif (is_edge(schedule_model["model"][RHS_map[label]])):
  215. // Edge
  216. src = read_attribute(schedule_model, reverseKeyLookup(schedule_model["model"], read_edge_src(schedule_model["model"][RHS_map[label]])), "label")
  217. dst = read_attribute(schedule_model, reverseKeyLookup(schedule_model["model"], read_edge_dst(schedule_model["model"][RHS_map[label]])), "label")
  218. // First check whether both source and destination are already created
  219. if (bool_and(dict_in(new_mapping, src), dict_in(new_mapping, dst))):
  220. // Both are present, so we can make the link
  221. typename = reverseKeyLookup(schedule_model["metamodel"]["model"], dict_read_node(schedule_model["type_mapping"], schedule_model["model"][RHS_map[label]]))
  222. original_typename = string_substr(typename, 5, string_len(typename))
  223. new_name = instantiate_link(host_model, original_typename, "", new_mapping[src], new_mapping[dst])
  224. dict_add(new_mapping, label, new_name)
  225. else:
  226. // Delay this a bit, until all are bound
  227. list_append(labels_to_add, label)
  228. else:
  229. // Node
  230. // Create the node and add it
  231. typename = reverseKeyLookup(schedule_model["metamodel"]["model"], dict_read_node(schedule_model["type_mapping"], schedule_model["model"][RHS_map[label]]))
  232. original_typename = string_substr(typename, 5, string_len(typename))
  233. new_name = instantiate_node(host_model, original_typename, "")
  234. dict_add(new_mapping, label, new_name)
  235. while (read_nr_out(original_RHS_labels) > 0):
  236. label = set_pop(original_RHS_labels)
  237. action = read_attribute(schedule_model, RHS_map[label], "action")
  238. if (element_neq(action, read_root())):
  239. action(host_model, new_mapping[label], mapping)
  240. while (read_nr_out(labels_to_remove) > 0):
  241. // Remove the elements linked to these labels
  242. label = set_pop(labels_to_remove)
  243. model_delete_element(host_model, mapping[label])
  244. dict_delete(new_mapping, label)
  245. // Execute global action (whatever it may be)
  246. action = read_attribute(schedule_model, RHS, "action")
  247. if (element_neq(action, read_root())):
  248. action(host_model, new_mapping)
  249. return!
  250. Element function transform(host_model : Element, schedule_model : Element):
  251. // Find initial model
  252. Element all_composites
  253. String composite
  254. String current
  255. Element merged
  256. Element original_mm
  257. // Now start transforming for real
  258. all_composites = allInstances(schedule_model, "Composite")
  259. if (read_nr_out(all_composites) == 1):
  260. // Only one, so it is easy
  261. current = set_pop(all_composites)
  262. else:
  263. // Filter out those that are themselves contained
  264. while (read_nr_out(all_composites) > 0):
  265. composite = set_pop(all_composites)
  266. if (read_nr_out(allIncomingAssociationInstances(schedule_model, composite, "Contains")) == 0):
  267. // Isn't contained in any, so this is the root model!
  268. current = composite
  269. if (transform_composite(host_model, schedule_model, current)):
  270. // Success, so return True if it is in-place, or the new model if it is out-place
  271. log("Transform success!")
  272. return True!
  273. else:
  274. log("Transform failed!")
  275. return False!
  276. Boolean function transform_composite(host_model : Element, schedule_model : Element, composite : String):
  277. String current
  278. String typename
  279. Boolean result
  280. current = set_pop(allAssociationDestinations(schedule_model, composite, "Initial"))
  281. while (is_nominal_instance(schedule_model, current, "Rule")):
  282. // Still a rule that we must execute
  283. typename = reverseKeyLookup(schedule_model["metamodel"]["model"], dict_read_node(schedule_model["type_mapping"], schedule_model["model"][current]))
  284. if (typename == "Atomic"):
  285. result = transform_atomic(host_model, schedule_model, current)
  286. elif (typename == "Query"):
  287. result = transform_query(host_model, schedule_model, current)
  288. elif (typename == "Composite"):
  289. result = transform_composite(host_model, schedule_model, current)
  290. elif (typename == "ForAll"):
  291. result = transform_forall(host_model, schedule_model, current)
  292. if (result):
  293. current = set_pop(allAssociationDestinations(schedule_model, current, "OnSuccess"))
  294. else:
  295. current = set_pop(allAssociationDestinations(schedule_model, current, "OnFailure"))
  296. // No longer a rule, so it is either success or failure
  297. if (is_nominal_instance(schedule_model, current, "Success")):
  298. return True!
  299. else:
  300. return False!
  301. Boolean function transform_atomic(host_model : Element, schedule_model : Element, current : String):
  302. // Execute the atomic transformation
  303. Element mappings
  304. String LHS
  305. Element mapping
  306. LHS = set_pop(allAssociationDestinations(schedule_model, current, "AtomicLHS"))
  307. mappings = match(host_model, schedule_model, LHS)
  308. if (read_nr_out(mappings) > 0):
  309. // Pick one!
  310. mapping = random_choice(set_to_list(mappings))
  311. String RHS
  312. RHS = set_pop(allAssociationDestinations(schedule_model, current, "AtomicRHS"))
  313. rewrite(host_model, schedule_model, RHS, mapping)
  314. return True!
  315. else:
  316. return False!
  317. Boolean function transform_forall(host_model : Element, schedule_model : Element, current : String):
  318. // Execute the atomic transformation
  319. Element mappings
  320. String LHS
  321. String RHS
  322. Element mapping
  323. Boolean result
  324. LHS = set_pop(allAssociationDestinations(schedule_model, current, "AtomicLHS"))
  325. mappings = match(host_model, schedule_model, LHS)
  326. if (read_nr_out(mappings) > 0):
  327. result = True
  328. else:
  329. result = False
  330. while (read_nr_out(mappings) > 0):
  331. mapping = set_pop(mappings)
  332. // TODO check if there are actually no deletions happening in the meantime of other matched elements...
  333. RHS = set_pop(allAssociationDestinations(schedule_model, current, "AtomicRHS"))
  334. rewrite(host_model, schedule_model, RHS, mapping)
  335. return result!
  336. Boolean function transform_query(host_model : Element, schedule_model : Element, current : String):
  337. // Execute the transformation
  338. String LHS
  339. Element mappings
  340. Element mapping
  341. LHS = set_pop(allAssociationDestinations(schedule_model, current, "QueryLHS"))
  342. mappings = match(host_model, schedule_model, LHS)
  343. if (read_nr_out(mappings) > 0):
  344. return True!
  345. else:
  346. return False!