transform.alc 19 KB

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