transform.alc 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  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. include "library.alh"
  8. Element function make_matching_schedule(schedule_model : Element, LHS : String, ignore : Element):
  9. Element schedule
  10. Element workset
  11. Element all_elements
  12. Element full_all_elements
  13. Integer required_size
  14. String new_element
  15. Integer counter
  16. String next
  17. Element tmp
  18. // Initialize
  19. schedule = create_node()
  20. workset = create_node()
  21. all_elements = allAssociationDestinations(schedule_model, LHS, "LHS_contains")
  22. full_all_elements = set_copy(all_elements)
  23. required_size = read_nr_out(all_elements)
  24. // Need to keep adding to the schedule
  25. while (read_nr_out(schedule) < required_size):
  26. // workset is empty, but we still need to add to the list
  27. // Therefore, we pick a random, unbound node, and add it to the workset
  28. new_element = set_pop(all_elements)
  29. while (bool_or(set_in(schedule, new_element), is_edge(schedule_model["model"][new_element]))):
  30. // Element is not usable, so pick another one
  31. new_element = set_pop(all_elements)
  32. set_add(workset, new_element)
  33. // Handle the workset
  34. while (read_nr_out(workset) > 0):
  35. // Still elements in the workset, so pop from these first
  36. next = set_pop(workset)
  37. // Check if element might not be already used somewhere
  38. if (bool_not(set_in(schedule, next))):
  39. if (set_in(full_all_elements, next)):
  40. if (bool_not(set_in(ignore, read_attribute(schedule_model, next, "label")))):
  41. list_append(schedule, next)
  42. else:
  43. required_size = required_size - 1
  44. // If it is an edge, we should also add the target and source
  45. if (is_edge(schedule_model["model"][next])):
  46. // Add the target/source to the schedule
  47. set_add(workset, reverseKeyLookup(schedule_model["model"], read_edge_src(schedule_model["model"][next])))
  48. set_add(workset, reverseKeyLookup(schedule_model["model"], read_edge_dst(schedule_model["model"][next])))
  49. // Also add all outgoing links
  50. counter = read_nr_out(schedule_model["model"][next])
  51. while (counter > 0):
  52. counter = counter - 1
  53. if (set_in_node(schedule_model["model"], read_out(schedule_model["model"][next], counter))):
  54. set_add(workset, reverseKeyLookup(schedule_model["model"], read_out(schedule_model["model"][next], counter)))
  55. // And incoming links
  56. counter = read_nr_in(schedule_model["model"][next])
  57. while (counter > 0):
  58. counter = counter - 1
  59. if (set_in_node(schedule_model["model"], read_in(schedule_model["model"][next], counter))):
  60. set_add(workset, reverseKeyLookup(schedule_model["model"], read_in(schedule_model["model"][next], counter)))
  61. return schedule!
  62. Element function get_possible_bindings(host_model : Element, schedule_model : Element, current_element : String, map : Element):
  63. Element options
  64. String src_label
  65. String dst_label
  66. String typename
  67. String original_typename
  68. options = create_node()
  69. typename = read_type(schedule_model, current_element)
  70. original_typename = string_substr(typename, 4, string_len(typename))
  71. if (is_edge(schedule_model["model"][current_element])):
  72. // Is an edge, so check for already bound source/target
  73. src_label = read_attribute(schedule_model, reverseKeyLookup(schedule_model["model"], read_edge_src(schedule_model["model"][current_element])), "label")
  74. dst_label = read_attribute(schedule_model, reverseKeyLookup(schedule_model["model"], read_edge_dst(schedule_model["model"][current_element])), "label")
  75. if (bool_and(set_in(dict_keys(map), src_label), set_in(dict_keys(map), dst_label))):
  76. // Source and destination are bound
  77. options = allOutgoingAssociationInstances(host_model, map[src_label], original_typename)
  78. options = set_overlap(options, allIncomingAssociationInstances(host_model, map[dst_label], original_typename))
  79. elif (set_in(dict_keys(map), src_label)):
  80. // Source is bound
  81. options = allOutgoingAssociationInstances(host_model, map[src_label], original_typename)
  82. elif (set_in(dict_keys(map), dst_label)):
  83. // Destination is bound
  84. options = allIncomingAssociationInstances(host_model, map[dst_label], original_typename)
  85. else:
  86. // Neither is bound, so just get all of them
  87. return allInstances(host_model, original_typename)!
  88. else:
  89. // Is a node, so find whether or not there are some connections that are already resolved
  90. Element ic
  91. Element oc
  92. String poll
  93. // TODO optimize: do outgoing first, and only get incoming when not yet failed for incoming (incoming much slower!)
  94. ic = allIncomingAssociationInstances(schedule_model, current_element, "PreElement")
  95. oc = allOutgoingAssociationInstances(schedule_model, current_element, "PreElement")
  96. String value
  97. while (bool_and(read_nr_out(ic) > 0, read_nr_out(options) < 2)):
  98. poll = set_pop(ic)
  99. if (dict_in(map, read_attribute(schedule_model, poll, "label"))):
  100. // This incoming link is already defined, so we just have one option: the destination of the link we matched
  101. value = readAssociationDestination(host_model, map[read_attribute(schedule_model, poll, "label")])
  102. if (bool_not(set_in(options, value))):
  103. set_add(options, value)
  104. while (bool_and(read_nr_out(oc) > 0, read_nr_out(options) < 2)):
  105. poll = set_pop(oc)
  106. if (dict_in(map, read_attribute(schedule_model, poll, "label"))):
  107. // This incoming link is already defined, so we just have one option: the destination of the link we matched
  108. value = readAssociationSource(host_model, map[read_attribute(schedule_model, poll, "label")])
  109. if (bool_not(set_in(options, value))):
  110. set_add(options, value)
  111. if (read_nr_out(options) == 0):
  112. // Is a node and no connections, so we just pick all options
  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. return create_node()!
  117. // Filter options further
  118. Element filtered_options
  119. String option
  120. filtered_options = create_node()
  121. while (read_nr_out(options) > 0):
  122. option = set_pop(options)
  123. // Check for detecting same element twice
  124. if (bool_not(set_in(map, option))):
  125. // Option is already present with another label, so skip this!
  126. // Check if it conforms to the desired type
  127. if (is_nominal_instance(host_model, option, original_typename)):
  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(import_node(constraint_function))
  139. result = func(host_model, option)
  140. if (result):
  141. set_add(filtered_options, option)
  142. Element attributes
  143. String attribute
  144. Element value
  145. Element func
  146. Boolean result
  147. Element attributes_copy
  148. options = filtered_options
  149. filtered_options = create_node()
  150. // Check whether all attributes have a satisfied condition
  151. attributes_copy = dict_keys(getAttributeList(schedule_model, current_element))
  152. while (read_nr_out(options) > 0):
  153. option = set_pop(options)
  154. attributes = set_copy(attributes_copy)
  155. result = True
  156. while (read_nr_out(attributes) > 0):
  157. attribute = set_pop(attributes)
  158. if (bool_not(string_startswith(attribute, "constraint_"))):
  159. continue!
  160. value = read_attribute(schedule_model, current_element, attribute)
  161. // Attribute might be undefined, so skip if it is
  162. if (element_neq(value, read_root())):
  163. func = get_func_AL_model(import_node(value))
  164. result = func(read_attribute(host_model, option, string_substr(attribute, string_len("constraint_"), string_len(attribute) + 1)))
  165. else:
  166. result = True
  167. if (bool_not(result)):
  168. break!
  169. // Check value of last result, which will be True if all passed, or False otherwise
  170. if (result):
  171. set_add(filtered_options, option)
  172. options = filtered_options
  173. return options!
  174. Element function full_match(host_model : Element, schedule_model : Element, current : String):
  175. Element NACs
  176. String LHS
  177. String NAC
  178. Element mappings
  179. Element mapping
  180. Integer i
  181. Element result
  182. Element final_mappings
  183. Boolean allowed
  184. final_mappings = create_node()
  185. // First match the LHS part itself to get initial mappings
  186. LHS = set_pop(allAssociationDestinations(schedule_model, current, "LHSLink"))
  187. mappings = match(host_model, schedule_model, LHS, create_node())
  188. // Got a list of all possible mappings, now filter based on NACs
  189. NACs = allAssociationDestinations(schedule_model, current, "NACLink")
  190. // For each possible mapping, we check all NACs!
  191. while (read_nr_out(mappings) > 0):
  192. mapping = set_pop(mappings)
  193. i = 0
  194. allowed = True
  195. while (i < read_nr_out(NACs)):
  196. NAC = read_edge_dst(read_out(NACs, i))
  197. result = match(host_model, schedule_model, NAC, mapping)
  198. if (read_nr_out(result) > 0):
  199. // NAC could be matched, and therefore is not allowed
  200. allowed = False
  201. break!
  202. i = i + 1
  203. if (allowed):
  204. set_add(final_mappings, mapping)
  205. return final_mappings!
  206. Element function match(host_model : Element, schedule_model : Element, LHS : String, initial_mapping : Element):
  207. // Match the schedule_model to the host_model, returning all possible mappings from schedule_model elements to host_model elements
  208. // Make the schedule first
  209. Element schedule
  210. schedule = make_matching_schedule(schedule_model, LHS, dict_keys(initial_mapping))
  211. // Now follow the schedule, incrementally building all mappings
  212. Element mappings
  213. Element new_mappings
  214. Element new_map
  215. String current_element
  216. Element map
  217. String option
  218. Element options
  219. mappings = create_node()
  220. set_add(mappings, initial_mapping)
  221. while (read_nr_out(schedule) > 0):
  222. current_element = list_pop(schedule, 0)
  223. log("Binding element with label " + cast_v2s(read_attribute(schedule_model, current_element, "label")))
  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. log("Remaining options: " + cast_v2s(read_nr_out(mappings)))
  235. if (read_nr_out(mappings) == 0):
  236. // Stop because we have no more options remaining!
  237. return create_node()!
  238. // Finished, so try the global constraint
  239. String constraint
  240. Element func
  241. Boolean result
  242. new_mappings = create_node()
  243. constraint = read_attribute(schedule_model, LHS, "constraint")
  244. if (element_neq(constraint, read_root())):
  245. while (read_nr_out(mappings) > 0):
  246. map = set_pop(mappings)
  247. func = get_func_AL_model(import_node(constraint))
  248. result = func(host_model, map)
  249. if (result):
  250. set_add(new_mappings, map)
  251. mappings = new_mappings
  252. return mappings!
  253. Void function rewrite(host_model : Element, schedule_model : Element, RHS : String, mapping : Element):
  254. // Rewrite the host model based on the mapping combined with the RHS
  255. Element LHS_labels
  256. Element RHS_labels
  257. Element RHS_elements
  258. Element remaining
  259. String elem
  260. String label
  261. Element labels_to_remove
  262. Element labels_to_add
  263. String typename
  264. String original_typename
  265. String src
  266. String dst
  267. Element new_mapping
  268. String new_name
  269. Element RHS_map
  270. String tmp
  271. Element value
  272. Element action
  273. Element original_RHS_labels
  274. LHS_labels = dict_keys(mapping)
  275. RHS_labels = create_node()
  276. RHS_map = create_node()
  277. RHS_elements = allAssociationDestinations(schedule_model, RHS, "RHS_contains")
  278. while (read_nr_out(RHS_elements) > 0):
  279. tmp = set_pop(RHS_elements)
  280. label = read_attribute(schedule_model, tmp, "label")
  281. set_add(RHS_labels, label)
  282. dict_add(RHS_map, label, tmp)
  283. remaining = set_overlap(LHS_labels, RHS_labels)
  284. original_RHS_labels = set_copy(RHS_labels)
  285. while (read_nr_out(remaining) > 0):
  286. elem = set_pop(remaining)
  287. set_remove(LHS_labels, elem)
  288. set_remove(RHS_labels, elem)
  289. labels_to_remove = LHS_labels
  290. labels_to_add = set_to_list(RHS_labels)
  291. new_mapping = dict_copy(mapping)
  292. while (read_nr_out(labels_to_add) > 0):
  293. // Add the elements linked to these labels
  294. label = list_pop(labels_to_add, 0)
  295. if (is_edge(schedule_model["model"][RHS_map[label]])):
  296. // Edge
  297. src = read_attribute(schedule_model, reverseKeyLookup(schedule_model["model"], read_edge_src(schedule_model["model"][RHS_map[label]])), "label")
  298. dst = read_attribute(schedule_model, reverseKeyLookup(schedule_model["model"], read_edge_dst(schedule_model["model"][RHS_map[label]])), "label")
  299. // First check whether both source and destination are already created
  300. if (bool_and(dict_in(new_mapping, src), dict_in(new_mapping, dst))):
  301. // Both are present, so we can make the link
  302. typename = read_type(schedule_model, RHS_map[label])
  303. original_typename = string_substr(typename, 5, string_len(typename))
  304. new_name = instantiate_link(host_model, original_typename, "", new_mapping[src], new_mapping[dst])
  305. dict_add(new_mapping, label, new_name)
  306. else:
  307. // Delay this a bit, until all are bound
  308. list_append(labels_to_add, label)
  309. else:
  310. // Node
  311. // Create the node and add it
  312. typename = read_type(schedule_model, RHS_map[label])
  313. original_typename = string_substr(typename, 5, string_len(typename))
  314. new_name = instantiate_node(host_model, original_typename, "")
  315. dict_add(new_mapping, label, new_name)
  316. Element attributes
  317. String attribute
  318. Element result
  319. Element func
  320. while (read_nr_out(original_RHS_labels) > 0):
  321. // Perform actions
  322. label = set_pop(original_RHS_labels)
  323. // Do all attribute actions that are defined
  324. attributes = dict_keys(getAttributeList(schedule_model, RHS_map[label]))
  325. while (read_nr_out(attributes) > 0):
  326. attribute = set_pop(attributes)
  327. if (bool_not(string_startswith(attribute, "value_"))):
  328. continue!
  329. value = read_attribute(schedule_model, RHS_map[label], attribute)
  330. if (element_neq(value, read_root())):
  331. func = get_func_AL_model(import_node(value))
  332. result = func(host_model, new_mapping[label], mapping)
  333. if (has_value(result)):
  334. // New value defined, so assign!
  335. instantiate_attribute(host_model, new_mapping[label], string_substr(attribute, string_len("value_"), string_len(attribute) + 1), result)
  336. else:
  337. // Non-value return means to destroy the attribute!
  338. unset_attribute(host_model, new_mapping[label], string_substr(attribute, string_len("value_"), string_len(attribute) + 1))
  339. // Do the global action of each element
  340. action = read_attribute(schedule_model, RHS_map[label], "action")
  341. if (element_neq(action, read_root())):
  342. Element func
  343. func = get_func_AL_model(import_node(action))
  344. func(host_model, new_mapping[label], mapping)
  345. while (read_nr_out(labels_to_remove) > 0):
  346. // Remove the elements linked to these labels
  347. label = set_pop(labels_to_remove)
  348. model_delete_element(host_model, mapping[label])
  349. dict_delete(new_mapping, label)
  350. // Execute global action (whatever it may be)
  351. action = read_attribute(schedule_model, RHS, "action")
  352. if (element_neq(action, read_root())):
  353. Element func
  354. func = get_func_AL_model(import_node(action))
  355. func(host_model, new_mapping)
  356. return!
  357. Element function transform(host_model : Element, schedule_model : Element):
  358. // Find initial model
  359. Element all_composites
  360. String composite
  361. String current
  362. Element merged
  363. Element original_mm
  364. // Now start transforming for real
  365. all_composites = allInstances(schedule_model, "Composite")
  366. if (read_nr_out(all_composites) == 1):
  367. // Only one, so it is easy
  368. current = set_pop(all_composites)
  369. else:
  370. // Filter out those that are themselves contained
  371. while (read_nr_out(all_composites) > 0):
  372. composite = set_pop(all_composites)
  373. if (read_nr_out(allIncomingAssociationInstances(schedule_model, composite, "Contains")) == 0):
  374. // Isn't contained in any, so this is the root model!
  375. current = composite
  376. if (transform_composite(host_model, schedule_model, current)):
  377. // Success, so return True if it is in-place, or the new model if it is out-place
  378. return True!
  379. else:
  380. return False!
  381. Boolean function transform_composite(host_model : Element, schedule_model : Element, composite : String):
  382. String current
  383. String typename
  384. Boolean result
  385. current = set_pop(allAssociationDestinations(schedule_model, composite, "Initial"))
  386. while (is_nominal_instance(schedule_model, current, "Rule")):
  387. log("Executing " + current)
  388. // Still a rule that we must execute
  389. typename = read_type(schedule_model, current)
  390. if (typename == "Atomic"):
  391. result = transform_atomic(host_model, schedule_model, current)
  392. elif (typename == "Query"):
  393. result = transform_query(host_model, schedule_model, current)
  394. elif (typename == "Composite"):
  395. result = transform_composite(host_model, schedule_model, current)
  396. elif (typename == "ForAll"):
  397. result = transform_forall(host_model, schedule_model, current)
  398. if (result):
  399. current = set_pop(allAssociationDestinations(schedule_model, current, "OnSuccess"))
  400. else:
  401. current = set_pop(allAssociationDestinations(schedule_model, current, "OnFailure"))
  402. // No longer a rule, so it is either success or failure
  403. if (is_nominal_instance(schedule_model, current, "Success")):
  404. return True!
  405. else:
  406. return False!
  407. Boolean function transform_atomic(host_model : Element, schedule_model : Element, current : String):
  408. // Execute the atomic transformation
  409. Element mappings
  410. Element mapping
  411. mappings = full_match(host_model, schedule_model, current)
  412. if (read_nr_out(mappings) > 0):
  413. // Pick one!
  414. mapping = random_choice(set_to_list(mappings))
  415. String RHS
  416. RHS = set_pop(allAssociationDestinations(schedule_model, current, "RHSLink"))
  417. rewrite(host_model, schedule_model, RHS, mapping)
  418. return True!
  419. else:
  420. return False!
  421. Boolean function transform_forall(host_model : Element, schedule_model : Element, current : String):
  422. // Execute the atomic transformation
  423. Element mappings
  424. String RHS
  425. Element mapping
  426. Boolean result
  427. mappings = full_match(host_model, schedule_model, current)
  428. if (read_nr_out(mappings) > 0):
  429. result = True
  430. else:
  431. result = False
  432. log("Matches in forall: " + cast_v2s(read_nr_out(mappings)))
  433. while (read_nr_out(mappings) > 0):
  434. mapping = set_pop(mappings)
  435. // TODO check if there are actually no deletions happening in the meantime of other matched elements...
  436. RHS = set_pop(allAssociationDestinations(schedule_model, current, "RHSLink"))
  437. rewrite(host_model, schedule_model, RHS, mapping)
  438. return result!
  439. Boolean function transform_query(host_model : Element, schedule_model : Element, current : String):
  440. // Execute the transformation
  441. Element mappings
  442. Element mapping
  443. mappings = full_match(host_model, schedule_model, current)
  444. if (read_nr_out(mappings) > 0):
  445. return True!
  446. else:
  447. return False!