helics  2.8.1
Recorder.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/Subscriptions.hpp"
11 #include "helicsApp.hpp"
12 
13 #include <map>
14 #include <memory>
15 #include <set>
16 #include <string>
17 #include <tuple>
18 #include <utility>
19 #include <vector>
20 
21 namespace helics {
22 class CloningFilter;
23 
24 namespace apps {
26  class HELICS_CXX_EXPORT Recorder: public App {
27  public:
33  Recorder(const std::string& name, FederateInfo& fi);
37  explicit Recorder(std::vector<std::string> args);
39  Recorder(int argc, char* argv[]);
40 
47  Recorder(const std::string& name,
48  const std::shared_ptr<Core>& core,
49  const FederateInfo& fi);
55  Recorder(const std::string& name, CoreApp& core, const FederateInfo& fi);
60  Recorder(const std::string& name, const std::string& jsonString);
62  Recorder(Recorder&& other_recorder) = default;
64  Recorder& operator=(Recorder&& other_recorder) = default;
66  ~Recorder();
68  virtual void runTo(Time runToTime) override;
70  void addSubscription(const std::string& key);
72  void addEndpoint(const std::string& endpoint);
74  void addSourceEndpointClone(const std::string& sourceEndpoint);
76  void addDestEndpointClone(const std::string& destEndpoint);
80  void addCapture(const std::string& captureDesc);
82  void saveFile(const std::string& filename);
84  auto pointCount() const { return points.size(); }
86  auto messageCount() const { return messages.size(); }
92  std::tuple<Time, std::string, std::string> getValue(int index) const;
97  std::unique_ptr<Message> getMessage(int index) const;
98 
99  private:
103  virtual void loadJsonFile(const std::string& jsonString) override;
105  virtual void loadTextFile(const std::string& textFile) override;
107  void writeJsonFile(const std::string& filename);
109  void writeTextFile(const std::string& filename);
110 
111  virtual void initialize() override;
112  void generateInterfaces();
113  void captureForCurrentTime(Time currentTime, int iteration = 0);
114  void loadCaptureInterfaces();
115 
117  std::shared_ptr<helicsCLI11App> buildArgParserApp();
119  void processArgs();
120 
121  protected:
123  class ValueCapture {
124  public:
125  helics::Time time;
126  int index{-1};
127  int16_t iteration{0};
128  bool first{false};
129  std::string value;
130  ValueCapture() = default;
131  ValueCapture(helics::Time t1, int id1, const std::string& val):
132  time(t1), index(id1), value(val)
133  {
134  }
135  };
136 
138  class ValueStats {
139  public:
140  helics::Time time{helics::Time::minVal()};
141  std::string lastVal;
142  std::string key;
143  int cnt{0};
144  };
145 
146  bool allow_iteration{false};
147  bool verbose{false};
148  Time nextPrintTimeStep{
150  std::unique_ptr<CloningFilter> cFilt;
151  std::vector<ValueCapture> points;
152  std::vector<Input> subscriptions;
153  std::vector<Endpoint> endpoints;
154  std::unique_ptr<Endpoint> cloneEndpoint;
155  std::vector<std::unique_ptr<Message>> messages;
156  std::map<helics::interface_handle, int> subids;
157  std::map<std::string, int> subkeys;
158  std::map<helics::interface_handle, int> eptids; // translate subscription id to index
159  std::map<std::string, int> eptNames;
160  std::vector<ValueStats> vStat;
161  std::vector<std::string> captureInterfaces;
162  std::string mapfile;
163  std::string outFileName{"out.txt"};
164  };
165 
166 } // namespace apps
167 } // namespace helics
helics::timeZero
constexpr Time timeZero
Definition: helics-time.hpp:31
helics::apps::Recorder::saveFile
void saveFile(const std::string &filename)
Definition: Recorder.cpp:587
helics::apps::Recorder::subscriptions
std::vector< Input > subscriptions
the actual subscription objects
Definition: Recorder.hpp:152
helics::iteration_result::next_step
@ next_step
helics::apps::Recorder::runTo
virtual void runTo(Time runToTime) override
Definition: Recorder.cpp:455
helics::apps::Recorder::pointCount
auto pointCount() const
Definition: Recorder.hpp:84
helics::Time
TimeRepresentation< count_time< 9 > > Time
Definition: helics-time.hpp:27
helics::apps::Recorder::outFileName
std::string outFileName
the final output file
Definition: Recorder.hpp:163
helics_sequencing_mode_ordered
@ helics_sequencing_mode_ordered
Definition: helics_enums.h:337
helics::apps::Recorder::addSourceEndpointClone
void addSourceEndpointClone(const std::string &sourceEndpoint)
Definition: Recorder.cpp:543
helics::apps::Recorder::points
std::vector< ValueCapture > points
lists of points that were captured
Definition: Recorder.hpp:151
helics::apps::App::masterFileName
std::string masterFileName
the name of the master file used to do the construction
Definition: helicsApp.hpp:129
helics::apps::Recorder::subids
std::map< helics::interface_handle, int > subids
map of the subscription ids
Definition: Recorder.hpp:156
helics::apps::App::loadJsonFileConfiguration
void loadJsonFileConfiguration(const std::string &appName, const std::string &jsonString)
Definition: helicsApp.cpp:159
helics::apps::Recorder::cloneEndpoint
std::unique_ptr< Endpoint > cloneEndpoint
the endpoint for cloned message delivery
Definition: Recorder.hpp:154
helics::apps::Recorder::mapfile
std::string mapfile
file name for the on-line file updater
Definition: Recorder.hpp:162
helics::apps::Recorder::messageCount
auto messageCount() const
Definition: Recorder.hpp:86
helics::iteration_request::iterate_if_needed
@ iterate_if_needed
indicator that the iterations need to continue
helics::apps::Recorder::vStat
std::vector< ValueStats > vStat
storage for statistics capture
Definition: Recorder.hpp:160
helics::apps::Recorder::captureInterfaces
std::vector< std::string > captureInterfaces
storage for the interfaces to capture
Definition: Recorder.hpp:161
helics::FederateInfo
Definition: FederateInfo.hpp:20
helics::apps::Recorder::nextPrintTimeStep
Time nextPrintTimeStep
the time advancement period for printing markers
Definition: Recorder.hpp:148
helics::apps::Recorder::messages
std::vector< std::unique_ptr< Message > > messages
list of messages
Definition: Recorder.hpp:155
loadJson
Json::Value loadJson(const std::string &jsonString)
Definition: JsonProcessingFunctions.cpp:24
helics::Federate::modes::startup
@ startup
when created the federate is in startup state
helics::apps::Recorder::ValueCapture
Definition: Recorder.hpp:123
helics::apps::Recorder::endpoints
std::vector< Endpoint > endpoints
the actual endpoint objects
Definition: Recorder.hpp:153
helics::apps::Recorder::Recorder
Recorder(const std::string &name, FederateInfo &fi)
Definition: Recorder.cpp:44
helics::apps::Recorder::addEndpoint
void addEndpoint(const std::string &endpoint)
Definition: Recorder.cpp:531
helics::apps::Recorder::addCapture
void addCapture(const std::string &captureDesc)
Definition: Recorder.cpp:563
helics::apps::Recorder::addSubscription
void addSubscription(const std::string &key)
Definition: Recorder.cpp:519
helics
the main namespace for the helics co-simulation library User functions will be in the helics namespac...
Definition: AsyncFedCallInfo.hpp:14
helics::apps::Recorder::getMessage
std::unique_ptr< Message > getMessage(int index) const
Definition: Recorder.cpp:578
helics::apps::Recorder::addDestEndpointClone
void addDestEndpointClone(const std::string &destEndpoint)
Definition: Recorder.cpp:553
helics::apps::Recorder::verbose
bool verbose
print all captured values to the screen
Definition: Recorder.hpp:147
helics::apps::Recorder::getValue
std::tuple< Time, std::string, std::string > getValue(int index) const
Definition: Recorder.cpp:568
helics_flag_observer
@ helics_flag_observer
Definition: helics_enums.h:86
helics::apps::App::fed
std::shared_ptr< CombinationFederate > fed
the federate created for the Player
Definition: helicsApp.hpp:127
helics::apps::Recorder::ValueStats
Definition: Recorder.hpp:138
helics::apps::Recorder::cFilt
std::unique_ptr< CloningFilter > cFilt
a pointer to a clone filter
Definition: Recorder.hpp:150
helics::getValue
X getValue(ValueFederate &fed, const std::string &key)
Definition: Subscriptions.hpp:67
helics::apps::App
Definition: helicsApp.hpp:28
helics::vectorizeQueryResult
std::vector< std::string > vectorizeQueryResult(std::string &&queryres)
Definition: queryFunctions.cpp:17
helics::waitForInit
bool waitForInit(helics::Federate *fed, const std::string &fedName, std::chrono::milliseconds timeout)
Definition: queryFunctions.cpp:90
helics::apps::Recorder::subkeys
std::map< std::string, int > subkeys
translate subscription names to an index
Definition: Recorder.hpp:157
helics::apps::App::loadFile
void loadFile(const std::string &filename)
Definition: helicsApp.cpp:124
helics::apps::Recorder
Definition: Recorder.hpp:26
helics::apps::Recorder::~Recorder
~Recorder()
Definition: Recorder.cpp:92
helics::apps::Recorder::allow_iteration
bool allow_iteration
trigger to allow Iteration
Definition: Recorder.hpp:146
helics::apps::Recorder::eptNames
std::map< std::string, int > eptNames
translate endpoint name to index
Definition: Recorder.hpp:159
helics::isValidIndex
bool isValidIndex(sizeType testSize, const SizedDataType &vec)
Definition: core-data.hpp:249