helics  3.3.0
queryHelpers.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 
8 #pragma once
9 #include "../common/JsonGeneration.hpp"
10 
11 #include <functional>
12 #include <string>
13 #include <string_view>
14 #include <type_traits>
15 
16 namespace helics {
17 class HandleManager;
18 class GlobalFederateId;
19 class FederateState;
20 class InterfaceInfo;
21 
22 // enumeration of subqueries that cascade and need multiple levels of processing
23 enum Subqueries : std::uint16_t {
24  GENERAL_QUERY = 0,
25  FEDERATE_MAP = 1,
26  CURRENT_TIME_MAP = 2,
27  DEPENDENCY_GRAPH = 3,
28  DATA_FLOW_GRAPH = 4,
29  VERSION_ALL = 5,
30  GLOBAL_STATE = 6,
31  GLOBAL_TIME_DEBUGGING = 7,
32  GLOBAL_FLUSH = 8,
33  GLOBAL_STATUS = 9
34 };
35 
36 } // namespace helics
37 
38 template<typename X, typename Proc>
39 std::string generateStringVector(const X& data, Proc generator)
40 {
41  static_assert(std::is_convertible<decltype(generator(*(data.begin()))), std::string>::value,
42  "generator output must be convertible to std::string");
43  std::string ret{"["};
44  for (auto& ele : data) {
45  ret.append(helics::generateJsonQuotedString(generator(ele)));
46  ret.push_back(',');
47  }
48  if (ret.size() > 1) {
49  ret.back() = ']';
50  } else {
51  ret.push_back(']');
52  }
53  return ret;
54 }
55 
56 template<typename X, typename Proc, typename validator>
57 std::string generateStringVector_if(const X& data, Proc generator, validator valid)
58 {
59  static_assert(std::is_convertible<decltype(generator(*(data.begin()))), std::string>::value,
60  "generator output must be convertible to std::string");
61  std::string ret{"["};
62  for (auto& ele : data) {
63  if (valid(ele)) {
64  ret.append(helics::generateJsonQuotedString(generator(ele)));
65  ret.push_back(',');
66  }
67  }
68  if (ret.size() > 1) {
69  ret.back() = ']';
70  } else {
71  ret.push_back(']');
72  }
73  return ret;
74 }
75 
76 namespace helics {
77 void generateInterfaceConfig(Json::Value& iblock,
78  const helics::HandleManager& hm,
79  const helics::GlobalFederateId& fed);
80 
81 Json::Value generateInterfaceConfig(const helics::HandleManager& hm,
82  const helics::GlobalFederateId& fed);
83 
84 void addFederateTags(Json::Value& v, const helics::FederateState* fed);
86 std::string generateInterfaceQueryResults(std::string_view request,
87  const HandleManager& handles,
88  const GlobalFederateId fed,
89  const std::function<void(Json::Value&)>& addHeaderInfo);
90 
91 std::string generateInterfaceQueryResults(std::string_view request,
92  const InterfaceInfo& info,
93  const std::function<void(Json::Value&)>& addHeaderInfo);
94 } // namespace helics
helics::GlobalFederateId
Definition: GlobalFederateId.hpp:72
helics::InterfaceType::FILTER
@ FILTER
handle to a filter
helics::InterfaceType::INPUT
@ INPUT
handle to a input interface
helics::HandleManager
Definition: HandleManager.hpp:25
helics::InterfaceType::TRANSLATOR
@ TRANSLATOR
handle to a translator object
InterfaceInfo.hpp
helics::FederateState
Definition: FederateState.hpp:47
helics::GlobalFederateId::isValid
constexpr bool isValid() const
Definition: GlobalFederateId.hpp:109
helics::generateInterfaceQueryResults
std::string generateInterfaceQueryResults(std::string_view request, const HandleManager &handles, const GlobalFederateId fed, const std::function< void(Json::Value &)> &addHeaderInfo)
Definition: queryHelpers.cpp:208
helics
the main namespace for the helics co-simulation library User functions will be in the helics namespac...
Definition: AsyncFedCallInfo.hpp:14
helics::InterfaceType::ENDPOINT
@ ENDPOINT
handle to an endpoint
helics::InterfaceType::PUBLICATION
@ PUBLICATION
handle to output interface