16#ifndef YASMIN__STATE_MACHINE_HPP_
17#define YASMIN__STATE_MACHINE_HPP_
20#include <condition_variable>
50 const std::string &,
const std::string &)>;
78 bool handle_sigint =
false);
99 void add_state(
const std::string &
name, State::SharedPtr state,
116 const std::string &
get_name() const noexcept {
return this->name; }
200 void validate(
bool strict_mode = false);
226 std::
string operator()();
229 using
State::operator();
314 const State::SharedPtr &state);
334 const std::string &from_state,
335 const std::string &to_state,
336 const std::string &outcome);
345 const std::string &outcome);
A thread-safe storage for key-value pairs of varying types.
Definition blackboard.hpp:67
std::shared_ptr< Blackboard > SharedPtr
Shared pointer type for Blackboard.
Definition blackboard.hpp:100
std::unique_ptr< std::mutex > current_state_mutex
Mutex for current state access.
Definition state_machine.hpp:281
std::atomic_bool validated
Flag to indicate if the state machine has been validated.
Definition state_machine.hpp:286
std::string execute()
Executes the state machine using a default blackboard.
Definition state_machine.cpp:526
void cancel_state_machine()
Cancels the complete state machine execution.
Definition state_machine.cpp:549
std::vector< EndCallbackType > end_cbs
End callbacks executed before the state machine.
Definition state_machine.hpp:299
void call_end_cbs(Blackboard::SharedPtr blackboard, const std::string &outcome)
Calls end callbacks with the given blackboard and outcome.
Definition state_machine.cpp:303
RemappingsMap remappings
A dictionary of remappings to set in the blackboard in each transition.
Definition state_machine.hpp:272
std::string current_state
Name of the current state.
Definition state_machine.hpp:279
static Remappings compose_remappings(const Remappings &parent_remappings, const Remappings &state_remappings)
Compose parent and state blackboard remappings.
Definition state_machine.cpp:318
const std::string & get_name() const noexcept
Gets the name of the state machine.
Definition state_machine.hpp:116
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.
Definition state_machine.cpp:287
std::vector< StartCallbackType > start_cbs
Start callbacks executed before the state machine.
Definition state_machine.hpp:295
std::atomic_bool execution_active
Flag to indicate whether the execute loop is currently active.
Definition state_machine.hpp:290
StateMap const & get_states() const noexcept
Gets a constant reference to the map of states.
Definition state_machine.cpp:242
void apply_parameter_mappings(const std::string &state_name, const State::SharedPtr &state)
Applies this container's parameter mappings to a direct child.
Definition state_machine.cpp:171
void throw_if_cancel_state_machine_requested()
Throws if a hard state machine cancel was requested.
Definition state_machine.cpp:227
void configure() override
Executes the state machine.
Definition state_machine.cpp:202
std::string to_string() const override
Converts the state machine to a string representation.
Definition state_machine.cpp:593
void set_sigint_handler(bool handle=true)
Sets whether the state machine should handle SIGINT for cancel.
Definition state_machine.cpp:573
void add_end_cb(EndCallbackType cb)
Adds a callback function to be called when the state machine ends.
Definition state_machine.cpp:269
TransitionsMap transitions
Map of transitions.
Definition state_machine.hpp:270
std::string const & get_start_state() const noexcept
Retrieves the name of the start state.
Definition state_machine.cpp:238
const ParameterMappingsMap & get_parameter_mappings() const noexcept
Gets all parameter mappings.
Definition state_machine.cpp:167
std::condition_variable current_state_cond
Condition variable for current state changes.
Definition state_machine.hpp:283
void set_parameter_mappings(const std::string &state_name, const ParameterMappings ¶meter_mappings)
Sets parameter mappings for a child state.
Definition state_machine.cpp:153
StateMachine(const Outcomes &outcomes, bool handle_sigint=false)
Shared pointer type for StateMachine.
Definition state_machine.cpp:48
std::atomic_bool cancel_state_machine_requested
Flag to indicate that a hard state machine cancel was requested.
Definition state_machine.hpp:292
void set_current_state(const std::string &state_name)
Sets the current state name.
Definition state_machine.cpp:255
ParameterMappingsMap parameter_mappings
Per-child parameter mappings applied during configure().
Definition state_machine.hpp:274
void validate(bool strict_mode=false)
Validates the state machine configuration.
Definition state_machine.cpp:334
~StateMachine()
Destroy the StateMachine object.
Definition state_machine.cpp:58
void call_start_cbs(Blackboard::SharedPtr blackboard, const std::string &start_state)
Calls start callbacks with the given blackboard and start state.
Definition state_machine.cpp:273
std::string start_state
Name of the start state.
Definition state_machine.hpp:277
void set_start_state(const std::string &state_name)
Sets the start state for the state machine.
Definition state_machine.cpp:134
std::string wait_for_current_state()
Waits until a current state is available while execution is active.
Definition state_machine.cpp:219
std::atomic_bool configured
Flag to indicate if the state machine has been configured.
Definition state_machine.hpp:288
std::string name
Definition state_machine.hpp:265
void add_transition_cb(TransitionCallbackType cb)
Adds a callback function for state transitions.
Definition state_machine.cpp:265
std::string const & get_current_state() const
Retrieves the current state name.
Definition state_machine.cpp:250
TransitionsMap const & get_transitions() const noexcept
Gets a constant reference to the map of transitions.
Definition state_machine.cpp:246
std::vector< TransitionCallbackType > transition_cbs
Transition callbacks executed before changing the state.
Definition state_machine.hpp:297
std::function< void(Blackboard::SharedPtr, const std::string &)> EndCallbackType
Alias for a callback function executed after running the state machine.
Definition state_machine.hpp:52
void add_start_cb(StartCallbackType cb)
Adds a callback function to be called when the state machine starts.
Definition state_machine.cpp:261
std::function< void(Blackboard::SharedPtr, const std::string &, const std::string &, const std::string &)> TransitionCallbackType
Alias for a callback function executed before changing the state.
Definition state_machine.hpp:48
StateMap states
Map of states.
Definition state_machine.hpp:268
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.
Definition state_machine.cpp:65
void set_name(const std::string &name)
Sets the name of the state machine.
Definition state_machine.hpp:109
std::function< void(Blackboard::SharedPtr, const std::string &)> StartCallbackType
Alias for a callback function executed before running the state machine.
Definition state_machine.hpp:45
void cancel_state() override
Cancels the currently active child state.
Definition state_machine.cpp:538
Represents a state in a state machine.
Definition state.hpp:49
Outcomes outcomes
The possible outcomes of this state.
Definition state.hpp:53
State(const Outcomes &outcomes)
Shared pointer type for State.
Definition state.cpp:64
Definition blackboard.hpp:34
std::unordered_map< std::string, std::shared_ptr< State > > StateMap
Map of state names to state pointers.
Definition types.hpp:108
StringMap Transitions
Map of transitions (string to string).
Definition types.hpp:84
StringSet Outcomes
Set of possible outcomes for states.
Definition types.hpp:77
std::unordered_map< std::string, Remappings > RemappingsMap
Map of keys to remappings.
Definition types.hpp:90
std::unordered_map< std::string, ParameterMappings > ParameterMappingsMap
Map of state names to parameter mappings.
Definition types.hpp:94
StringMap ParameterMappings
Map of child parameter names to parent parameter names.
Definition types.hpp:92
std::unordered_map< std::string, Transitions > TransitionsMap
Map of state names to transitions.
Definition types.hpp:86
StringMap Remappings
Map of remappings (string to string).
Definition types.hpp:88
#define YASMIN_PTR_ALIASES(ClassName)
Macro to define all pointer aliases for a class.
Definition types.hpp:52