transform.alc 19 KB

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