Allow the hierarchical machine to provide back the nested machines
For those that wish to introspect the hierarchical machines that are contained inside of it; provide back a way to get this information.
This commit is contained in:
parent
230ade3c5e
commit
468a9923b5
@ -409,6 +409,7 @@ class HierarchicalFiniteMachine(FiniteMachine):
|
||||
super(HierarchicalFiniteMachine, self).__init__(
|
||||
default_start_state=default_start_state)
|
||||
self._runner = _HierarchicalRunner(self)
|
||||
self._nested_machines = {}
|
||||
|
||||
@classmethod
|
||||
def _effect_builder(cls, new_state, event):
|
||||
@ -424,6 +425,16 @@ class HierarchicalFiniteMachine(FiniteMachine):
|
||||
state, terminal=terminal, on_enter=on_enter, on_exit=on_exit)
|
||||
if machine is not None:
|
||||
self._states[state]['machine'] = machine
|
||||
self._nested_machines[state] = machine
|
||||
|
||||
def copy(self, shallow=False, unfreeze=False):
|
||||
c = super(HierarchicalFiniteMachine, self).copy(shallow=shallow,
|
||||
unfreeze=unfreeze)
|
||||
if shallow:
|
||||
c._nested_machines = self._nested_machines
|
||||
else:
|
||||
c._nested_machines = self._nested_machines.copy()
|
||||
return c
|
||||
|
||||
def initialize(self, start_state=None):
|
||||
super(HierarchicalFiniteMachine, self).initialize(
|
||||
@ -432,6 +443,10 @@ class HierarchicalFiniteMachine(FiniteMachine):
|
||||
if 'machine' in data:
|
||||
data['machine'].initialize()
|
||||
|
||||
@property
|
||||
def nested_machines(self):
|
||||
return self._nested_machines
|
||||
|
||||
|
||||
class _HierarchicalRunner(object):
|
||||
"""Hierarchical machine runner used to run a hierarchical machine."""
|
||||
|
@ -310,6 +310,10 @@ class HFSMTest(FSMTest):
|
||||
dialer.add_transition("talk", "hangup", 'hangup')
|
||||
return dialer, number_calling
|
||||
|
||||
def test_nested_machines(self):
|
||||
dialer, _number_calling = self._make_phone_dialer()
|
||||
self.assertEqual(1, len(dialer.nested_machines))
|
||||
|
||||
def test_phone_dialer_iter(self):
|
||||
dialer, number_calling = self._make_phone_dialer()
|
||||
self.assertEqual(0, len(number_calling))
|
||||
|
Loading…
x
Reference in New Issue
Block a user