transform.alc 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  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. result = True
  161. while (read_nr_out(attributes) > 0):
  162. attribute = set_pop(attributes)
  163. if (bool_not(string_startswith(attribute, "constraint_"))):
  164. continue!
  165. log("Check attribute: " + attribute)
  166. value = read_attribute(schedule_model, current_element, attribute)
  167. // Attribute might be undefined, so skip if it is
  168. if (element_neq(value, read_root())):
  169. log("Read value: " + cast_e2s(value))
  170. func = get_func_AL_model(value)
  171. log("Func: " + cast_e2s(func))
  172. log("EXEC")
  173. result = func(host_model, option, attribute)
  174. log("Got result of constraint eval: " + cast_v2s(result))
  175. else:
  176. log("Attribute unconstrained, so add always")
  177. result = True
  178. if (bool_not(result)):
  179. break!
  180. // Check value of last result, which will be True if all passed, or False otherwise
  181. if (result):
  182. log("Adding")
  183. set_add(filtered_options, option)
  184. else:
  185. log("Dropping")
  186. options = filtered_options
  187. return options!
  188. Element function full_match(host_model : Element, schedule_model : Element, current : String):
  189. Element NACs
  190. String LHS
  191. String NAC
  192. Element mappings
  193. Element mapping
  194. Integer i
  195. Element result
  196. Element final_mappings
  197. Boolean allowed
  198. final_mappings = create_node()
  199. // First match the LHS part itself to get initial mappings
  200. LHS = set_pop(allAssociationDestinations(schedule_model, current, "LHSLink"))
  201. mappings = match(host_model, schedule_model, LHS, create_node())
  202. // Got a list of all possible mappings, now filter based on NACs
  203. NACs = allAssociationDestinations(schedule_model, current, "NACLink")
  204. // For each possible mapping, we check all NACs!
  205. while (read_nr_out(mappings) > 0):
  206. mapping = set_pop(mappings)
  207. i = 0
  208. allowed = True
  209. while (i < read_nr_out(NACs)):
  210. NAC = read_edge_dst(read_out(NACs, i))
  211. result = match(host_model, schedule_model, NAC, mapping)
  212. if (read_nr_out(result) > 0):
  213. // NAC could be matched, and therefore is not allowed
  214. allowed = False
  215. break!
  216. i = i + 1
  217. if (allowed):
  218. set_add(final_mappings, mapping)
  219. return final_mappings!
  220. Element function match(host_model : Element, schedule_model : Element, LHS : String, initial_mapping : Element):
  221. // Match the schedule_model to the host_model, returning all possible mappings from schedule_model elements to host_model elements
  222. // Make the schedule first
  223. Element schedule
  224. schedule = make_matching_schedule(schedule_model, LHS, dict_keys(initial_mapping))
  225. // Now follow the schedule, incrementally building all mappings
  226. Element mappings
  227. Element new_mappings
  228. Element new_map
  229. String current_element
  230. Element map
  231. String option
  232. Element options
  233. mappings = create_node()
  234. set_add(mappings, initial_mapping)
  235. while (bool_and(read_nr_out(schedule) > 0, read_nr_out(mappings) > 0)):
  236. current_element = list_pop(schedule, 0)
  237. new_mappings = create_node()
  238. while (read_nr_out(mappings) > 0):
  239. map = set_pop(mappings)
  240. options = get_possible_bindings(host_model, schedule_model, current_element, map)
  241. while (read_nr_out(options) > 0):
  242. option = set_pop(options)
  243. new_map = dict_copy(map)
  244. dict_add(new_map, read_attribute(schedule_model, current_element, "label"), option)
  245. set_add(new_mappings, new_map)
  246. mappings = new_mappings
  247. // Finished, so try the global constraint
  248. String constraint
  249. Element func
  250. Boolean result
  251. new_mappings = create_node()
  252. constraint = read_attribute(schedule_model, LHS, "constraint")
  253. if (element_neq(constraint, read_root())):
  254. while (read_nr_out(mappings) > 0):
  255. map = set_pop(mappings)
  256. func = get_func_AL_model(constraint)
  257. result = func(host_model, map)
  258. log("Execute global constraint on " + dict_to_string(map))
  259. log("Result: " + cast_e2s(result))
  260. if (result):
  261. set_add(new_mappings, map)
  262. mappings = new_mappings
  263. return mappings!
  264. Void function rewrite(host_model : Element, schedule_model : Element, RHS : String, mapping : Element):
  265. // Rewrite the host model based on the mapping combined with the RHS
  266. Element LHS_labels
  267. Element RHS_labels
  268. Element RHS_elements
  269. Element remaining
  270. String elem
  271. String label
  272. Element labels_to_remove
  273. Element labels_to_add
  274. String typename
  275. String original_typename
  276. String src
  277. String dst
  278. Element new_mapping
  279. String new_name
  280. Element RHS_map
  281. String tmp
  282. Element value
  283. Element value_function
  284. Element action
  285. Element original_RHS_labels
  286. LHS_labels = dict_keys(mapping)
  287. RHS_labels = create_node()
  288. RHS_map = create_node()
  289. RHS_elements = allAssociationDestinations(schedule_model, RHS, "RHS_contains")
  290. while (read_nr_out(RHS_elements) > 0):
  291. tmp = set_pop(RHS_elements)
  292. label = read_attribute(schedule_model, tmp, "label")
  293. set_add(RHS_labels, label)
  294. dict_add(RHS_map, label, tmp)
  295. remaining = set_overlap(LHS_labels, RHS_labels)
  296. original_RHS_labels = set_copy(RHS_labels)
  297. while (read_nr_out(remaining) > 0):
  298. elem = set_pop(remaining)
  299. set_remove(LHS_labels, elem)
  300. set_remove(RHS_labels, elem)
  301. labels_to_remove = LHS_labels
  302. labels_to_add = set_to_list(RHS_labels)
  303. new_mapping = dict_copy(mapping)
  304. while (read_nr_out(labels_to_add) > 0):
  305. // Add the elements linked to these labels
  306. label = list_pop(labels_to_add, 0)
  307. if (element_neq(read_attribute(schedule_model, RHS_map[label], "value"), read_root())):
  308. // There is a value associated with this node
  309. value_function = read_attribute(schedule_model, RHS_map[label], "value")
  310. value = value_function(host_model, mapping)
  311. typename = read_type(schedule_model, RHS_map[label])
  312. original_typename = string_substr(typename, 5, string_len(typename))
  313. new_name = instantiate_value(host_model, original_typename, "", value)
  314. dict_add(new_mapping, label, new_name)
  315. elif (is_edge(schedule_model["model"][RHS_map[label]])):
  316. // Edge
  317. src = read_attribute(schedule_model, reverseKeyLookup(schedule_model["model"], read_edge_src(schedule_model["model"][RHS_map[label]])), "label")
  318. dst = read_attribute(schedule_model, reverseKeyLookup(schedule_model["model"], read_edge_dst(schedule_model["model"][RHS_map[label]])), "label")
  319. // First check whether both source and destination are already created
  320. if (bool_and(dict_in(new_mapping, src), dict_in(new_mapping, dst))):
  321. // Both are present, so we can make the link
  322. typename = read_type(schedule_model, RHS_map[label])
  323. original_typename = string_substr(typename, 5, string_len(typename))
  324. new_name = instantiate_link(host_model, original_typename, "", new_mapping[src], new_mapping[dst])
  325. dict_add(new_mapping, label, new_name)
  326. else:
  327. // Delay this a bit, until all are bound
  328. list_append(labels_to_add, label)
  329. else:
  330. // Node
  331. // Create the node and add it
  332. typename = read_type(schedule_model, RHS_map[label])
  333. original_typename = string_substr(typename, 5, string_len(typename))
  334. new_name = instantiate_node(host_model, original_typename, "")
  335. dict_add(new_mapping, label, new_name)
  336. while (read_nr_out(original_RHS_labels) > 0):
  337. label = set_pop(original_RHS_labels)
  338. action = read_attribute(schedule_model, RHS_map[label], "action")
  339. if (element_neq(action, read_root())):
  340. Element func
  341. func = get_func_AL_model(action)
  342. func(host_model, new_mapping[label], mapping)
  343. while (read_nr_out(labels_to_remove) > 0):
  344. // Remove the elements linked to these labels
  345. label = set_pop(labels_to_remove)
  346. model_delete_element(host_model, mapping[label])
  347. dict_delete(new_mapping, label)
  348. // Execute global action (whatever it may be)
  349. action = read_attribute(schedule_model, RHS, "action")
  350. if (element_neq(action, read_root())):
  351. Element func
  352. func = get_func_AL_model(action)
  353. func(host_model, new_mapping)
  354. return!
  355. Element function transform(host_model : Element, schedule_model : Element):
  356. // Find initial model
  357. Element all_composites
  358. String composite
  359. String current
  360. Element merged
  361. Element original_mm
  362. // Now start transforming for real
  363. all_composites = allInstances(schedule_model, "Composite")
  364. if (read_nr_out(all_composites) == 1):
  365. // Only one, so it is easy
  366. current = set_pop(all_composites)
  367. else:
  368. // Filter out those that are themselves contained
  369. while (read_nr_out(all_composites) > 0):
  370. composite = set_pop(all_composites)
  371. if (read_nr_out(allIncomingAssociationInstances(schedule_model, composite, "Contains")) == 0):
  372. // Isn't contained in any, so this is the root model!
  373. current = composite
  374. if (transform_composite(host_model, schedule_model, current)):
  375. // Success, so return True if it is in-place, or the new model if it is out-place
  376. return True!
  377. else:
  378. return False!
  379. Boolean function transform_composite(host_model : Element, schedule_model : Element, composite : String):
  380. String current
  381. String typename
  382. Boolean result
  383. current = set_pop(allAssociationDestinations(schedule_model, composite, "Initial"))
  384. while (is_nominal_instance(schedule_model, current, "Rule")):
  385. // Still a rule that we must execute
  386. typename = read_type(schedule_model, current)
  387. log("RULE: " + current)
  388. if (typename == "Atomic"):
  389. result = transform_atomic(host_model, schedule_model, current)
  390. elif (typename == "Query"):
  391. result = transform_query(host_model, schedule_model, current)
  392. elif (typename == "Composite"):
  393. result = transform_composite(host_model, schedule_model, current)
  394. elif (typename == "ForAll"):
  395. result = transform_forall(host_model, schedule_model, current)
  396. if (result):
  397. current = set_pop(allAssociationDestinations(schedule_model, current, "OnSuccess"))
  398. else:
  399. current = set_pop(allAssociationDestinations(schedule_model, current, "OnFailure"))
  400. // No longer a rule, so it is either success or failure
  401. if (is_nominal_instance(schedule_model, current, "Success")):
  402. return True!
  403. else:
  404. return False!
  405. Boolean function transform_atomic(host_model : Element, schedule_model : Element, current : String):
  406. // Execute the atomic transformation
  407. Element mappings
  408. Element mapping
  409. mappings = full_match(host_model, schedule_model, current)
  410. if (read_nr_out(mappings) > 0):
  411. // Pick one!
  412. mapping = random_choice(set_to_list(mappings))
  413. String RHS
  414. RHS = set_pop(allAssociationDestinations(schedule_model, current, "RHSLink"))
  415. rewrite(host_model, schedule_model, RHS, mapping)
  416. return True!
  417. else:
  418. return False!
  419. Boolean function transform_forall(host_model : Element, schedule_model : Element, current : String):
  420. // Execute the atomic transformation
  421. Element mappings
  422. String RHS
  423. Element mapping
  424. Boolean result
  425. mappings = full_match(host_model, schedule_model, current)
  426. if (read_nr_out(mappings) > 0):
  427. result = True
  428. else:
  429. result = False
  430. while (read_nr_out(mappings) > 0):
  431. mapping = set_pop(mappings)
  432. // TODO check if there are actually no deletions happening in the meantime of other matched elements...
  433. log("Execute RHS for binding: " + dict_to_string(mapping))
  434. RHS = set_pop(allAssociationDestinations(schedule_model, current, "RHSLink"))
  435. rewrite(host_model, schedule_model, RHS, mapping)
  436. return result!
  437. Boolean function transform_query(host_model : Element, schedule_model : Element, current : String):
  438. // Execute the transformation
  439. Element mappings
  440. Element mapping
  441. mappings = full_match(host_model, schedule_model, current)
  442. if (read_nr_out(mappings) > 0):
  443. return True!
  444. else:
  445. return False!