16#ifndef YASMIN__STATE_MACHINE_HPP
17#define YASMIN__STATE_MACHINE_HPP
46 std::shared_ptr<yasmin::blackboard::Blackboard>,
const std::string &,
47 const std::vector<std::string> &)>;
50 std::shared_ptr<yasmin::blackboard::Blackboard>,
const std::string &,
51 const std::string &,
const std::string &,
52 const std::vector<std::string> &)>;
55 std::shared_ptr<yasmin::blackboard::Blackboard>,
const std::string &,
56 const std::vector<std::string> &)>;
79 void add_state(std::string name, std::shared_ptr<State> state,
89 void add_state(std::string name, std::shared_ptr<State> state);
111 std::map<std::string, std::shared_ptr<State>>
const &
get_states();
118 std::map<std::string, std::map<std::string, std::string>>
const &
143 std::vector<std::string> args = {});
160 call_start_cbs(std::shared_ptr<yasmin::blackboard::Blackboard> blackboard,
172 std::shared_ptr<yasmin::blackboard::Blackboard> blackboard,
173 const std::string &from_state,
const std::string &to_state,
174 const std::string &outcome);
182 void call_end_cbs(std::shared_ptr<yasmin::blackboard::Blackboard> blackboard,
183 const std::string &outcome);
192 void validate(
bool strict_mode =
false);
203 execute(std::shared_ptr<blackboard::Blackboard> blackboard)
override;
219 using State::operator();
235 std::map<std::string, std::shared_ptr<State>>
states;
237 std::map<std::string, std::map<std::string, std::string>>
transitions;
249 std::vector<std::pair<StartCallbackType, std::vector<std::string>>>
start_cbs;
251 std::vector<std::pair<TransitionCallbackType, std::vector<std::string>>>
254 std::vector<std::pair<EndCallbackType, std::vector<std::string>>>
end_cbs;
A class that implements a state machine with a set of states, transitions, and callback mechanisms fo...
Definition state_machine.hpp:42
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:246
std::string execute()
Executes the state machine using a default blackboard.
Definition state_machine.cpp:368
std::vector< std::pair< EndCallbackType, std::vector< std::string > > > end_cbs
End callbacks executed before the state machine.
Definition state_machine.hpp:254
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:49
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:153
std::string current_state
Name of the current state.
Definition state_machine.hpp:241
std::string get_current_state()
Retrieves the current state name.
Definition state_machine.cpp:133
std::string operator()()
Invokes the state machine using a default blackboard.
Definition state_machine.cpp:376
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:54
std::map< std::string, std::shared_ptr< State > > states
Map of states.
Definition state_machine.hpp:235
void add_state(std::string name, std::shared_ptr< State > state, std::map< std::string, std::string > transitions)
Adds a state to the state machine with specified transitions.
Definition state_machine.cpp:38
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:129
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:170
void validate(bool strict_mode=false)
Validates the state machine configuration.
Definition state_machine.cpp:205
void add_start_cb(StartCallbackType cb, std::vector< std::string > args={})
Adds a callback function to be called when the state machine starts.
Definition state_machine.cpp:138
void add_end_cb(EndCallbackType cb, std::vector< std::string > args={})
Adds a callback function to be called when the state machine ends.
Definition state_machine.cpp:148
std::vector< std::pair< TransitionCallbackType, std::vector< std::string > > > transition_cbs
Transition callbacks executed before changing the state.
Definition state_machine.hpp:252
std::string start_state
Name of the start state.
Definition state_machine.hpp:239
std::map< std::string, std::map< std::string, std::string > > transitions
Map of transitions.
Definition state_machine.hpp:237
std::string get_start_state()
Retrieves the name of the start state.
Definition state_machine.cpp:121
void set_start_state(std::string state_name)
Sets the start state for the state machine.
Definition state_machine.cpp:103
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:45
std::string to_string()
Converts the state machine to a string representation.
Definition state_machine.cpp:392
std::map< std::string, std::shared_ptr< State > > const & get_states()
Gets a constant reference to the map of states.
Definition state_machine.cpp:124
StateMachine(std::set< std::string > outcomes)
Construct a new StateMachine object.
Definition state_machine.cpp:34
void add_transition_cb(TransitionCallbackType cb, std::vector< std::string > args={})
Adds a callback function for state transitions.
Definition state_machine.cpp:143
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:188
void cancel_state() override
Cancels the current state execution.
Definition state_machine.cpp:383
std::vector< std::pair< StartCallbackType, std::vector< std::string > > > start_cbs
Start callbacks executed before the state machine.
Definition state_machine.hpp:249
Represents a state in a state machine.
Definition state.hpp:42
std::set< std::string > outcomes
The possible outcomes of this state.
Definition state.hpp:46
Definition blackboard.hpp:29