Belle II Software  release-06-00-14
Machine Class Reference
Inheritance diagram for Machine:

Public Member Functions

def __init__ (self, states=None, initial_state="default_initial")
 
def add_state (self, state, enter=None, exit=None)
 
def initial_state (self)
 
def initial_state (self, state)
 
def state (self)
 
def state (self, state)
 
def add_transition (self, trigger, source, dest, conditions=None, before=None, after=None)
 
def __getattr__ (self, name, **kwargs)
 
def get_transitions (self, source)
 
def get_transition_dict (self, state, transition)
 
def save_graph (self, filename, graphname)
 

Static Public Member Functions

def default_condition (**kwargs)
 

Public Attributes

 states
 Valid states for this machine.
 
 initial_state
 Pointless docstring since it's a property.
 
 transitions
 Allowed transitions between states.
 
 state
 Current State of machine.
 

Private Member Functions

def _trigger (self, transition_name, transition_dict, **kwargs)
 

Static Private Member Functions

def _callback (func, **kwargs)
 

Private Attributes

 _initial_state
 Actual attribute holding initial state for this machine.
 
 _state
 Actual attribute holding the Current state. More...
 

Detailed Description

Parameters:
  states (list[str]): A list of possible states of the machine.
  initial_state (str):

Base class for a final state machine wrapper.
Implements the framwork that a more complex machine can inherit from.

The `transitions` attribute is a dictionary of trigger name keys, each value of
which is another dictionary of 'source' states, 'dest' states, and 'conditions'
methods. 'conditions' should be a list of callables or a single one. A transition is
valid if it goes from an allowed state to an allowed state.
Conditions are optional but must be a callable that returns True or False based
on some state of the machine. They cannot have input arguments currently.

Every condition/before/after callback function MUST take ``**kwargs`` as the only
argument (except ``self`` if it's a class method). This is because it's basically
impossible to determine which arguments to pass to which functions for a transition.
Therefore this machine just enforces that every function should simply take ``**kwargs``
and use the dictionary of arguments (even if it doesn't need any arguments).

This also means that if you call a trigger with arguments e.g. ``machine.walk(speed=5)``
you MUST use the keyword arguments rather than positional ones. So ``machine.walk(5)``
will *not* work.

Definition at line 138 of file state_machines.py.

Constructor & Destructor Documentation

◆ __init__()

def __init__ (   self,
  states = None,
  initial_state = "default_initial" 
)
Basic Setup of states and initial_state.

Reimplemented in AlgorithmMachine.

Definition at line 165 of file state_machines.py.

Member Function Documentation

◆ __getattr__()

def __getattr__ (   self,
  name,
**  kwargs 
)
Allows us to create a new method for each trigger on the fly.
If there is no trigger name in the machine to match, then the normal
AttributeError is called.

Definition at line 300 of file state_machines.py.

◆ _callback()

def _callback (   func,
**  kwargs 
)
staticprivate
Calls a condition/before/after.. function using arguments passed (or not).

Definition at line 336 of file state_machines.py.

◆ _trigger()

def _trigger (   self,
  transition_name,
  transition_dict,
**  kwargs 
)
private
Runs the transition logic. Callbacks are evaluated in the order:
conditions -> before -> <new state set here> -> after.

Definition at line 312 of file state_machines.py.

◆ add_state()

def add_state (   self,
  state,
  enter = None,
  exit = None 
)
Adds a single state to the list of possible ones.
Should be a unique string or a State object with a unique name.

Definition at line 187 of file state_machines.py.

◆ add_transition()

def add_transition (   self,
  trigger,
  source,
  dest,
  conditions = None,
  before = None,
  after = None 
)
Adds a single transition to the dictionary of possible ones.
Trigger is the method name that begins the transtion between the
source state and the destination state.

The condition is an optional function that returns True or False
depending on the current state/input.

Definition at line 258 of file state_machines.py.

◆ default_condition()

def default_condition ( **  kwargs)
static
Method to always return True.

Definition at line 252 of file state_machines.py.

◆ get_transition_dict()

def get_transition_dict (   self,
  state,
  transition 
)
Returns the transition dictionary for a state and transition out of it.

Definition at line 353 of file state_machines.py.

◆ get_transitions()

def get_transitions (   self,
  source 
)
Returns allowed transitions from a given state.

Definition at line 342 of file state_machines.py.

◆ initial_state() [1/2]

def initial_state (   self)
The initial state of the machine. Needs a special property to prevent trying to run on_enter callbacks when set.

Definition at line 203 of file state_machines.py.

◆ initial_state() [2/2]

def initial_state (   self,
  state 
)
 

Definition at line 210 of file state_machines.py.

◆ save_graph()

def save_graph (   self,
  filename,
  graphname 
)
Does a simple dot file creation to visualise states and transiitons.

Definition at line 364 of file state_machines.py.

◆ state() [1/2]

def state (   self)
        The current state of the machine. Actually a `property` decorator. It will call the exit method of the
        current state and enter method of the new one. To get around the behaviour e.g. for setting initial states,
        either use the `initial_state` property or directly set the _state attribute itself (at your own risk!).

Definition at line 221 of file state_machines.py.

◆ state() [2/2]

def state (   self,
  state 
)
 

Definition at line 230 of file state_machines.py.

Member Data Documentation

◆ _state

_state
private

Actual attribute holding the Current state.

Current state (private)

Definition at line 183 of file state_machines.py.


The documentation for this class was generated from the following file: