Note
Even though these types are made for public consumption and usage should be encouraged/easily possible it should be noted that these may be moved out to new libraries at various points in the future (for example the FSM code may move to its own oslo supported automaton library at some point in the future [1]). If you are using these types without using the rest of this library it is strongly encouraged that you be a vocal proponent of getting these made into isolated libraries (as using these types in this manner is not the expected and/or desired usage).
Bases: exceptions.Exception
Exception raised when a frozen machine is modified.
Bases: taskflow.exceptions.TaskFlowException
Error raised when an action is attempted on a not inited machine.
Bases: object
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_iter(); one where external events 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.
Returns whether the state machine is in a terminal state.
Returns: | whether the state machine is in terminal state or not |
---|---|
Return type: | boolean |
Adds a given state to the state machine.
Parameters: |
|
---|
Adds a reaction that may get triggered by the given event & state.
Parameters: |
|
---|
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. 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 an allowed transition from start -> end for the given event.
Parameters: |
|
---|
Trigger a state change in response to the provided event.
Parameters: | event – event to be processed to cause a potential transition |
---|
Copies the current state machine.
NOTE(harlowja): the copy will be left in an uninitialized state.
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...
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.
Example:
>>> from taskflow.types import fsm
>>> f = fsm.FSM("sits")
>>> f.add_state("sits")
>>> f.add_state("barks")
>>> f.add_state("wags tail")
>>> f.add_transition("sits", "barks", "squirrel!")
>>> f.add_transition("barks", "wags tail", "gets petted")
>>> f.add_transition("wags tail", "sits", "gets petted")
>>> f.add_transition("wags tail", "barks", "squirrel!")
>>> print(f.pformat())
+-----------+-------------+-----------+----------+---------+
Start | Event | End | On Enter | On Exit
+-----------+-------------+-----------+----------+---------+
barks | gets petted | wags tail | |
sits[^] | squirrel! | barks | |
wags tail | gets petted | sits | |
wags tail | squirrel! | barks | |
+-----------+-------------+-----------+----------+---------+
Bases: object
A tiny pretty printing table (like prettytable/tabulate but smaller).
Creates simply formatted tables (with no special sauce):
>>> from taskflow.types import table
>>> tbl = table.PleasantTable(['Name', 'City', 'State', 'Country'])
>>> tbl.add_row(["Josh", "San Jose", "CA", "USA"])
>>> print(tbl.pformat())
+------+----------+-------+---------+
Name | City | State | Country
+------+----------+-------+---------+
Josh | San Jose | CA | USA
+------+----------+-------+---------+
Bases: exceptions.Exception
Exception raised when a frozen node is modified.
Bases: object
A n-ary node class that can be used to create tree structures.
Returns the node for an item if it exists in this node.
This will search not only this node but also any children nodes and finally if nothing is found then None is returned instead of a node object.
Parameters: | item – item to lookup. |
---|---|
Returns: | the node for an item if it exists in this node |
Recursively formats a node into a nice string representation.
Example:
>>> from taskflow.types import tree
>>> yahoo = tree.Node("CEO")
>>> yahoo.add(tree.Node("Infra"))
>>> yahoo[0].add(tree.Node("Boss"))
>>> yahoo[0][0].add(tree.Node("Me"))
>>> yahoo.add(tree.Node("Mobile"))
>>> yahoo.add(tree.Node("Mail"))
>>> print(yahoo.pformat())
CEO
|__Infra
| |__Boss
| |__Me
|__Mobile
|__Mail
[1] | See: https://review.openstack.org/#/c/141961 for a proposal to do this. |