transform.alc 19 KB

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