automaton.machines.
State
(name, is_terminal=False, next_states=None, on_enter=None, on_exit=None)[source]¶Container that defines needed components of a single state.
Usage of this and the build()
make creating finite
state machines that much easier.
Variables: |
|
---|
automaton.machines.
FiniteMachine
(default_start_state=None)[source]¶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…
Effect
(reaction, terminal)¶The result of processing an event (cause and effect…)
reaction
¶Alias for field number 0
terminal
¶Alias for field number 1
add_reaction
(state, event, reaction, *args, **kwargs)[source]¶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.
add_state
(state, terminal=False, on_enter=None, on_exit=None)[source]¶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.
add_transition
(start, end, event, replace=False)[source]¶Adds an allowed transition from start -> end for the given event.
Parameters: |
|
---|
build
(state_space)[source]¶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.
copy
(shallow=False, unfreeze=False)[source]¶Copies the current state machine.
NOTE(harlowja): the copy will be left in an uninitialized state.
current_state
¶The current state the machine is in (or none if not initialized).
default_start_state
¶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.
events
¶Returns how many events exist.
initialize
(start_state=None)[source]¶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. |
---|
pformat
(sort=True, empty='.')[source]¶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.
process_event
(event)[source]¶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 |
states
¶Returns the state names.
terminated
¶Returns whether the state machine is in a terminal state.
automaton.machines.
HierarchicalFiniteMachine
(default_start_state=None)[source]¶A fsm that understands how to run in a hierarchical mode.
Effect
(reaction, terminal, machine)¶The result of processing an event (cause and effect…)
machine
¶Alias for field number 2
reaction
¶Alias for field number 0
terminal
¶Alias for field number 1
add_state
(state, terminal=False, on_enter=None, on_exit=None, machine=None)[source]¶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()
.
initialize
(start_state=None, nested_start_state_fetcher=None)[source]¶Sets up the state machine (sets current state to start state…).
Parameters: |
|
---|
nested_machines
¶Dictionary of all nested state machines this machine may use.
automaton.runners.
Runner
(machine)[source]¶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).
run_iter
(event, initialize=True)[source]¶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…
automaton.runners.
FiniteRunner
(machine)[source]¶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).
automaton.runners.
HierarchicalRunner
(machine)[source]¶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).
run_iter
(event, initialize=True)[source]¶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…
automaton.converters.pydot.
convert
(machine, graph_name, graph_attrs=None, node_attrs_cb=None, edge_attrs_cb=None, add_start_state=True, name_translations=None)[source]¶Translates the state machine into a pydot graph.
Parameters: |
|
---|
automaton.exceptions.
AutomatonException
[source]¶Base class for most exceptions emitted from this library.
automaton.exceptions.
FrozenMachine
[source]¶Exception raised when a frozen machine is modified.
automaton.exceptions.
InvalidState
[source]¶Raised when a invalid state transition is attempted while executing.
automaton.exceptions.
NotFound
[source]¶Raised when some entry in some object doesn’t exist.
automaton.exceptions.
NotInitialized
[source]¶Error raised when an action is attempted on a not inited machine.
Except where otherwise noted, this document is licensed under Creative Commons Attribution 3.0 License. See all OpenStack Legal Documents.