C++ YASMIN (Yet Another State MachINe)
Loading...
Searching...
No Matches
concurrence.hpp
Go to the documentation of this file.
1// Copyright (C) 2025 Georgia Tech Research Institute
2// Supported by USDA-NIFA CSIAPP Grant. No. 2023-70442-39232
3//
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with this program. If not, see <https://www.gnu.org/licenses/>.
16
17#ifndef YASMIN__CONCURRENCE_HPP_
18#define YASMIN__CONCURRENCE_HPP_
19
20#include <atomic>
21#include <mutex>
22#include <string>
23#include <thread>
24
25#include "yasmin/blackboard.hpp"
26#include "yasmin/state.hpp"
27#include "yasmin/types.hpp"
28
29namespace yasmin {
30
40class Concurrence : public State {
41
42protected:
45
47 const std::string default_outcome;
48
52
55
60
61private:
64
66 std::atomic_bool configured{false};
67
73 void apply_parameter_mappings(const std::string &state_name,
74 const State::SharedPtr &state);
75
81 static Outcomes
83 const std::string &default_outcome);
84
85public:
90
91
99 Concurrence(const StateMap &states, const std::string &default_outcome,
100 const OutcomeMap &outcome_map,
102
109 void set_parameter_mappings(const std::string &state_name,
111
116 const ParameterMappingsMap &get_parameter_mappings() const noexcept;
117
121 void configure() override;
122
132 std::string execute(Blackboard::SharedPtr blackboard) override;
133
139 void cancel_state() override;
140
145 const StateMap &get_states() const noexcept;
146
151 const OutcomeMap &get_outcome_map() const noexcept;
152
157 const std::string &get_default_outcome() const noexcept;
158
163 std::string to_string() const override;
164};
165
166} // namespace yasmin
167
168#endif // YASMIN__CONCURRENCE_HPP_
Runs a series of states in parallel.
Definition concurrence.hpp:40
const StateMap states
The states to run concurrently (name -> state).
Definition concurrence.hpp:44
std::string execute(Blackboard::SharedPtr blackboard) override
Executes the state's specific logic.
Definition concurrence.cpp:153
std::atomic_bool configured
Flag to indicate if this concurrence state has been configured.
Definition concurrence.hpp:66
ParameterMappingsMap parameter_mappings
Per-child parameter mappings applied during configure().
Definition concurrence.hpp:59
void configure() override
Configures this concurrence state and all of its children.
Definition concurrence.cpp:136
Outcomes possible_outcomes
The set of possible outcomes.
Definition concurrence.hpp:57
void set_parameter_mappings(const std::string &state_name, const ParameterMappings &parameter_mappings)
Sets parameter mappings for a child state.
Definition concurrence.cpp:87
Concurrence(const StateMap &states, const std::string &default_outcome, const OutcomeMap &outcome_map, const ParameterMappingsMap &parameter_mappings={})
Shared pointer type for Concurrence.
Definition concurrence.cpp:31
StateOutcomeMap intermediate_outcome_map
Stores the intermediate outcomes of the concurrent states.
Definition concurrence.hpp:54
std::string to_string() const override
Converts the state to a string representation.
Definition concurrence.cpp:266
const std::string & get_default_outcome() const noexcept
Returns the default outcome for this concurrence state.
Definition concurrence.cpp:248
void cancel_state() override
Cancels the current state execution.
Definition concurrence.cpp:233
const StateMap & get_states() const noexcept
Returns the map of states managed by this concurrence state.
Definition concurrence.cpp:240
std::mutex intermediate_outcome_mutex
Mutex for intermediate outcome map.
Definition concurrence.hpp:63
void apply_parameter_mappings(const std::string &state_name, const State::SharedPtr &state)
Applies this container's parameter mappings to a direct child.
Definition concurrence.cpp:105
const ParameterMappingsMap & get_parameter_mappings() const noexcept
Returns the parameter mappings for this concurrence state.
Definition concurrence.cpp:101
const std::string default_outcome
Default outcome.
Definition concurrence.hpp:47
OutcomeMap outcome_map
Definition concurrence.hpp:51
static Outcomes generate_possible_outcomes(const OutcomeMap &outcome_map, const std::string &default_outcome)
Helper function to generate a set of possible outcomes from an outcome map.
Definition concurrence.cpp:253
const OutcomeMap & get_outcome_map() const noexcept
Returns the outcome map for this concurrence state.
Definition concurrence.cpp:244
State(const Outcomes &outcomes)
Shared pointer type for State.
Definition state.cpp:64
Definition blackboard.hpp:34
std::unordered_map< std::string, StateOutcomeMap > OutcomeMap
Map of outcomes to state outcome maps.
Definition types.hpp:81
std::unordered_map< std::string, std::shared_ptr< State > > StateMap
Map of state names to state pointers.
Definition types.hpp:108
StringSet Outcomes
Set of possible outcomes for states.
Definition types.hpp:77
StringMap StateOutcomeMap
Map of state names to their outcomes.
Definition types.hpp:79
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
#define YASMIN_PTR_ALIASES(ClassName)
Macro to define all pointer aliases for a class.
Definition types.hpp:52