transform.alc 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  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 = list_create()
  22. workset = set_create()
  23. all_elements = allAssociationDestinations(schedule_model, LHS, "LHS_contains")
  24. full_all_elements = set_copy(all_elements)
  25. required_size = set_len(all_elements)
  26. // Need to keep adding to the schedule
  27. while (list_len(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(list_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 (set_len(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(list_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 = set_create()
  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 (set_len(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(set_len(oc) > 0, set_len(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 (set_len(options) < 2):
  114. ic = allIncomingAssociationInstances(schedule_model, current_element, "PreElement")
  115. String value
  116. while (bool_and(set_len(ic) > 0, set_len(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 (set_len(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 (set_len(options) > 1):
  128. // Multiple "only" options, which will not work out: no options!
  129. return set_create()!
  130. log("INT1 options: " + set_to_string(options))
  131. // Filter options further
  132. Element filtered_options
  133. String option
  134. filtered_options = set_create()
  135. Element bound
  136. bound = dict_values(map)
  137. while (set_len(options) > 0):
  138. option = set_pop(options)
  139. // Check for detecting same element twice
  140. if (bool_not(set_in(bound, option))):
  141. // Option is already present with another label, so skip this!
  142. // Check if it conforms to the desired type
  143. if (bool_not(guaranteed_instance)):
  144. if (bool_not(is_nominal_instance(host_model, option, original_typename))):
  145. // Not an actual instance, so skip!
  146. continue!
  147. // Check for local (matching) constraints of element
  148. if (element_eq(read_attribute(schedule_model, current_element, "constraint"), read_root())):
  149. // No local constraints, so all is well
  150. set_add(filtered_options, option)
  151. else:
  152. // Check local constraints and add only if positive
  153. Element constraint_function
  154. Boolean result
  155. Element func
  156. constraint_function = read_attribute(schedule_model, current_element, "constraint")
  157. func = get_func_AL_model(import_node(constraint_function))
  158. result = func(host_model, option)
  159. if (result):
  160. set_add(filtered_options, option)
  161. Element attributes
  162. String attribute
  163. Element value
  164. Element func
  165. Boolean result
  166. Element attributes_copy
  167. options = filtered_options
  168. log("INT2 options: " + set_to_string(options))
  169. filtered_options = set_create()
  170. // Check whether all attributes have a satisfied condition
  171. attributes_copy = dict_keys(getAttributeList(schedule_model, current_element))
  172. while (set_len(options) > 0):
  173. option = set_pop(options)
  174. attributes = set_copy(attributes_copy)
  175. result = True
  176. while (set_len(attributes) > 0):
  177. attribute = set_pop(attributes)
  178. if (bool_not(string_startswith(attribute, "constraint_"))):
  179. continue!
  180. value = read_attribute(schedule_model, current_element, attribute)
  181. // Attribute might be undefined, so skip if it is
  182. if (element_neq(value, read_root())):
  183. func = get_func_AL_model(import_node(value))
  184. result = func(read_attribute(host_model, option, string_substr(attribute, string_len("constraint_"), string_len(attribute) + 1)))
  185. else:
  186. result = True
  187. if (bool_not(result)):
  188. break!
  189. // Check value of last result, which will be True if all passed, or False otherwise
  190. if (result):
  191. set_add(filtered_options, option)
  192. options = filtered_options
  193. log("Return options: " + set_to_string(options))
  194. return options!
  195. Element function full_match(host_model : Element, schedule_model : Element, current : String, single_ok : Boolean):
  196. Element NACs
  197. String LHS
  198. String NAC
  199. Element mappings
  200. Element mapping
  201. Element result
  202. Element final_mappings
  203. Boolean allowed
  204. final_mappings = set_create()
  205. // First match the LHS part itself to get initial mappings
  206. LHS = set_pop(allAssociationDestinations(schedule_model, current, "LHSLink"))
  207. mappings = match(host_model, schedule_model, LHS, dict_create())
  208. // Got a list of all possible mappings, now filter based on NACs
  209. NACs = allAssociationDestinations(schedule_model, current, "NACLink")
  210. // For each possible mapping, we check all NACs!
  211. while (set_len(mappings) > 0):
  212. mapping = set_pop(mappings)
  213. log("Got mapping from match: " + dict_to_string(mapping))
  214. allowed = True
  215. while (set_len(NACs) > 0):
  216. NAC = set_pop(NACs)
  217. result = match(host_model, schedule_model, NAC, mapping)
  218. if (set_len(result) > 0):
  219. // NAC could be matched, and therefore is not allowed
  220. allowed = False
  221. break!
  222. if (allowed):
  223. set_add_node(final_mappings, mapping)
  224. if (single_ok):
  225. break!
  226. return final_mappings!
  227. Element function match(host_model : Element, schedule_model : Element, LHS : String, initial_mapping : Element):
  228. // Match the schedule_model to the host_model, returning all possible mappings from schedule_model elements to host_model elements
  229. // Make the schedule first
  230. Element schedule
  231. schedule = make_matching_schedule(schedule_model, LHS, dict_keys(initial_mapping))
  232. log("Got schedule: " + list_to_string(schedule))
  233. // Now follow the schedule, incrementally building all mappings
  234. Element mappings
  235. Element new_mappings
  236. Element new_map
  237. String current_element
  238. Element map
  239. String option
  240. Element options
  241. mappings = set_create()
  242. set_add_node(mappings, initial_mapping)
  243. while (list_len(schedule) > 0):
  244. current_element = list_pop(schedule, list_len(schedule) - 1)
  245. //log("Binding element with label " + cast_v2s(read_attribute(schedule_model, current_element, "label")))
  246. new_mappings = dict_create()
  247. while (set_len(mappings) > 0):
  248. map = set_pop(mappings)
  249. options = get_possible_bindings(host_model, schedule_model, current_element, map)
  250. log("Options to bind: " + set_to_string(options))
  251. while (set_len(options) > 0):
  252. option = set_pop(options)
  253. log("Pop option: " + option)
  254. new_map = dict_copy(map)
  255. log("Copied: " + dict_to_string(new_map))
  256. dict_add_fast(new_map, read_attribute(schedule_model, current_element, "label"), option)
  257. log("PRE Got new mapping: " + dict_to_string(new_map))
  258. set_add_node(new_mappings, new_map)
  259. log("Got new mapping: " + dict_to_string(new_map))
  260. mappings = new_mappings
  261. //log("Remaining options: " + cast_v2s(set_len(mappings)))
  262. if (set_len(mappings) == 0):
  263. // Stop because we have no more options remaining!
  264. return set_create()!
  265. // Finished, so try the global constraint
  266. String constraint
  267. Element func
  268. Boolean result
  269. new_mappings = dict_create()
  270. constraint = read_attribute(schedule_model, LHS, "constraint")
  271. if (element_neq(constraint, read_root())):
  272. while (set_len(mappings) > 0):
  273. map = set_pop(mappings)
  274. func = get_func_AL_model(import_node(constraint))
  275. result = func(host_model, map)
  276. if (result):
  277. set_add_node(new_mappings, map)
  278. mappings = new_mappings
  279. return mappings!
  280. Void function rewrite(host_model : Element, schedule_model : Element, RHS : String, mapping : Element):
  281. // Rewrite the host model based on the mapping combined with the RHS
  282. Element LHS_labels
  283. Element RHS_labels
  284. Element RHS_elements
  285. Element remaining
  286. String elem
  287. String label
  288. Element labels_to_remove
  289. Element labels_to_add
  290. String typename
  291. String original_typename
  292. String src
  293. String dst
  294. Element new_mapping
  295. String new_name
  296. Element RHS_map
  297. String tmp
  298. Element value
  299. Element action
  300. Element original_RHS_labels
  301. Element reverse
  302. reverse = make_reverse_dictionary(schedule_model["model"])
  303. LHS_labels = dict_keys(mapping)
  304. RHS_labels = set_create()
  305. RHS_map = dict_create()
  306. RHS_elements = allAssociationDestinations(schedule_model, RHS, "RHS_contains")
  307. while (set_len(RHS_elements) > 0):
  308. tmp = set_pop(RHS_elements)
  309. label = read_attribute(schedule_model, tmp, "label")
  310. set_add(RHS_labels, label)
  311. dict_add_fast(RHS_map, label, tmp)
  312. remaining = set_overlap(LHS_labels, RHS_labels)
  313. original_RHS_labels = set_copy(RHS_labels)
  314. while (set_len(remaining) > 0):
  315. elem = set_pop(remaining)
  316. set_remove(LHS_labels, elem)
  317. set_remove(RHS_labels, elem)
  318. labels_to_remove = LHS_labels
  319. labels_to_add = set_to_list(RHS_labels)
  320. new_mapping = dict_copy(mapping)
  321. while (list_len(labels_to_add) > 0):
  322. // Add the elements linked to these labels
  323. label = list_pop(labels_to_add, list_len(labels_to_add) - 1)
  324. if (is_edge(schedule_model["model"][RHS_map[label]])):
  325. // Edge
  326. src = read_attribute(schedule_model, reverse[cast_id2s(read_edge_src(schedule_model["model"][RHS_map[label]]))], "label")
  327. dst = read_attribute(schedule_model, reverse[cast_id2s(read_edge_dst(schedule_model["model"][RHS_map[label]]))], "label")
  328. // First check whether both source and destination are already created
  329. if (bool_and(dict_in(new_mapping, src), dict_in(new_mapping, dst))):
  330. // Both are present, so we can make the link
  331. typename = read_type(schedule_model, RHS_map[label])
  332. original_typename = string_substr(typename, 5, string_len(typename))
  333. new_name = instantiate_link(host_model, original_typename, "", new_mapping[src], new_mapping[dst])
  334. dict_add_fast(new_mapping, label, new_name)
  335. else:
  336. // Delay this a bit, until all are bound
  337. list_insert(labels_to_add, label, 0)
  338. else:
  339. // Node
  340. // Create the node and add it
  341. typename = read_type(schedule_model, RHS_map[label])
  342. original_typename = string_substr(typename, 5, string_len(typename))
  343. new_name = instantiate_node(host_model, original_typename, "")
  344. dict_add_fast(new_mapping, label, new_name)
  345. Element attributes
  346. String attribute
  347. Element result
  348. Element func
  349. while (set_len(original_RHS_labels) > 0):
  350. // Perform actions
  351. label = set_pop(original_RHS_labels)
  352. // Do all attribute actions that are defined
  353. attributes = dict_keys(getAttributeList(schedule_model, RHS_map[label]))
  354. while (set_len(attributes) > 0):
  355. attribute = set_pop(attributes)
  356. if (bool_not(string_startswith(attribute, "value_"))):
  357. continue!
  358. value = read_attribute(schedule_model, RHS_map[label], attribute)
  359. if (element_neq(value, read_root())):
  360. func = get_func_AL_model(import_node(value))
  361. result = func(host_model, new_mapping[label], mapping)
  362. if (has_value(result)):
  363. // New value defined, so assign!
  364. instantiate_attribute(host_model, new_mapping[label], string_substr(attribute, string_len("value_"), string_len(attribute) + 1), result)
  365. else:
  366. // Non-value return means to destroy the attribute!
  367. unset_attribute(host_model, new_mapping[label], string_substr(attribute, string_len("value_"), string_len(attribute) + 1))
  368. // Do the global action of each element
  369. action = read_attribute(schedule_model, RHS_map[label], "action")
  370. if (element_neq(action, read_root())):
  371. Element func
  372. func = get_func_AL_model(import_node(action))
  373. func(host_model, new_mapping[label], mapping)
  374. while (set_len(labels_to_remove) > 0):
  375. // Remove the elements linked to these labels
  376. label = set_pop(labels_to_remove)
  377. model_delete_element(host_model, mapping[label])
  378. dict_delete(new_mapping, label)
  379. // Execute global action (whatever it may be)
  380. action = read_attribute(schedule_model, RHS, "action")
  381. if (element_neq(action, read_root())):
  382. Element func
  383. func = get_func_AL_model(import_node(action))
  384. func(host_model, new_mapping)
  385. return!
  386. Element function transform(host_model : Element, schedule_model : Element):
  387. // Find initial model
  388. Element all_composites
  389. String composite
  390. String current
  391. Element merged
  392. Element original_mm
  393. // Now start transforming for real
  394. all_composites = allInstances(schedule_model, "Composite")
  395. if (set_len(all_composites) == 1):
  396. // Only one, so it is easy
  397. current = set_pop(all_composites)
  398. else:
  399. // Filter out those that are themselves contained
  400. while (set_len(all_composites) > 0):
  401. composite = set_pop(all_composites)
  402. if (set_len(allIncomingAssociationInstances(schedule_model, composite, "Contains")) == 0):
  403. // Isn't contained in any, so this is the root model!
  404. current = composite
  405. if (transform_composite(host_model, schedule_model, current)):
  406. // Success, so return True if it is in-place, or the new model if it is out-place
  407. return True!
  408. else:
  409. return False!
  410. Boolean function transform_composite(host_model : Element, schedule_model : Element, composite : String):
  411. String current
  412. String typename
  413. Boolean result
  414. current = set_pop(allAssociationDestinations(schedule_model, composite, "Initial"))
  415. while (is_nominal_instance(schedule_model, current, "Rule")):
  416. //log("Executing " + current)
  417. // Still a rule that we must execute
  418. typename = read_type(schedule_model, current)
  419. if (typename == "Atomic"):
  420. result = transform_atomic(host_model, schedule_model, current)
  421. elif (typename == "Query"):
  422. result = transform_query(host_model, schedule_model, current)
  423. elif (typename == "Composite"):
  424. result = transform_composite(host_model, schedule_model, current)
  425. elif (typename == "ForAll"):
  426. result = transform_forall(host_model, schedule_model, current)
  427. if (result):
  428. current = set_pop(allAssociationDestinations(schedule_model, current, "OnSuccess"))
  429. else:
  430. current = set_pop(allAssociationDestinations(schedule_model, current, "OnFailure"))
  431. // No longer a rule, so it is either success or failure
  432. if (is_nominal_instance(schedule_model, current, "Success")):
  433. return True!
  434. else:
  435. return False!
  436. Boolean function transform_atomic(host_model : Element, schedule_model : Element, current : String):
  437. // Execute the atomic transformation
  438. Element mappings
  439. Element mapping
  440. mappings = full_match(host_model, schedule_model, current, True)
  441. if (set_len(mappings) > 0):
  442. // Pick one!
  443. mapping = random_choice(set_to_list(mappings))
  444. String RHS
  445. RHS = set_pop(allAssociationDestinations(schedule_model, current, "RHSLink"))
  446. rewrite(host_model, schedule_model, RHS, mapping)
  447. return True!
  448. else:
  449. return False!
  450. Boolean function transform_forall(host_model : Element, schedule_model : Element, current : String):
  451. // Execute the atomic transformation
  452. Element mappings
  453. String RHS
  454. Element mapping
  455. Boolean result
  456. mappings = full_match(host_model, schedule_model, current, False)
  457. if (set_len(mappings) > 0):
  458. result = True
  459. else:
  460. result = False
  461. //log("Matches in forall: " + cast_v2s(set_len(mappings)))
  462. while (set_len(mappings) > 0):
  463. mapping = set_pop(mappings)
  464. RHS = set_pop(allAssociationDestinations(schedule_model, current, "RHSLink"))
  465. rewrite(host_model, schedule_model, RHS, mapping)
  466. return result!
  467. Boolean function transform_query(host_model : Element, schedule_model : Element, current : String):
  468. // Execute the transformation
  469. Element mappings
  470. Element mapping
  471. mappings = full_match(host_model, schedule_model, current, True)
  472. if (set_len(mappings) > 0):
  473. return True!
  474. else:
  475. return False!