|
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={}, const ParameterMappings ¶meter_mappings={}) |
| 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 | set_parameter_mappings (const std::string &state_name, const ParameterMappings ¶meter_mappings) |
| Sets parameter mappings for a child state. | |
| const ParameterMappingsMap & | get_parameter_mappings () const noexcept |
| Gets all parameter mappings. | |
| void | validate (bool strict_mode=false) |
| Validates the state machine configuration. | |
| void | configure () override |
| Executes the state machine. | |
| std::string | execute (Blackboard::SharedPtr blackboard) override |
| Executes the state's specific logic. | |
| 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 currently active child state. | |
| void | cancel_state_machine () |
| Cancels the complete state machine 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 () |
| Virtual destructor. | |
| Blackboard::SharedPtr | get_parameters_blackboard () const |
| Gets the private parameter blackboard of this state. | |
| 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. | |
| void | set_description (const std::string &description) |
| Sets the description for this state. | |
| const std::string & | get_description () const |
| Gets the description of this state. | |
| void | set_outcome_description (const std::string &outcome, const std::string &description) |
| Sets a human-readable description for an outcome. | |
| const std::string & | get_outcome_description (const std::string &outcome) const |
| Gets the human-readable description for an outcome. | |
| const std::unordered_map< std::string, std::string > & | get_outcome_descriptions () const |
| Gets all outcome descriptions. | |
| void | add_input_key (const BlackboardKeyInfo &key_info) |
| Adds an input key to the state's metadata. | |
| void | add_input_key (const std::string &key_name) |
| Adds an input key with a name only. | |
| void | add_input_key (const std::string &key_name, const std::string &description) |
| Adds an input key with a name and description. | |
| template<typename T> | |
| void | add_input_key (const std::string &key_name, const std::string &description, T default_value) |
| Adds an input key with a name, default value and description. | |
| void | add_output_key (const BlackboardKeyInfo &key_info) |
| Adds an output key to the state's metadata. | |
| void | add_output_key (const std::string &key_name) |
| Adds an output key with a name only. | |
| void | add_output_key (const std::string &key_name, const std::string &description) |
| Adds an output key with a name and description. | |
| const std::vector< BlackboardKeyInfo > & | get_input_keys () const |
| Gets the input keys metadata. | |
| const std::vector< BlackboardKeyInfo > & | get_output_keys () const |
| Gets the output keys metadata. | |
| void | declare_parameter (const BlackboardKeyInfo ¶meter_info) |
| Declares a parameter in the state metadata. | |
| void | declare_parameter (const std::string ¶meter_name) |
| Declares a parameter with a name only. | |
| void | declare_parameter (const std::string ¶meter_name, const std::string &description) |
| Declares a parameter with a name and description. | |
| template<typename T> | |
| void | declare_parameter (const std::string ¶meter_name, const std::string &description, T default_value) |
| Declares a parameter with a name, description, and default value. | |
| bool | has_parameter (const std::string ¶meter_name) const |
| Checks if a parameter exists in the state-local parameter storage. | |
| template<typename T> | |
| T | get_parameter (const std::string ¶meter_name) const |
| Gets a parameter from the state-local parameter storage. | |
| template<typename T> | |
| void | set_parameter (const std::string ¶meter_name, T value) |
| Sets a parameter in the state-local parameter storage. | |
| bool | is_parameter_declared (const std::string ¶meter_name) const |
| Checks whether a parameter was declared in the state metadata. | |
| void | copy_parameter_from (const State &source_state, const std::string &source_parameter_name, const std::string &target_parameter_name) |
| Copies a parameter value from another state. | |
| const std::vector< BlackboardKeyInfo > & | get_parameters () const |
| Gets the declared parameter metadata. | |
| const StateMetadata & | get_metadata () const |
| Gets the complete state metadata. | |
Private Member Functions | |
| void | set_current_state (const std::string &state_name) |
| Sets the current state name. | |
| void | apply_parameter_mappings (const std::string &state_name, const State::SharedPtr &state) |
| Applies this container's parameter mappings to a direct child. | |
| 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. | |
| std::string | wait_for_current_state () |
| Waits until a current state is available while execution is active. | |
| void | throw_if_cancel_state_machine_requested () |
| Throws if a hard state machine cancel was requested. | |
Static Private Member Functions | |
| static Remappings | compose_remappings (const Remappings &parent_remappings, const Remappings &state_remappings) |
| Compose parent and state blackboard remappings. | |
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. | |
| ParameterMappingsMap | parameter_mappings |
| Per-child parameter mappings applied during configure(). | |
| 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::atomic_bool | configured {false} |
| Flag to indicate if the state machine has been configured. | |
| std::atomic_bool | execution_active {false} |
| Flag to indicate whether the execute loop is currently active. | |
| std::atomic_bool | cancel_state_machine_requested {false} |
| Flag to indicate that a hard state machine cancel was requested. | |
| 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.
| 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 = {}, | ||
| const ParameterMappings & | parameter_mappings = {} ) |
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 |
Applies this container's parameter mappings to a direct child.
| state_name | The child state name. |
| state | The child state instance. |
|
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 currently active child state.
This lightweight cancel request is forwarded to the active child state. If the state machine continues executing afterwards, its running status is restored inside the execution loop so later cancel requests can still be propagated.
Reimplemented from yasmin::State.
| void StateMachine::cancel_state_machine | ( | ) |
Cancels the complete state machine execution.
This requests a hard stop of the current state machine. The request is propagated recursively to an active child state machine and causes the execution loop to abort by throwing StateMachineCancelException.
|
staticprivate |
Compose parent and state blackboard remappings.
The state remappings are resolved against the currently active parent remappings. If a state target already points to a remapped parent key, the resulting mapping is flattened to the final parent target.
| parent_remappings | Remappings currently active in the parent scope. |
| state_remappings | Remappings defined by the current state. |
|
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::execute | ( | ) |
Executes the state machine using a default blackboard.
|
overridevirtual |
Executes the state's specific logic.
| blackboard | A shared pointer to the Blackboard to use during execution. |
This method is intended to be overridden by derived classes to provide specific execution logic.
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 |
Gets all parameter mappings.
|
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_parameter_mappings | ( | const std::string & | state_name, |
| const ParameterMappings & | parameter_mappings ) |
Sets parameter mappings for a child state.
Each mapping entry is interpreted as: child_parameter -> parent_parameter
| state_name | The child state name. |
| parameter_mappings | The parameter mappings to apply. |
| 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. |
|
private |
Throws if a hard state machine cancel was requested.
|
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 |
Waits until a current state is available while execution is active.
|
private |
Flag to indicate that a hard state machine cancel was requested.
|
private |
Flag to indicate if the state machine has been configured.
|
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 |
Flag to indicate whether the execute loop is currently active.
|
private |
|
private |
Per-child parameter mappings applied during configure().
|
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.