C++ YASMIN (Yet Another State MachINe)
|
A class that implements a state machine with a set of states, transitions, and callback mechanisms for state changes. More...
#include <state_machine.hpp>
Public Member Functions | |
StateMachine (std::set< std::string > outcomes) | |
Construct a new StateMachine object. | |
void | add_state (std::string name, std::shared_ptr< State > state, std::map< std::string, std::string > transitions) |
Adds a state to the state machine with specified transitions. | |
void | add_state (std::string name, std::shared_ptr< State > state) |
Adds a state to the state machine without transitions. | |
void | set_start_state (std::string state_name) |
Sets the start state for the state machine. | |
std::string | get_start_state () |
Retrieves the name of the start state. | |
std::map< std::string, std::shared_ptr< State > > const & | get_states () |
Gets a constant reference to the map of states. | |
std::map< std::string, std::map< std::string, std::string > > const & | get_transitions () |
Gets a constant reference to the map of transitions. | |
std::string | get_current_state () |
Retrieves the current state name. | |
void | add_start_cb (StartCallbackType cb, std::vector< std::string > args={}) |
Adds a callback function to be called when the state machine starts. | |
void | add_transition_cb (TransitionCallbackType cb, std::vector< std::string > args={}) |
Adds a callback function for state transitions. | |
void | add_end_cb (EndCallbackType cb, std::vector< std::string > args={}) |
Adds a callback function to be called when the state machine ends. | |
void | call_start_cbs (std::shared_ptr< yasmin::blackboard::Blackboard > blackboard, const std::string &start_state) |
Calls start callbacks with the given blackboard and start state. | |
void | call_transition_cbs (std::shared_ptr< yasmin::blackboard::Blackboard > blackboard, const std::string &from_state, const std::string &to_state, const std::string &outcome) |
Calls transition callbacks when transitioning between states. | |
void | call_end_cbs (std::shared_ptr< yasmin::blackboard::Blackboard > blackboard, const std::string &outcome) |
Calls end callbacks with the given blackboard and outcome. | |
void | validate (bool strict_mode=false) |
Validates the state machine configuration. | |
std::string | execute (std::shared_ptr< blackboard::Blackboard > blackboard) override |
Executes the state machine. | |
std::string | execute () |
Executes the state machine using a default blackboard. | |
std::string | operator() () |
Invokes the state machine using a default blackboard. | |
void | cancel_state () override |
Cancels the current state execution. | |
std::string | to_string () |
Converts the state machine to a string representation. | |
Public Member Functions inherited from yasmin::State | |
State (std::set< std::string > outcomes) | |
Constructs a State with a set of possible outcomes. | |
std::string | operator() (std::shared_ptr< blackboard::Blackboard > blackboard) |
Executes the state and returns the outcome. | |
bool | is_canceled () const |
Checks if the state has been canceled. | |
bool | is_running () const |
Checks if the state is currently running. | |
std::set< std::string > const & | get_outcomes () |
Gets the set of possible outcomes for this state. | |
Private Types | |
using | StartCallbackType |
Alias for a callback function executed before running the state machine. | |
using | TransitionCallbackType |
Alias for a callback function executed before changing the state. | |
using | EndCallbackType |
Alias for a callback function executed after running the state machine. | |
Private Attributes | |
std::map< std::string, std::shared_ptr< State > > | states |
Map of states. | |
std::map< std::string, std::map< std::string, std::string > > | transitions |
Map of transitions. | |
std::string | start_state |
Name of the start state. | |
std::string | current_state |
Name of the current state. | |
std::unique_ptr< std::mutex > | current_state_mutex |
Mutex for current state access. | |
std::atomic_bool | validated {false} |
Flag to indicate if the state machine has been validated. | |
std::vector< std::pair< StartCallbackType, std::vector< std::string > > > | start_cbs |
Start callbacks executed before the state machine. | |
std::vector< std::pair< TransitionCallbackType, std::vector< std::string > > > | transition_cbs |
Transition callbacks executed before changing the state. | |
std::vector< std::pair< EndCallbackType, std::vector< std::string > > > | end_cbs |
End callbacks executed before the state machine. | |
Additional Inherited Members | |
Protected Attributes inherited from yasmin::State | |
std::set< std::string > | outcomes |
The possible outcomes of this state. | |
A class that implements a state machine with a set of states, transitions, and callback mechanisms for state changes.
The StateMachine class inherits from the State class and allows the registration of states with their respective transitions and callbacks for start, transition, and end events.
|
private |
Alias for a callback function executed after running the state machine.
|
private |
Alias for a callback function executed before running the state machine.
|
private |
Alias for a callback function executed before changing the state.
StateMachine::StateMachine | ( | std::set< std::string > | outcomes | ) |
Construct a new StateMachine object.
outcomes | A set of possible outcomes for the state machine. |
void StateMachine::add_end_cb | ( | EndCallbackType | cb, |
std::vector< std::string > | args = {} ) |
Adds a callback function to be called when the state machine ends.
cb | The callback function to execute. |
args | Optional arguments to pass to the callback. |
void StateMachine::add_start_cb | ( | StartCallbackType | cb, |
std::vector< std::string > | args = {} ) |
Adds a callback function to be called when the state machine starts.
cb | The callback function to execute. |
args | Optional arguments to pass to the callback. |
void StateMachine::add_state | ( | std::string | name, |
std::shared_ptr< State > | state ) |
Adds a state to the state machine without transitions.
name | The name of the state. |
state | A shared pointer to the State object representing the new state. |
void StateMachine::add_state | ( | std::string | name, |
std::shared_ptr< State > | state, | ||
std::map< std::string, std::string > | transitions ) |
Adds a state to the state machine with specified transitions.
name | The name of the state. |
state | A shared pointer to the State object representing the new state. |
transitions | A map of transitions where the key is the outcome and the value is the target state name. |
std::logic_error | If the state is already registered or is an outcome. |
std::invalid_argument | If any transition has empty source or target, or references unregistered outcomes. |
void StateMachine::add_transition_cb | ( | TransitionCallbackType | cb, |
std::vector< std::string > | args = {} ) |
Adds a callback function for state transitions.
cb | The callback function to execute. |
args | Optional arguments to pass to the callback. |
void StateMachine::call_end_cbs | ( | std::shared_ptr< yasmin::blackboard::Blackboard > | blackboard, |
const std::string & | outcome ) |
Calls end callbacks with the given blackboard and outcome.
blackboard | A shared pointer to the blackboard. |
outcome | The outcome when the state machine ends. |
void StateMachine::call_start_cbs | ( | std::shared_ptr< yasmin::blackboard::Blackboard > | blackboard, |
const std::string & | start_state ) |
Calls start callbacks with the given blackboard and start state.
blackboard | A shared pointer to the blackboard. |
start_state | The name of the start state. |
void StateMachine::call_transition_cbs | ( | std::shared_ptr< yasmin::blackboard::Blackboard > | blackboard, |
const std::string & | from_state, | ||
const std::string & | to_state, | ||
const std::string & | outcome ) |
Calls transition callbacks when transitioning between states.
blackboard | A shared pointer to the blackboard. |
from_state | The state being transitioned from. |
to_state | The state being transitioned to. |
outcome | The outcome that triggered the transition. |
|
overridevirtual |
Cancels the current state execution.
Reimplemented from yasmin::State.
std::string StateMachine::execute | ( | ) |
Executes the state machine using a default blackboard.
|
overridevirtual |
Executes the state machine.
blackboard | A shared pointer to the blackboard used during execution. |
std::runtime_error | If the execution cannot be completed due to invalid states or transitions. |
Reimplemented from yasmin::State.
std::string StateMachine::get_current_state | ( | ) |
Retrieves the current state name.
std::string StateMachine::get_start_state | ( | ) |
Retrieves the name of the start state.
std::map< std::string, std::shared_ptr< State > > const & StateMachine::get_states | ( | ) |
Gets a constant reference to the map of states.
std::map< std::string, std::map< std::string, std::string > > const & StateMachine::get_transitions | ( | ) |
Gets a constant reference to the map of transitions.
std::string StateMachine::operator() | ( | ) |
Invokes the state machine using a default blackboard.
void StateMachine::set_start_state | ( | std::string | state_name | ) |
Sets the start state for the state machine.
state_name | The name of the state to set as the start state. |
std::invalid_argument | If the state name is empty or not registered. |
|
virtual |
Converts the state machine to a string representation.
Reimplemented from yasmin::State.
void StateMachine::validate | ( | bool | strict_mode = false | ) |
Validates the state machine configuration.
strict | Whether the validation is strict, which means checking if all state outcomes are used and all state machine outcomes are reached. |
std::runtime_error | If the state machine is misconfigured. |
|
private |
Name of the current state.
|
private |
Mutex for current state access.
|
private |
End callbacks executed before the state machine.
|
private |
Start callbacks executed before the state machine.
|
private |
Name of the start state.
|
private |
Map of states.
|
private |
Transition callbacks executed before changing the state.
|
private |
Map of transitions.
|
private |
Flag to indicate if the state machine has been validated.