helics  3.5.2
helicsApp.hpp
1 /*
2 Copyright (c) 2017-2024,
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/CombinationFederate.hpp"
10 
11 #include <fstream>
12 #include <memory>
13 #include <string>
14 #include <string_view>
15 #include <vector>
16 
17 namespace CLI {
18 class App;
19 } // namespace CLI
20 namespace Json {
21 class Value;
22 } // namespace Json
23 
24 namespace helics::apps {
25 
26 class AppTextParser;
27 
32 class HELICS_CXX_EXPORT App {
33  public:
35  App() = default;
40  App(std::string_view defaultAppName, std::vector<std::string> args);
46  App(std::string_view defaultAppName, int argc, char* argv[]);
52  App(std::string_view appName, const FederateInfo& fedInfo);
58  App(std::string_view appName, const std::shared_ptr<Core>& core, const FederateInfo& fedInfo);
64  App(std::string_view appName, CoreApp& core, const FederateInfo& fedInfo);
71  App(std::string_view appName, const std::string& configString);
72 
74  App(App&& other_app) = default;
76  App(const App& other_app) = delete;
78  App& operator=(App&& app) = default;
81  App& operator=(const App& app) = delete;
82  virtual ~App();
83 
90  void loadFile(const std::string& filename, bool enableFederateInterfaceRegistration = true);
97  virtual void initialize();
98  /*run the Player*/
99  virtual void run();
100 
104  virtual void runTo(Time stopTime_input) = 0;
105 
107  virtual void finalize();
108 
110  bool isActive() const { return !deactivated; }
112  const CombinationFederate& accessUnderlyingFederate() const { return *fed; }
113 
114  protected:
118  virtual void loadJsonFile(const std::string& jsonString,
119  bool enableFederateInterfaceRegistration);
125  void loadJsonFileConfiguration(const std::string& appName,
126  const std::string& jsonString,
127  bool enableFederateInterfaceRegistration);
129  virtual void loadTextFile(const std::string& textFile);
131  void loadInputFiles();
133  void loadConfigOptions(AppTextParser& aparser);
134 
135  private:
136  void loadConfigOptions(const Json::Value& element);
138  std::unique_ptr<helicsCLI11App> generateParser();
140  void processArgs(std::unique_ptr<helicsCLI11App>& app,
141  FederateInfo& fedInfo,
142  std::string_view defaultAppName);
143 
144  protected:
145  std::shared_ptr<CombinationFederate> fed;
146  Time stopTime = Time::maxVal();
147  std::string configFileName;
148  std::string inputFileName;
149  bool useLocal{false};
150  bool fileLoaded{false};
151  bool deactivated{false};
152  bool quietMode{false};
153  bool helpMode{false};
154  std::vector<std::string> remArgs;
155 };
156 
158  public:
159  explicit AppTextParser(const std::string& filename);
161  std::vector<int> preParseFile(const std::vector<char>& klines);
162 
163  const std::string& configString() const { return configStr; }
165  bool loadNextLine(std::string& line, int& lineNumber);
166  void reset();
167 
168  private:
169  bool mLineComment{false};
170  std::ifstream filePtr;
171  std::string configStr;
172  std::string mFileName;
173  int currentLineNumber{0};
174 };
175 } // namespace helics::apps
Definition: application_api/CombinationFederate.hpp:18
Definition: FederateInfo.hpp:29
Definition: helicsApp.hpp:157
std::vector< int > preParseFile(const std::vector< char > &klines)
Definition: helicsApp.cpp:146
bool loadNextLine(std::string &line, int &lineNumber)
Definition: helicsApp.cpp:193
Definition: helicsApp.hpp:32
std::string inputFileName
the name of the app input file
Definition: helicsApp.hpp:148
std::string configFileName
name of the config file used for constructing the federate
Definition: helicsApp.hpp:147
App & operator=(App &&app)=default
std::shared_ptr< CombinationFederate > fed
the federate created for the App
Definition: helicsApp.hpp:145
virtual void runTo(Time stopTime_input)=0
App(const App &other_app)=delete
App(App &&other_app)=default
bool isActive() const
Definition: helicsApp.hpp:110
App & operator=(const App &app)=delete
const CombinationFederate & accessUnderlyingFederate() const
Definition: helicsApp.hpp:112
TimeRepresentation< count_time< 9 > > Time
Definition: helicsTime.hpp:27