transform.alc 19 KB

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