SCCD_execute.alc 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767
  1. include "primitives.alh"
  2. include "modelling.alh"
  3. include "object_operations.alh"
  4. include "utils.alh"
  5. include "random.alh"
  6. include "library.alh"
  7. include "io.alh"
  8. Element function resolve_function(location : String, data : Element):
  9. if (bool_not(dict_in(data["cache_operations"], location))):
  10. dict_add(data["cache_operations"], location, get_func_AL_model(import_node(location)))
  11. return data["cache_operations"][location]!
  12. Void function print_states(model : Element, data : Element):
  13. Element classes
  14. Element states
  15. Element class
  16. String state
  17. log("Current states:")
  18. classes = set_copy(data["classes"])
  19. while (set_len(classes) > 0):
  20. class = set_pop(classes)
  21. log(string_join(string_join(string_join(" ", class["ID"]), " : "), read_attribute(model, class["type"], "name")))
  22. log(" Attributes: " + dict_to_string(class["attributes"]))
  23. states = set_copy(class["states"])
  24. log(" States:")
  25. while (set_len(states) > 0):
  26. state = set_pop(states)
  27. log(string_join(" ", read_attribute(model, state, "name")))
  28. return!
  29. Element function filter(model : Element, set : Element, attribute_name : String, attribute_value : Element):
  30. Element keys
  31. String key
  32. Element result
  33. result = create_node()
  34. while (set_len(set) > 0):
  35. key = set_pop(set)
  36. if (value_eq(read_attribute(model, key, attribute_name), attribute_value)):
  37. set_add(result, key)
  38. return result!
  39. Element function filter_exists(model : Element, set : Element, attribute_name : String):
  40. Element keys
  41. String key
  42. Element result
  43. result = create_node()
  44. while (set_len(set) > 0):
  45. key = set_pop(set)
  46. if (element_neq(read_attribute(model, key, attribute_name), read_root())):
  47. set_add(result, key)
  48. return result!
  49. Element function expand_current_state(model : Element, state : String, data : Element):
  50. // Find the hierarchy of all current states, and select those that contain the currently selected state
  51. Element result
  52. Element current_states
  53. result = create_node()
  54. current_states = set_copy(data["current_class_handle"]["states"])
  55. Element hierarchy
  56. String deep_state
  57. while (set_len(current_states) > 0):
  58. deep_state = set_pop(current_states)
  59. hierarchy = find_hierarchy(model, deep_state, data)
  60. // Got the hierarchy of one of the states
  61. if (set_in(hierarchy, state)):
  62. // This hierarchy contains the root state we are checking for, so add to set
  63. set_add(result, deep_state)
  64. return result!
  65. Element function expand_initial_state(model : Element, state : String, data : Element):
  66. String t
  67. t = read_type(model, state)
  68. if (t == "SCCD/CompositeState"):
  69. // Recurse further in the composite
  70. return expand_composite_state(model, state, data)!
  71. elif (t == "SCCD/ParallelState"):
  72. // Split up all components
  73. return expand_parallel_state(model, state, data)!
  74. elif (t == "SCCD/HistoryState"):
  75. // Reset the history
  76. // This is not really an initial state, but it is called in exactly the same places
  77. return data["current_class_handle"]["history"][get_parent(model, state)]!
  78. else:
  79. // Probably just an atomic, so return this one only
  80. Element result
  81. result = create_node()
  82. set_add(result, state)
  83. return result!
  84. Element function expand_composite_state(model : Element, composite_state : String, data : Element):
  85. // Resolve all initial states from a single composite state
  86. String initial
  87. // Fetch the initial state
  88. initial = set_pop(filter(model, allAssociationDestinations(model, composite_state, "SCCD/composite_children"), "isInitial", True))
  89. // Expand the initial state, depending on what it is
  90. return expand_initial_state(model, initial, data)!
  91. Element function expand_parallel_state(model : Element, parallel_state : String, data : Element):
  92. // Resolve all initial states from a single parallel state
  93. Element children
  94. Element result
  95. Element expanded_children
  96. children = allAssociationDestinations(model, parallel_state, "SCCD/parallel_children")
  97. result = create_node()
  98. while (set_len(children) > 0):
  99. set_merge(result, expand_initial_state(model, set_pop(children), data))
  100. return result!
  101. Void function delete_class(model : Element, data : Element, identifier : String):
  102. // Stop a specific class instance, with attached statechart, from executing
  103. dict_delete(data["classes"], identifier)
  104. Void function start_class(model : Element, data : Element, class : String, identifier : String, parameters : Element):
  105. // Start up the class and assign its initial state to it
  106. // Create the data structure for a running class
  107. Element class_handle
  108. class_handle = create_node()
  109. dict_add(class_handle, "type", class)
  110. dict_add(class_handle, "ID", identifier)
  111. dict_add(class_handle, "events", create_node())
  112. dict_add(class_handle, "new_events", create_node())
  113. dict_add(class_handle, "timers", create_node())
  114. dict_add(data["classes"], class_handle["ID"], class_handle)
  115. String prev_class
  116. prev_class = data["current_class"]
  117. dict_overwrite(data, "current_class", identifier)
  118. dict_overwrite(data, "current_class_handle", data["classes"][identifier])
  119. // Add the current state of the class
  120. String initial_state
  121. // Should only be one behaviour linked to it!
  122. initial_state = set_pop(allAssociationDestinations(model, class, "SCCD/behaviour"))
  123. dict_add(class_handle, "states", expand_initial_state(model, initial_state, class_handle))
  124. // Initialize history for all composite states
  125. Element history
  126. Element cstates
  127. String cstate
  128. history = create_node()
  129. dict_add(class_handle, "history", history)
  130. cstates = allInstances(model, "SCCD/CompositeState")
  131. while (set_len(cstates) > 0):
  132. cstate = set_pop(cstates)
  133. dict_add(history, cstate, expand_initial_state(model, cstate, class_handle))
  134. // Add all attributes
  135. Element attributes
  136. attributes = create_node()
  137. Element attrs
  138. attrs = allAssociationDestinations(model, class, "SCCD/class_attributes")
  139. while (set_len(attrs) > 0):
  140. dict_add(attributes, read_attribute(model, set_pop(attrs), "name"), read_root())
  141. dict_add(class_handle, "attributes", attributes)
  142. // Invoke constructor
  143. Element constructor
  144. constructor = read_attribute(model, class, "constructor_body")
  145. if (element_neq(constructor, read_root())):
  146. // Constructor, so execute
  147. constructor = resolve_function(constructor, data)
  148. constructor(attributes, parameters)
  149. // Execute all entry actions
  150. Element init
  151. init = create_node()
  152. set_add(init, "")
  153. // Initial state before initialization is the set with an empty hierarchy
  154. // Empty set would not find any difference between the source and target
  155. execute_actions(model, init, set_copy(class_handle["states"]), data, "")
  156. dict_overwrite(data, "current_class", prev_class)
  157. dict_overwrite(data, "current_class_handle", data["classes"][prev_class])
  158. return!
  159. Element function get_enabled_transitions(model : Element, state : String, data : Element):
  160. // Returns all enabled transitions
  161. Element result
  162. Element to_filter
  163. String attr
  164. String transition
  165. Element cond
  166. String evt_name
  167. Element evt
  168. Element events
  169. Element event_names
  170. Element event_parameters
  171. result = create_node()
  172. to_filter = allOutgoingAssociationInstances(model, state, "SCCD/transition")
  173. event_names = create_node()
  174. event_parameters = create_node()
  175. events = set_copy(data["current_class_handle"]["events"])
  176. while (set_len(events) > 0):
  177. evt = set_pop(events)
  178. evt_name = list_read(evt, 0)
  179. if (bool_not(set_in(event_names, evt_name))):
  180. // Not yet registered the event
  181. set_add(event_names, evt_name)
  182. dict_add(event_parameters, evt_name, create_node())
  183. // Add event parameters
  184. set_add(event_parameters[evt_name], list_read(evt, 1))
  185. while (set_len(to_filter) > 0):
  186. transition = set_pop(to_filter)
  187. // Check event
  188. attr = read_attribute(model, transition, "event")
  189. if (bool_not(bool_or(element_eq(attr, read_root()), set_in(event_names, attr)))):
  190. // At least one enabled event is found
  191. continue!
  192. // Check after
  193. // Only an after if there was no event!
  194. if (bool_and(element_eq(attr, read_root()), read_attribute(model, transition, "after"))):
  195. if (dict_in(data["current_class_handle"]["timers"], transition)):
  196. // Registered timer already, let's check if it has expired
  197. if (float_gt(data["current_class_handle"]["timers"][transition], data["time_sim"])):
  198. // Not enabled yet
  199. continue!
  200. else:
  201. // Not registered even, so not enabled either
  202. continue!
  203. // Check condition, but depends on whether there was an event or not
  204. cond = read_attribute(model, transition, "cond")
  205. if (element_neq(cond, read_root())):
  206. // Got a condition, so resolve
  207. cond = resolve_function(cond, data)
  208. if (element_neq(attr, read_root())):
  209. // We have an event to take into account!
  210. Element params
  211. Element param
  212. params = set_copy(event_parameters[attr])
  213. while (set_len(params) > 0):
  214. param = set_pop(params)
  215. if (element_neq(cond, read_root())):
  216. // Got a condition to check first
  217. if (bool_not(cond(data["current_class_handle"]["attributes"], param))):
  218. // Condition failed, so skip
  219. continue!
  220. // Fine to add this one with the specified parameters
  221. set_add_node(result, create_tuple(transition, param))
  222. else:
  223. // No event to think about, just add the transition
  224. if (element_neq(cond, read_root())):
  225. // Check the condition first
  226. if (bool_not(cond(data["current_class_handle"]["attributes"], read_root()))):
  227. // Condition false, so skip
  228. continue!
  229. // Fine to add this one without event parameters (no event)
  230. set_add_node(result, create_tuple(transition, read_root()))
  231. return result!
  232. Void function process_raised_event(model : Element, event : Element, parameter_action : Element, data : Element):
  233. String scope
  234. scope = read_attribute(model, event, "scope")
  235. if (scope == "cd"):
  236. // Is an event for us internally, so don't append
  237. // Instead, we process it directly
  238. String operation
  239. operation = read_attribute(model, event, "event")
  240. if (operation == "create_instance"):
  241. // Start up a new class of the desired type
  242. // Parameters of this call:
  243. // class -- type of the class to instantiate
  244. // identifier -- name of this instance, for future reference
  245. // parameters -- parameters for constructor
  246. String class
  247. String identifier
  248. Element parameters
  249. class = set_pop(filter(model, allInstances(model, "SCCD/Class"), "name", list_read(parameter_action, 0)))
  250. identifier = list_read(parameter_action, 1)
  251. parameters = list_read(parameter_action, 2)
  252. start_class(model, data, class, identifier, parameters)
  253. elif (operation == "delete_instance"):
  254. // Delete the requested class
  255. String identifier
  256. identifier = list_read(parameter_action, 0)
  257. delete_class(model, data, identifier)
  258. elif (scope == "broad"):
  259. // Send to all classes
  260. Element classes
  261. classes = dict_keys(data["classes"])
  262. while(set_len(classes) > 0):
  263. set_add_node(data["classes"][set_pop(classes)]["new_events"], create_tuple(read_attribute(model, event, "event"), parameter_action))
  264. elif (scope == "narrow"):
  265. // Send to the specified class only
  266. // TODO some error checking would be nice...
  267. set_add_node(data["classes"][read_attribute(model, event, "target")]["new_events"], create_tuple(read_attribute(model, event, "event"), parameter_action))
  268. else:
  269. // Same as local
  270. set_add_node(data["current_class_handle"]["new_events"], create_tuple(read_attribute(model, event, "event"), parameter_action))
  271. return !
  272. Element function execute_transition(model : Element, data : Element, transition_tuple : Element):
  273. // Execute the script (if any)
  274. Element script
  275. String transition
  276. Element event_parameter
  277. transition = list_read(transition_tuple, 0)
  278. event_parameter = list_read(transition_tuple, 1)
  279. script = read_attribute(model, transition, "script")
  280. if (element_neq(script, read_root())):
  281. script = resolve_function(script, data)
  282. script(data["current_class_handle"]["attributes"], event_parameter)
  283. // Raise events (if any)
  284. Element events
  285. String event
  286. events = allAssociationDestinations(model, transition, "SCCD/transition_raises")
  287. while (set_len(events) > 0):
  288. event = set_pop(events)
  289. Element parameter_action
  290. parameter_action = read_attribute(model, event, "parameter")
  291. if (element_neq(parameter_action, read_root())):
  292. // Got a parameter to evaluate
  293. parameter_action = resolve_function(parameter_action, data)
  294. parameter_action = parameter_action(data["current_class_handle"]["attributes"], event_parameter)
  295. process_raised_event(model, event, parameter_action, data)
  296. // Find new set of states
  297. Element target_states
  298. Element source_states
  299. source_states = expand_current_state(model, readAssociationSource(model, transition), data)
  300. target_states = expand_initial_state(model, readAssociationDestination(model, transition), data)
  301. execute_actions(model, source_states, target_states, data, readAssociationSource(model, transition))
  302. return target_states!
  303. Boolean function step_class(model : Element, data : Element, class : String):
  304. // Find enabled transitions in a class and execute it, updating the state
  305. // Iterate over all current states, searching for enabled transitions
  306. // Search for enabled transitions in higher levels as well!
  307. Element states
  308. Element new_states
  309. String state
  310. Element transitions
  311. String transition
  312. Boolean transitioned
  313. Element hierarchy
  314. String current_state
  315. Boolean found
  316. if (bool_not(dict_in(data["classes"], class))):
  317. // Seems like this class was removed, so stop execution
  318. return False!
  319. // Notify everyone of the current class
  320. dict_overwrite(data, "current_class", class)
  321. dict_overwrite(data, "current_class_handle", data["classes"][class])
  322. states = set_copy(data["current_class_handle"]["states"])
  323. new_states = create_node()
  324. transitioned = False
  325. while (set_len(states) > 0):
  326. state = set_pop(states)
  327. found = False
  328. // Loop over the hierarchy of this state and try to apply transitions
  329. hierarchy = find_hierarchy(model, state, data)
  330. while (list_len(hierarchy) > 0):
  331. current_state = list_pop(hierarchy, 0)
  332. transitions = get_enabled_transitions(model, current_state, data)
  333. if (set_len(transitions) > 0):
  334. // Found an enabled transition, so store that one
  335. transition = random_choice(set_to_list(transitions))
  336. // Execute transition
  337. set_merge(new_states, execute_transition(model, data, transition))
  338. // When leaving an orthogonal component, we must also pop all related states that might be processed in the future!
  339. Element leaving
  340. leaving = expand_current_state(model, current_state, data)
  341. set_difference(states, leaving)
  342. transitioned = True
  343. found = True
  344. break!
  345. if (bool_not(found)):
  346. // Nothing found, so stay in the current state
  347. set_add(new_states, state)
  348. // Update states
  349. dict_overwrite(data["current_class_handle"], "states", new_states)
  350. return transitioned!
  351. String function get_parent(model : Element, state : String):
  352. Element tmp_set
  353. tmp_set = allAssociationOrigins(model, state, "SCCD/composite_children")
  354. set_merge(tmp_set, allAssociationOrigins(model, state, "SCCD/parallel_children"))
  355. if (set_len(tmp_set) > 0):
  356. return set_pop(tmp_set)!
  357. else:
  358. return ""!
  359. Element function find_hierarchy(model : Element, state : String, data : Element):
  360. // Try to cache as much as possible!
  361. if (bool_not(dict_in(data["cache_hierarchy"], state))):
  362. Element result
  363. if (state == ""):
  364. result = create_node()
  365. else:
  366. String parent
  367. parent = get_parent(model, state)
  368. // We have a parent, so take the parent list first
  369. result = find_hierarchy(model, parent, data)
  370. list_append(result, state)
  371. dict_add(data["cache_hierarchy"], state, result)
  372. return dict_copy(data["cache_hierarchy"][state])!
  373. Void function execute_actions(model : Element, source_states : Element, target_states : Element, data : Element, transition_source : String):
  374. Element exit
  375. Element entry
  376. exit = create_node()
  377. entry = create_node()
  378. source_states = set_copy(source_states)
  379. target_states = set_copy(target_states)
  380. // Add all exit and entry actions to the list of actions to execute
  381. // Do this by finding the common parent, and then doing all exit actions up to that node, and all entry actions up to the target_state
  382. // First, find the hierarchy!
  383. Element hierarchy_sources
  384. Element hierarchy_targets
  385. Element all_hierarchies
  386. hierarchy_sources = create_node()
  387. while (set_len(source_states) > 0):
  388. set_add(hierarchy_sources, find_hierarchy(model, set_pop(source_states), data))
  389. hierarchy_targets = create_node()
  390. while (set_len(target_states) > 0):
  391. set_add(hierarchy_targets, find_hierarchy(model, set_pop(target_states), data))
  392. all_hierarchies = set_copy(hierarchy_sources)
  393. set_merge(all_hierarchies, hierarchy_targets)
  394. // Difference these all lists, finding the first common entry
  395. Element iter_hierarchies
  396. Integer i
  397. String current
  398. Element hierarchy
  399. Boolean finished
  400. Integer transition_depth
  401. Integer lca_depth
  402. lca_depth = 0
  403. finished = False
  404. if (transition_source != ""):
  405. // Find out the level of the transition_source by fetching its hierarchy
  406. transition_depth = list_len(find_hierarchy(model, transition_source, data)) - 1
  407. // Now check for the least common ancestor
  408. while (bool_not(finished)):
  409. // Check the i-th element in both and see if they are equal
  410. current = ""
  411. iter_hierarchies = set_copy(all_hierarchies)
  412. while (set_len(iter_hierarchies) > 0):
  413. hierarchy = set_pop(iter_hierarchies)
  414. // Exhausted one of the lists
  415. if (lca_depth >= list_len(hierarchy)):
  416. finished = True
  417. break!
  418. // Reached the same level as transition depth already, so no need to increase
  419. if (lca_depth == transition_depth):
  420. finished = True
  421. break!
  422. // First entry, so read out value as reference
  423. if (current == ""):
  424. current = list_read(hierarchy, lca_depth)
  425. // Check with reference element
  426. if (bool_not(value_eq(list_read(hierarchy, lca_depth), current))):
  427. finished = True
  428. break!
  429. // i-th element equal for all hierarchies, so go to next element
  430. if (bool_not(finished)):
  431. lca_depth = lca_depth + 1
  432. if (lca_depth < transition_depth):
  433. i = lca_depth
  434. else:
  435. i = transition_depth
  436. else:
  437. // Initial, so just set i to zero
  438. i = 0
  439. // Found the first differing element at position i
  440. // All elements remaining in hierarchy_source are to be traversed in REVERSE order for the exit actions
  441. // All elements remaining in hierarchy_target are to be traversed in NORMAL order for the entry actions
  442. // This is not that simple either, as we need to consider that some actions might already have been added to the list...
  443. // Add hierarchy_sources actions
  444. String state
  445. Element visited
  446. Element action
  447. Element spliced_hierarchy
  448. Element hierarchy_source
  449. Element hierarchy_target
  450. visited = create_node()
  451. while (set_len(hierarchy_sources) > 0):
  452. // Get one of these hierarchies
  453. hierarchy_source = set_pop(hierarchy_sources)
  454. spliced_hierarchy = list_splice(hierarchy_source, i, list_len(hierarchy_source))
  455. while (list_len(spliced_hierarchy) > 0):
  456. state = list_pop(spliced_hierarchy, list_len(spliced_hierarchy) - 1)
  457. if (set_in(visited, state)):
  458. // Already added this state, so don't bother
  459. continue!
  460. else:
  461. // New state, so prepend it to the list
  462. // Prepend, instead of append, as we want to do these operations in reverse order!
  463. list_insert(exit, state, 0)
  464. // Add this state as visited
  465. set_add(visited, state)
  466. // Add hierarchy_targets actions
  467. // Clear visited, just to be safe, though it should not matter
  468. visited = create_node()
  469. while (set_len(hierarchy_targets) > 0):
  470. // Get one of these hierarchies
  471. hierarchy_target = set_pop(hierarchy_targets)
  472. spliced_hierarchy = list_splice(hierarchy_target, i, list_len(hierarchy_target))
  473. while (list_len(spliced_hierarchy) > 0):
  474. state = list_pop(spliced_hierarchy, list_len(spliced_hierarchy) - 1)
  475. if (set_in(visited, state)):
  476. // Already added this state, so don't bother
  477. continue!
  478. else:
  479. // New state, so append it to the list
  480. // Append, instead of prepend, as we want to do these operations in normal order!
  481. list_append(entry, state)
  482. // Add this state as visited, even though there might not have been an associated action
  483. set_add(visited, state)
  484. // Now we have a list of traversed states!
  485. // Start executing all their operations in order
  486. Element events
  487. String event
  488. // First do exit actions
  489. while (list_len(exit) > 0):
  490. state = list_pop(exit, 0)
  491. // Set history when leaving
  492. if (read_type(model, state) == "SCCD/CompositeState"):
  493. dict_overwrite(data["current_class_handle"]["history"], state, expand_current_state(model, state, data))
  494. // Do exit actions
  495. action = read_attribute(model, state, "onExitScript")
  496. if (element_neq(action, read_root())):
  497. // Got a script, so execute!
  498. action = resolve_function(action, data)
  499. action(data["current_class_handle"]["attributes"])
  500. // Raise events
  501. events = allAssociationDestinations(model, state, "SCCD/onExitRaise")
  502. while (set_len(events) > 0):
  503. event = set_pop(events)
  504. Element parameter_action
  505. parameter_action = read_attribute(model, event, "parameter")
  506. if (element_neq(parameter_action, read_root())):
  507. // Got a parameter to evaluate
  508. parameter_action = resolve_function(parameter_action, data)
  509. parameter_action = parameter_action(data["current_class_handle"]["attributes"])
  510. process_raised_event(model, event, parameter_action, data)
  511. // Unschedule after events
  512. Element timed_transitions
  513. timed_transitions = filter_exists(model, allOutgoingAssociationInstances(model, state, "SCCD/transition"), "after")
  514. while (set_len(timed_transitions) > 0):
  515. dict_delete(data["current_class_handle"]["timers"], set_pop(timed_transitions))
  516. // Then do entry actions
  517. while (list_len(entry) > 0):
  518. state = list_pop(entry, 0)
  519. // Do entry actions
  520. action = read_attribute(model, state, "onEntryScript")
  521. if (element_neq(action, read_root())):
  522. // Got a script, so execute!
  523. action = resolve_function(action, data)
  524. action(data["current_class_handle"]["attributes"])
  525. // Raise events
  526. events = allAssociationDestinations(model, state, "SCCD/onEntryRaise")
  527. while (set_len(events) > 0):
  528. event = set_pop(events)
  529. Element parameter_action
  530. parameter_action = read_attribute(model, event, "parameter")
  531. if (element_neq(parameter_action, read_root())):
  532. // Got a parameter to evaluate
  533. parameter_action = resolve_function(parameter_action, data)
  534. parameter_action = parameter_action(data["current_class_handle"]["attributes"])
  535. process_raised_event(model, event, parameter_action, data)
  536. // Schedule after events
  537. Element timed_transitions
  538. String transition
  539. Element after
  540. timed_transitions = filter_exists(model, allOutgoingAssociationInstances(model, state, "SCCD/transition"), "after")
  541. while (set_len(timed_transitions) > 0):
  542. transition = set_pop(timed_transitions)
  543. after = resolve_function(read_attribute(model, transition, "after"), data)
  544. dict_add(data["current_class_handle"]["timers"], transition, float_addition(data["time_sim"], after(data["current_class_handle"]["attributes"])))
  545. return !
  546. Float function step(model : Element, data : Element):
  547. // Step through all classes
  548. Element classes
  549. Element class
  550. Float t_min
  551. Float t_current
  552. Boolean transitioned
  553. Element keys
  554. String key
  555. t_min = 999999.0
  556. classes = dict_keys(data["classes"])
  557. transitioned = False
  558. while (set_len(classes) > 0):
  559. class = set_pop(classes)
  560. if (step_class(model, data, class)):
  561. transitioned = True
  562. if (bool_not(transitioned)):
  563. // Find minimum timer for this class, and store that
  564. keys = dict_keys(data["classes"][class]["timers"])
  565. while (set_len(keys) > 0):
  566. key = set_pop(keys)
  567. t_current = data["classes"][class]["timers"][key]
  568. if (t_current < t_min):
  569. t_min = t_current
  570. if (transitioned):
  571. // Do another step, as we can transition
  572. return data["time_sim"]!
  573. else:
  574. return t_min!
  575. Boolean function main(model : Element):
  576. // Executes the provided SCCD model
  577. Element data
  578. data = create_node()
  579. dict_add(data, "classes", create_node())
  580. dict_add(data, "cache_operations", create_node())
  581. dict_add(data, "cache_hierarchy", create_node())
  582. dict_add(data, "current_class", "")
  583. Float time_0
  584. Float time_sim
  585. Float time_wallclock
  586. time_0 = time()
  587. time_sim = 0.0
  588. dict_add(data, "time_sim", 0.0)
  589. // Prepare for input
  590. output("Ready for input!")
  591. // Find initial
  592. String default_class
  593. default_class = set_pop(filter(model, allInstances(model, "SCCD/Class"), "default", True))
  594. // Start up the default class
  595. start_class(model, data, default_class, "main", read_root())
  596. Float timeout
  597. Element interrupt
  598. timeout = 0.0
  599. while (True):
  600. interrupt = input_timeout(timeout)
  601. if (value_eq(interrupt, "#EXIT#")):
  602. // Stop execution
  603. return True!
  604. if (element_neq(interrupt, read_root())):
  605. // Send out, as otherwise the client doesn't get a dialog
  606. output("Processed event, ready for more!")
  607. // Update the simulated time to the time of interrupt
  608. time_sim = time() - time_0
  609. dict_overwrite(data, "new_events", create_node())
  610. Element classes
  611. classes = dict_keys(data["classes"])
  612. while(set_len(classes) > 0):
  613. String class
  614. class = set_pop(classes)
  615. dict_overwrite(data["classes"][class], "events", data["classes"][class]["new_events"])
  616. dict_overwrite(data["classes"][class], "new_events", create_node())
  617. if (element_neq(interrupt, read_root())):
  618. // Got interrupt, so append it already
  619. set_add_node(data["classes"][class]["events"], create_tuple(interrupt, read_root()))
  620. // Else we timeout, and thus keep the time_sim
  621. dict_overwrite(data, "time_sim", time_sim)
  622. time_sim = step(model, data)
  623. if (float_gt(time_sim, data["time_sim"])):
  624. print_states(model, data)
  625. if (dict_len(data["classes"]) == 0):
  626. // No more active classes left: terminate!
  627. log("Finished SCCD execution")
  628. break!
  629. time_wallclock = time() - time_0
  630. timeout = time_sim - time_wallclock
  631. log("Pause for: " + cast_v2s(timeout))
  632. // We should never get here!
  633. return False!