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"
29
30namespace yasmin {
31
41class StateMachine : public State {
42
43public:
46 std::function<void(Blackboard::SharedPtr, const std::string &)>;
49 std::function<void(Blackboard::SharedPtr, const std::string &,
50 const std::string &, const std::string &)>;
53 std::function<void(Blackboard::SharedPtr, const std::string &)>;
54
59
60
67 StateMachine(const Outcomes &outcomes, bool handle_sigint = false);
68
77 StateMachine(const std::string &name, const Outcomes &outcomes,
78 bool handle_sigint = false);
79
84
99 void add_state(const std::string &name, State::SharedPtr state,
100 const Transitions &transitions = {},
101 const Remappings &remappings = {},
103
109 void set_name(const std::string &name) { this->name = name; }
110
116 const std::string &get_name() const noexcept { return this->name; }
117
124 void set_start_state(const std::string &state_name);
125
131 std::string const &get_start_state() const noexcept;
132
138 StateMap const &get_states() const noexcept;
139
145 TransitionsMap const &get_transitions() const noexcept;
146
152 std::string const &get_current_state() const;
153
160
167
174
184 void set_parameter_mappings(const std::string &state_name,
186
191 const ParameterMappingsMap &get_parameter_mappings() const noexcept;
192
200 void validate(bool strict_mode = false);
201
210 void configure() override;
211
212 std::string execute(Blackboard::SharedPtr blackboard) override;
213
219 std::string execute();
220
226 std::string operator()();
227
228 // Use the base class operator()
229 using State::operator();
230
239 void cancel_state() override;
240
249
254 void set_sigint_handler(bool handle = true);
255
261 std::string to_string() const override;
262
263private:
264 // Name of the state machine (used if this is the root state machine)
265 std::string name;
266
275
277 std::string start_state;
279 std::string current_state;
281 std::unique_ptr<std::mutex> current_state_mutex;
283 std::condition_variable current_state_cond;
284
286 std::atomic_bool validated{false};
288 std::atomic_bool configured{false};
290 std::atomic_bool execution_active{false};
292 std::atomic_bool cancel_state_machine_requested{false};
293
295 std::vector<StartCallbackType> start_cbs;
297 std::vector<TransitionCallbackType> transition_cbs;
299 std::vector<EndCallbackType> end_cbs;
300
306 void set_current_state(const std::string &state_name);
307
313 void apply_parameter_mappings(const std::string &state_name,
314 const State::SharedPtr &state);
315
323 const std::string &start_state);
324
334 const std::string &from_state,
335 const std::string &to_state,
336 const std::string &outcome);
337
344 void call_end_cbs(Blackboard::SharedPtr blackboard,
345 const std::string &outcome);
346
359 static Remappings compose_remappings(const Remappings &parent_remappings,
360 const Remappings &state_remappings);
361
367 std::string wait_for_current_state();
368
373};
374
375} // namespace yasmin
376
377#endif // YASMIN__STATE_MACHINE_HPP_
A thread-safe storage for key-value pairs of varying types.
Definition blackboard.hpp:67
std::shared_ptr< Blackboard > SharedPtr
Shared pointer type for Blackboard.
Definition blackboard.hpp:100
std::unique_ptr< std::mutex > current_state_mutex
Mutex for current state access.
Definition state_machine.hpp:281
std::atomic_bool validated
Flag to indicate if the state machine has been validated.
Definition state_machine.hpp:286
std::string execute()
Executes the state machine using a default blackboard.
Definition state_machine.cpp:526
void cancel_state_machine()
Cancels the complete state machine execution.
Definition state_machine.cpp:549
std::vector< EndCallbackType > end_cbs
End callbacks executed before the state machine.
Definition state_machine.hpp:299
void call_end_cbs(Blackboard::SharedPtr blackboard, const std::string &outcome)
Calls end callbacks with the given blackboard and outcome.
Definition state_machine.cpp:303
RemappingsMap remappings
A dictionary of remappings to set in the blackboard in each transition.
Definition state_machine.hpp:272
std::string current_state
Name of the current state.
Definition state_machine.hpp:279
static Remappings compose_remappings(const Remappings &parent_remappings, const Remappings &state_remappings)
Compose parent and state blackboard remappings.
Definition state_machine.cpp:318
const std::string & get_name() const noexcept
Gets the name of the state machine.
Definition state_machine.hpp:116
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:287
std::vector< StartCallbackType > start_cbs
Start callbacks executed before the state machine.
Definition state_machine.hpp:295
std::atomic_bool execution_active
Flag to indicate whether the execute loop is currently active.
Definition state_machine.hpp:290
StateMap const & get_states() const noexcept
Gets a constant reference to the map of states.
Definition state_machine.cpp:242
void apply_parameter_mappings(const std::string &state_name, const State::SharedPtr &state)
Applies this container's parameter mappings to a direct child.
Definition state_machine.cpp:171
void throw_if_cancel_state_machine_requested()
Throws if a hard state machine cancel was requested.
Definition state_machine.cpp:227
void configure() override
Executes the state machine.
Definition state_machine.cpp:202
std::string to_string() const override
Converts the state machine to a string representation.
Definition state_machine.cpp:593
void set_sigint_handler(bool handle=true)
Sets whether the state machine should handle SIGINT for cancel.
Definition state_machine.cpp:573
void add_end_cb(EndCallbackType cb)
Adds a callback function to be called when the state machine ends.
Definition state_machine.cpp:269
TransitionsMap transitions
Map of transitions.
Definition state_machine.hpp:270
std::string const & get_start_state() const noexcept
Retrieves the name of the start state.
Definition state_machine.cpp:238
const ParameterMappingsMap & get_parameter_mappings() const noexcept
Gets all parameter mappings.
Definition state_machine.cpp:167
std::condition_variable current_state_cond
Condition variable for current state changes.
Definition state_machine.hpp:283
void set_parameter_mappings(const std::string &state_name, const ParameterMappings &parameter_mappings)
Sets parameter mappings for a child state.
Definition state_machine.cpp:153
StateMachine(const Outcomes &outcomes, bool handle_sigint=false)
Shared pointer type for StateMachine.
Definition state_machine.cpp:48
std::atomic_bool cancel_state_machine_requested
Flag to indicate that a hard state machine cancel was requested.
Definition state_machine.hpp:292
void set_current_state(const std::string &state_name)
Sets the current state name.
Definition state_machine.cpp:255
ParameterMappingsMap parameter_mappings
Per-child parameter mappings applied during configure().
Definition state_machine.hpp:274
void validate(bool strict_mode=false)
Validates the state machine configuration.
Definition state_machine.cpp:334
~StateMachine()
Destroy the StateMachine object.
Definition state_machine.cpp:58
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:273
std::string start_state
Name of the start state.
Definition state_machine.hpp:277
void set_start_state(const std::string &state_name)
Sets the start state for the state machine.
Definition state_machine.cpp:134
std::string wait_for_current_state()
Waits until a current state is available while execution is active.
Definition state_machine.cpp:219
std::atomic_bool configured
Flag to indicate if the state machine has been configured.
Definition state_machine.hpp:288
std::string name
Definition state_machine.hpp:265
void add_transition_cb(TransitionCallbackType cb)
Adds a callback function for state transitions.
Definition state_machine.cpp:265
std::string const & get_current_state() const
Retrieves the current state name.
Definition state_machine.cpp:250
TransitionsMap const & get_transitions() const noexcept
Gets a constant reference to the map of transitions.
Definition state_machine.cpp:246
std::vector< TransitionCallbackType > transition_cbs
Transition callbacks executed before changing the state.
Definition state_machine.hpp:297
std::function< void(Blackboard::SharedPtr, const std::string &)> EndCallbackType
Alias for a callback function executed after running the state machine.
Definition state_machine.hpp:52
void add_start_cb(StartCallbackType cb)
Adds a callback function to be called when the state machine starts.
Definition state_machine.cpp:261
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:48
StateMap states
Map of states.
Definition state_machine.hpp:268
void add_state(const std::string &name, State::SharedPtr state, const Transitions &transitions={}, const Remappings &remappings={}, const ParameterMappings &parameter_mappings={})
Adds a state to the state machine with specified transitions.
Definition state_machine.cpp:65
void set_name(const std::string &name)
Sets the name of the state machine.
Definition state_machine.hpp:109
std::function< void(Blackboard::SharedPtr, const std::string &)> StartCallbackType
Alias for a callback function executed before running the state machine.
Definition state_machine.hpp:45
void cancel_state() override
Cancels the currently active child state.
Definition state_machine.cpp:538
Represents a state in a state machine.
Definition state.hpp:49
Outcomes outcomes
The possible outcomes of this state.
Definition state.hpp:53
State(const Outcomes &outcomes)
Shared pointer type for State.
Definition state.cpp:64
Definition blackboard.hpp:34
std::unordered_map< std::string, std::shared_ptr< State > > StateMap
Map of state names to state pointers.
Definition types.hpp:108
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, ParameterMappings > ParameterMappingsMap
Map of state names to parameter mappings.
Definition types.hpp:94
StringMap ParameterMappings
Map of child parameter names to parent parameter names.
Definition types.hpp:92
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