|
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 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. | |
Public Member Functions | |
| StateMachine (const Outcomes &outcomes, bool handle_sigint=false) | |
| Shared pointer type for StateMachine. | |
| StateMachine (const std::string &name, const Outcomes &outcomes, bool handle_sigint=false) | |
| Construct a new StateMachine object. | |
| ~StateMachine () | |
| Destroy the StateMachine object. | |
| void | add_state (const std::string &name, State::SharedPtr state, const Transitions &transitions={}, const Remappings &remappings={}) |
| Adds a state to the state machine with specified transitions. | |
| void | set_name (const std::string &name) |
| Sets the name of the state machine. | |
| const std::string & | get_name () const noexcept |
| Gets the name of the state machine. | |
| void | set_start_state (const std::string &state_name) |
| Sets the start state for the state machine. | |
| std::string const & | get_start_state () const noexcept |
| Retrieves the name of the start state. | |
| StateMap const & | get_states () const noexcept |
| Gets a constant reference to the map of states. | |
| TransitionsMap const & | get_transitions () const noexcept |
| Gets a constant reference to the map of transitions. | |
| std::string const & | get_current_state () const |
| Retrieves the current state name. | |
| void | add_start_cb (StartCallbackType cb) |
| Adds a callback function to be called when the state machine starts. | |
| void | add_transition_cb (TransitionCallbackType cb) |
| Adds a callback function for state transitions. | |
| void | add_end_cb (EndCallbackType cb) |
| Adds a callback function to be called when the state machine ends. | |
| void | validate (bool strict_mode=false) |
| Validates the state machine configuration. | |
| std::string | execute (Blackboard::SharedPtr 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. | |
| void | set_sigint_handler (bool handle=true) |
| Sets whether the state machine should handle SIGINT for cancel. | |
| std::string | to_string () const override |
| Converts the state machine to a string representation. | |
| std::string | operator() (Blackboard::SharedPtr blackboard) |
| Executes the state and returns the outcome. | |
Public Member Functions inherited from yasmin::State | |
| State (const Outcomes &outcomes) | |
| Shared pointer type for State. | |
| virtual | ~State ()=default |
| Virtual destructor for proper polymorphic destruction. | |
| bool | is_idle () const noexcept |
| Checks if the state is idle. | |
| bool | is_running () const noexcept |
| Checks if the state is currently running. | |
| bool | is_canceled () const noexcept |
| Checks if the state has been canceled. | |
| bool | is_completed () const noexcept |
| Checks if the state has completed execution. | |
| std::string | operator() (Blackboard::SharedPtr blackboard) |
| Executes the state and returns the outcome. | |
| Outcomes const & | get_outcomes () const noexcept |
| Gets the set of possible outcomes for this state. | |
Private Member Functions | |
| void | set_current_state (const std::string &state_name) |
| Sets the current state name. | |
| void | call_start_cbs (Blackboard::SharedPtr blackboard, const std::string &start_state) |
| Calls start callbacks with the given blackboard and start state. | |
| void | call_transition_cbs (Blackboard::SharedPtr 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 (Blackboard::SharedPtr blackboard, const std::string &outcome) |
| Calls end callbacks with the given blackboard and outcome. | |
Private Attributes | |
| std::string | name |
| StateMap | states |
| Map of states. | |
| TransitionsMap | transitions |
| Map of transitions. | |
| RemappingsMap | remappings |
| A dictionary of remappings to set in the blackboard in each transition. | |
| 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::condition_variable | current_state_cond |
| Condition variable for current state changes. | |
| std::atomic_bool | validated {false} |
| Flag to indicate if the state machine has been validated. | |
| std::vector< StartCallbackType > | start_cbs |
| Start callbacks executed before the state machine. | |
| std::vector< TransitionCallbackType > | transition_cbs |
| Transition callbacks executed before changing the state. | |
| std::vector< EndCallbackType > | end_cbs |
| End callbacks executed before the state machine. | |
Additional Inherited Members | |
Protected Attributes inherited from yasmin::State | |
| Outcomes | 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.
Alias for a callback function executed after running the state machine.
Alias for a callback function executed before running the state machine.
Alias for a callback function executed before changing the state.
| StateMachine::StateMachine | ( | const Outcomes & | outcomes, |
| bool | handle_sigint = false ) |
Shared pointer type for StateMachine.
Construct a new StateMachine object.
| outcomes | A set of possible outcomes for the state machine. |
| handle_sigint | Whether to handle SIGINT for canceling the state machine. |
| StateMachine::StateMachine | ( | const std::string & | name, |
| const Outcomes & | outcomes, | ||
| bool | handle_sigint = false ) |
Construct a new StateMachine object.
| name | The name of the state machine. |
| outcomes | A set of possible outcomes for the state machine. |
| handle_sigint | Whether to handle SIGINT for canceling the state machine. |
| StateMachine::~StateMachine | ( | ) |
Destroy the StateMachine object.
| void StateMachine::add_end_cb | ( | EndCallbackType | cb | ) |
Adds a callback function to be called when the state machine ends.
| cb | The callback function to execute. |
| void StateMachine::add_start_cb | ( | StartCallbackType | cb | ) |
Adds a callback function to be called when the state machine starts.
| cb | The callback function to execute. |
| void StateMachine::add_state | ( | const std::string & | name, |
| State::SharedPtr | state, | ||
| const Transitions & | transitions = {}, | ||
| const Remappings & | remappings = {} ) |
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. |
| remappings | A map of remappings keys for the blackboard. |
| 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 | ) |
Adds a callback function for state transitions.
| cb | The callback function to execute. |
|
private |
Calls end callbacks with the given blackboard and outcome.
| blackboard | A shared pointer to the blackboard. |
| outcome | The outcome when the state machine ends. |
|
private |
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. |
|
private |
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 const & StateMachine::get_current_state | ( | ) | const |
Retrieves the current state name.
|
inlinenoexcept |
Gets the name of the state machine.
|
noexcept |
Retrieves the name of the start state.
|
noexcept |
Gets a constant reference to the map of states.
|
noexcept |
Gets a constant reference to the map of transitions.
| std::string StateMachine::operator() | ( | ) |
Invokes the state machine using a default blackboard.
| std::string State::operator() | ( | Blackboard::SharedPtr | blackboard | ) |
Executes the state and returns the outcome.
| blackboard | A shared pointer to the Blackboard to use during execution. |
This function stores the state as running, invokes the execute method, and checks if the returned outcome is valid. If the outcome is not valid, a std::logic_error is thrown.
| std::logic_error | If the outcome is not in the set of outcomes. |
|
private |
Sets the current state name.
| state_name | The name of the state to set as the current state. |
|
inline |
Sets the name of the state machine.
| name | The name to set for the state machine. |
| void StateMachine::set_sigint_handler | ( | bool | handle = true | ) |
Sets whether the state machine should handle SIGINT for cancel.
| handle | True to handle SIGINT, false to ignore or reset the handler. |
| void StateMachine::set_start_state | ( | const 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. |
|
overridevirtual |
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 |
Condition variable for current state changes.
|
private |
Mutex for current state access.
|
private |
End callbacks executed before the state machine.
|
private |
|
private |
A dictionary of remappings to set in the blackboard in each transition.
|
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.