helics  3.6.1
Connector.hpp
1 /*
2 Copyright (c) 2017-2025,
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{nullptr};
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>& coreObj,
65  const FederateInfo& fedInfo);
71  Connector(std::string_view name, CoreApp& coreObj, 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  using ConnectionsType = std::unordered_multimap<std::string_view, Connection>;
123 
124  private:
125  std::unique_ptr<helicsCLI11App> generateParser();
127  void processArgs();
128 
130  void initialSetup();
134  virtual void loadJsonFile(const std::string& jsonString,
135  bool enableFederateInterfaceRegistration) override;
137  virtual void loadTextFile(const std::string& filename) override;
138 
139  bool addConnectionVector(const std::vector<std::string>& connection);
141  void establishPotentialInterfaces(ConnectionsList& possibleConnections);
142 
144  void scanPotentialInterfaces(ConnectionsList& possibleConnections);
145 
147  void scanPotentialInterfaceTemplates(ConnectionsList& possibleConnections);
148 
150  void scanUnconnectedInterfaces(ConnectionsList& possibleConnections);
151 
153  void makeConnections(ConnectionsList& possibleConnections);
155  int makeTargetConnection(
156  std::string_view origin,
157  const std::vector<std::size_t>& tagList,
158  std::unordered_set<std::string_view>& possibleConnections,
159  const std::unordered_multimap<std::string_view, std::string_view>& aliases,
160  const std::function<void(std::string_view origin, std::string_view target)>& callback);
161  bool makePotentialConnection(
162  std::string_view interfaceName,
163  const std::vector<std::size_t>& tagList,
164  std::unordered_map<std::string_view, PotentialConnections>& potentials,
165  const std::unordered_multimap<std::string_view, std::string_view>& aliases);
166 
167  bool makePotentialTemplateConnection(
168  std::string_view interfaceName,
169  const std::vector<std::size_t>& tagList,
170  std::vector<TemplateMatcher>& potentialTemplates,
171  const std::unordered_multimap<std::string_view, std::string_view>& aliases);
172 
173  bool checkPotentialConnection(
174  std::string_view interface,
175  const std::vector<std::size_t>& tagList,
176  std::unordered_set<std::string_view>& possibleConnections,
177  std::unordered_map<std::string_view, PotentialConnections>& potentials,
178  std::vector<TemplateMatcher>& potentialTemplates,
179  const std::unordered_multimap<std::string_view, std::string_view>& aliases);
181  std::vector<Connection>
182  buildPossibleConnectionList(std::string_view startingInterface,
183  const std::vector<std::size_t>& tagList) const;
185  void generateRegexMatchers();
186 
187  private:
188  CoreApp core;
190  ConnectionsType connections;
191  std::vector<Connection> matchers;
192  std::vector<std::shared_ptr<RegexMatcher>> regexMatchers;
193  std::unordered_map<std::size_t, std::string> tags;
194  std::unordered_set<std::string> interfaces;
195  std::uint64_t matchCount{0};
196  std::uint64_t interfacesRequested{0};
198  bool matchTargetEndpoints{false};
200  bool matchMultiple{false};
202  bool alwaysCheckRegex{false};
203 };
204 } // namespace helics::apps
Definition: FederateInfo.hpp:28
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