helics  3.3.0
Recorder.hpp
1 /*
2 Copyright (c) 2017-2022,
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 <deque>
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 Recorder: public App {
28  public:
34  Recorder(std::string_view name, FederateInfo& fi);
38  explicit Recorder(std::vector<std::string> args);
40  Recorder(int argc, char* argv[]);
41 
48  Recorder(std::string_view name, const std::shared_ptr<Core>& core, const FederateInfo& fi);
54  Recorder(std::string_view name, CoreApp& core, const FederateInfo& fi);
59  Recorder(std::string_view name, const std::string& jsonString);
61  Recorder(Recorder&& other_recorder) = default;
63  Recorder& operator=(Recorder&& other_recorder) = default;
65  ~Recorder();
67  virtual void runTo(Time runToTime) override;
69  void addSubscription(std::string_view key);
71  void addEndpoint(std::string_view endpoint);
73  void addSourceEndpointClone(std::string_view sourceEndpoint);
75  void addDestEndpointClone(std::string_view destEndpoint);
79  void addCapture(std::string_view captureDesc);
81  void saveFile(const std::string& filename);
83  auto pointCount() const { return points.size(); }
85  auto messageCount() const { return messages.size(); }
91  std::tuple<Time, std::string_view, std::string> getValue(std::size_t index) const;
96  std::unique_ptr<Message> getMessage(std::size_t index) const;
97 
98  private:
102  virtual void loadJsonFile(const std::string& jsonString) override;
104  virtual void loadTextFile(const std::string& textFile) override;
106  void writeJsonFile(const std::string& filename);
108  void writeTextFile(const std::string& filename);
109 
110  virtual void initialize() override;
111  void generateInterfaces();
112  void captureForCurrentTime(Time currentTime, int iteration = 0);
113  void loadCaptureInterfaces();
114 
116  std::shared_ptr<helicsCLI11App> buildArgParserApp();
118  void processArgs();
119 
120  protected:
122  class ValueCapture {
123  public:
124  helics::Time time;
125  int index{-1};
126  int16_t iteration{0};
127  bool first{false};
128  std::string value;
129  ValueCapture() = default;
130  ValueCapture(helics::Time t1, int id1, std::string_view val):
131  time(t1), index(id1), value(val)
132  {
133  }
134  };
135 
137  class ValueStats {
138  public:
139  helics::Time time{helics::Time::minVal()};
140  std::string lastVal;
141  std::string key;
142  int cnt{0};
143  };
144 
145  bool allow_iteration{false};
146  bool verbose{false};
147  Time nextPrintTimeStep{
149  std::unique_ptr<CloningFilter> cFilt;
150  std::vector<ValueCapture> points;
151  std::deque<Input> subscriptions;
152  std::vector<std::string> targets;
153  std::deque<Endpoint> endpoints;
154  std::unique_ptr<Endpoint> cloneEndpoint;
155  std::vector<std::unique_ptr<Message>> messages;
156  std::map<helics::InterfaceHandle, int> subids;
157  std::map<std::string_view, int> subkeys;
158  std::map<helics::InterfaceHandle, int> eptids; // translate subscription id to index
159  std::map<std::string_view, 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: helicsTime.hpp:31
helics::apps::Recorder::addSourceEndpointClone
void addSourceEndpointClone(std::string_view sourceEndpoint)
Definition: Recorder.cpp:545
helics::apps::Recorder::saveFile
void saveFile(const std::string &filename)
Definition: Recorder.cpp:587
helics::apps::Recorder::getMessage
std::unique_ptr< Message > getMessage(std::size_t index) const
Definition: Recorder.cpp:578
HELICS_SEQUENCING_MODE_ORDERED
@ HELICS_SEQUENCING_MODE_ORDERED
Definition: helics_enums.h:399
helics::apps::Recorder::eptNames
std::map< std::string_view, int > eptNames
translate endpoint name to index
Definition: Recorder.hpp:159
helics::apps::Recorder::runTo
virtual void runTo(Time runToTime) override
Definition: Recorder.cpp:456
helics::apps::Recorder::addEndpoint
void addEndpoint(std::string_view endpoint)
Definition: Recorder.cpp:533
helics::apps::Recorder::pointCount
auto pointCount() const
Definition: Recorder.hpp:83
helics::getValue
X getValue(ValueFederate &fed, std::string_view key)
Definition: Subscriptions.hpp:31
helics::apps::Recorder::subkeys
std::map< std::string_view, int > subkeys
translate subscription names to an index
Definition: Recorder.hpp:157
helics::Time
TimeRepresentation< count_time< 9 > > Time
Definition: helicsTime.hpp:27
helics::apps::Recorder::outFileName
std::string outFileName
the final output file
Definition: Recorder.hpp:163
helics::apps::Recorder::getValue
std::tuple< Time, std::string_view, std::string > getValue(std::size_t index) const
Definition: Recorder.cpp:570
helics::vectorizeQueryResult
std::vector< std::string > vectorizeQueryResult(std::string_view queryres)
Definition: queryFunctions.cpp:19
HELICS_FLAG_OBSERVER
@ HELICS_FLAG_OBSERVER
Definition: helics_enums.h:101
helics::apps::Recorder::points
std::vector< ValueCapture > points
lists of points that were captured
Definition: Recorder.hpp:150
helics::apps::App::masterFileName
std::string masterFileName
the name of the master file used to do the construction
Definition: helicsApp.hpp:132
helics::apps::App::loadJsonFileConfiguration
void loadJsonFileConfiguration(const std::string &appName, const std::string &jsonString)
Definition: helicsApp.cpp:158
helics::apps::Recorder::addSubscription
void addSubscription(std::string_view key)
Definition: Recorder.cpp:520
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:85
helics::apps::Recorder::subids
std::map< helics::InterfaceHandle, int > subids
map of the subscription ids
Definition: Recorder.hpp:156
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:24
helics::apps::Recorder::nextPrintTimeStep
Time nextPrintTimeStep
the time advancement period for printing markers
Definition: Recorder.hpp:147
helics::apps::Recorder::endpoints
std::deque< Endpoint > endpoints
the actual endpoint objects
Definition: Recorder.hpp:153
helics::apps::Recorder::messages
std::vector< std::unique_ptr< Message > > messages
list of messages
Definition: Recorder.hpp:155
helics::apps::Recorder::addDestEndpointClone
void addDestEndpointClone(std::string_view destEndpoint)
Definition: Recorder.cpp:555
helics::IterationRequest::ITERATE_IF_NEEDED
@ ITERATE_IF_NEEDED
indicator that the iterations need to continue
helics::apps::Recorder::ValueCapture
Definition: Recorder.hpp:122
helics::waitForInit
bool waitForInit(helics::Federate *fed, std::string_view fedName, std::chrono::milliseconds timeout)
Definition: queryFunctions.cpp:101
helics::apps::Recorder::targets
std::vector< std::string > targets
specified targets for the subscriptions
Definition: Recorder.hpp:152
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::Recorder
Recorder(std::string_view name, FederateInfo &fi)
Definition: Recorder.cpp:43
helics::IterationResult::NEXT_STEP
@ NEXT_STEP
helics::apps::Recorder::verbose
bool verbose
print all captured values to the screen
Definition: Recorder.hpp:146
helics::apps::App::fed
std::shared_ptr< CombinationFederate > fed
the federate created for the Player
Definition: helicsApp.hpp:130
helics::apps::Recorder::ValueStats
Definition: Recorder.hpp:137
helics::apps::Recorder::cFilt
std::unique_ptr< CloningFilter > cFilt
a pointer to a clone filter
Definition: Recorder.hpp:149
helics::apps::Recorder::addCapture
void addCapture(std::string_view captureDesc)
Definition: Recorder.cpp:565
helics::apps::App
Definition: helicsApp.hpp:29
helics::Federate::Modes::STARTUP
@ STARTUP
helics::apps::App::loadFile
void loadFile(const std::string &filename)
Definition: helicsApp.cpp:123
helics::apps::Recorder
Definition: Recorder.hpp:27
helics::apps::Recorder::subscriptions
std::deque< Input > subscriptions
the actual subscription objects
Definition: Recorder.hpp:151
helics::apps::Recorder::~Recorder
~Recorder()
Definition: Recorder.cpp:94
helics::apps::Recorder::allow_iteration
bool allow_iteration
trigger to allow Iteration
Definition: Recorder.hpp:145
helics::isValidIndex
bool isValidIndex(sizeType testSize, const SizedDataType &vec)
Definition: core-data.hpp:171