Hierarchical state machine with RSB support based on transitions (version >= 0.4.0).
Features include:
- full lifecycle state management
- automatic listener management for transitions
- and much more
The basic usage of rsbhsm
does not differ much from other transition state machines.
from rsbhsm import RSBHierarchicalStateMachine as Machine
class Model(object):
def well_trained():
return True
model = Model()
states = ['stand', 'walk', 'run'] # state as strings
transitions = [
['acc', 'stand', 'walk'], # array transition
{'trigger':'acc', 'source':'walk', 'dest': 'run'}, # or dictionary
# with conditions and/or unless
{'trigger': 'spring', 'source':'stand', 'dest': 'run', 'conditions':'well_trained'},
# also direct function pointers
{'trigger': 'break_required', 'source':'run', 'dest': 'stand', 'unless':model.well_trained},
# chained processing; since previous transitions fails, this one will be executed
{'trigger': 'break_required', 'source':'run', 'dest': 'walk'}
]
machine = Machine(model, states=states, transitions=transitions)
The additional keyword for RSBState
is action
:
from my.action import AnotherClass
states = [
{'name': 'run', 'action': 'my.action.Class'},
{'name': 'sit', 'action': AnotherClass}
]
If an RSBState
is entered, an object of 'my.action.Class' is created and destroyed whenever it is exited.
This class must implement an enter
and exit
method which will be called according to the event.
A minimal action can look like this:
class BaseAction(object):
def __init__(self, *args, **kwargs):
self.model = kwargs['model']
def enter(self, *args, **kwargs):
pass
def exit(self, *args, **kwargs):
pass
The additional keywords for RSBTransition
are scope
and type
:
transitions = [
{'trigger':'acc', 'source':'stand', 'dest':'walk', 'scope':'/foo/bar/baz', 'type': 'rst.generic.Value'}
]
scope
defines the RSB scope and type
the RST
message type as a string. If a native type is required, use 'type':str
or similar.
A listener on this scope will be created if and only if the transition is possible from the current state.
Additionally, the listener will be destroyed if the transition is no longer valid.