transform.alc 19 KB

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