transform.alc 19 KB

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