NRP Core  1.4.1
computational_node.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 COMPUTATIONAL_NODE_H
23 #define COMPUTATIONAL_NODE_H
24 
25 #include <string>
26 #include <map>
27 
32 public:
33 
35  enum NodeType {
39  };
40 
41  const static std::map<NodeType, std::string> nodeTypeStr;
42 
43  ComputationalNode() = delete;
44  virtual ~ComputationalNode() = default;
45 
49  ComputationalNode(std::string id, NodeType type) :
50  _id(std::move(id)),
51  _type(type)
52  { }
53 
57  const std::string& id() const
58  { return this->_id; }
59 
63  NodeType type() const
64  { return this->_type; }
65 
69  virtual std::string typeStr() const
70  { return this->nodeTypeStr.at(this->type()); }
71 
75  void setVisited(bool visited)
76  { this->_visited = visited; }
77 
81  bool isVisited() const
82  { return this->_visited; }
83 
88  { _doCompute = doCompute; }
89 
96  virtual bool doCompute() const
97  { return _doCompute; }
98 
106  static std::pair<std::string, std::string> parseNodeAddress(const std::string& address, bool hasPort = true)
107  {
108  if(address.at(0) != '/')
109  throw std::invalid_argument("Error while parsing node address \""+ address +"\". Computational Graph addresses must start with '/'");
110 
111  auto n = address.find('/',1);
112  if(n == std::string::npos && hasPort)
113  throw std::invalid_argument("Error while parsing node address \""+ address +"\". Expected format is '/node_id/port_id'");
114 
115  auto node = hasPort ? address.substr(1, n-1) : address.substr(1);
116  auto port = hasPort ? address.substr(n+1, address.size()) : node;
117 
118  return std::make_pair(node, port);
119  }
120 
121 protected:
122 
126  virtual void configure() = 0;
127 
131  virtual void compute() = 0;
132 
136  virtual void graphCycleStartCB()
137  { }
138 
142  virtual void graphLoadedCB()
143  { }
144 
145  friend class ComputationalGraph;
148 
149 private:
150 
152  std::string _id;
154  NodeType _type;
156  bool _visited = false;
158  bool _doCompute = false;
159 };
160 
161 #endif //COMPUTATIONAL_NODE_H
ComputationalNode::NodeType
NodeType
All the possible node types.
Definition: computational_node.h:35
ComputationalNode::ComputationalNode
ComputationalNode()=delete
ComputationalNode::setDoCompute
void setDoCompute(bool doCompute)
Sets a value for the node 'doCompute' property, used in some graph execution modes.
Definition: computational_node.h:87
ComputationalNode::type
NodeType type() const
Returns the node 'type'.
Definition: computational_node.h:63
ComputationalNode::setVisited
void setVisited(bool visited)
Sets a value for the node 'visited' property, used for graph traversing.
Definition: computational_node.h:75
ComputationalNode::Output
@ Output
Definition: computational_node.h:37
ComputationalNode::id
const std::string & id() const
Returns the node 'id'.
Definition: computational_node.h:57
ComputationalNode::isVisited
bool isVisited() const
Returns true if the node has been marked as visited, false otherwise.
Definition: computational_node.h:81
ComputationalNode::compute
virtual void compute()=0
Requests the node to execute its computation.
ComputationalNode::ComputationalGraphPythonNodes_PYTHON_DECORATORS_BASIC_Test
friend class ComputationalGraphPythonNodes_PYTHON_DECORATORS_BASIC_Test
Definition: computational_node.h:147
ComputationalNode::parseNodeAddress
static std::pair< std::string, std::string > parseNodeAddress(const std::string &address, bool hasPort=true)
Parses a computational node address returning the node id and the port (if any) contained in the addr...
Definition: computational_node.h:106
ComputationalNode::doCompute
virtual bool doCompute() const
Tells if this node should be executed in this graph execution cycle, used in some graph execution mod...
Definition: computational_node.h:96
ComputationalNode::nodeTypeStr
const static std::map< NodeType, std::string > nodeTypeStr
Definition: computational_node.h:41
ComputationalNode::Functional
@ Functional
Definition: computational_node.h:38
ComputationalNode::configure
virtual void configure()=0
Configures the node making it ready to execute 'compute'.
ComputationalNode::~ComputationalNode
virtual ~ComputationalNode()=default
ComputationalNode::ComputationalNode
ComputationalNode(std::string id, NodeType type)
Constructor.
Definition: computational_node.h:49
python_json_engine.port
port
Definition: python_json_engine.py:197
ComputationalNode::graphLoadedCB
virtual void graphLoadedCB()
Function called by the Computational Graph to nodes that the graph has been completely loaded.
Definition: computational_node.h:142
ComputationalGraph
Definition: computational_graph.h:53
ComputationalGraphManager
Singleton class managing a computational graph.
Definition: computational_graph_manager.h:45
ComputationalNode::graphCycleStartCB
virtual void graphCycleStartCB()
Function called by the Computational Graph at the beginning of a new execution cycle.
Definition: computational_node.h:136
ComputationalNode::Input
@ Input
Definition: computational_node.h:36
ComputationalNode
Base class implementing a node in the computational graph.
Definition: computational_node.h:31
ComputationalNode::typeStr
virtual std::string typeStr() const
Returns the node 'type' as a string.
Definition: computational_node.h:69