16#ifndef YASMIN__STATE_MACHINE_HPP_
17#define YASMIN__STATE_MACHINE_HPP_
20#include <condition_variable>
49 const std::string &,
const std::string &)>;
77 bool handle_sigint =
false);
98 void add_state(
const std::string &
name, State::SharedPtr state,
114 const std::string &
get_name() const noexcept {
return this->name; }
180 void validate(
bool strict_mode = false);
204 std::
string operator()();
207 using
State::operator();
282 const std::string &from_state,
283 const std::string &to_state,
284 const std::string &outcome);
293 const std::string &outcome);
A thread-safe storage for key-value pairs of varying types.
Definition blackboard.hpp:64
std::shared_ptr< Blackboard > SharedPtr
Shared pointer type for Blackboard.
Definition blackboard.hpp:85
std::unique_ptr< std::mutex > current_state_mutex
Mutex for current state access.
Definition state_machine.hpp:243
std::atomic_bool validated
Flag to indicate if the state machine has been validated.
Definition state_machine.hpp:248
std::string execute()
Executes the state machine using a default blackboard.
Definition state_machine.cpp:379
std::vector< EndCallbackType > end_cbs
End callbacks executed before the state machine.
Definition state_machine.hpp:255
void call_end_cbs(Blackboard::SharedPtr blackboard, const std::string &outcome)
Calls end callbacks with the given blackboard and outcome.
Definition state_machine.cpp:211
RemappingsMap remappings
A dictionary of remappings to set in the blackboard in each transition.
Definition state_machine.hpp:236
std::string current_state
Name of the current state.
Definition state_machine.hpp:241
const std::string & get_name() const noexcept
Gets the name of the state machine.
Definition state_machine.hpp:114
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:195
std::function< void(Blackboard::SharedPtr, const std::string &)> EndCallbackType
Alias for a callback function executed after running the state machine.
Definition state_machine.hpp:51
std::vector< StartCallbackType > start_cbs
Start callbacks executed before the state machine.
Definition state_machine.hpp:251
StateMap const & get_states() const noexcept
Gets a constant reference to the map of states.
Definition state_machine.cpp:150
std::string to_string() const override
Converts the state machine to a string representation.
Definition state_machine.cpp:430
void set_sigint_handler(bool handle=true)
Sets whether the state machine should handle SIGINT for cancel.
Definition state_machine.cpp:410
void add_end_cb(EndCallbackType cb)
Adds a callback function to be called when the state machine ends.
Definition state_machine.cpp:177
TransitionsMap transitions
Map of transitions.
Definition state_machine.hpp:234
std::function< void(Blackboard::SharedPtr, const std::string &)> StartCallbackType
Alias for a callback function executed before running the state machine.
Definition state_machine.hpp:44
std::string const & get_start_state() const noexcept
Retrieves the name of the start state.
Definition state_machine.cpp:146
std::condition_variable current_state_cond
Condition variable for current state changes.
Definition state_machine.hpp:245
StateMachine(const Outcomes &outcomes, bool handle_sigint=false)
Shared pointer type for StateMachine.
Definition state_machine.cpp:46
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.
Definition state_machine.cpp:62
void set_current_state(const std::string &state_name)
Sets the current state name.
Definition state_machine.cpp:163
void validate(bool strict_mode=false)
Validates the state machine configuration.
Definition state_machine.cpp:225
~StateMachine()
Destroy the StateMachine object.
Definition state_machine.cpp:56
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:181
std::string start_state
Name of the start state.
Definition state_machine.hpp:239
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:47
void set_start_state(const std::string &state_name)
Sets the start state for the state machine.
Definition state_machine.cpp:128
std::string name
Definition state_machine.hpp:229
void add_transition_cb(TransitionCallbackType cb)
Adds a callback function for state transitions.
Definition state_machine.cpp:173
std::string const & get_current_state() const
Retrieves the current state name.
Definition state_machine.cpp:158
TransitionsMap const & get_transitions() const noexcept
Gets a constant reference to the map of transitions.
Definition state_machine.cpp:154
std::vector< TransitionCallbackType > transition_cbs
Transition callbacks executed before changing the state.
Definition state_machine.hpp:253
void add_start_cb(StartCallbackType cb)
Adds a callback function to be called when the state machine starts.
Definition state_machine.cpp:169
StateMap states
Map of states.
Definition state_machine.hpp:232
void set_name(const std::string &name)
Sets the name of the state machine.
Definition state_machine.hpp:107
void cancel_state() override
Cancels the current state execution.
Definition state_machine.cpp:391
Represents a state in a state machine.
Definition state.hpp:46
Outcomes outcomes
The possible outcomes of this state.
Definition state.hpp:50
State(const Outcomes &outcomes)
Shared pointer type for State.
Definition state.cpp:32
Definition blackboard.hpp:31
std::unordered_map< std::string, std::shared_ptr< State > > StateMap
Map of state names to state pointers.
Definition types.hpp:104
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, 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