C++ YASMIN (Yet Another State MachINe)
Loading...
Searching...
No Matches
state.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_HPP_
17#define YASMIN__STATE_HPP_
18
19#include <atomic>
20#include <functional>
21#include <string>
22#include <unordered_map>
23
24#include "yasmin/blackboard.hpp"
26#include "yasmin/logs.hpp"
27
28namespace yasmin {
29
40
49class State {
50
51protected:
54
55private:
64
66 std::atomic<StateStatus> status{StateStatus::IDLE};
67
72 void set_status(StateStatus new_status);
73
78 StateStatus get_status() const;
79
80public:
85
86
90 State(const Outcomes &outcomes);
91
97 virtual ~State();
98
106
111 bool is_idle() const noexcept;
112
117 bool is_running() const noexcept;
118
123 bool is_canceled() const noexcept;
124
129 bool is_completed() const noexcept;
130
142 std::string operator()(Blackboard::SharedPtr blackboard);
143
153 virtual std::string execute(Blackboard::SharedPtr blackboard) {
154 (void)blackboard; // Suppress unused parameter warning
155 return "";
156 }
157
165 virtual void configure() {}
166
172 virtual void cancel_state() {
173 YASMIN_LOG_INFO("Canceling state '%s'", this->to_string().c_str());
175 }
176
181 Outcomes const &get_outcomes() const noexcept;
182
187 void set_description(const std::string &description);
188
193 const std::string &get_description() const;
194
201 void set_outcome_description(const std::string &outcome,
202 const std::string &description);
203
210 const std::string &get_outcome_description(const std::string &outcome) const;
211
216 const std::unordered_map<std::string, std::string> &
218
223 void add_input_key(const BlackboardKeyInfo &key_info);
224
229 void add_input_key(const std::string &key_name);
230
236 void add_input_key(const std::string &key_name,
237 const std::string &description);
238
246 template <typename T>
247 void add_input_key(const std::string &key_name,
248 const std::string &description, T default_value) {
249 this->add_input_key(
250 BlackboardKeyInfo(key_name, description, default_value));
251 }
252
257 void add_output_key(const BlackboardKeyInfo &key_info);
258
263 void add_output_key(const std::string &key_name);
264
270 void add_output_key(const std::string &key_name,
271 const std::string &description);
272
277 const std::vector<BlackboardKeyInfo> &get_input_keys() const;
278
283 const std::vector<BlackboardKeyInfo> &get_output_keys() const;
284
289 void declare_parameter(const BlackboardKeyInfo &parameter_info);
290
295 void declare_parameter(const std::string &parameter_name);
296
302 void declare_parameter(const std::string &parameter_name,
303 const std::string &description);
304
312 template <typename T>
313 void declare_parameter(const std::string &parameter_name,
314 const std::string &description, T default_value) {
315 this->declare_parameter(
316 BlackboardKeyInfo(parameter_name, description, default_value));
317 }
318
324 bool has_parameter(const std::string &parameter_name) const;
325
332 template <typename T>
333 T get_parameter(const std::string &parameter_name) const {
334 return this->get_parameters_blackboard()->get<T>(parameter_name);
335 }
336
343 template <typename T>
344 void set_parameter(const std::string &parameter_name, T value) {
345 this->get_parameters_blackboard()->set<T>(parameter_name, value);
346 }
347
353 bool is_parameter_declared(const std::string &parameter_name) const;
354
361 void copy_parameter_from(const State &source_state,
362 const std::string &source_parameter_name,
363 const std::string &target_parameter_name);
364
369 const std::vector<BlackboardKeyInfo> &get_parameters() const;
370
375 const StateMetadata &get_metadata() const;
376
384 virtual std::string to_string() const;
385};
386
387} // namespace yasmin
388
389#endif // YASMIN__STATE_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
Represents a state in a state machine.
Definition state.hpp:49
const std::vector< BlackboardKeyInfo > & get_input_keys() const
Gets the input keys metadata.
Definition state.cpp:233
void set_parameter(const std::string &parameter_name, T value)
Sets a parameter in the state-local parameter storage.
Definition state.hpp:344
bool is_running() const noexcept
Checks if the state is currently running.
Definition state.cpp:85
StateStatus get_status() const
Gets the current status of the state.
Definition state.cpp:79
const std::vector< BlackboardKeyInfo > & get_parameters() const
Gets the declared parameter metadata.
Definition state.cpp:290
const std::unordered_map< std::string, std::string > & get_outcome_descriptions() const
Gets all outcome descriptions.
Definition state.cpp:194
Outcomes outcomes
The possible outcomes of this state.
Definition state.hpp:53
virtual void configure()
Configures the state before execution.
Definition state.hpp:165
bool is_parameter_declared(const std::string &parameter_name) const
Checks whether a parameter was declared in the state metadata.
Definition state.cpp:274
bool is_idle() const noexcept
Checks if the state is idle.
Definition state.cpp:81
void declare_parameter(const std::string &parameter_name, const std::string &description, T default_value)
Declares a parameter with a name, description, and default value.
Definition state.hpp:313
State(const Outcomes &outcomes)
Shared pointer type for State.
Definition state.cpp:64
virtual std::string execute(Blackboard::SharedPtr blackboard)
Executes the state's specific logic.
Definition state.hpp:153
void add_input_key(const BlackboardKeyInfo &key_info)
Adds an input key to the state's metadata.
Definition state.cpp:198
std::atomic< StateStatus > status
Current status of the state.
Definition state.hpp:66
bool is_canceled() const noexcept
Checks if the state has been canceled.
Definition state.cpp:89
const StateMetadata & get_metadata() const
Gets the complete state metadata.
Definition state.cpp:294
void set_status(StateStatus new_status)
Sets the current status of the state.
Definition state.cpp:75
bool is_completed() const noexcept
Checks if the state has completed execution.
Definition state.cpp:93
StateMetadata & get_metadata_ref() const
Gets a mutable reference to this state's metadata.
Definition state.cpp:54
void copy_parameter_from(const State &source_state, const std::string &source_parameter_name, const std::string &target_parameter_name)
Copies a parameter value from another state.
Definition state.cpp:282
void add_output_key(const BlackboardKeyInfo &key_info)
Adds an output key to the state's metadata.
Definition state.cpp:213
const std::string & get_outcome_description(const std::string &outcome) const
Gets the human-readable description for an outcome.
Definition state.cpp:175
void set_description(const std::string &description)
Sets the description for this state.
Definition state.cpp:156
const std::vector< BlackboardKeyInfo > & get_output_keys() const
Gets the output keys metadata.
Definition state.cpp:237
const std::string & get_description() const
Gets the description of this state.
Definition state.cpp:160
virtual std::string to_string() const
Converts the state to a string representation.
Definition state.cpp:298
Outcomes const & get_outcomes() const noexcept
Gets the set of possible outcomes for this state.
Definition state.cpp:154
bool has_parameter(const std::string &parameter_name) const
Checks if a parameter exists in the state-local parameter storage.
Definition state.cpp:270
T get_parameter(const std::string &parameter_name) const
Gets a parameter from the state-local parameter storage.
Definition state.hpp:333
virtual void cancel_state()
Cancels the current state execution.
Definition state.hpp:172
void declare_parameter(const BlackboardKeyInfo &parameter_info)
Declares a parameter in the state metadata.
Definition state.cpp:241
void set_outcome_description(const std::string &outcome, const std::string &description)
Sets a human-readable description for an outcome.
Definition state.cpp:164
virtual ~State()
Virtual destructor.
Definition state.cpp:70
Blackboard::SharedPtr get_parameters_blackboard() const
Gets the private parameter blackboard of this state.
Definition state.cpp:59
#define YASMIN_LOG_INFO(text,...)
Definition logs.hpp:169
Definition blackboard.hpp:34
StringSet Outcomes
Set of possible outcomes for states.
Definition types.hpp:77
StateStatus
Enumeration representing the current status of a state.
Definition state.hpp:34
@ RUNNING
State is currently executing.
Definition state.hpp:36
@ COMPLETED
State execution has completed successfully.
Definition state.hpp:38
@ IDLE
State is idle and ready to execute.
Definition state.hpp:35
@ CANCELED
State execution has been canceled.
Definition state.hpp:37
Information about a blackboard key including name and optional default value.
Definition blackboard_key_info.hpp:35
Metadata for a state including description and blackboard key information.
Definition blackboard_key_info.hpp:123
#define YASMIN_PTR_ALIASES(ClassName)
Macro to define all pointer aliases for a class.
Definition types.hpp:52