C++ YASMIN (Yet Another State MachINe)
Loading...
Searching...
No Matches
state_machine.hpp
Go to the documentation of this file.
1// Copyright (C) 2023 Miguel Ángel González Santamarta
2//
3// This program is free software: you can redistribute it and/or modify
4// it under the terms of the GNU General Public License as published by
5// the Free Software Foundation, either version 3 of the License, or
6// (at your option) any later version.
7//
8// This program is distributed in the hope that it will be useful,
9// but WITHOUT ANY WARRANTY; without even the implied warranty of
10// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11// GNU General Public License for more details.
12//
13// You should have received a copy of the GNU General Public License
14// along with this program. If not, see <https://www.gnu.org/licenses/>.
15
16#ifndef YASMIN__STATE_MACHINE_HPP_
17#define YASMIN__STATE_MACHINE_HPP_
18
19#include <atomic>
20#include <condition_variable>
21#include <functional>
22#include <mutex>
23#include <string>
24#include <vector>
25
26#include "yasmin/blackboard.hpp"
27#include "yasmin/state.hpp"
28
29namespace yasmin {
30
40class StateMachine : public State {
41
42public:
45 std::function<void(Blackboard::SharedPtr, const std::string &)>;
48 std::function<void(Blackboard::SharedPtr, const std::string &,
49 const std::string &, const std::string &)>;
52 std::function<void(Blackboard::SharedPtr, const std::string &)>;
53
58
59
66 StateMachine(const Outcomes &outcomes, bool handle_sigint = false);
67
76 StateMachine(const std::string &name, const Outcomes &outcomes,
77 bool handle_sigint = false);
78
83
98 void add_state(const std::string &name, State::SharedPtr state,
99 const Transitions &transitions = {},
100 const Remappings &remappings = {});
101
107 void set_name(const std::string &name) { this->name = name; }
108
114 const std::string &get_name() const noexcept { return this->name; }
115
122 void set_start_state(const std::string &state_name);
123
129 std::string const &get_start_state() const noexcept;
130
136 StateMap const &get_states() const noexcept;
137
143 TransitionsMap const &get_transitions() const noexcept;
144
150 std::string const &get_current_state() const;
151
158
165
172
180 void validate(bool strict_mode = false);
181
190 std::string execute(Blackboard::SharedPtr blackboard) override;
191
197 std::string execute();
198
204 std::string operator()();
205
206 // Use the base class operator()
207 using State::operator();
208
212 void cancel_state() override;
213
218 void set_sigint_handler(bool handle = true);
219
225 std::string to_string() const override;
226
227private:
228 // Name of the state machine (used if this is the root state machine)
229 std::string name;
230
237
239 std::string start_state;
241 std::string current_state;
243 std::unique_ptr<std::mutex> current_state_mutex;
245 std::condition_variable current_state_cond;
246
248 std::atomic_bool validated{false};
249
251 std::vector<StartCallbackType> start_cbs;
253 std::vector<TransitionCallbackType> transition_cbs;
255 std::vector<EndCallbackType> end_cbs;
256
262 void set_current_state(const std::string &state_name);
263
271 const std::string &start_state);
272
282 const std::string &from_state,
283 const std::string &to_state,
284 const std::string &outcome);
285
292 void call_end_cbs(Blackboard::SharedPtr blackboard,
293 const std::string &outcome);
294};
295
296} // namespace yasmin
297
298#endif // YASMIN__STATE_MACHINE_HPP_
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