transform.alc 18 KB

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