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 <functional>
21#include <map>
22#include <memory>
23#include <mutex>
24#include <set>
25#include <string>
26#include <vector>
27
29#include "yasmin/state.hpp"
30
31namespace yasmin {
32
42class StateMachine : public State {
43
45 using StartCallbackType = std::function<void(
46 std::shared_ptr<yasmin::blackboard::Blackboard>, const std::string &,
47 const std::vector<std::string> &)>;
49 using TransitionCallbackType = std::function<void(
50 std::shared_ptr<yasmin::blackboard::Blackboard>, const std::string &,
51 const std::string &, const std::string &,
52 const std::vector<std::string> &)>;
54 using EndCallbackType = std::function<void(
55 std::shared_ptr<yasmin::blackboard::Blackboard>, const std::string &,
56 const std::vector<std::string> &)>;
57
58public:
64 StateMachine(std::set<std::string> outcomes);
65
79 void add_state(std::string name, std::shared_ptr<State> state,
80 std::map<std::string, std::string> transitions);
81
89 void add_state(std::string name, std::shared_ptr<State> state);
90
97 void set_start_state(std::string state_name);
98
104 std::string get_start_state();
105
111 std::map<std::string, std::shared_ptr<State>> const &get_states();
112
118 std::map<std::string, std::map<std::string, std::string>> const &
120
126 std::string get_current_state();
127
134 void add_start_cb(StartCallbackType cb, std::vector<std::string> args = {});
135
143 std::vector<std::string> args = {});
144
151 void add_end_cb(EndCallbackType cb, std::vector<std::string> args = {});
152
159 void
160 call_start_cbs(std::shared_ptr<yasmin::blackboard::Blackboard> blackboard,
161 const std::string &start_state);
162
172 std::shared_ptr<yasmin::blackboard::Blackboard> blackboard,
173 const std::string &from_state, const std::string &to_state,
174 const std::string &outcome);
175
182 void call_end_cbs(std::shared_ptr<yasmin::blackboard::Blackboard> blackboard,
183 const std::string &outcome);
184
192 void validate(bool strict_mode = false);
193
202 std::string
203 execute(std::shared_ptr<blackboard::Blackboard> blackboard) override;
204
210 std::string execute();
211
217 std::string operator()();
218
219 using State::operator();
220
224 void cancel_state() override;
225
231 std::string to_string();
232
233private:
235 std::map<std::string, std::shared_ptr<State>> states;
237 std::map<std::string, std::map<std::string, std::string>> transitions;
239 std::string start_state;
241 std::string current_state;
243 std::unique_ptr<std::mutex> current_state_mutex;
244
246 std::atomic_bool validated{false};
247
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;
255};
256
257} // namespace yasmin
258
259#endif // YASMIN__STATE_MACHINE_HPP
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