From 8af49b7f9132fda14476c07eb90bc95e86f1c330 Mon Sep 17 00:00:00 2001 From: erick Date: Sun, 15 Nov 2009 02:30:02 +0000 Subject: Added tests for the engine, added automatic transition support --- (limited to 'tutorius/engine.py') diff --git a/tutorius/engine.py b/tutorius/engine.py index b0a49a8..c945e49 100644 --- a/tutorius/engine.py +++ b/tutorius/engine.py @@ -6,6 +6,7 @@ from sugar.bundle.activitybundle import ActivityBundle from .vault import Vault from .TProbe import ProbeManager from .dbustools import save_args +from .tutorial import Tutorial, AutomaticTransitionEvent class TutorialRunner(object): @@ -36,17 +37,17 @@ class TutorialRunner(object): def start(self): self.setCurrentActivity() #Temp Hack until activity in events/actions - self.setState(self._tutorial.INIT) + self.enterState(self._tutorial.INIT) def stop(self): self.setCurrentActivity() #Temp Hack until activity in events/actions - self.setState(self._tutorial.END) + self.enterState(self._tutorial.END) self._teardownState() self._state = None def _handleEvent(self, next_state, event): - #FIXME sanity check - self.setState(next_state) + #FIXME sanity check, log event that was not installed and ignore + self.enterState(next_state) def _teardownState(self): if self._state is None: @@ -67,22 +68,51 @@ class TutorialRunner(object): if self._state is None: raise RuntimeError("Attempting to setupState without a state") + # Handle the automatic event + state_name = self._state + self._actions = self._tutorial.get_action_dict(self._state) transitions = self._tutorial.get_transition_dict(self._state) + for (event, next_state) in transitions.values(): + if isinstance(event, AutomaticTransitionEvent): + state_name = next_state + break + self._sEvents.add(self._pM.subscribe(event, save_args(self._handleEvent, next_state))) + for action in self._actions.values(): self._pM.install(action) - def setState(self, state_name): + return state_name + + def enterState(self, state_name): + """ + Starting from the state_name, the runner execute states until + no automatic transition are found and will wait for an external + event to occur. + + When entering the state, actions and events from the previous + state are respectively uninstalled and unsubscribed and actions + and events from the state_name will be installed and subscribed. + + @param state_name The name of the state to enter in + """ self.setCurrentActivity() #Temp Hack until activity in events/actions + + # Recursive base case if state_name == self._state: #Nothing to do return self._teardownState() self._state = state_name - self._setupState() + + # Recursively call the enterState in case there was an automatic + # transition in the state definition + self.enterState(self._setupState()) + + class Engine: -- cgit v0.9.1