transform.alc 19 KB

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