helics  3.5.2
Connector.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 "CoreApp.hpp"
10 #include "helicsApp.hpp"
11 
12 #include <memory>
13 #include <string>
14 #include <string_view>
15 #include <unordered_map>
16 #include <unordered_set>
17 #include <vector>
18 
19 namespace helics::apps {
20 enum class InterfaceDirection { TO_FROM = -1, BIDIRECTIONAL = 0, FROM_TO = 1 };
21 
22 struct Connection {
23  std::string_view interface1;
24  std::string_view interface2;
25  InterfaceDirection direction;
26  std::vector<std::size_t> tags;
27  std::shared_ptr<std::string> stringBuffer;
28 };
29 
30 struct ConnectionsList;
31 struct PotentialConnections;
32 class RegexMatcher;
33 class TemplateMatcher;
34 
40 class HELICS_CXX_EXPORT Connector: public App {
41  public:
43  Connector() = default;
47  explicit Connector(std::vector<std::string> args);
52  Connector(int argc, char* argv[]);
57  explicit Connector(std::string_view name, const FederateInfo& fedInfo);
63  Connector(std::string_view name,
64  const std::shared_ptr<Core>& core,
65  const FederateInfo& fedInfo);
71  Connector(std::string_view name, CoreApp& core, const FederateInfo& fedInfo);
77  Connector(std::string_view appName, const std::string& configString);
78 
80  Connector(Connector&& other_player) = default;
82  Connector& operator=(Connector&& fed) = default;
83 
90  virtual void initialize() override;
91 
95  virtual void runTo(Time stopTime_input) override;
96 
103  void addConnection(std::string_view interface1,
104  std::string_view interface2,
105  InterfaceDirection direction = InterfaceDirection::BIDIRECTIONAL,
106  const std::vector<std::string>& tags = {});
107 
109  std::size_t addTag(std::string_view tagName);
110 
113  std::string_view addInterface(std::string_view interfaceName);
114 
116  auto connectionCount() const { return connections.size(); }
118  auto madeConnections() const { return matchCount; }
119  void allowMultipleConnections(bool value = true) { matchMultiple = value; }
120  void matchEndpointTargets(bool value = true) { matchTargetEndpoints = value; }
121 
122  private:
123  std::unique_ptr<helicsCLI11App> generateParser();
125  void processArgs();
126 
128  void initialSetup();
132  virtual void loadJsonFile(const std::string& jsonString,
133  bool enableFederateInterfaceRegistration) override;
135  virtual void loadTextFile(const std::string& filename) override;
136 
137  bool addConnectionVector(const std::vector<std::string>& connection);
139  void establishPotentialInterfaces(ConnectionsList& possibleConnections);
140 
142  void scanPotentialInterfaces(ConnectionsList& possibleConnections);
143 
145  void scanPotentialInterfaceTemplates(ConnectionsList& possibleConnections);
146 
148  void scanUnconnectedInterfaces(ConnectionsList& possibleConnections);
149 
151  void makeConnections(ConnectionsList& possibleConnections);
153  int makeTargetConnection(
154  std::string_view origin,
155  const std::vector<std::size_t>& tagList,
156  std::unordered_set<std::string_view>& possibleConnections,
157  const std::unordered_multimap<std::string_view, std::string_view>& aliases,
158  const std::function<void(std::string_view origin, std::string_view target)>& callback);
159  bool makePotentialConnection(
160  std::string_view interfaceName,
161  const std::vector<std::size_t>& tagList,
162  std::unordered_map<std::string_view, PotentialConnections>& potentials,
163  const std::unordered_multimap<std::string_view, std::string_view>& aliases);
164 
165  bool makePotentialTemplateConnection(
166  std::string_view interfaceName,
167  const std::vector<std::size_t>& tagList,
168  std::vector<TemplateMatcher>& potentialTemplates,
169  const std::unordered_multimap<std::string_view, std::string_view>& aliases);
170 
171  bool checkPotentialConnection(
172  std::string_view interface,
173  const std::vector<std::size_t>& tagList,
174  std::unordered_set<std::string_view>& possibleConnections,
175  std::unordered_map<std::string_view, PotentialConnections>& potentials,
176  std::vector<TemplateMatcher>& potentialTemplates,
177  const std::unordered_multimap<std::string_view, std::string_view>& aliases);
179  std::vector<Connection>
180  buildPossibleConnectionList(std::string_view startingInterface,
181  const std::vector<std::size_t>& tagList) const;
183  void generateRegexMatchers();
184 
185  private:
186  CoreApp core;
188  std::unordered_multimap<std::string_view, Connection> connections;
189  std::vector<Connection> matchers;
190  std::vector<std::shared_ptr<RegexMatcher>> regexMatchers;
191  std::unordered_map<std::size_t, std::string> tags;
192  std::unordered_set<std::string> interfaces;
193  std::uint64_t matchCount{0};
194  std::uint64_t interfacesRequested{0};
196  bool matchTargetEndpoints{false};
198  bool matchMultiple{false};
200  bool alwaysCheckRegex{false};
201 };
202 } // namespace helics::apps
Definition: FederateInfo.hpp:29
Definition: helicsApp.hpp:32
Definition: Connector.hpp:40
Connector & operator=(Connector &&fed)=default
auto connectionCount() const
Definition: Connector.hpp:116
Connector(Connector &&other_player)=default
auto madeConnections() const
Definition: Connector.hpp:118
TimeRepresentation< count_time< 9 > > Time
Definition: helicsTime.hpp:27
Definition: Connector.hpp:22