C++ YASMIN (Yet Another State MachINe)
Loading...
Searching...
No Matches
yasmin::StateMachine Class Reference

A class that implements a state machine with a set of states, transitions, and callback mechanisms for state changes. More...

#include <state_machine.hpp>

Inheritance diagram for yasmin::StateMachine:
Collaboration diagram for yasmin::StateMachine:

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.
 

Detailed Description

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.

Member Typedef Documentation

◆ EndCallbackType

Initial value:
std::function<void(
std::shared_ptr<yasmin::blackboard::Blackboard>, const std::string &,
const std::vector<std::string> &)>

Alias for a callback function executed after running the state machine.

◆ StartCallbackType

Initial value:
std::function<void(
std::shared_ptr<yasmin::blackboard::Blackboard>, const std::string &,
const std::vector<std::string> &)>

Alias for a callback function executed before running the state machine.

◆ TransitionCallbackType

Initial value:
std::function<void(
std::shared_ptr<yasmin::blackboard::Blackboard>, const std::string &,
const std::string &, const std::string &,
const std::vector<std::string> &)>

Alias for a callback function executed before changing the state.

Constructor & Destructor Documentation

◆ StateMachine()

StateMachine::StateMachine ( std::set< std::string > outcomes)

Construct a new StateMachine object.

Parameters
outcomesA set of possible outcomes for the state machine.

Member Function Documentation

◆ add_end_cb()

void StateMachine::add_end_cb ( EndCallbackType cb,
std::vector< std::string > args = {} )

Adds a callback function to be called when the state machine ends.

Parameters
cbThe callback function to execute.
argsOptional arguments to pass to the callback.

◆ add_start_cb()

void StateMachine::add_start_cb ( StartCallbackType cb,
std::vector< std::string > args = {} )

Adds a callback function to be called when the state machine starts.

Parameters
cbThe callback function to execute.
argsOptional arguments to pass to the callback.

◆ add_state() [1/2]

void StateMachine::add_state ( std::string name,
std::shared_ptr< State > state )

Adds a state to the state machine without transitions.

Parameters
nameThe name of the state.
stateA shared pointer to the State object representing the new state.

◆ add_state() [2/2]

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.

Parameters
nameThe name of the state.
stateA shared pointer to the State object representing the new state.
transitionsA map of transitions where the key is the outcome and the value is the target state name.
Exceptions
std::logic_errorIf the state is already registered or is an outcome.
std::invalid_argumentIf any transition has empty source or target, or references unregistered outcomes.

◆ add_transition_cb()

void StateMachine::add_transition_cb ( TransitionCallbackType cb,
std::vector< std::string > args = {} )

Adds a callback function for state transitions.

Parameters
cbThe callback function to execute.
argsOptional arguments to pass to the callback.

◆ call_end_cbs()

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.

Parameters
blackboardA shared pointer to the blackboard.
outcomeThe outcome when the state machine ends.

◆ call_start_cbs()

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.

Parameters
blackboardA shared pointer to the blackboard.
start_stateThe name of the start state.

◆ call_transition_cbs()

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.

Parameters
blackboardA shared pointer to the blackboard.
from_stateThe state being transitioned from.
to_stateThe state being transitioned to.
outcomeThe outcome that triggered the transition.

◆ cancel_state()

void StateMachine::cancel_state ( )
overridevirtual

Cancels the current state execution.

Reimplemented from yasmin::State.

◆ execute() [1/2]

std::string StateMachine::execute ( )

Executes the state machine using a default blackboard.

Returns
The outcome of the state machine execution.

◆ execute() [2/2]

std::string StateMachine::execute ( std::shared_ptr< blackboard::Blackboard > blackboard)
overridevirtual

Executes the state machine.

Parameters
blackboardA shared pointer to the blackboard used during execution.
Returns
The outcome of the state machine execution.
Exceptions
std::runtime_errorIf the execution cannot be completed due to invalid states or transitions.

Reimplemented from yasmin::State.

◆ get_current_state()

std::string StateMachine::get_current_state ( )

Retrieves the current state name.

Returns
The name of the current state.

◆ get_start_state()

std::string StateMachine::get_start_state ( )

Retrieves the name of the start state.

Returns
The name of the start state.

◆ get_states()

std::map< std::string, std::shared_ptr< State > > const & StateMachine::get_states ( )

Gets a constant reference to the map of states.

Returns
A constant reference to the map of states.

◆ get_transitions()

std::map< std::string, std::map< std::string, std::string > > const & StateMachine::get_transitions ( )

Gets a constant reference to the map of transitions.

Returns
A constant reference to the map of transitions.

◆ operator()()

std::string StateMachine::operator() ( )

Invokes the state machine using a default blackboard.

Returns
The outcome of the state machine execution.

◆ set_start_state()

void StateMachine::set_start_state ( std::string state_name)

Sets the start state for the state machine.

Parameters
state_nameThe name of the state to set as the start state.
Exceptions
std::invalid_argumentIf the state name is empty or not registered.

◆ to_string()

std::string StateMachine::to_string ( )
virtual

Converts the state machine to a string representation.

Returns
A string describing the state machine and its states.

Reimplemented from yasmin::State.

◆ validate()

void StateMachine::validate ( bool strict_mode = false)

Validates the state machine configuration.

Parameters
strictWhether the validation is strict, which means checking if all state outcomes are used and all state machine outcomes are reached.
Exceptions
std::runtime_errorIf the state machine is misconfigured.

Member Data Documentation

◆ current_state

std::string yasmin::StateMachine::current_state
private

Name of the current state.

◆ current_state_mutex

std::unique_ptr<std::mutex> yasmin::StateMachine::current_state_mutex
private

Mutex for current state access.

◆ end_cbs

std::vector<std::pair<EndCallbackType, std::vector<std::string> > > yasmin::StateMachine::end_cbs
private

End callbacks executed before the state machine.

◆ start_cbs

std::vector<std::pair<StartCallbackType, std::vector<std::string> > > yasmin::StateMachine::start_cbs
private

Start callbacks executed before the state machine.

◆ start_state

std::string yasmin::StateMachine::start_state
private

Name of the start state.

◆ states

std::map<std::string, std::shared_ptr<State> > yasmin::StateMachine::states
private

Map of states.

◆ transition_cbs

std::vector<std::pair<TransitionCallbackType, std::vector<std::string> > > yasmin::StateMachine::transition_cbs
private

Transition callbacks executed before changing the state.

◆ transitions

std::map<std::string, std::map<std::string, std::string> > yasmin::StateMachine::transitions
private

Map of transitions.

◆ validated

std::atomic_bool yasmin::StateMachine::validated {false}
private

Flag to indicate if the state machine has been validated.


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