helics  3.3.0
Player.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 #pragma once
8 
9 #include "../application_api/Endpoints.hpp"
10 #include "../application_api/HelicsPrimaryTypes.hpp"
11 #include "../application_api/Publications.hpp"
12 #include "helicsApp.hpp"
13 
14 #include <deque>
15 #include <map>
16 #include <memory>
17 #include <set>
18 #include <string>
19 #include <vector>
20 
21 namespace helics {
22 namespace apps {
23  struct ValueSetter {
24  Time time;
25  int iteration = 0;
26  int index;
27  std::string type;
28  std::string pubName;
29  defV value;
30  };
31 
32  struct MessageHolder {
33  Time sendTime;
34  int index;
35  Message mess;
36  };
37 
43  class HELICS_CXX_EXPORT Player: public App {
44  public:
46  Player() = default;
50  explicit Player(std::vector<std::string> args);
55  Player(int argc, char* argv[]);
60  explicit Player(std::string_view name, const FederateInfo& fi);
66  Player(std::string_view name, const std::shared_ptr<Core>& core, const FederateInfo& fi);
72  Player(std::string_view name, CoreApp& core, const FederateInfo& fi);
78  Player(std::string_view appName, const std::string& configString);
79 
81  Player(Player&& other_player) = default;
83  Player& operator=(Player&& fed) = default;
84 
91  virtual void initialize() override;
92 
96  virtual void runTo(Time stopTime_input) override;
97 
103  void addPublication(std::string_view key,
104  DataType type,
105  std::string_view pubUnits = std::string_view());
106 
111  template<class valType>
112  typename std::enable_if_t<helicsType<valType>() != DataType::HELICS_CUSTOM>
113  addPublication(std::string_view key, std::string_view pubUnits = std::string_view())
114  {
115  if (!useLocal) {
116  publications.emplace_back(
117  InterfaceVisibility::GLOBAL, fed.get(), key, helicsType<valType>(), pubUnits);
118  } else {
119  publications.emplace_back(fed.get(), key, helicsType<valType>(), pubUnits);
120  }
121 
122  pubids[key] = static_cast<int>(publications.size()) - 1;
123  }
124 
129  void addEndpoint(std::string_view endpointName,
130  std::string_view endpointType = std::string_view());
136  template<class valType>
137  void addPoint(Time pubTime, std::string_view key, const valType& val)
138  {
139  points.resize(points.size() + 1);
140  points.back().time = pubTime;
141  points.back().pubName = key;
142  points.back().value = val;
143  }
144 
151  template<class valType>
152  void addPoint(Time pubTime, int iteration, std::string_view key, const valType& val)
153  {
154  points.resize(points.size() + 1);
155  points.back().time = pubTime;
156  points.back().iteration = iteration;
157  points.back().pubName = key;
158  points.back().value = val;
159  }
166  void addMessage(Time sendTime,
167  std::string_view src,
168  std::string_view dest,
169  std::string_view payload);
170 
178  void addMessage(Time sendTime,
179  Time actionTime,
180  std::string_view src,
181  std::string_view dest,
182  std::string_view payload);
183 
185  auto pointCount() const { return points.size(); }
187  auto messageCount() const { return messages.size(); }
189  auto publicationCount() const { return publications.size(); }
191  auto endpointCount() const { return endpoints.size(); }
193  const auto& getPoint(int index) const { return points[index]; }
195  const auto& getMessage(int index) const { return messages[index]; }
196 
197  private:
198  std::unique_ptr<helicsCLI11App> generateParser();
200  void processArgs();
204  virtual void loadJsonFile(const std::string& jsonString) override;
206  virtual void loadTextFile(const std::string& filename) override;
208  void sortTags();
210  void generatePublications();
212  void generateEndpoints();
214  void cleanUpPointList();
215 
217  void sendInformation(Time sendTime, int iteration = 0);
218 
223  helics::Time extractTime(std::string_view str, int lineNumber = 0) const;
224 
225  private:
226  std::vector<ValueSetter> points;
227  std::vector<MessageHolder> messages;
228  std::map<std::string, std::string> tags;
229  std::set<std::string> epts;
230  std::deque<Publication> publications;
231  std::deque<Endpoint> endpoints;
232  std::map<std::string_view, int> pubids;
233  std::map<std::string_view, int> eptids;
234  helics::DataType defType =
235  helics::DataType::HELICS_STRING;
236  size_t pointIndex = 0;
237  size_t messageIndex = 0;
238  time_units units = time_units::sec;
239  double timeMultiplier =
240  1.0;
241  Time nextPrintTimeStep =
243  };
244 } // namespace apps
245 } // namespace helics
helics::timeZero
constexpr Time timeZero
Definition: helicsTime.hpp:31
helics::defV
std::variant< double, int64_t, std::string, std::complex< double >, std::vector< double >, std::vector< std::complex< double > >, NamedPoint > defV
Definition: HelicsPrimaryTypes.hpp:34
helics::apps::Player::addPublication
std::enable_if_t< helicsType< valType >) !=DataType::HELICS_CUSTOM > addPublication(std::string_view key, std::string_view pubUnits=std::string_view())
Definition: Player.hpp:113
helics::apps::Player
Definition: Player.hpp:43
helics::apps::MessageHolder
Definition: Player.hpp:32
helics::apps::Player::getPoint
const auto & getPoint(int index) const
Definition: Player.hpp:193
helics::apps::Player::Player
Player()=default
helics::apps::Player::getMessage
const auto & getMessage(int index) const
Definition: Player.hpp:195
helics::Time
TimeRepresentation< count_time< 9 > > Time
Definition: helicsTime.hpp:27
HELICS_FLAG_SOURCE_ONLY
@ HELICS_FLAG_SOURCE_ONLY
Definition: helics_enums.h:107
helics::getTypeFromString
DataType getTypeFromString(std::string_view typeName)
Definition: helicsTypes.cpp:238
helics::apps::ValueSetter
Definition: Player.hpp:23
helics::IterationRequest::FORCE_ITERATION
@ FORCE_ITERATION
force an iteration whether it is needed or not
helics::apps::App::masterFileName
std::string masterFileName
the name of the master file used to do the construction
Definition: helicsApp.hpp:132
helics::DataType
DataType
Definition: helicsTypes.hpp:273
helics::apps::App::loadJsonFileConfiguration
void loadJsonFileConfiguration(const std::string &appName, const std::string &jsonString)
Definition: helicsApp.cpp:158
helics::apps::Player::addMessage
void addMessage(Time sendTime, std::string_view src, std::string_view dest, std::string_view payload)
Definition: Player.cpp:148
helics::apps::Player::initialize
virtual void initialize() override
Definition: Player.cpp:624
helics::FederateInfo
Definition: FederateInfo.hpp:24
helics::apps::Player::addPublication
void addPublication(std::string_view key, DataType type, std::string_view pubUnits=std::string_view())
Definition: Player.cpp:739
helics::apps::Player::endpointCount
auto endpointCount() const
Definition: Player.hpp:191
helics::apps::App::loadTextFile
virtual void loadTextFile(const std::string &textFile)
Definition: helicsApp.cpp:133
helics
the main namespace for the helics co-simulation library User functions will be in the helics namespac...
Definition: AsyncFedCallInfo.hpp:14
helics::apps::Player::runTo
virtual void runTo(Time stopTime_input) override
Definition: Player.cpp:668
helics::apps::App::fed
std::shared_ptr< CombinationFederate > fed
the federate created for the Player
Definition: helicsApp.hpp:130
helics::apps::Player::publicationCount
auto publicationCount() const
Definition: Player.hpp:189
helics::apps::App
Definition: helicsApp.hpp:29
helics::loadTimeFromString
Time loadTimeFromString(std::string_view timeString)
Definition: timeOperations.cpp:21
helics::Federate::Modes::STARTUP
@ STARTUP
helics::apps::Player::addPoint
void addPoint(Time pubTime, int iteration, std::string_view key, const valType &val)
Definition: Player.hpp:152
helics::apps::Player::pointCount
auto pointCount() const
Definition: Player.hpp:185
helics::apps::App::loadFile
void loadFile(const std::string &filename)
Definition: helicsApp.cpp:123
helics::apps::Player::addEndpoint
void addEndpoint(std::string_view endpointName, std::string_view endpointType=std::string_view())
Definition: Player.cpp:761
helics::apps::Player::addPoint
void addPoint(Time pubTime, std::string_view key, const valType &val)
Definition: Player.hpp:137
helics::Federate::Modes::EXECUTING
@ EXECUTING
helics::Message
Definition: core-data.hpp:29
helics::isValidIndex
bool isValidIndex(sizeType testSize, const SizedDataType &vec)
Definition: core-data.hpp:171
helics::apps::Player::messageCount
auto messageCount() const
Definition: Player.hpp:187