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 <mutex>
23#include <string>
24#include <thread>
25
26#ifdef __GNUG__ // If using GCC/G++
27#include <cxxabi.h> // For abi::__cxa_demangle
28#endif
29
30#include "yasmin/blackboard.hpp"
31#include "yasmin/state.hpp"
32#include "yasmin/types.hpp"
33
34namespace yasmin {
35
45class Concurrence : public State {
46
47protected:
50
52 const std::string default_outcome;
53
57
60
63
64private:
67
73 static Outcomes
75 const std::string &default_outcome);
76
77public:
82
83
91 Concurrence(const StateMap &states, const std::string &default_outcome,
92 const OutcomeMap &outcome_map);
93
103 std::string execute(Blackboard::SharedPtr blackboard) override;
104
110 void cancel_state() override;
111
116 const StateMap &get_states() const noexcept;
117
122 const OutcomeMap &get_outcome_map() const noexcept;
123
128 const std::string &get_default_outcome() const noexcept;
129
134 std::string to_string() const override;
135};
136
137} // namespace yasmin
138
139#endif // YASMIN__CONCURRENCE_HPP_
std::shared_ptr< Blackboard > SharedPtr
Shared pointer type for Blackboard.
Definition blackboard.hpp:85
const StateMap states
The states to run concurrently (name -> state)
Definition concurrence.hpp:49
std::string execute(Blackboard::SharedPtr blackboard) override
Executes the state's specific logic.
Definition concurrence.cpp:85
Outcomes possible_outcomes
The set of possible outcomes.
Definition concurrence.hpp:62
StateOutcomeMap intermediate_outcome_map
Stores the intermediate outcomes of the concurrent states.
Definition concurrence.hpp:59
std::string to_string() const override
Converts the state to a string representation.
Definition concurrence.cpp:186
const std::string & get_default_outcome() const noexcept
Returns the default outcome for this concurrence state.
Definition concurrence.cpp:168
void cancel_state() override
Cancels the current state execution.
Definition concurrence.cpp:153
const StateMap & get_states() const noexcept
Returns the map of states managed by this concurrence state.
Definition concurrence.cpp:160
std::mutex intermediate_outcome_mutex
Mutex for intermediate outcome map.
Definition concurrence.hpp:66
const std::string default_outcome
Default outcome.
Definition concurrence.hpp:52
OutcomeMap outcome_map
Definition concurrence.hpp:56
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:173
const OutcomeMap & get_outcome_map() const noexcept
Returns the outcome map for this concurrence state.
Definition concurrence.cpp:164
Concurrence(const StateMap &states, const std::string &default_outcome, const OutcomeMap &outcome_map)
Shared pointer type for Concurrence.
Definition concurrence.cpp:30
State(const Outcomes &outcomes)
Shared pointer type for State.
Definition state.cpp:32
Definition blackboard.hpp:31
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:104
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
#define YASMIN_PTR_ALIASES(ClassName)
Macro to define all pointer aliases for a class.
Definition types.hpp:52