SCCD_execute.alc 24 KB

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