transform.alc 18 KB

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