Add FSM.is_stable() method
This adds an FSM.is_stable(state) method which returns True if the state is stable and False otherwise. Invalid states will cause an exception to be raised. Change-Id: Ic6a3c937be9370477fdb1fe2c462f6672497f838
This commit is contained in:
@@ -65,6 +65,18 @@ class FSM(machines.FiniteMachine):
|
|||||||
def target_state(self):
|
def target_state(self):
|
||||||
return self._target_state
|
return self._target_state
|
||||||
|
|
||||||
|
def is_stable(self, state):
|
||||||
|
"""Is the state stable?
|
||||||
|
|
||||||
|
:param state: the state of interest
|
||||||
|
:raises: InvalidState if the state is invalid
|
||||||
|
:returns True if it is a stable state; False otherwise
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
return self._states[state]['stable']
|
||||||
|
except KeyError:
|
||||||
|
raise excp.InvalidState(_("State '%s' does not exist") % state)
|
||||||
|
|
||||||
@_translate_excp
|
@_translate_excp
|
||||||
def add_state(self, state, on_enter=None, on_exit=None,
|
def add_state(self, state, on_enter=None, on_exit=None,
|
||||||
target=None, terminal=None, stable=False):
|
target=None, terminal=None, stable=False):
|
||||||
@@ -110,7 +122,7 @@ class FSM(machines.FiniteMachine):
|
|||||||
if target not in self._states:
|
if target not in self._states:
|
||||||
raise excp.InvalidState(
|
raise excp.InvalidState(
|
||||||
_("Target state '%s' does not exist") % target)
|
_("Target state '%s' does not exist") % target)
|
||||||
if not self._states[target]['stable']:
|
if not self.is_stable(target):
|
||||||
raise excp.InvalidState(
|
raise excp.InvalidState(
|
||||||
_("Target state '%s' is not a 'stable' state") % target)
|
_("Target state '%s' is not a 'stable' state") % target)
|
||||||
|
|
||||||
|
|||||||
@@ -30,6 +30,15 @@ class FSMTest(base.TestCase):
|
|||||||
m.add_transition('wakeup', 'working', 'walk')
|
m.add_transition('wakeup', 'working', 'walk')
|
||||||
self.fsm = m
|
self.fsm = m
|
||||||
|
|
||||||
|
def test_is_stable(self):
|
||||||
|
self.assertTrue(self.fsm.is_stable('working'))
|
||||||
|
|
||||||
|
def test_is_stable_not(self):
|
||||||
|
self.assertFalse(self.fsm.is_stable('daydream'))
|
||||||
|
|
||||||
|
def test_is_stable_invalid_state(self):
|
||||||
|
self.assertRaises(excp.InvalidState, self.fsm.is_stable, 'foo')
|
||||||
|
|
||||||
def test_target_state_stable(self):
|
def test_target_state_stable(self):
|
||||||
# Test to verify that adding a new state with a 'target' state pointing
|
# Test to verify that adding a new state with a 'target' state pointing
|
||||||
# to a 'stable' state does not raise an exception
|
# to a 'stable' state does not raise an exception
|
||||||
|
|||||||
Reference in New Issue
Block a user