This module provides a simple but powerful way to define testable, hierarchical finite state machines. You should know how a state machine works. This implementation provides following features:
References:
Todo
tutorial (explaining the abilities, entry/exit/actions/guard, different kinds of transitions)
Todo
set log level through event handling method?? so one could debug one instance of a statemachine at once.
Todo
python debug mode should log what it does but respect the debug level (?)
Versioning scheme based on: http://en.wikipedia.org/wiki/Versioning#Designating_development_stage
+-- api change, probably incompatible with older versions
| +-- enhancements but no api change
| |
major.minor[.build[.revision]]
|
+-|* 0 for alpha (status)
|* 1 for beta (status)
|* 2 for release candidate
|* 3 for (public) release
Changed in version 2.0.2.0: introduced __versionnumber__ because __version__ should be a string according to PEP8. __versionnumber__ is a tuple containing int as used in the versioning scheme. This way its comparable out of the box, e.g. __versionnumber__ >= (2, 2, 3)
Bases: unittest.TestCase
Base TestCase that already defines test code for testing state machines build using an events and action interface (see: http://accu.org/index.php/journals/1548)
Bases: object
This is a class that records the names of the functions called on it. Instead of writing a TestActions class, that records which action was activated, this class can be used. Just use the method names of the Action interface to compare the actually called method with the expected method in the tests.
Instancevariable : | |
---|---|
|
Bases: object
A TestVector is basically the data container needed to test one transition.
Parameters : |
|
---|
Test one transition.
Parameters : |
|
---|
Test a sequence of transitions by passing in a sequence of event and checking the actions.
Parameters : |
|
---|
Bases: object
BaseState from which all hirarchical states should inherit. :Note: The state itself is ‘stateless’.
Parameters : |
|
---|
Bases: exceptions.Exception
Exception raised if the initial state is not replaced.
Bases: exceptions.Exception
Exception raised if the initial state is not set.
Bases: exceptions.Exception
Exception is raised if initial state is already set.
Bases: exceptions.Exception
Exception raised when a child has already a parent set
Bases: exceptions.Exception
Exception raised if the replaced initial state is not a child.
Bases: exceptions.Exception
Exception raised if the set parent is not the same state containint it as child
Adds another state as child to this state.
Parameters : |
|
---|---|
Raises : | ParentAlreadySetError if the childs parent is already set. |
Raises : | InitialStateAlreadySetError if another initial state has already been defined. |
Checks the consistency of the state hierarchy. It checks mainly two things:
- if the initial state is set for each state having a child or
children, raises InitialNotSetError otherwise
- if each child of a state has the parent attribute set to that
state, raises WrongParentError otherwise
Deprecated since version 1.0.3.0: Use Structure.check_consistency() instead.
Raises : | InitialNotSetError if no initial state has been set when this state has children. |
---|---|
Raises : | WrongParentError if a child has not the parent set where it is a child. |
Checks if a state has a certain state as a child.
Parameters : |
|
---|---|
Returns: | bool |
Checks if this state is a child state of a parent state.
Parameters : |
|
---|---|
Returns: | bool |
Removes a child state. If the removed child state was the initial state it has to be replaced.
Parameters : |
|
---|---|
Raises : | InitialNotReplacedError if the initial state is removed but no other inital state is defined. |
Raises : | ReplacementStateIsNotChildError if the initial replacement isn’t a child of this state. |
Bases: exceptions.Exception
Exception raised if the state is not known in the structure.
Bases: object
This is the class holding the state machine structure, e.g. the number of states and their relationship (hierarchy) and its transitions in between them.
Ths is also the code that is shared by many instances of the same statemachine.
Parameters : |
|
---|
Bases: exceptions.Exception
Exception raised when the event is already defined for that state
Bases: exceptions.Exception
Exception raised when the parent is unkown.
Bases: exceptions.Exception
Exception raised when the parent is missing or the root has already been set.
Bases: exceptions.Exception
Exception raised when another state has the same state identifier.
Add a new node representing a state to the structure.
Parameters : |
|
---|---|
Raises : | ParentUnkownError if the parent state identifier is not already known. |
Raises : | RootAlreadySetOrParentMissingError if a second root node is added (maybe the parent is missing). |
Raises : | StateIdentifierAlreadyUsed if the chosen state identifier is already in use. |
Add a transition between two states for a certain event.
Parameters : |
|
---|---|
Raises : | StateUnknownError if either the state- or the target-identifier is not known. |
Raises : | EventAlreadyDefinedError if this event is already defined for that state. |
Checks the consistency of the state hierarchy. It checks mainly two things:
- if the initial state is set for each state having a child or
children, raises InitialNotSetError otherwise
- if each child of a state has the parent attribute set to that
state, raises WrongParentError otherwise
New in version 2.0.2.0.
Optimizes the event processing of the state machine. Call this method before you pass the structure to the constructor to create a state machine instance.
Note
It is not recommended to alter the structure after a call to this method, althought now it will just update the optimization.
Changed in version 2.0.2.0: Does not raise any exception anymore if called multiple times, it rebuilts internal structure used for optimization.
Bases: object
Deprecated since version 1.0.3.0: (use SympleHFSM instead!)
Bases: object
Todo
should transition.action be able to return something to the caller?
Todo
should it be possible to pass in arguments for the transition action through the event handler method?
Base state machine logic. It implements the state transition logic.
Parameters : |
|
---|
New in version 2.0.2.0.
Bases: exceptions.Exception
Exception raised if init is calle more than once.
New in version 2.0.2.0: Raised when init is called multiple times.
Bases: exceptions.Exception
Exception raised if it is attemped to process an event before init has been called.
Bases: exceptions.Exception
Exception raised if an event is already processing.
Current state identifier or None if the state machine is not initialized
Exits the state machine. Starting from the current_state it calls exit along the parent attribute on each state until the root state is exited.
Handles the event and does a state change if needed. Raises a ‘ReentrantEventException’ if it is currently processing an event.
Parameters : |
|
---|
Initialize the state machine. It descents along the ‘initial’ attribute of the states and sets the current_state accordingly.
Parameters : |
|
---|---|
Raises : | InitAlreadyCalledError if calle more than once. |
Set the state directly as the current state without calling any entry or exit or any other events on any state. Don’t use it unless you need to (like initializing). Use with caution. Raises a ‘ReentrantEventException’ if it is currently processing an event. If the state is not known, then a ‘StateUnkownError’ is raised.
Note : | No actions are called! e.g.: exit, entry, transition action are not called, use init() instead! |
---|---|
Parameters : |
|
Raises : | StateUnknownError if there is no state defined for given state_identifier. |
---|---|
Raises : | ReentrantEventException if this method is called during a event is handled. |
Bases: object
This class holds the data needed for a transition.
Represents the transition between (composite) states (just the arrow in the state chart). The transition itself is ‘stateless’.
Parameters : |
|
---|