helics  2.8.1
fileConnections.hpp
1 /*
2 Copyright (c) 2017-2021,
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 "../common/JsonProcessingFunctions.hpp"
10 #include "../common/TomlProcessingFunctions.hpp"
11 #include "../common/addTargets.hpp"
12 #include "Broker.hpp"
13 #include "Core.hpp"
14 #include "core-exceptions.hpp"
15 
16 #include <string>
17 #include <type_traits>
18 
19 namespace helics {
20 template<class brkX>
21 void makeConnectionsToml(brkX* brk, const std::string& file)
22 {
23  toml::value uVal;
24  static_assert(std::is_base_of<Broker, brkX>::value || std::is_base_of<Core, brkX>::value,
25  "broker must be Core or broker");
26  toml::value doc;
27  try {
28  doc = loadToml(file);
29  }
30  catch (const std::invalid_argument& ia) {
31  throw(helics::InvalidParameter(ia.what()));
32  }
33 
34  auto conns = toml::find_or(doc, "connections", uVal);
35  if (!conns.is_uninitialized()) {
36  auto& connArray = conns.as_array();
37  for (const auto& conn : connArray) {
38  if (conn.is_array()) {
39  auto& connAct = conn.as_array();
40  brk->dataLink(connAct[0].as_string(), connAct[1].as_string());
41  } else {
42  std::string pub = getOrDefault(conn, "publication", std::string());
43  if (!pub.empty()) {
44  addTargets(conn, "targets", [brk, &pub](const std::string& target) {
45  brk->dataLink(pub, target);
46  });
47  } else {
48  std::string ipt = getOrDefault(conn, "input", std::string());
49  addTargets(conn, "targets", [brk, &ipt](const std::string& target) {
50  brk->dataLink(target, ipt);
51  });
52  }
53  }
54  }
55  }
56  auto filts = toml::find_or(doc, "filters", uVal);
57  if (!filts.is_uninitialized()) {
58  auto& filtArray = filts.as_array();
59  for (const auto& filt : filtArray) {
60  if (filt.is_array()) {
61  auto& filtAct = filt.as_array();
62  brk->addSourceFilterToEndpoint(filtAct[0].as_string(), filtAct[1].as_string());
63  } else {
64  std::string fname = getOrDefault(filt, "filter", std::string());
65  if (!fname.empty()) {
66  auto asrc = [brk, &fname](const std::string& ept) {
67  brk->addSourceFilterToEndpoint(fname, ept);
68  };
69  addTargets(filt, "endpoints", asrc);
70  addTargets(filt, "source_endpoints", asrc);
71  addTargets(filt, "sourceEndpoints", asrc);
72  auto adst = [brk, &fname](const std::string& ept) {
73  brk->addDestinationFilterToEndpoint(fname, ept);
74  };
75  addTargets(filt, "dest_endpoints", adst);
76  addTargets(filt, "destEndpoints", adst);
77  }
78  }
79  }
80  }
81  auto globals = toml::find_or(doc, "globals", uVal);
82  if (!globals.is_uninitialized()) {
83  if (globals.is_array()) {
84  for (auto& val : globals.as_array()) {
85  brk->setGlobal(val.as_array()[0].as_string(), val.as_array()[1].as_string());
86  }
87  } else {
88  for (const auto& val : globals.as_table()) {
89  brk->setGlobal(val.first, val.second.as_string());
90  }
91  }
92  }
93 }
94 
95 template<class brkX>
96 void makeConnectionsJson(brkX* brk, const std::string& file)
97 {
98  static_assert(std::is_base_of<Broker, brkX>::value || std::is_base_of<Core, brkX>::value,
99  "broker must be Core or broker");
100  Json::Value doc;
101  try {
102  doc = loadJson(file);
103  }
104  catch (const std::invalid_argument& ia) {
105  throw(helics::InvalidParameter(ia.what()));
106  }
107 
108  if (doc.isMember("connections")) {
109  for (const auto& conn : doc["connections"]) {
110  if (conn.isArray()) {
111  brk->dataLink(conn[0].asString(), conn[1].asString());
112  } else {
113  std::string pub = getOrDefault(conn, "publication", std::string());
114  if (!pub.empty()) {
115  addTargets(conn, "targets", [brk, &pub](const std::string& target) {
116  brk->dataLink(pub, target);
117  });
118  } else {
119  std::string ipt = getOrDefault(conn, "input", std::string());
120  addTargets(conn, "targets", [brk, &ipt](const std::string& target) {
121  brk->dataLink(target, ipt);
122  });
123  }
124  }
125  }
126  }
127  if (doc.isMember("filters")) {
128  for (const auto& filt : doc["filters"]) {
129  if (filt.isArray()) {
130  brk->addSourceFilterToEndpoint(filt[0].asString(), filt[1].asString());
131  } else {
132  std::string fname = getOrDefault(filt, "filter", std::string());
133  if (!fname.empty()) {
134  auto asrc = [brk, &fname](const std::string& ept) {
135  brk->addSourceFilterToEndpoint(fname, ept);
136  };
137  addTargets(filt, "endpoints", asrc);
138  addTargets(filt, "source_endpoints", asrc);
139  addTargets(filt, "sourceEndpoints", asrc);
140  auto adst = [brk, &fname](const std::string& ept) {
141  brk->addDestinationFilterToEndpoint(fname, ept);
142  };
143  addTargets(filt, "dest_endpoints", adst);
144  addTargets(filt, "destEndpoints", adst);
145  }
146  }
147  }
148  }
149  if (doc.isMember("globals")) {
150  if (doc["globals"].isArray()) {
151  for (auto& val : doc["globals"]) {
152  brk->setGlobal(val[0].asString(), val[1].asString());
153  }
154  } else {
155  auto members = doc["globals"].getMemberNames();
156  for (auto& val : members) {
157  brk->setGlobal(val, doc["globals"][val].asString());
158  }
159  }
160  }
161 }
162 
163 } // namespace helics
loadToml
toml::value loadToml(const std::string &tomlString)
Definition: TomlProcessingFunctions.cpp:22
helics::InvalidParameter
Definition: core-exceptions.hpp:48
loadJson
Json::Value loadJson(const std::string &jsonString)
Definition: JsonProcessingFunctions.cpp:24
helics
the main namespace for the helics co-simulation library User functions will be in the helics namespac...
Definition: AsyncFedCallInfo.hpp:14
core-exceptions.hpp