transform.alc 16 KB

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