16#ifndef YASMIN__STATE_MACHINE_HPP
17#define YASMIN__STATE_MACHINE_HPP
20#include <condition_variable>
47 std::shared_ptr<yasmin::blackboard::Blackboard>,
const std::string &,
48 const std::vector<std::string> &)>;
51 std::shared_ptr<yasmin::blackboard::Blackboard>,
const std::string &,
52 const std::string &,
const std::string &,
53 const std::vector<std::string> &)>;
56 std::shared_ptr<yasmin::blackboard::Blackboard>,
const std::string &,
57 const std::vector<std::string> &)>;
94 void add_state(
const std::string &
name, std::shared_ptr<State> state,
95 const std::map<std::string, std::string> &
transitions = {},
96 const std::map<std::string, std::string> &
remappings = {});
110 const std::string &
get_name()
const {
return this->name; }
132 std::map<std::string, std::shared_ptr<State>>
const &
get_states();
139 std::map<std::string, std::map<std::string, std::string>>
const &
156 const std::vector<std::string> &args = {});
165 const std::vector<std::string> &args = {});
174 const std::vector<std::string> &args = {});
183 call_start_cbs(std::shared_ptr<yasmin::blackboard::Blackboard> blackboard,
195 std::shared_ptr<yasmin::blackboard::Blackboard> blackboard,
196 const std::string &from_state,
const std::string &to_state,
197 const std::string &outcome);
205 void call_end_cbs(std::shared_ptr<yasmin::blackboard::Blackboard> blackboard,
206 const std::string &outcome);
215 void validate(
bool strict_mode =
false);
226 execute(std::shared_ptr<blackboard::Blackboard> blackboard)
override;
242 using State::operator();
260 std::map<std::string, std::shared_ptr<State>>
states;
262 std::map<std::string, std::map<std::string, std::string>>
transitions;
264 std::map<std::string, std::map<std::string, std::string>>
remappings;
278 std::vector<std::pair<StartCallbackType, std::vector<std::string>>>
start_cbs;
280 std::vector<std::pair<TransitionCallbackType, std::vector<std::string>>>
283 std::vector<std::pair<EndCallbackType, std::vector<std::string>>>
end_cbs;
std::unique_ptr< std::mutex > current_state_mutex
Mutex for current state access.
Definition state_machine.hpp:270
void add_end_cb(EndCallbackType cb, const std::vector< std::string > &args={})
Adds a callback function to be called when the state machine ends.
Definition state_machine.cpp:167
std::atomic_bool validated
Flag to indicate if the state machine has been validated.
Definition state_machine.hpp:275
std::string execute()
Executes the state machine using a default blackboard.
Definition state_machine.cpp:383
std::vector< std::pair< EndCallbackType, std::vector< std::string > > > end_cbs
End callbacks executed before the state machine.
Definition state_machine.hpp:283
std::function< void( std::shared_ptr< yasmin::blackboard::Blackboard >, const std::string &, const std::string &, const std::string &, const std::vector< std::string > &)> TransitionCallbackType
Alias for a callback function executed before changing the state.
Definition state_machine.hpp:50
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.
Definition state_machine.cpp:172
std::string current_state
Name of the current state.
Definition state_machine.hpp:268
std::string get_current_state()
Retrieves the current state name.
Definition state_machine.cpp:146
std::string operator()()
Invokes the state machine using a default blackboard.
Definition state_machine.cpp:391
void add_start_cb(StartCallbackType cb, const std::vector< std::string > &args={})
Adds a callback function to be called when the state machine starts.
Definition state_machine.cpp:157
std::function< void( std::shared_ptr< yasmin::blackboard::Blackboard >, const std::string &, const std::vector< std::string > &)> EndCallbackType
Alias for a callback function executed after running the state machine.
Definition state_machine.hpp:55
void add_transition_cb(TransitionCallbackType cb, const std::vector< std::string > &args={})
Adds a callback function for state transitions.
Definition state_machine.cpp:162
std::map< std::string, std::shared_ptr< State > > states
Map of states.
Definition state_machine.hpp:260
std::condition_variable current_state_cond
Condition variable for current state changes.
Definition state_machine.hpp:272
std::map< std::string, std::map< std::string, std::string > > const & get_transitions()
Gets a constant reference to the map of transitions.
Definition state_machine.cpp:142
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.
Definition state_machine.cpp:189
void set_current_state(const std::string &state_name)
Sets the current state name.
Definition state_machine.cpp:151
void add_state(const std::string &name, std::shared_ptr< State > state, const std::map< std::string, std::string > &transitions={}, const std::map< std::string, std::string > &remappings={})
Adds a state to the state machine with specified transitions.
Definition state_machine.cpp:48
void validate(bool strict_mode=false)
Validates the state machine configuration.
Definition state_machine.cpp:224
std::vector< std::pair< TransitionCallbackType, std::vector< std::string > > > transition_cbs
Transition callbacks executed before changing the state.
Definition state_machine.hpp:281
~StateMachine()
Destroy the StateMachine object.
Definition state_machine.cpp:42
std::string start_state
Name of the start state.
Definition state_machine.hpp:266
std::map< std::string, std::map< std::string, std::string > > transitions
Map of transitions.
Definition state_machine.hpp:262
const std::string & get_name() const
Gets the name of the state machine.
Definition state_machine.hpp:110
StateMachine(const std::set< std::string > &outcomes)
Construct a new StateMachine object.
Definition state_machine.cpp:34
void set_start_state(const std::string &state_name)
Sets the start state for the state machine.
Definition state_machine.cpp:116
std::string get_start_state()
Retrieves the name of the start state.
Definition state_machine.cpp:134
std::string name
Definition state_machine.hpp:258
std::function< void( std::shared_ptr< yasmin::blackboard::Blackboard >, const std::string &, const std::vector< std::string > &)> StartCallbackType
Alias for a callback function executed before running the state machine.
Definition state_machine.hpp:46
std::map< std::string, std::map< std::string, std::string > > remappings
A dictionary of remappings to set in the blackboard in each transition.
Definition state_machine.hpp:264
std::string to_string()
Converts the state machine to a string representation.
Definition state_machine.cpp:417
std::map< std::string, std::shared_ptr< State > > const & get_states()
Gets a constant reference to the map of states.
Definition state_machine.cpp:137
void set_name(const std::string &name)
Sets the name of the state machine.
Definition state_machine.hpp:103
void call_end_cbs(std::shared_ptr< yasmin::blackboard::Blackboard > blackboard, const std::string &outcome)
Calls end callbacks with the given blackboard and outcome.
Definition state_machine.cpp:207
void cancel_state() override
Cancels the current state execution.
Definition state_machine.cpp:398
std::vector< std::pair< StartCallbackType, std::vector< std::string > > > start_cbs
Start callbacks executed before the state machine.
Definition state_machine.hpp:278
State(const std::set< std::string > &outcomes)
Constructs a State with a set of possible outcomes.
Definition state.cpp:27
std::set< std::string > outcomes
The possible outcomes of this state.
Definition state.hpp:57
Definition blackboard.hpp:29