SCCD_execute.alc 27 KB

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