From dbc890f928ecfea3ee306b5bd0c7053eeb5070c9 Mon Sep 17 00:00:00 2001 From: Joshua Harlow Date: Thu, 18 Dec 2014 22:22:21 -0800 Subject: [PATCH] Correctly trigger 'on_exit' of starting/initial state Instead of not calling the 'on_exit' of the initialized and/or starting state we should make an attempt to call it if a function exists/was provided. This function will be called on the first event to be processed (which will cause the state machine to transition out of the starting state to a new stable state). Fixes bug 1404124 Change-Id: I037439313f9071af23c0859a62832d735f9abcd8 --- taskflow/tests/unit/test_types.py | 4 +++- taskflow/types/fsm.py | 7 ++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/taskflow/tests/unit/test_types.py b/taskflow/tests/unit/test_types.py index 395d7d9b..28b57251 100644 --- a/taskflow/tests/unit/test_types.py +++ b/taskflow/tests/unit/test_types.py @@ -304,7 +304,9 @@ class FSMTest(test.TestCase): m.process_event('fall') self.assertEqual([('down', 'beat'), ('up', 'jump'), ('down', 'fall')], enter_transitions) - self.assertEqual([('down', 'jump'), ('up', 'fall')], exit_transitions) + self.assertEqual( + [('start', 'beat'), ('down', 'jump'), ('up', 'fall')], + exit_transitions) def test_run_iter(self): up_downs = [] diff --git a/taskflow/types/fsm.py b/taskflow/types/fsm.py index 9cf94d7b..2519e840 100644 --- a/taskflow/types/fsm.py +++ b/taskflow/types/fsm.py @@ -196,7 +196,12 @@ class FSM(object): if self._states[self._start_state]['terminal']: raise excp.InvalidState("Can not start from a terminal" " state '%s'" % (self._start_state)) - self._current = _Jump(self._start_state, None, None) + # No on enter will be called, since we are priming the state machine + # and have not really transitioned from anything to get here, we will + # though allow 'on_exit' to be called on the event that causes this + # to be moved from... + self._current = _Jump(self._start_state, None, + self._states[self._start_state]['on_exit']) def run(self, event, initialize=True): """Runs the state machine, using reactions only."""