Represent a set of connected nodes that define a Direct Acyclic Computational Graph Ownership of nodes is given to the graph at construction time. More...
#include <Dataflow/Core/DataflowGraph.hpp>
Public Member Functions | |
DataflowGraph (const std::string &name) | |
void | init () override |
Initializes the node content. | |
bool | execute () override |
Executes the node. | |
void | destroy () override |
Delete the node's content. | |
bool | loadFromJson (const std::string &jsonFilePath) |
Loads nodes and links from a JSON file. | |
void | saveToJson (const std::string &jsonFilePath) |
Saves nodes and links to a JSON file. | |
virtual bool | add_node (std::shared_ptr< Node > newNode) |
Adds a node to the graph. | |
template<typename T , typename... U> | |
std::shared_ptr< T > | add_node (U &&... u) |
Conveniance typed alias. | |
virtual bool | remove_node (std::shared_ptr< Node > node) |
Removes a node from the graph. | |
bool | add_link (const std::shared_ptr< Node > &nodeFrom, const std::string &nodeFromOutputName, const std::shared_ptr< Node > &nodeTo, const std::string &nodeToInputName) |
Connects two nodes of the graph. | |
bool | add_link (const std::shared_ptr< Node > &nodeFrom, Node::PortIndex portOutIdx, const std::shared_ptr< Node > &nodeTo, Node::PortIndex portInIdx) |
Convenience alias using port index. | |
bool | add_link (Node::PortBaseOutRawPtr outputPort, Node::PortBaseInRawPtr inputPort) |
Convenience alias using port raw ptr, checks if ports' nodes are in the graph. | |
template<typename T , typename U > | |
bool | add_link (const std::shared_ptr< PortOut< T > > &outputPort, const std::shared_ptr< PortIn< U > > &inputPort) |
Convenience typed alias. | |
bool | can_link (const std::shared_ptr< Node > &nodeFrom, Node::PortIndex portOutIdx, const std::shared_ptr< Node > &nodeTo, Node::PortIndex portInIdx) const |
bool | can_link (const Node *nodeFrom, Node::PortIndex portOutIdx, const Node *nodeTo, Node::PortIndex portInIdx) const |
Convenience alias using raw pointer. | |
bool | remove_link (std::shared_ptr< Node > node, const std::string &nodeInputName) |
Removes the link connected to a node's input port. | |
bool | remove_link (std::shared_ptr< Node > node, const PortIndex &in_port_index) |
Removes the link connected to a node's input port. | |
const std::vector< std::shared_ptr< Node > > & | nodes () const |
Get the vector of all the nodes on the graph. | |
std::shared_ptr< Node > | node (const std::string &instanceNameNode) const |
template<typename T > | |
std::shared_ptr< T > | node (const std::string &instanceNameNode) const |
const std::vector< std::vector< Node * > > & | nodes_by_level () const |
Gets the nodes ordered by level (after compilation) | |
bool | compile () override |
Compile the graph to check its validity and simplify it. | |
void | generate_ports () |
fill input and output ports of graph from its input and output nodes if exists. | |
size_t | node_count () const |
Gets the number of nodes. | |
virtual void | clear_nodes () |
Deletes all nodes from the render graph. | |
bool | is_compiled () const |
Test if the graph is compiled. | |
void | needs_recompile () |
Node::PortBaseInRawPtr | input_node_port (const std::string &nodeName, const std::string &portName) |
Gets an input port form a node of the graph. | |
Node::PortBaseOutRawPtr | output_node_port (const std::string &nodeName, const std::string &portName) |
Gets an output port from a node of the graph. | |
bool | shouldBeSaved () |
void | setNodesAndLinksProtection (bool on) |
protect nodes and links from deletion. | |
bool | nodesAndLinksProtection () const |
get the protection status protect nodes and links from deletion | |
void | add_input_output_nodes () |
Create (if not already created) input/output node of the graph, and fills graph input/output. | |
void | remove_unlinked_input_output_ports () |
Removes unsused (unlinked) input/output ports. | |
std::shared_ptr< GraphOutputNode > | output_node () |
std::shared_ptr< GraphInputNode > | input_node () |
PortIndex | add_input (PortBaseInPtr in) |
Convenience alias to add_port(inputs(), in) | |
template<typename T , typename... U> | |
auto | add_input (U &&... u) |
Adds a typed input port. | |
PortIndex | add_output (PortBaseOutPtr out) |
Convenience alias to add_port(outputs(), out) | |
template<typename T , typename... U> | |
auto | add_output (U &&... u) |
Adds a typed output port. | |
![]() | |
virtual | ~Node ()=default |
make Node a base abstract class | |
bool | operator== (const Node &node) |
Two nodes are considered equal if there model and instance names are the same. | |
void | add_metadata (const nlohmann::json &data) |
Add a metadata to the node to store application specific information. | |
const nlohmann::json & | metadata () |
Give access to extra json data stored on the node. | |
bool | is_initialized () const |
Ra::Core::VariableSet & | parameters () |
Return node's parameters. | |
Ra::Core::VariableSet & | input_variables () |
Return a variable set of input ports default value reference, if any. | |
bool | is_output () |
Node is output if none of the output ports is linked. | |
bool | is_input () |
Node is input if all input ports have default values and not linked. | |
Node (const Node &)=delete | |
Node & | operator= (const Node &)=delete |
auto | port_by_name (const std::string &type, const std::string &name) const -> IndexAndPort< PortBaseRawPtr > |
Get a port by its name. | |
auto | input_by_name (const std::string &name) const -> IndexAndPort< PortBaseInRawPtr > |
Convenience alias to port_by_name("in", name) | |
auto | output_by_name (const std::string &name) const -> IndexAndPort< PortBaseOutRawPtr > |
Convenience alias to port_by_name("out", name) | |
PortBaseRawPtr | port_by_index (const std::string &type, PortIndex index) const |
Get a port by its index. | |
auto | input_by_index (PortIndex index) const |
Convenience alias to port_by_index("in", index) | |
auto | output_by_index (PortIndex index) const |
Convenience alias to port_by_index("out", index) | |
template<typename T > | |
auto | input_by_index (PortIndex index) const |
Convenience alias with typed port. | |
template<typename T > | |
auto | output_by_index (PortIndex index) const |
Convenience alias with typed port. | |
const PortBaseInCollection & | inputs () const |
Gets the in ports of the node. | |
const PortBaseOutCollection & | outputs () const |
Gets the out ports of the node. | |
const std::string & | model_name () const |
Gets the model (type/class) name of the node. | |
const std::string & | display_name () const |
Gets the display name of the node (e.g. for gui), no need to be unique in a graph. | |
void | set_display_name (const std::string &name) |
Set the display name. | |
const std::string & | instance_name () const |
Gets the instance name of the node. | |
void | set_instance_name (const std::string &name) |
Sets the instance name the node (unused?) instance name must be unique in a graph. | |
void | toJson (nlohmann::json &data) const |
Serialize the content of the node. | |
bool | fromJson (const nlohmann::json &data) |
Unserialized the content of the node. | |
Static Public Member Functions | |
static const std::string & | node_typename () |
static std::shared_ptr< DataflowGraph > | loadGraphFromJsonFile (const std::string &filename) |
Load a graph from the given file. | |
Protected Member Functions | |
DataflowGraph (const std::string &instanceName, const std::string &typeName) | |
Allow derived class to construct the graph with their own static type. | |
bool | fromJsonInternal (const nlohmann::json &data) override |
Internal json representation of the Node. | |
void | toJsonInternal (nlohmann::json &) const override |
Internal json representation of the Node. | |
bool | has_node_by_name (const std::string &instance, const std::string &model) const |
Check if there node with same instance and model is in the graph. | |
bool | contains_node_recursive (const Node *node) const |
Check if node is part of the graph, or part of its "inner" graph. | |
![]() | |
Node (const std::string &instance, const std::string &typeName) | |
Construct the base node given its name and type. | |
template<typename PortType > | |
auto | port_by_name (const PortCollection< PortPtr< PortType > > &ports, const std::string &name) const -> IndexAndPort< PortRawPtr< PortType > > |
Gets the Port By Name. | |
template<typename PortType > | |
auto | port_base (const PortCollection< PortPtr< PortType > > &ports, PortIndex idx) const -> PortRawPtr< PortType > |
Gets the PortBase In or Out by its index. | |
template<typename T , typename PortType > | |
auto | port (const PortCollection< PortPtr< PortType > > &ports, PortIndex index) const |
Gets a port in a collection by its index. | |
template<typename PortType > | |
PortIndex | add_port (PortCollection< PortPtr< PortType > > &, PortPtr< PortType > port) |
Adds a port to port collection. | |
PortIndex | add_input (PortBaseInPtr in) |
Convenience alias to add_port(inputs(), in) | |
PortIndex | add_output (PortBaseOutPtr out) |
Convenience alias to add_port(outputs(), out) | |
template<typename T , typename... U> | |
auto | add_input (U &&... u) |
Adds a typed input port. | |
template<typename T , typename... U> | |
auto | add_output (U &&... u) |
Adds a typed output port. | |
template<typename T > | |
auto | input_port (PortIndex index) |
Gets a typed (input/output) port. | |
template<typename T > | |
auto | output_port (PortIndex index) |
Gets a typed (input/output) port. | |
void | remove_input (PortIndex index) |
Remove the given port from the managed (input/output) ports. | |
void | remove_output (PortIndex index) |
Remove the given port from the managed (input/output) ports. | |
template<typename T > | |
auto | add_parameter (const std::string &name, const T &value) |
template<typename T > | |
bool | remove_parameter (const std::string &name) |
template<typename T > | |
bool | remove_parameter (ParamHandle< T > &handle) |
Additional Inherited Members | |
![]() | |
using | PortIndex = Ra::Core::Utils::Index |
template<typename Port > | |
using | PortCollection = std::vector<Port> |
template<typename Port > | |
using | PortPtr = std::shared_ptr<Port> |
template<typename Port > | |
using | PortRawPtr = Port* |
template<typename Type > | |
using | PortInPtr = PortPtr<PortIn<Type>> |
template<typename Type > | |
using | PortInRawPtr = PortIn<Type>* |
template<typename Type > | |
using | PortOutPtr = PortPtr<PortOut<Type>> |
template<typename Type > | |
using | PortOutRawPtr = PortOut<Type>* |
using | PortBasePtr = PortPtr<PortBase> |
using | PortBaseRawPtr = PortRawPtr<PortBase> |
using | PortBaseCollection = PortCollection<PortBasePtr> |
using | PortBaseInPtr = PortPtr<PortBaseIn> |
using | PortBaseInRawPtr = PortRawPtr<PortBaseIn> |
using | PortBaseInCollection = PortCollection<PortBaseInPtr> |
using | PortBaseOutPtr = PortPtr<PortBaseOut> |
using | PortBaseOutRawPtr = PortRawPtr<PortBaseOut> |
using | PortBaseOutCollection = PortCollection<PortBaseOutPtr> |
template<typename Port > | |
using | IndexAndPort = std::pair<PortIndex, Port> |
template<typename T > | |
using | ParamHandle = Ra::Core::VariableSet::VariableHandle<T> |
![]() | |
bool | m_initialized { false } |
Flag that checks if the node is already initialized. | |
std::string | m_model_name |
The type name of the node. Initialized once at construction. | |
std::string | m_instance_name |
The instance name of the node. | |
std::string | m_display_name { "" } |
Node's name if needed for display. | |
PortCollection< PortPtr< PortBaseIn > > | m_inputs |
The in ports of the node (own by the node) | |
PortCollection< PortPtr< PortBaseOut > > | m_outputs |
The out ports of the node (own by the node) | |
Ra::Core::VariableSet | m_parameters |
The editable parameters of the node. | |
Ra::Core::VariableSet | m_input_variables |
nlohmann::json | m_metadata |
Additional data on the node, added by application or gui or ... | |
Represent a set of connected nodes that define a Direct Acyclic Computational Graph Ownership of nodes is given to the graph at construction time.
Definition at line 23 of file DataflowGraph.hpp.
|
explicit |
The nodes pointing to external data are created here.
name | The name of the render graph. |
Definition at line 20 of file DataflowGraph.cpp.
|
protected |
Allow derived class to construct the graph with their own static type.
Definition at line 22 of file DataflowGraph.cpp.
|
inline |
|
inline |
|
inline |
Create (if not already created) input/output node of the graph, and fills graph input/output.
These nodes are usefull for using graph as node and stating the graph as node input/output ports.
Definition at line 415 of file DataflowGraph.hpp.
bool Ra::Dataflow::Core::DataflowGraph::add_link | ( | const std::shared_ptr< Node > & | nodeFrom, |
const std::string & | nodeFromOutputName, | ||
const std::shared_ptr< Node > & | nodeTo, | ||
const std::string & | nodeToInputName ) |
Connects two nodes of the graph.
The two nodes must already be in the graph (with the add_node), in order to be linked the seconde node's in port must be free and the connected in port and out port must have the same type of data.
nodeFrom | The node that contains the out port. |
nodeFromOutputName | The name of the out port in nodeFrom. |
nodeTo | The node that contains the in port. |
nodeToInputName | The name of the in port in nodeTo. |
Definition at line 305 of file DataflowGraph.cpp.
bool Ra::Dataflow::Core::DataflowGraph::add_link | ( | const std::shared_ptr< Node > & | nodeFrom, |
Node::PortIndex | portOutIdx, | ||
const std::shared_ptr< Node > & | nodeTo, | ||
Node::PortIndex | portInIdx ) |
Convenience alias using port index.
Definition at line 325 of file DataflowGraph.cpp.
bool Ra::Dataflow::Core::DataflowGraph::add_link | ( | const std::shared_ptr< PortOut< T > > & | outputPort, |
const std::shared_ptr< PortIn< U > > & | inputPort ) |
Convenience typed alias.
Definition at line 360 of file DataflowGraph.hpp.
bool Ra::Dataflow::Core::DataflowGraph::add_link | ( | Node::PortBaseOutRawPtr | outputPort, |
Node::PortBaseInRawPtr | inputPort ) |
Convenience alias using port raw ptr, checks if ports' nodes are in the graph.
Definition at line 368 of file DataflowGraph.cpp.
|
virtual |
Adds a node to the graph.
newNode | The node to add to the graph. |
Definition at line 259 of file DataflowGraph.cpp.
std::shared_ptr< T > Ra::Dataflow::Core::DataflowGraph::add_node | ( | U &&... | u | ) |
Conveniance typed alias.
Definition at line 353 of file DataflowGraph.hpp.
|
inline |
|
inline |
|
inline |
Convenience alias using raw pointer.
Definition at line 376 of file DataflowGraph.hpp.
|
inline |
Definition at line 369 of file DataflowGraph.hpp.
|
virtual |
Deletes all nodes from the render graph.
Definition at line 504 of file DataflowGraph.cpp.
|
overridevirtual |
Compile the graph to check its validity and simplify it.
The compilation has multiple goals:
Node -> level, linked nodes
Reimplemented from Ra::Dataflow::Core::Node.
Definition at line 429 of file DataflowGraph.cpp.
|
protected |
Check if node is part of the graph, or part of its "inner" graph.
node | Raw pointer of the node to find. |
Definition at line 412 of file DataflowGraph.cpp.
|
overridevirtual |
Delete the node's content.
The destroy() function is called once at the end of the lifetime of the node. Its goal is to free the internal data that have been allocated.
Reimplemented from Ra::Dataflow::Core::Node.
Definition at line 57 of file DataflowGraph.cpp.
|
overridevirtual |
Executes the node.
Execute the node function on the input ports (to be fetched) and write the results to the output ports.
Implements Ra::Dataflow::Core::Node.
Definition at line 36 of file DataflowGraph.cpp.
|
overrideprotectedvirtual |
Internal json representation of the Node.
Default implementation warn about unsupported deserialization. Effective deserialzation must be implemented by inheriting classes. Be careful with template specialization and function member overriding in derived classes.
Reimplemented from Ra::Dataflow::Core::Node.
Definition at line 174 of file DataflowGraph.cpp.
void Ra::Dataflow::Core::DataflowGraph::generate_ports | ( | ) |
fill input and output ports of graph from its input and output nodes if exists.
Definition at line 424 of file DataflowGraph.cpp.
|
protected |
Check if there node with same instance and model is in the graph.
instance | Instance name to search |
model | Model name to search |
Definition at line 405 of file DataflowGraph.cpp.
|
overridevirtual |
Initializes the node content.
The init() function should be called once at the beginning of the lifetime of the node by the owner of the node (the graph which contains the node). Its goal is to initialize the node's internal data if any. The default do nothing.
Reimplemented from Ra::Dataflow::Core::Node.
Definition at line 25 of file DataflowGraph.cpp.
|
inline |
Definition at line 256 of file DataflowGraph.hpp.
|
inline |
Gets an input port form a node of the graph.
This input port could then be used through setter->set_default_value( data ) to set the graph input from the data.
nodeNome | The name of the node |
portName | The name of the input port |
Definition at line 398 of file DataflowGraph.hpp.
|
inline |
Test if the graph is compiled.
Definition at line 174 of file DataflowGraph.hpp.
bool Ra::Dataflow::Core::DataflowGraph::loadFromJson | ( | const std::string & | jsonFilePath | ) |
Loads nodes and links from a JSON file.
jsonFilePath | The path to the JSON file. |
Definition at line 124 of file DataflowGraph.cpp.
|
static |
Load a graph from the given file.
Any type of graph that inherits from DataflowGraph can be loaded by this function as soon as the appropriate constructor is registered in Ra::Dataflow::NodeFactoriesManager.
filename |
Definition at line 585 of file DataflowGraph.cpp.
|
inline |
Mark the graph as needing recompilation (useful to force recompilation and resources update)
Definition at line 393 of file DataflowGraph.hpp.
std::shared_ptr< Node > Ra::Dataflow::Core::DataflowGraph::node | ( | const std::string & | instanceNameNode | ) | const |
Gets a specific node according to its instance name.
instanceNameNode | The instance name of the node. |
Definition at line 574 of file DataflowGraph.cpp.
|
inline |
Gets a specific node according to its instance name.
instanceNameNode | The instance name of the node. |
Dynamic cast of the node pointer to T
T | Node's type to cast node to. |
Definition at line 143 of file DataflowGraph.hpp.
|
inline |
Gets the number of nodes.
Definition at line 168 of file DataflowGraph.hpp.
|
inlinestatic |
Definition at line 430 of file DataflowGraph.hpp.
|
inline |
Get the vector of all the nodes on the graph.
Definition at line 130 of file DataflowGraph.hpp.
|
inline |
Gets the nodes ordered by level (after compilation)
Definition at line 148 of file DataflowGraph.hpp.
|
inline |
get the protection status protect nodes and links from deletion
Definition at line 231 of file DataflowGraph.hpp.
|
inline |
Definition at line 255 of file DataflowGraph.hpp.
|
inline |
Gets an output port from a node of the graph.
Allows to get the data stored at this port after the execution of the graph. The return port can be use as in port->data().
nodeNome | The name of the node |
portName | The name of the output port |
Definition at line 406 of file DataflowGraph.hpp.
bool Ra::Dataflow::Core::DataflowGraph::remove_link | ( | std::shared_ptr< Node > | node, |
const PortIndex & | in_port_index ) |
Removes the link connected to a node's input port.
node | the node to unlink |
in_port_index | index of the port's input port to unlink |
Definition at line 387 of file DataflowGraph.cpp.
bool Ra::Dataflow::Core::DataflowGraph::remove_link | ( | std::shared_ptr< Node > | node, |
const std::string & | nodeInputName ) |
Removes the link connected to a node's input port.
node | the node to unlink |
nodeInputName | the name of the port to unlink |
Definition at line 382 of file DataflowGraph.cpp.
|
virtual |
Removes a node from the graph.
node | The node to remove from the graph. |
Definition at line 269 of file DataflowGraph.cpp.
|
inline |
Removes unsused (unlinked) input/output ports.
A (e.g. input) port is unlink, if the corresponding ports of input_node is unlink both on input (outside the graph) and input (inside the graph).
Definition at line 424 of file DataflowGraph.hpp.
void Ra::Dataflow::Core::DataflowGraph::saveToJson | ( | const std::string & | jsonFilePath | ) |
Saves nodes and links to a JSON file.
jsonFilePath | The path to the JSON file. |
Definition at line 66 of file DataflowGraph.cpp.
|
inline |
protect nodes and links from deletion.
on | true to protect, false to unprotect. |
Definition at line 225 of file DataflowGraph.hpp.
|
inline |
Definition at line 206 of file DataflowGraph.hpp.
|
overrideprotectedvirtual |
Internal json representation of the Node.
Default implementation warn about unsupported deserialization. Effective deserialzation must be implemented by inheriting classes. Be careful with template specialization and function member overriding in derived classes.
Reimplemented from Ra::Dataflow::Core::Node.
Definition at line 76 of file DataflowGraph.cpp.