NRP Core  1.4.1
graph_utils.h
Go to the documentation of this file.
1 /* * NRP Core - Backend infrastructure to synchronize simulations
2  *
3  * Copyright 2020-2023 NRP Team
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * This project has received funding from the European Union’s Horizon 2020
18  * Framework Programme for Research and Innovation under the Specific Grant
19  * Agreement No. 945539 (Human Brain Project SGA3).
20  */
21 
22 #ifndef GRAPH_UTILS_H
23 #define GRAPH_UTILS_H
24 
25 #include <filesystem>
26 #include <nlohmann/json.hpp>
27 
29 
31 
32 inline void createPythonGraphFromConfig(const nlohmann::json &config, const ComputationalGraph::ExecMode& execMode,
33  const boost::python::dict &globalDict)
34 {
35  // Load Computation Graph
38  gm.setExecMode(execMode);
39 
40  for(const auto &fn : config) {
41  auto fileName = fn.get<std::string>();
42  if(std::filesystem::exists(fileName)) {
43  try {
44  boost::python::exec_file(fileName.c_str(), globalDict, globalDict);
45  NRPLogger::info("Loaded computation graph file \"" + fileName + "\"");
46  }
47  catch (boost::python::error_already_set &) {
49  "Loading of computation graph file \"" + fileName + "\" failed: " + handle_pyerror());
50  }
51  }
52  else
53  throw NRPException::logCreate("Loading of computation graph file \"" + fileName + "\" failed: the file doesn't exist.");
54  }
55 
56  gm.graphLoadComplete();
57 
58  gm.configure();
59 }
60 
61 inline std::pair<InputClockNode*, InputIterationNode*> findTimeNodes()
62 {
64  InputClockNode* _clock = nullptr;
65  InputIterationNode* _iteration = nullptr;
66 
67  // Find Clock and Iteration nodes
68  if(gm.getNode("clock_node")) {
69  _clock = dynamic_cast<InputClockNode *>(gm.getNode("clock_node"));
70  if (!_clock)
72  "Exception in the ComputationalGraph: found Node with id 'clock_node', which is reserved for the input Clock node");
73  }
74  if(gm.getNode("iteration_node")) {
75  _iteration = dynamic_cast<InputIterationNode*>(gm.getNode("iteration_node"));
76  if(!_iteration)
77  throw NRPException::logCreate("Exception in the ComputationalGraph: found Node with id 'iteration_node', which is reserved for the input Iteration node");
78  }
79 
80  return std::make_pair(_clock, _iteration);
81 }
82 
83 #endif //GRAPH_UTILS_H
ComputationalGraphManager::configure
void configure()
Configure ComputationalGraph.
Definition: computational_graph_manager.h:132
createPythonGraphFromConfig
void createPythonGraphFromConfig(const nlohmann::json &config, const ComputationalGraph::ExecMode &execMode, const boost::python::dict &globalDict)
Definition: graph_utils.h:32
InputClockNode
Specialization of InputTimeBaseNode which stores and sends system clock in milliseconds.
Definition: input_time.h:77
ComputationalGraphManager::resetInstance
static ComputationalGraphManager & resetInstance()
Reset singleton instance.
Definition: computational_graph_manager.cpp:36
NRPLogger::info
static void info(const FormatString &fmt, const Args &...args)
NRP logging function with message formatting for info level.
Definition: nrp_logger.h:138
ComputationalGraphManager::getInstance
static ComputationalGraphManager & getInstance()
Get singleton instance of ComputationalGraphManager.
Definition: computational_graph_manager.cpp:31
ComputationalGraphManager::getNode
ComputationalNode * getNode(const std::string &id)
Retrieve a node from the graph as a pointer.
Definition: computational_graph_manager.h:95
computational_graph_manager.h
ComputationalGraph::ExecMode
ExecMode
Definition: computational_graph.h:62
NRPException::logCreate
static EXCEPTION logCreate(LOG_EXCEPTION_T &exception, const std::string &msg, NRPLogger::spdlog_out_fcn_t spdlogCall=NRPLogger::critical)
Definition: nrp_exceptions.h:73
ComputationalGraphManager::setExecMode
void setExecMode(ComputationalGraph::ExecMode mode)
Definition: computational_graph_manager.h:160
findTimeNodes
std::pair< InputClockNode *, InputIterationNode * > findTimeNodes()
Definition: graph_utils.h:61
InputIterationNode
Specialization of InputTimeBaseNode which stores and sends system iteration number.
Definition: input_time.h:109
input_time.h
ComputationalGraphManager::graphLoadComplete
void graphLoadComplete()
Function to be called externally after all nodes has been added to the graph.
Definition: computational_graph_manager.h:145
handle_pyerror
std::string handle_pyerror()
Read out a properly formatted Python exception string. Only call if a Python exception was thrown.
Definition: python_error_handler.cpp:27
ComputationalGraphManager
Singleton class managing a computational graph.
Definition: computational_graph_manager.h:45
json
nlohmann::json json
Definition: engine_json_server.cpp:31