helics  3.0.1
Clone.hpp
1 /*
2 Copyright (c) 2017-2021,
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 
8 #pragma once
9 #include "../application_api/Endpoints.hpp"
10 #include "../application_api/Publications.hpp"
11 #include "../application_api/Subscriptions.hpp"
12 #include "helicsApp.hpp"
13 
14 #include <map>
15 #include <memory>
16 #include <set>
17 #include <string>
18 #include <tuple>
19 #include <utility>
20 #include <vector>
21 
22 namespace helics {
23 class CloningFilter;
24 
25 namespace apps {
27  class HELICS_CXX_EXPORT Clone: public App {
28  public:
34  Clone(const std::string& appName, FederateInfo& fi);
38  explicit Clone(std::vector<std::string> args);
40  Clone(int argc, char* argv[]);
41 
48  Clone(const std::string& appName,
49  const std::shared_ptr<Core>& core,
50  const FederateInfo& fi);
51 
57  Clone(const std::string& appName, CoreApp& core, const FederateInfo& fi);
58 
63  Clone(const std::string& appName, const std::string& jsonString);
65  Clone(Clone&& other_recorder) = default;
67  Clone& operator=(Clone&& record) = default;
69  ~Clone();
71  virtual void runTo(Time runToTime) override;
73  void saveFile(const std::string& filename = std::string{});
75  auto pointCount() const { return points.size(); }
77  auto messageCount() const { return messages.size(); }
82  std::tuple<Time, std::string, std::string> getValue(int index) const;
87  std::unique_ptr<Message> getMessage(int index) const;
88 
92  void setFederateToClone(const std::string& federateName);
95  void setOutputFile(std::string fileName) { outFileName = std::move(fileName); }
96 
97  private:
99  void addSubscription(const std::string& key);
100 
102  void addSourceEndpointClone(const std::string& sourceEndpoint);
103 
104  virtual void initialize() override;
105  void generateInterfaces();
106  void captureForCurrentTime(Time currentTime, int iteration = 0);
108  std::shared_ptr<helicsCLI11App> buildArgParserApp();
110  void processArgs();
111 
112  protected:
114  class ValueCapture {
115  public:
116  helics::Time time;
117  int index{-1};
118  int16_t iteration{0};
119  bool first{false};
120  std::string value;
121  ValueCapture() = default;
122  ValueCapture(helics::Time t1, int id1, const std::string& val):
123  time(t1), index(id1), value(val)
124  {
125  }
126  };
127 
128  bool allow_iteration{false};
129  bool verbose{false};
130  bool fileSaved{false};
131  Time nextPrintTimeStep{
133  std::unique_ptr<CloningFilter> cFilt;
134  std::vector<ValueCapture> points;
135  std::vector<Input> subscriptions;
136  std::vector<std::string>
138  std::unique_ptr<Endpoint> cloneEndpoint;
139  std::vector<std::unique_ptr<Message>> messages;
140  std::map<helics::InterfaceHandle, int> subids;
141  std::map<std::string, int> subkeys;
142  std::map<helics::InterfaceHandle, int> eptids; // translate subscription id to index
143  std::map<std::string, int> eptNames;
144  std::string captureFederate;
145  std::string fedConfig;
146  std::string outFileName{"clone.json"};
147  std::vector<int> pubPointCount;
148  };
149 
150 } // namespace apps
151 } // namespace helics
helics::timeZero
constexpr Time timeZero
Definition: helicsTime.hpp:31
helics::apps::Clone::cFilt
std::unique_ptr< CloningFilter > cFilt
a pointer to a clone filter
Definition: Clone.hpp:133
helics::apps::App::loadJsonFile
virtual void loadJsonFile(const std::string &jsonString)
Definition: helicsApp.cpp:154
helics::apps::Clone::points
std::vector< ValueCapture > points
lists of points that were captured
Definition: Clone.hpp:134
HELICS_SEQUENCING_MODE_ORDERED
@ HELICS_SEQUENCING_MODE_ORDERED
Definition: helics_enums.h:367
helics::apps::Clone::saveFile
void saveFile(const std::string &filename=std::string{})
Definition: Clone.cpp:100
helics::queryFederateSubscriptions
std::string queryFederateSubscriptions(helics::Federate *fed, const std::string &fedName)
Definition: queryFunctions.cpp:175
helics::apps::Clone::fedConfig
std::string fedConfig
storage for the federateConfiguration
Definition: Clone.hpp:145
helics::Time
TimeRepresentation< count_time< 9 > > Time
Definition: helicsTime.hpp:27
helics::apps::Clone::subkeys
std::map< std::string, int > subkeys
translate subscription names to an index
Definition: Clone.hpp:141
helics::apps::Clone::subscriptions
std::vector< Input > subscriptions
the actual subscription objects
Definition: Clone.hpp:135
helics::apps::Clone::ValueCapture
Definition: Clone.hpp:114
HELICS_FLAG_OBSERVER
@ HELICS_FLAG_OBSERVER
Definition: helics_enums.h:99
helics::apps::Clone::messageCount
auto messageCount() const
Definition: Clone.hpp:77
helics::apps::Clone::verbose
bool verbose
print all captured values to the screen
Definition: Clone.hpp:129
helics::apps::App::masterFileName
std::string masterFileName
the name of the master file used to do the construction
Definition: helicsApp.hpp:129
helics::apps::Clone::fileSaved
bool fileSaved
true if the file has been saved already
Definition: Clone.hpp:130
helics::apps::Clone::cloneEndpoint
std::unique_ptr< Endpoint > cloneEndpoint
the endpoint for cloned message delivery
Definition: Clone.hpp:138
helics::apps::Clone::setOutputFile
void setOutputFile(std::string fileName)
Definition: Clone.hpp:95
helics::apps::Clone::messages
std::vector< std::unique_ptr< Message > > messages
list of messages
Definition: Clone.hpp:139
helics::FederateInfo
Definition: FederateInfo.hpp:20
helics::apps::Clone::subids
std::map< helics::InterfaceHandle, int > subids
map of the subscription ids
Definition: Clone.hpp:140
helics::apps::Clone::~Clone
~Clone()
Definition: Clone.cpp:89
helics::apps::Clone::allow_iteration
bool allow_iteration
trigger to allow Iteration
Definition: Clone.hpp:128
helics::apps::Clone
Definition: Clone.hpp:27
helics::apps::Clone::nextPrintTimeStep
Time nextPrintTimeStep
the time advancement period for printing markers
Definition: Clone.hpp:131
helics::IterationRequest::ITERATE_IF_NEEDED
@ ITERATE_IF_NEEDED
indicator that the iterations need to continue
helics::apps::Clone::outFileName
std::string outFileName
the final output file
Definition: Clone.hpp:146
helics::apps::Clone::getMessage
std::unique_ptr< Message > getMessage(int index) const
Definition: Clone.cpp:344
helics::apps::Clone::cloneSubscriptionNames
std::vector< std::string > cloneSubscriptionNames
string of the subscriptions of the cloned federate
Definition: Clone.hpp:137
helics
the main namespace for the helics co-simulation library User functions will be in the helics namespac...
Definition: AsyncFedCallInfo.hpp:14
helics::apps::Clone::captureFederate
std::string captureFederate
storage for the name of the federate to clone
Definition: Clone.hpp:144
helics::IterationResult::NEXT_STEP
@ NEXT_STEP
helics::apps::Clone::runTo
virtual void runTo(Time runToTime) override
Definition: Clone.cpp:271
helics::FunctionExecutionFailure
Definition: core-exceptions.hpp:29
helics::apps::Clone::pointCount
auto pointCount() const
Definition: Clone.hpp:75
helics::apps::App::fed
std::shared_ptr< CombinationFederate > fed
the federate created for the Player
Definition: helicsApp.hpp:127
helics::apps::Clone::eptNames
std::map< std::string, int > eptNames
translate endpoint name to index
Definition: Clone.hpp:143
helics::getValue
X getValue(ValueFederate &fed, const std::string &key)
Definition: Subscriptions.hpp:30
helics::apps::App
Definition: helicsApp.hpp:28
helics::vectorizeQueryResult
std::vector< std::string > vectorizeQueryResult(std::string &&queryres)
Definition: queryFunctions.cpp:19
helics::waitForInit
bool waitForInit(helics::Federate *fed, const std::string &fedName, std::chrono::milliseconds timeout)
Definition: queryFunctions.cpp:136
helics::apps::Clone::getValue
std::tuple< Time, std::string, std::string > getValue(int index) const
Definition: Clone.cpp:334
helics::apps::App::loadFile
void loadFile(const std::string &filename)
Definition: helicsApp.cpp:124
helics::apps::Clone::setFederateToClone
void setFederateToClone(const std::string &federateName)
Definition: Clone.cpp:329
helics::apps::Clone::Clone
Clone(const std::string &appName, FederateInfo &fi)
Definition: Clone.cpp:44
helics::apps::Clone::pubPointCount
std::vector< int > pubPointCount
a vector containing the counts of each publication
Definition: Clone.hpp:147
helics::isValidIndex
bool isValidIndex(sizeType testSize, const SizedDataType &vec)
Definition: core-data.hpp:132