SCCD_execute.alc 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. include "primitives.alh"
  2. include "modelling.alh"
  3. include "object_operations.alh"
  4. include "utils.alh"
  5. include "random.alh"
  6. Void function print_states(model : Element, data : Element):
  7. Element classes
  8. Element states
  9. Element class
  10. String state
  11. log("Current states:")
  12. classes = set_copy(data["classes"])
  13. while (read_nr_out(classes) > 0):
  14. class = set_pop(classes)
  15. log(string_join(string_join(string_join(" ", class["ID"]), " : "), read_attribute(model, class["type"], "name")))
  16. states = set_copy(class["states"])
  17. while (read_nr_out(states) > 0):
  18. state = set_pop(states)
  19. log(string_join(" ", read_attribute(model, state, "name")))
  20. return!
  21. Element function filter(model : Element, set : Element, attribute_name : String, attribute_value : Element):
  22. Element keys
  23. String key
  24. Element result
  25. result = create_node()
  26. while (read_nr_out(set) > 0):
  27. key = set_pop(set)
  28. if (value_eq(read_attribute(model, key, attribute_name), attribute_value)):
  29. set_add(result, key)
  30. return result!
  31. Element function expand_state(model : Element, state : String):
  32. String t
  33. t = read_type(model, state)
  34. if (t == "SCCD/CompositeState"):
  35. // Recurse further in the composite
  36. return expand_composite_state(model, state)!
  37. elif (t == "SCCD/ParallelState"):
  38. // Split up all components
  39. return expand_parallel_state(model, state)!
  40. else:
  41. // Probably just an atomic, so return this one only
  42. Element result
  43. result = create_node()
  44. set_add(result, state)
  45. return result!
  46. Element function expand_composite_state(model : Element, composite_state : String):
  47. // Resolve all initial states from a single composite state
  48. String initial
  49. // Fetch the initial state
  50. initial = set_pop(filter(model, allAssociationDestinations(model, composite_state, "SCCD/composite_children"), "isInitial", True))
  51. log("Got initial state: " + initial)
  52. // Expand the initial state, depending on what it is
  53. return expand_state(model, initial)!
  54. Element function expand_parallel_state(model : Element, parallel_state : String):
  55. // Resolve all initial states from a single parallel state
  56. Element children
  57. Element result
  58. Element expanded_children
  59. children = allAssociationDestinations(model, parallel_state, "SCCD/parallel_children")
  60. result = create_node()
  61. while (read_nr_out(children) > 0):
  62. set_merge(result, expand_state(model, set_pop(children)))
  63. return result!
  64. Void function start_class(model : Element, data : Element, class : String):
  65. // Start up the class and assign its initial state to it
  66. // Create the data structure for a running class
  67. Element class_handle
  68. class_handle = create_node()
  69. dict_add(class_handle, "type", class)
  70. dict_add(class_handle, "ID", cast_id2s(create_node()))
  71. // Add the current state of the class
  72. String initial_state
  73. // Should only be one behaviour linked to it!
  74. initial_state = set_pop(allAssociationDestinations(model, class, "SCCD/behaviour"))
  75. dict_add(class_handle, "states", expand_state(model, initial_state))
  76. set_add(data["classes"], class_handle)
  77. return!
  78. Element function get_enabled_transitions(model : Element, state : String, interrupt : Element):
  79. // Returns all enabled transitions
  80. // TODO ignore conditions and afters
  81. Element result
  82. Element to_filter
  83. String attr
  84. String transition
  85. result = create_node()
  86. to_filter = allOutgoingAssociationInstances(model, state, "SCCD/transition")
  87. while (read_nr_out(to_filter) > 0):
  88. transition = set_pop(to_filter)
  89. attr = read_attribute(model, transition, "event")
  90. if (element_eq(attr, read_root())):
  91. // No event defined, so is fine
  92. set_add(result, transition)
  93. elif (value_eq(attr, interrupt)):
  94. // Event defined, and that is the one of the event we got in
  95. set_add(result, transition)
  96. return result!
  97. Float function step_class(model : Element, data : Element, class : Element, interrupt : Element):
  98. // Find enabled transitions in a class and execute it, updating the state
  99. // Iterate over all current states, searching for enabled transitions
  100. // Search for enabled transitions in higher levels as well!
  101. Element states
  102. Element new_states
  103. String state
  104. Element transitions
  105. String transition
  106. log(string_join("Stepping ", class["ID"]))
  107. states = set_copy(class["states"])
  108. new_states = create_node()
  109. while (read_nr_out(states) > 0):
  110. state = set_pop(states)
  111. // Fetch transitions in this state specifically (NO parent)
  112. transitions = get_enabled_transitions(model, state, interrupt)
  113. log("Got enabled transitions: " + cast_v2s(read_nr_out(transitions)))
  114. if (read_nr_out(transitions) != 0):
  115. // Found an enabled transition, so store that one
  116. transition = random_choice(transitions)
  117. log("Executing enabled transition: " + cast_v2s(read_attribute(model, transition, "name")))
  118. set_add(new_states, readAssociationDestination(model, transition))
  119. log("Found new state: " + cast_v2s(read_attribute(model, readAssociationDestination(model, transition), "name")))
  120. else:
  121. // Try going to the parent
  122. // TODO
  123. // Nothing found, so stay in the current state
  124. set_add(new_states, state)
  125. // Update states
  126. dict_overwrite(class, "states", new_states)
  127. return 1.0!
  128. Float function step(model : Element, data : Element, interrupt : Element):
  129. // Step through all classes
  130. Element classes
  131. Element class
  132. Float t_min
  133. Float t_class
  134. t_min = 99999.0
  135. classes = set_copy(data["classes"])
  136. while (read_nr_out(classes) > 0):
  137. class = set_pop(classes)
  138. t_class = step_class(model, data, class, interrupt)
  139. if (t_class < t_min):
  140. t_min = t_class
  141. return t_min!
  142. Boolean function main(model : Element):
  143. // Executes the provided SCCD model
  144. Element data
  145. data = create_node()
  146. dict_add(data, "classes", create_node())
  147. // Prepare for input
  148. output("Ready for input!")
  149. // Find initial
  150. String default_class
  151. default_class = set_pop(filter(model, allInstances(model, "SCCD/Class"), "default", True))
  152. log("Found default class: " + default_class)
  153. // Start up the default class
  154. start_class(model, data, default_class)
  155. Float timeout
  156. Element interrupt
  157. timeout = 0.0
  158. while (True):
  159. print_states(model, data)
  160. interrupt = input_timeout(timeout)
  161. if (value_eq(interrupt, "#EXIT#")):
  162. // Stop execution
  163. return True!
  164. if (element_neq(interrupt, read_root())):
  165. // Got interrupt
  166. log("Got event: " + cast_v2s(interrupt))
  167. output("Processed event, ready for more!")
  168. timeout = step(model, data, interrupt)
  169. return True!