State machine modelling, copied from TaskFlow project.
This work will be turned into a library.
See https://github.com/harlowja/automaton
This is being used in the implementation of:
http://specs.openstack.org/openstack/ironic-specs/specs/kilo/new-ironic-state-machine.html
-
class ironic.common.fsm.FSM(start_state=None)[source]
Bases: object
A finite state machine.
This class models a state machine, and expects an outside caller to
manually trigger the state changes one at a time by invoking process_event
-
add_state(state, on_enter=None, on_exit=None, target=None, terminal=None, stable=False)[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.
| Parameters: |
- stable – Use this to specify that this state is a stable/passive
state. A state must have been previously defined as
‘stable’ before it can be used as a ‘target’
- target – The target state for ‘state’ to go to. Before a state
can be used as a target it must have been previously
added and specified as ‘stable’
|
-
add_transition(start, end, event)[source]
Adds an allowed transition from start -> end for the given event.
-
copy(shallow=False)[source]
Copies the current state machine (shallow or deep).
NOTE(harlowja): the copy will be left in an uninitialized state.
- NOTE(harlowja): when a shallow copy is requested the copy will share
- the same transition table and state table as the
source; this can be advantageous if you have a machine
and transitions + states that is defined somewhere
and want to use copies to run with (the copies have
the current state that is different between machines).
-
initialize(state=None)[source]
Sets up the state machine.
sets the current state to the specified state, or start_state
if no state was specified..
-
is_valid_event(event)[source]
Check whether the event is actionable in the current state.
-
process_event(event)[source]
Trigger a state change in response to the provided event.