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 <iostream>
22#include <map>
23#include <memory>
24#include <mutex>
25#include <set>
26#include <string>
27#include <thread>
28#include <vector>
29
30#ifdef __GNUG__ // If using GCC/G++
31#include <cxxabi.h> // For abi::__cxa_demangle
32#endif
33
35#include "yasmin/state.hpp"
36
37namespace yasmin {
38
48class Concurrence : public State {
49
50public:
51 typedef std::map<std::string, std::string> StateOutcomeMap;
52 typedef std::map<std::string, StateOutcomeMap> OutcomeMap;
53
54protected:
56 const std::map<std::string, std::shared_ptr<State>> states;
57
59 const std::string default_outcome;
60
64
66 std::map<std::string, std::shared_ptr<std::string>> intermediate_outcome_map;
67
69 std::set<std::string> possible_outcomes;
70
71private:
74
80 static std::set<std::string>
82 const std::string &default_outcome);
83
84public:
93 Concurrence(const std::map<std::string, std::shared_ptr<State>> &states,
94 const std::string &default_outcome,
95 const OutcomeMap &outcome_map);
96
106 std::string
107 execute(std::shared_ptr<blackboard::Blackboard> blackboard) override;
108
114 void cancel_state() override;
115
120 const std::map<std::string, std::shared_ptr<State>> &get_states() const;
121
126 const OutcomeMap &get_outcome_map() const;
127
132 const std::string &get_default_outcome() const;
133
138 std::string to_string() override {
139 std::string name = "Concurrence [";
140
141 for (auto it = states.begin(); it != states.end(); ++it) {
142 name += it->first + " (" + it->second->to_string() + ")";
143
144 // Add a comma if this is not the last element
145 if (std::next(it) != states.end()) {
146 name += ", ";
147 }
148 }
149
150 name += "]";
151
152 return name;
153 }
154};
155
156} // namespace yasmin
157
158#endif // YASMIN__CONCURRENCE_HPP
std::map< std::string, std::shared_ptr< std::string > > intermediate_outcome_map
Stores the intermediate outcomes of the concurrent states.
Definition concurrence.hpp:66
const std::string & get_default_outcome() const
Returns the default outcome for this concurrence state.
Definition concurrence.cpp:170
std::string execute(std::shared_ptr< blackboard::Blackboard > blackboard) override
Executes the state's specific logic.
Definition concurrence.cpp:84
std::set< std::string > possible_outcomes
The set of possible outcomes.
Definition concurrence.hpp:69
void cancel_state() override
Cancels the current state execution.
Definition concurrence.cpp:154
static std::set< std::string > 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:175
const OutcomeMap & get_outcome_map() const
Returns the outcome map for this concurrence state.
Definition concurrence.cpp:166
const std::map< std::string, std::shared_ptr< State > > & get_states() const
Returns the map of states managed by this concurrence state.
Definition concurrence.cpp:162
std::map< std::string, StateOutcomeMap > OutcomeMap
Definition concurrence.hpp:52
std::string to_string() override
Converts the state to a string representation.
Definition concurrence.hpp:138
std::mutex intermediate_outcome_mutex
Mutex for intermediate outcome map.
Definition concurrence.hpp:73
std::map< std::string, std::string > StateOutcomeMap
Definition concurrence.hpp:51
Concurrence(const std::map< std::string, std::shared_ptr< State > > &states, const std::string &default_outcome, const OutcomeMap &outcome_map)
Constructs a State with a set of possible outcomes.
Definition concurrence.cpp:28
const std::string default_outcome
Default outcome.
Definition concurrence.hpp:59
OutcomeMap outcome_map
Definition concurrence.hpp:63
const std::map< std::string, std::shared_ptr< State > > states
The states to run concurrently (name -> state)
Definition concurrence.hpp:56
State(const std::set< std::string > &outcomes)
Constructs a State with a set of possible outcomes.
Definition state.cpp:27
Definition blackboard.hpp:30
Definition blackboard.hpp:29