SCCD_execute.alc 26 KB

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