| helics 3.6.1

application_api/ValueFederate.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 "../core/core-data.hpp"
10 #include "Federate.hpp"
11 #include "Inputs.hpp"
12 #include "Publications.hpp"
13 #include "ValueConverter.hpp"
14 #include "data_view.hpp"
15
16 #include <functional>
17 #include <memory>
18 #include <string>
19 #include <string_view>
20 #include <vector>
21
22 namespace helics {
24 class ValueFederateManager;
26 class HELICS_CXX_EXPORT ValueFederate:
27 public virtual Federate // using virtual inheritance to allow combination federate
28 {
29 public:
35 ValueFederate(std::string_view fedName, const FederateInfo& fedInfo);
36
44 ValueFederate(std::string_view fedName,
45 const std::shared_ptr<Core>& core,
46 const FederateInfo& fedInfo = FederateInfo{});
47
53 ValueFederate(std::string_view fedName,
54 CoreApp& core,
55 const FederateInfo& fedInfo = FederateInfo{});
56
61 explicit ValueFederate(const std::string& configString);
62
68 ValueFederate(std::string_view fedName, const std::string& configString);
69
71 explicit ValueFederate();
72
75 explicit ValueFederate(bool res);
76
79 explicit ValueFederate(const char* configString);
80
82 ValueFederate(const ValueFederate& fed) = delete;
84 ValueFederate(ValueFederate&& fed) noexcept;
86 virtual ~ValueFederate();
87
89 ValueFederate& operator=(ValueFederate&& fed) noexcept;
91 ValueFederate& operator=(const ValueFederate& fed) = delete;
99 Publication& registerPublication(std::string_view name,
100 std::string_view type,
101 std::string_view units = std::string_view{});
108 template<typename X>
109 Publication& registerPublication(std::string_view name,
110 std::string_view units = std::string_view{})
111 {
112 return registerPublication(name, ValueConverter<X>::type(), units);
113 }
114
122 Publication& registerGlobalPublication(std::string_view name,
123 std::string_view type,
124 std::string_view units = std::string_view{});
131 template<typename X>
132 Publication& registerGlobalPublication(std::string_view name,
133 std::string_view units = std::string_view{})
134 {
135 return registerGlobalPublication(name, ValueConverter<X>::type(), units);
136 }
137
146 template<typename X>
147 Publication& registerIndexedPublication(std::string_view name,
148 int index1,
149 std::string_view units = std::string_view{})
150 {
151 return registerGlobalPublication<X>(std::string(name) + '_' + std::to_string(index1),
152 units);
153 }
163 template<typename X>
164 Publication& registerIndexedPublication(std::string_view name,
165 int index1,
166 int index2,
167 std::string_view units = std::string_view{})
168 {
169 return registerGlobalPublication<X>(std::string(name) + '_' + std::to_string(index1) + '_' +
170 std::to_string(index2),
171 units);
172 }
173
180 Input& registerInput(std::string_view name,
181 std::string_view type,
182 std::string_view units = std::string_view{});
183
191 Input& registerGlobalInput(std::string_view name,
192 std::string_view type,
193 std::string_view units = std::string_view{});
196 template<typename X>
197 Input& registerInput(std::string_view name, std::string_view units = std::string_view{})
198 {
199 return registerInput(name, ValueConverter<X>::type(), units);
200 }
203 template<typename X>
204 Input& registerGlobalInput(std::string_view name, std::string_view units = std::string_view{})
205 {
206 return registerGlobalInput(name, ValueConverter<X>::type(), units);
207 }
208
216 template<typename X>
217 Input& registerIndexedInput(std::string_view name,
218 int index1,
219 std::string_view units = std::string_view())
220 {
221 return registerGlobalInput<X>(std::string(name) + '_' + std::to_string(index1), units);
222 }
223
233 template<typename X>
234 Input& registerIndexedInput(std::string_view name,
235 int index1,
236 int index2,
237 std::string_view units = std::string_view())
238 {
239 return registerGlobalInput<X>(std::string(name) + '_' + std::to_string(index1) + '_' +
240 std::to_string(index2),
241 units);
242 }
243
248 Input& registerSubscription(std::string_view target,
249 std::string_view units = std::string_view{});
250
257 Input& registerIndexedSubscription(std::string_view target,
258 int index1,
259 std::string_view units = std::string_view{})
260 {
261 return registerSubscription(std::string(target) + '_' + std::to_string(index1), units);
262 }
263
271 Input& registerIndexedSubscription(std::string_view target,
272 int index1,
273 int index2,
274 std::string_view units = std::string_view{})
275 {
276 return registerSubscription(std::string(target) + '_' + std::to_string(index1) + '_' +
277 std::to_string(index2),
278 units);
279 }
280
281 using Federate::addAlias;
288 void addAlias(const Input& inp, std::string_view shortcutName);
289
296 void addAlias(const Publication& pub, std::string_view shortcutName);
297
298 virtual void setFlagOption(int flag, bool flagValue = true) override;
299
306 void setDefaultValue(const Input& inp, data_view block);
307
314 virtual void registerInterfaces(const std::string& configString) override;
315
322 void registerValueInterfaces(const std::string& configString);
323
324 private:
325 void loadFederateData();
326
328 void registerValueInterfacesJson(const std::string& jsonString);
330 void registerValueInterfacesJsonDetail(const fileops::JsonBuffer& json, bool defaultGlobal);
332 void registerValueInterfacesToml(const std::string& tomlString);
333
334 public:
340 data_view getBytes(const Input& inp);
341
347 bool forceCoreUpdate(Input& inp);
348
354 void publishBytes(const Publication& pub, data_view block);
355
362 void publishBytes(const Publication& pub, const char* data, size_t data_size)
363 {
364 publishBytes(pub, data_view{data, data_size});
365 }
366
370 void registerFromPublicationJSON(const std::string& jsonString);
371
375 void publishJSON(const std::string& jsonString);
376
381 void addTarget(const Publication& pub, std::string_view target);
386 void addTarget(const Input& inp, std::string_view target);
391 void removeTarget(const Publication& pub, std::string_view target);
396 void removeTarget(const Input& inp, std::string_view target);
397
405 template<class iType>
406 void addIndexedTarget(const iType& iObject, std::string_view target, int index1)
407 {
408 addTarget(iObject, std::string(target) + '_' + std::to_string(index1));
409 }
410
418 template<class iType>
419 void addIndexedTarget(const iType& iObject, std::string_view target, int index1, int index2)
420 {
421 addTarget(iObject,
422 std::string(target) + '_' + std::to_string(index1) + '_' +
423 std::to_string(index2));
424 }
425
428 bool isUpdated(const Input& inp) const;
430 Time getLastUpdateTime(const Input& inp) const;
431
432 virtual void disconnect() override;
435 void clearUpdates();
438 void clearUpdate(const Input& inp);
439
440 protected:
441 virtual void updateTime(Time newTime, Time oldTime) override;
442 virtual void startupToInitializeStateTransition() override;
443 virtual void initializeToExecuteStateTransition(iteration_time result) override;
444 virtual std::string localQuery(std::string_view queryStr) const override;
445
446 public:
450 std::vector<int> queryUpdates();
451
454 const std::string& getTarget(const Input& inp) const;
458 const Input& getInput(std::string_view name) const;
462 Input& getInput(std::string_view name);
466 const Input& getInput(int index) const;
470 Input& getInput(int index);
474 const Input& getInput(std::string_view name, int index1) const;
478 const Input& getInput(std::string_view name, int index1, int index2) const;
479
483 [[deprecated("Use getInputByTarget")]] const Input&
484 getSubscription(std::string_view target) const
485 {
486 return getInputByTarget(target);
487 }
488
493 [[deprecated("Use getInputByTarget")]] Input& getSubscription(std::string_view target)
494 {
495 return getInputByTarget(target);
496 }
497
501 const Input& getInputByTarget(std::string_view target) const;
502
507 Input& getInputByTarget(std::string_view target);
508
513 Publication& getPublication(std::string_view name);
518 const Publication& getPublication(std::string_view name) const;
523 Publication& getPublication(int index);
527 const Publication& getPublication(int index) const;
528
533 const Publication& getPublication(std::string_view name, int index1) const;
539 const Publication& getPublication(std::string_view name, int index1, int index2) const;
540
545 void setInputNotificationCallback(std::function<void(Input&, Time)> callback);
550 void setInputNotificationCallback(Input& inp, std::function<void(Input&, Time)> callback);
551
553 int getPublicationCount() const;
555 int getInputCount() const;
556
557 private:
559 std::unique_ptr<ValueFederateManager> vfManager;
560 };
561
570 template<class... Us>
571 void publish(ValueFederate& fed, std::string_view pubName, Us... pargs)
572 {
573 fed.getPublication(pubName).publish(pargs...);
574 }
575
576 } // namespace helics
ValueConverter.hpp
helics::CoreApp
Definition: application_api/CoreApp.hpp:25
helics::FederateInfo
Definition: FederateInfo.hpp:28
helics::Federate
Definition: application_api/Federate.hpp:48
helics::Federate::addAlias
void addAlias(std::string_view interfaceName, std::string_view alias)
Definition: Federate.cpp:1921
helics::Input
Definition: Inputs.hpp:38
helics::Publication
Definition: Publications.hpp:25
helics::Publication::publish
void publish(double val)
Definition: Publications.cpp:70
helics::ValueConverter
Definition: ValueConverter.hpp:113
helics::ValueFederate
Definition: application_api/ValueFederate.hpp:28
helics::ValueFederate::registerGlobalInput
Input & registerGlobalInput(std::string_view name, std::string_view units=std::string_view{})
Definition: application_api/ValueFederate.hpp:204
helics::ValueFederate::registerInput
Input & registerInput(std::string_view name, std::string_view units=std::string_view{})
Definition: application_api/ValueFederate.hpp:197
helics::ValueFederate::ValueFederate
ValueFederate(ValueFederate &&fed) noexcept
helics::ValueFederate::registerIndexedInput
Input & registerIndexedInput(std::string_view name, int index1, int index2, std::string_view units=std::string_view())
Definition: application_api/ValueFederate.hpp:234
helics::ValueFederate::addIndexedTarget
void addIndexedTarget(const iType &iObject, std::string_view target, int index1)
Definition: application_api/ValueFederate.hpp:406
helics::ValueFederate::ValueFederate
ValueFederate()
helics::ValueFederate::addIndexedTarget
void addIndexedTarget(const iType &iObject, std::string_view target, int index1, int index2)
Definition: application_api/ValueFederate.hpp:419
helics::ValueFederate::registerIndexedSubscription
Input & registerIndexedSubscription(std::string_view target, int index1, std::string_view units=std::string_view{})
Definition: application_api/ValueFederate.hpp:257
helics::ValueFederate::getPublication
Publication & getPublication(std::string_view name)
Definition: ValueFederate.cpp:656
helics::ValueFederate::~ValueFederate
virtual ~ValueFederate()
helics::ValueFederate::operator=
ValueFederate & operator=(const ValueFederate &fed)=delete
helics::ValueFederate::getSubscription
Input & getSubscription(std::string_view target)
Definition: application_api/ValueFederate.hpp:493
helics::ValueFederate::publishBytes
void publishBytes(const Publication &pub, const char *data, size_t data_size)
Definition: application_api/ValueFederate.hpp:362
helics::ValueFederate::registerIndexedPublication
Publication & registerIndexedPublication(std::string_view name, int index1, std::string_view units=std::string_view{})
Definition: application_api/ValueFederate.hpp:147
helics::ValueFederate::registerIndexedInput
Input & registerIndexedInput(std::string_view name, int index1, std::string_view units=std::string_view())
Definition: application_api/ValueFederate.hpp:217
helics::ValueFederate::registerIndexedPublication
Publication & registerIndexedPublication(std::string_view name, int index1, int index2, std::string_view units=std::string_view{})
Definition: application_api/ValueFederate.hpp:164
helics::ValueFederate::ValueFederate
ValueFederate(const ValueFederate &fed)=delete
helics::ValueFederate::registerIndexedSubscription
Input & registerIndexedSubscription(std::string_view target, int index1, int index2, std::string_view units=std::string_view{})
Definition: application_api/ValueFederate.hpp:271
helics::ValueFederate::registerGlobalPublication
Publication & registerGlobalPublication(std::string_view name, std::string_view units=std::string_view{})
Definition: application_api/ValueFederate.hpp:132
helics::ValueFederate::registerPublication
Publication & registerPublication(std::string_view name, std::string_view units=std::string_view{})
Definition: application_api/ValueFederate.hpp:109
helics::ValueFederate::getSubscription
const Input & getSubscription(std::string_view target) const
Definition: application_api/ValueFederate.hpp:484
helics::data_view
Definition: data_view.hpp:22
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::publish
void publish(ValueFederate &fed, std::string_view pubName, Us... pargs)
Definition: application_api/ValueFederate.hpp:571
helics::iteration_time
Definition: helicsTime.hpp:43