| helics 3.6.1

ValueFederateManager.hpp
1 /*
2 Copyright (c) 2017-2025,
3 Battelle Memorial Institute; Lawrence Livermore National Security, LLC; Alliance for Sustainable
4 Energy, LLC. See the top-level NOTICE for additional details. All rights reserved.
5 SPDX-License-Identifier: BSD-3-Clause
6 */
7 #pragma once
8
9 #include "../common/GuardedTypes.hpp"
10 #include "../core/LocalFederateId.hpp"
11 #include "Inputs.hpp"
12 #include "ValueFederate.hpp"
13 #include "data_view.hpp"
14 #include "gmlc/containers/DualStringMappedVector.hpp"
15 #include "helicsTypes.hpp"
16
17 #include <deque>
18 #include <map>
19 #include <memory>
20 #include <string>
21 #include <vector>
22
23 namespace helics {
25 class Core;
26 class ValueFederate;
27
29 struct publication_info {
30 std::string name;
31 std::string type;
32 std::string units;
33 InterfaceHandle coreID;
34 PublicationId id;
35 int size{-1};
36 bool forward{true};
37 publication_info(std::string_view n_name, std::string_view n_type, std::string_view n_units):
38 name(n_name), type(n_type), units(n_units)
39 {
40 }
41 };
43 struct InputData {
44 InterfaceHandle coreID;
45 InputId id;
46 data_view lastData;
47 Time lastUpdate{0.0};
48 Time lastQuery{0.0};
49 int sourceIndex{0};
50 std::string name;
51 std::string type;
52 std::string units;
53 std::string pubtype;
54
55 std::function<void(Input&, Time)> callback;
56 bool hasUpdate = false;
57 InputData(std::string_view n_name, std::string_view n_type, std::string_view n_units):
58 name(n_name), type(n_type), units(n_units)
59 {
60 }
61 };
62
64 class ValueFederateManager {
65 public:
66 ValueFederateManager(Core* coreOb,
67 ValueFederate* vfed,
68 LocalFederateId id,
69 bool singleThreaded);
70 ~ValueFederateManager();
71
72 Publication&
73 registerPublication(std::string_view key, std::string_view type, std::string_view units);
77 Input& registerInput(std::string_view key, std::string_view type, std::string_view units);
78
85 void addAlias(const Input& inp, std::string_view shortcutName);
86
93 void addAlias(const Publication& pub, std::string_view shortcutName);
98 void addTarget(const Publication& pub, std::string_view target);
103 void addTarget(const Input& inp, std::string_view target);
104
109 void removeTarget(const Publication& pub, std::string_view target);
114 void removeTarget(const Input& inp, std::string_view target);
115
121 void setDefaultValue(const Input& inp, const data_view& block);
122
127 data_view getValue(const Input& inp);
128
130 void publish(const Publication& pub, const data_view& block);
131
133 static bool hasUpdate(const Input& inp);
135 static Time getLastUpdateTime(const Input& inp);
136
141 void updateTime(Time newTime, Time oldTime);
143 void startupToInitializeStateTransition();
145 void initializeToExecuteStateTransition(iteration_time result);
147 std::string localQuery(std::string_view queryStr) const;
152 std::vector<int> queryUpdates();
153
155 const std::string& getTarget(const Input& inp) const;
156
160 Input& getInput(std::string_view key);
161 const Input& getInput(std::string_view key) const;
163 Input& getInput(int index);
164 const Input& getInput(int index) const;
165
169 const Input& getInputByTarget(std::string_view key) const;
170 Input& getInputByTarget(std::string_view key);
171
175 Publication& getPublication(std::string_view key);
176 const Publication& getPublication(std::string_view key) const;
177
178 Publication& getPublication(int index);
179 const Publication& getPublication(int index) const;
180
185 void setInputNotificationCallback(std::function<void(Input&, Time)> callback);
190 static void setInputNotificationCallback(const Input& inp,
191 std::function<void(Input&, Time)> callback);
192
194 void disconnect();
195
197 int getPublicationCount() const;
199 int getInputCount() const;
202 void clearUpdates();
206 static void clearUpdate(const Input& inp);
207 bool getUpdateFromCore(Input& inp);
208
209 public:
210 bool useJsonSerialization{false};
211 private:
212 LocalFederateId fedID;
213 shared_guarded_m_opt<gmlc::containers::DualStringMappedVector<Input, InterfaceHandle>> inputs;
214 shared_guarded_m_opt<gmlc::containers::DualStringMappedVector<Publication, InterfaceHandle>>
215 publications;
216 Time CurrentTime{-1.0};
217 Core* coreObject{nullptr};
219 ValueFederate* fed{nullptr};
221 atomic_guarded<std::function<void(Input&, Time)>> allCallback;
223 shared_guarded_opt<std::deque<InputData>> inputData;
225 shared_guarded_opt<std::multimap<std::string, InterfaceHandle>> targetIDs;
227 shared_guarded_opt<std::multimap<InterfaceHandle, std::string>> inputTargets;
228
229 private:
230 void getUpdateFromCore(InterfaceHandle handle);
231 };
232
233 } // namespace helics
helics::Core
Definition: core/Core.hpp:41
helics::IdentifierId< IdentifierType, Identifiers::PUBLICATION, invalid_id_value >
helics::Input
Definition: Inputs.hpp:38
helics::InterfaceHandle
Definition: LocalFederateId.hpp:65
helics::LocalFederateId
Definition: LocalFederateId.hpp:22
helics::Publication
Definition: Publications.hpp:25
helics::ValueFederateManager
Definition: ValueFederateManager.hpp:64
helics::ValueFederateManager::queryUpdates
std::vector< int > queryUpdates()
Definition: ValueFederateManager.cpp:401
helics::ValueFederateManager::addTarget
void addTarget(const Publication &pub, std::string_view target)
Definition: ValueFederateManager.cpp:142
helics::ValueFederateManager::publish
void publish(const Publication &pub, const data_view &block)
Definition: ValueFederateManager.cpp:233
helics::ValueFederateManager::updateTime
void updateTime(Time newTime, Time oldTime)
Definition: ValueFederateManager.cpp:270
helics::ValueFederateManager::getInputByTarget
const Input & getInputByTarget(std::string_view key) const
Definition: ValueFederateManager.cpp:468
helics::ValueFederateManager::getValue
data_view getValue(const Input &inp)
Definition: ValueFederateManager.cpp:216
helics::ValueFederateManager::setDefaultValue
void setDefaultValue(const Input &inp, const data_view &block)
Definition: ValueFederateManager.cpp:188
helics::ValueFederateManager::disconnect
void disconnect()
Definition: ValueFederateManager.cpp:35
helics::ValueFederateManager::getLastUpdateTime
static Time getLastUpdateTime(const Input &inp)
Definition: ValueFederateManager.cpp:247
helics::ValueFederateManager::getInput
Input & getInput(std::string_view key)
Definition: ValueFederateManager.cpp:440
helics::ValueFederateManager::setInputNotificationCallback
void setInputNotificationCallback(std::function< void(Input &, Time)> callback)
Definition: ValueFederateManager.cpp:561
helics::ValueFederateManager::clearUpdates
void clearUpdates()
Definition: ValueFederateManager.cpp:548
helics::ValueFederateManager::getPublication
Publication & getPublication(std::string_view key)
Definition: ValueFederateManager.cpp:509
helics::ValueFederateManager::startupToInitializeStateTransition
void startupToInitializeStateTransition()
Definition: ValueFederateManager.cpp:312
helics::ValueFederateManager::removeTarget
void removeTarget(const Publication &pub, std::string_view target)
Definition: ValueFederateManager.cpp:168
helics::ValueFederateManager::localQuery
std::string localQuery(std::string_view queryStr) const
Definition: ValueFederateManager.cpp:324
helics::ValueFederateManager::useJsonSerialization
bool useJsonSerialization
all outgoing data should be serialized as JSON
Definition: ValueFederateManager.hpp:210
helics::ValueFederateManager::hasUpdate
static bool hasUpdate(const Input &inp)
Definition: ValueFederateManager.cpp:238
helics::ValueFederateManager::clearUpdate
static void clearUpdate(const Input &inp)
Definition: ValueFederateManager.cpp:553
helics::ValueFederateManager::getInputCount
int getInputCount() const
Definition: ValueFederateManager.cpp:543
helics::ValueFederateManager::getPublicationCount
int getPublicationCount() const
Definition: ValueFederateManager.cpp:538
helics::ValueFederateManager::initializeToExecuteStateTransition
void initializeToExecuteStateTransition(iteration_time result)
Definition: ValueFederateManager.cpp:319
helics::ValueFederateManager::getTarget
const std::string & getTarget(const Input &inp) const
Definition: ValueFederateManager.cpp:417
helics::ValueFederateManager::addAlias
void addAlias(const Input &inp, std::string_view shortcutName)
Definition: ValueFederateManager.cpp:119
helics::ValueFederateManager::registerInput
Input & registerInput(std::string_view key, std::string_view type, std::string_view units)
Definition: ValueFederateManager.cpp:89
helics::ValueFederate
Definition: application_api/ValueFederate.hpp:28
helics::data_view
Definition: data_view.hpp:22
helicsTypes.hpp
helics
the main namespace for the helics co-simulation library User functions will be in the helics namespac...
Definition: AsyncFedCallInfo.hpp:14
helics::Time
TimeRepresentation< count_time< 9 > > Time
Definition: helicsTime.hpp:27
helics::InputData
Definition: ValueFederateManager.hpp:43
helics::InputData::hasUpdate
bool hasUpdate
indicator that there was an update
Definition: ValueFederateManager.hpp:56
helics::InputData::lastUpdate
Time lastUpdate
the time the subscription was last updated
Definition: ValueFederateManager.hpp:47
helics::InputData::callback
std::function< void(Input &, Time)> callback
callback to trigger on update
Definition: ValueFederateManager.hpp:55
helics::InputData::units
std::string units
input units
Definition: ValueFederateManager.hpp:52
helics::InputData::type
std::string type
input type
Definition: ValueFederateManager.hpp:51
helics::InputData::lastQuery
Time lastQuery
the time the query was made
Definition: ValueFederateManager.hpp:48
helics::InputData::coreID
InterfaceHandle coreID
Handle from the core.
Definition: ValueFederateManager.hpp:44
helics::InputData::name
std::string name
input name
Definition: ValueFederateManager.hpp:50
helics::InputData::pubtype
std::string pubtype
the listed type of the corresponding publication
Definition: ValueFederateManager.hpp:53
helics::InputData::lastData
data_view lastData
the last published data from a target
Definition: ValueFederateManager.hpp:46
helics::InputData::id
InputId id
the id used as the identifier
Definition: ValueFederateManager.hpp:45
helics::InputData::sourceIndex
int sourceIndex
the index of the data source for multi-source inputs
Definition: ValueFederateManager.hpp:49
helics::iteration_time
Definition: helicsTime.hpp:43
helics::publication_info
Definition: ValueFederateManager.hpp:29
helics::publication_info::type
std::string type
publication type
Definition: ValueFederateManager.hpp:31
helics::publication_info::name
std::string name
publication name
Definition: ValueFederateManager.hpp:30
helics::publication_info::id
PublicationId id
the id used as the identifier
Definition: ValueFederateManager.hpp:34
helics::publication_info::size
int size
required size of a publication
Definition: ValueFederateManager.hpp:35
helics::publication_info::coreID
InterfaceHandle coreID
Handle from the core.
Definition: ValueFederateManager.hpp:33
helics::publication_info::units
std::string units
publication units
Definition: ValueFederateManager.hpp:32