Container that defines needed components of a single state.
Usage of this and the build() make creating finite state machines that much easier.
Variables: |
|
---|
A finite state machine.
This state machine can be used to automatically run a given set of transitions and states in response to events (either from callbacks or from generator/iterator send() values, see PEP 342). On each triggered event, a on_enter and on_exit callback can also be provided which will be called to perform some type of action on leaving a prior state and before entering a new state.
NOTE(harlowja): reactions will only be called when the generator/iterator from run_iter() does not send back a new event (they will always be called if the run() method is used). This allows for two unique ways (these ways can also be intermixed) to use this state machine when using run(); one where external event trigger the next state transition and one where internal reaction callbacks trigger the next state transition. The other way to use this state machine is to skip using run() or run_iter() completely and use the process_event() method explicitly and trigger the events via some external functionality/triggers...
The result of processing an event (cause and effect...)
Alias for field number 0
Alias for field number 1
Returns if this state exists in the machines known states.
Adds a reaction that may get triggered by the given event & state.
Reaction callbacks may (depending on how the state machine is ran) be used after an event is processed (and a transition occurs) to cause the machine to react to the newly arrived at stable state.
These callbacks are expected to accept three default positional parameters (although more can be passed in via args and **kwargs, these will automatically get provided to the callback when it is activated *ontop of the three default). The three default parameters are the last stable state, the new stable state and the event that caused the transition to this new stable state to be arrived at.
The expected result of a callback is expected to be a new event that the callback wants the state machine to react to. This new event may (depending on how the state machine is ran) get processed (and this process typically repeats) until the state machine reaches a terminal state.
Adds a given state to the state machine.
The on_enter and on_exit callbacks, if provided will be expected to take two positional parameters, these being the state being exited (for on_exit) or the state being entered (for on_enter) and a second parameter which is the event that is being processed that caused the state transition.
Adds an allowed transition from start -> end for the given event.
Parameters: |
|
---|
Builds a machine from a state space listing.
Each element of this list must be an instance of State or a dict with equivalent keys that can be used to construct a State instance.
Copies the current state machine.
NOTE(harlowja): the copy will be left in an uninitialized state.
The current state the machine is in (or none if not initialized).
Sets the default start state that the machine should use.
NOTE(harlowja): this will be used by initialize but only if that function is not given its own start_state that overrides this default.
Sets up the state machine (sets current state to start state...).
Parameters: | start_state – explicit start state to use to initialize the state machine to. If None is provided then the machine’s default start state will be used instead. |
---|
Check whether the event is actionable in the current state.
Pretty formats the state + transition table into a string.
NOTE(harlowja): the sort parameter can be provided to sort the states and transitions by sort order; with it being provided as false the rows will be iterated in addition order instead.
Trigger a state change in response to the provided event.
Returns: | Effect this is either a FiniteMachine.Effect or an Effect from a subclass of FiniteMachine. See the appropriate named tuple for a description of the actual items in the tuple. For example, FiniteMachine.Effect‘s first item is reaction: one could invoke this reaction’s callback to react to the new stable state. |
---|---|
Return type: | namedtuple |
A fsm that understands how to run in a hierarchical mode.
The result of processing an event (cause and effect...)
Alias for field number 2
Alias for field number 0
Alias for field number 1
Adds a given state to the state machine.
Parameters: | machine (FiniteMachine) – the nested state machine that will be transitioned into when this state is entered |
---|
Further arguments are interpreted as for FiniteMachine.add_state().
Sets up the state machine (sets current state to start state...).
Parameters: |
|
---|
Machine runner used to run a state machine.
Only one runner per machine should be active at the same time (aka there should not be multiple runners using the same machine instance at the same time).
Returns a iterator/generator that will run the state machine.
NOTE(harlowja): only one runner iterator/generator should be active for a machine, if this is not observed then it is possible for initialization and other local state to be corrupted and cause issues when running...
Finite machine runner used to run a finite machine.
Only one runner per machine should be active at the same time (aka there should not be multiple runners using the same machine instance at the same time).
Hierarchical machine runner used to run a hierarchical machine.
Only one runner per machine should be active at the same time (aka there should not be multiple runners using the same machine instance at the same time).
Returns a iterator/generator that will run the state machine.
This will keep a stack (hierarchy) of machines active and jumps through them as needed (depending on which machine handles which event) during the running lifecycle.
NOTE(harlowja): only one runner iterator/generator should be active for a machine hierarchy, if this is not observed then it is possible for initialization and other local state to be corrupted and causes issues when running...
Translates the state machine into a pydot graph.
Parameters: |
|
---|
Base class for most exceptions emitted from this library.
Exception raised when a frozen machine is modified.
Raised when a invalid state transition is attempted while executing.
Raised when some entry in some object doesn’t exist.
Error raised when an action is attempted on a not inited machine.