Python YASMIN (Yet Another State MachINe)
Loading...
Searching...
No Matches
yasmin.state_machine.StateMachine Class Reference
Inheritance diagram for yasmin.state_machine.StateMachine:
Collaboration diagram for yasmin.state_machine.StateMachine:

Public Member Functions

None __init__ (self, Set[str] outcomes)
 
None add_state (self, str name, State state, Dict[str, str] transitions=None)
 
None set_start_state (self, str state_name)
 
str get_start_state (self)
 
Dict[str, Union[State, Dict[str, str]]] get_states (self)
 
str get_current_state (self)
 
None add_start_cb (self, Callable cb, List[Any] args=None)
 
None add_transition_cb (self, Callable cb, List[Any] args=None)
 
None add_end_cb (self, Callable cb, List[Any] args=None)
 
None validate (self, bool strict_mode=False)
 
str execute (self, Blackboard blackboard)
 
None cancel_state (self)
 
str __str__ (self)
 
- Public Member Functions inherited from yasmin.state.State
str __call__ (self, Blackboard blackboard=None)
 
bool is_canceled (self)
 
bool is_running (self)
 
Set[str] get_outcomes (self)
 

Protected Member Functions

None _call_start_cbs (self, Blackboard blackboard, str start_state)
 
None _call_transition_cbs (self, Blackboard blackboard, str from_state, str to_state, str outcome)
 
None _call_end_cbs (self, Blackboard blackboard, str outcome)
 

Protected Attributes

dict _states = {}
 A dictionary mapping state names to their corresponding state objects and transitions.
 
str _start_state = None
 The name of the initial state of the state machine.
 
bool _validated = False
 A flag indicating whether the state machine has been validated.
 
 _outcomes
 
- Protected Attributes inherited from yasmin.state.State
Set _outcomes = set()
 A set of valid outcomes for this state.
 
bool _running = False
 A flag indicating whether the state is currently running.
 
bool _canceled = False
 A flag indicating whether the state has been canceled.
 

Private Attributes

str __current_state = None
 The name of the current state being executed.
 
Lock __current_state_lock = Lock()
 A threading lock to manage access to the current state.
 
List[ __start_cbs
 A list of callbacks to call when the state machine starts.
 
List[ __transition_cbs
 A list of callbacks to call during state transitions.
 
List[ __end_cbs
 A list of callbacks to call when the state machine ends.
 

Detailed Description

Represents a state machine that can manage states and transitions
between them, including callbacks for different state events.

Attributes:
    _states (Dict[str, Dict[str, Any]]): A dictionary mapping state names to their corresponding state objects and transitions.
    _start_state (str): The name of the initial state of the state machine.
    __current_state (str): The name of the current state being executed.
    __current_state_lock (Lock): A threading lock to manage access to the current state.
    _validated (bool): A flag indicating whether the state machine has been validated.
    __start_cbs (List[Tuple[Callable[[Blackboard, str, List[Any]], None], List[Any]]]): A list of callbacks to call when the state machine starts.
    __transition_cbs (List[Tuple[Callable[[Blackboard, str, List[Any]], None], List[Any]]]): A list of callbacks to call during state transitions.
    __end_cbs (List[Tuple[Callable[[Blackboard, str, List[Any]], None], List[Any]]]): A list of callbacks to call when the state machine ends.

Constructor & Destructor Documentation

◆ __init__()

None yasmin.state_machine.StateMachine.__init__ ( self,
Set[str] outcomes )
Initializes the StateMachine with a set of possible outcomes.

Parameters:
    outcomes (Set[str]): A set of possible outcomes for the state machine.

Reimplemented from yasmin.state.State.

Member Function Documentation

◆ __str__()

str yasmin.state_machine.StateMachine.__str__ ( self)
Returns a string representation of the state machine, listing all states and their types.

Returns:
    str: A string representation of the state machine.

Reimplemented from yasmin.state.State.

◆ _call_end_cbs()

None yasmin.state_machine.StateMachine._call_end_cbs ( self,
Blackboard blackboard,
str outcome )
protected
Executes all end callbacks.

Parameters:
    blackboard (Blackboard): The blackboard instance used for sharing state.
    outcome (str): The outcome of the state machine execution.

Raises:
    Exception: If an error occurs while executing a callback.

◆ _call_start_cbs()

None yasmin.state_machine.StateMachine._call_start_cbs ( self,
Blackboard blackboard,
str start_state )
protected
Executes all start callbacks.

Parameters:
    blackboard (Blackboard): The blackboard instance used for sharing state.
    start_state (str): The name of the state machine's starting state.

Raises:
    Exception: If an error occurs while executing a callback.

◆ _call_transition_cbs()

None yasmin.state_machine.StateMachine._call_transition_cbs ( self,
Blackboard blackboard,
str from_state,
str to_state,
str outcome )
protected
Executes all transition callbacks.

Parameters:
    blackboard (Blackboard): The blackboard instance used for sharing state.
    from_state (str): The state the transition is coming from.
    to_state (str): The state the transition is going to.
    outcome (str): The outcome of the transition.

Raises:
    Exception: If an error occurs while executing a callback.

◆ add_end_cb()

None yasmin.state_machine.StateMachine.add_end_cb ( self,
Callable cb,
List[Any] args = None )
Adds a callback to be called when the state machine ends.

Parameters:
    cb (Callable): The callback function to execute.
    args (List[Any], optional): A list of arguments to pass to the callback. Defaults to None.

◆ add_start_cb()

None yasmin.state_machine.StateMachine.add_start_cb ( self,
Callable cb,
List[Any] args = None )
Adds a callback to be called when the state machine starts.

Parameters:
    cb (Callable): The callback function to execute.
    args (List[Any], optional): A list of arguments to pass to the callback. Defaults to None.

◆ add_state()

None yasmin.state_machine.StateMachine.add_state ( self,
str name,
State state,
Dict[str, str] transitions = None )
Adds a new state to the state machine.

Parameters:
    name (str): The name of the state to add.
    state (State): The State object to associate with the name.
    transitions (Dict[str, str], optional): A dictionary mapping source outcomes to target states. Defaults to None.

Raises:
    KeyError: If the state name is already registered or is an outcome.
    ValueError: If transitions contain empty keys or values.
    KeyError: If transitions reference unregistered outcomes.

◆ add_transition_cb()

None yasmin.state_machine.StateMachine.add_transition_cb ( self,
Callable cb,
List[Any] args = None )
Adds a callback to be called during state transitions.

Parameters:
    cb (Callable): The callback function to execute.
    args (List[Any], optional): A list of arguments to pass to the callback. Defaults to None.

◆ cancel_state()

None yasmin.state_machine.StateMachine.cancel_state ( self)
Cancels the current state and any associated operations.

Overrides the cancel_state method from the parent State class.

Reimplemented from yasmin.state.State.

◆ execute()

str yasmin.state_machine.StateMachine.execute ( self,
Blackboard blackboard )
Executes the state machine starting from the initial state.

Parameters:
    blackboard (Blackboard): The blackboard instance used for sharing state.

Returns:
    str: The final outcome of the state machine execution.

Raises:
    RuntimeError: If the execution is canceled unexpectedly.
    KeyError: If an outcome does not belong to a state or the state machine.

Reimplemented from yasmin.state.State.

◆ get_current_state()

str yasmin.state_machine.StateMachine.get_current_state ( self)
Retrieves the name of the current state being executed.

Returns:
    str: The name of the current state, or an empty string if none is set.

◆ get_start_state()

str yasmin.state_machine.StateMachine.get_start_state ( self)
Retrieves the name of the current starting state.

Returns:
    str: The name of the starting state.

◆ get_states()

Dict[str, Union[State, Dict[str, str]]] yasmin.state_machine.StateMachine.get_states ( self)
Retrieves all states in the state machine.

Returns:
    Dict[str, Union[State, Dict[str, str]]]: A dictionary of states and their transitions.

◆ set_start_state()

None yasmin.state_machine.StateMachine.set_start_state ( self,
str state_name )
Sets the initial state for the state machine.

Parameters:
    state_name (str): The name of the initial state to set.

Raises:
    ValueError: If the state name is empty.
    KeyError: If the state name is not found in the states.

◆ validate()

None yasmin.state_machine.StateMachine.validate ( self,
bool strict_mode = False )
Validates the state machine to ensure all states and transitions are correct.

Parameters:
    strict_mode (bool): Whether the validation is strict, which means checking if all state outcomes are used and all state machine outcomes are reached.

Raises:
    RuntimeError: If no initial state is set.
    KeyError: If there are any unregistered outcomes or transitions.

Member Data Documentation

◆ __current_state

str yasmin.state_machine.StateMachine.__current_state = None
private

The name of the current state being executed.

◆ __current_state_lock

Lock yasmin.state_machine.StateMachine.__current_state_lock = Lock()
private

A threading lock to manage access to the current state.

◆ __end_cbs

List[ yasmin.state_machine.StateMachine.__end_cbs
private

A list of callbacks to call when the state machine ends.

◆ __start_cbs

List[ yasmin.state_machine.StateMachine.__start_cbs
private

A list of callbacks to call when the state machine starts.

◆ __transition_cbs

List[ yasmin.state_machine.StateMachine.__transition_cbs
private

A list of callbacks to call during state transitions.

◆ _outcomes

yasmin.state_machine.StateMachine._outcomes
protected

◆ _start_state

str yasmin.state_machine.StateMachine._start_state = None
protected

The name of the initial state of the state machine.

◆ _states

dict yasmin.state_machine.StateMachine._states = {}
protected

A dictionary mapping state names to their corresponding state objects and transitions.

◆ _validated

bool yasmin.state_machine.StateMachine._validated = False
protected

A flag indicating whether the state machine has been validated.


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