Loading [MathJax]/extensions/tex2jax.js
Radium Engine  1.5.28
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Node.cpp
1#include <Core/Utils/Log.hpp>
2#include <Dataflow/Core/Node.hpp>
3
4namespace Ra {
5namespace Dataflow {
6namespace Core {
7class PortBase;
8
9using namespace Ra::Core::Utils;
10
11// display_name is instanceName unless reset afterward
12Node::Node( const std::string& instanceName, const std::string& typeName ) :
13 m_model_name { typeName }, m_instance_name { instanceName }, m_display_name { instanceName } {}
14
15bool Node::fromJson( const nlohmann::json& data ) {
16 // This is to avoid wrong error message when creating node from the editor
17 if ( data.empty() ) { return true; }
18
19 auto it_instance = data.find( "instance" );
20 if ( it_instance != data.end() ) { m_instance_name = *it_instance; }
21 else {
22 LOG( logERROR ) << "Missing required instance name when loading node " << m_instance_name;
23 return false;
24 }
25 // get the common content of the Node from the json data
26 bool loaded = false;
27
28 auto it_model = data.find( "model" );
29 if ( it_model != data.end() ) {
31 // get the specific concrete node information
32 const auto& datamodel = *it_model;
33 loaded = fromJsonInternal( datamodel );
34 if ( !loaded ) { LOG( logERROR ) << "Fail to load model " << datamodel; }
35 auto it_display_name = datamodel.find( "display_name" );
36 if ( it_display_name != datamodel.end() ) { set_display_name( *it_display_name ); }
38 }
39 else {
40 LOG( logERROR ) << "Missing required model when loading a Dataflow::Node";
41 loaded = false;
42 }
43 // get the supplemental information related to application/gui/...
44 for ( auto& [key, value] : data.items() ) {
45 if ( key != "instance" && key != "model" ) { m_metadata.emplace( key, value ); }
46 }
47 return loaded;
48}
49
50void Node::toJson( nlohmann::json& data ) const {
51
52 // write the common content of the Node to the json data
53 data["instance"] = m_instance_name;
54
55 nlohmann::json model;
56 model["name"] = m_model_name;
57 model["display_name"] = display_name();
58 // Fill the specific concrete node information (model instance)
59 toJsonInternal( model );
60 data.emplace( "model", model );
61
62 // store the supplemental information related to application/gui/...
63 for ( auto& [key, value] : m_metadata.items() ) {
64 if ( key != "instance" && key != "model" ) { data.emplace( key, value ); }
65 }
66}
67
68void Node::add_metadata( const nlohmann::json& data ) {
69 m_metadata.merge_patch( data );
70}
71
72auto Node::port_by_name( const std::string& type, const std::string& name ) const
74 if ( type == "in" ) { return port_by_name( m_inputs, name ); }
75 return port_by_name( m_outputs, name );
76}
77
79 return port_by_name( m_inputs, name );
80}
81
83 return port_by_name( m_outputs, name );
84}
85
86PortBase* Node::port_by_index( const std::string& type, PortIndex idx ) const {
87 if ( type == "in" ) return port_base( m_inputs, idx );
88 return port_base( m_outputs, idx );
89}
90
91bool Node::fromJsonInternal( const nlohmann::json& data ) {
92 LOG( Ra::Core::Utils::logDEBUG )
93 << "default deserialization for " << instance_name() + " " + model_name() << ".";
94 if ( const auto& ports = data.find( "inputs" ); ports != data.end() ) {
95 for ( const auto& port : *ports ) {
96 int index = port["port_index"];
97 m_inputs[index]->from_json( port );
98 }
99 }
100 if ( const auto& ports = data.find( "outputs" ); ports != data.end() ) {
101 for ( const auto& port : *ports ) {
102 int index = port["port_index"];
103 m_outputs[index]->from_json( port );
104 }
105 }
106 return true;
107}
108
109void Node::toJsonInternal( nlohmann::json& data ) const {
110 std::string message =
111 std::string { "default serialization for " } + instance_name() + " " + model_name();
112
113 for ( size_t i = 0; i < m_inputs.size(); ++i ) {
114 const auto& p = m_inputs[i];
115 nlohmann::json port;
116 p->to_json( port );
117 port["port_index"] = i;
118 port["type"] = Ra::Core::Utils::simplifiedDemangledType( p->type() );
119 data["inputs"].push_back( port );
120 }
121 for ( size_t i = 0; i < m_outputs.size(); ++i ) {
122 const auto& p = m_outputs[i];
123 nlohmann::json port;
124 p->to_json( port );
125 port["port_index"] = i;
126 port["type"] = Ra::Core::Utils::simplifiedDemangledType( p->type() );
127 data["outputs"].push_back( port );
128 }
129 LOG( Ra::Core::Utils::logDEBUG ) << message;
130}
131
132} // namespace Core
133} // namespace Dataflow
134} // namespace Ra
std::string m_model_name
The type name of the node. Initialized once at construction.
Definition Node.hpp:404
std::string m_instance_name
The instance name of the node.
Definition Node.hpp:406
const std::string & instance_name() const
Gets the instance name of the node.
Definition Node.hpp:447
bool fromJson(const nlohmann::json &data)
Unserialized the content of the node.
Definition Node.cpp:15
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.
Definition Node.hpp:195
void set_display_name(const std::string &name)
Set the display name.
Definition Node.hpp:197
void toJson(nlohmann::json &data) const
Serialize the content of the node.
Definition Node.cpp:50
auto input_by_name(const std::string &name) const -> IndexAndPort< PortBaseInRawPtr >
Convenience alias to port_by_name("in", name)
Definition Node.cpp:78
auto output_by_name(const std::string &name) const -> IndexAndPort< PortBaseOutRawPtr >
Convenience alias to port_by_name("out", name)
Definition Node.cpp:82
auto port(const PortCollection< PortPtr< PortType > > &ports, PortIndex index) const
Gets a port in a collection by its index.
Definition Node.hpp:296
void add_metadata(const nlohmann::json &data)
Add a metadata to the node to store application specific information.
Definition Node.cpp:68
PortCollection< PortPtr< PortBaseIn > > m_inputs
The in ports of the node (own by the node)
Definition Node.hpp:411
PortCollection< PortPtr< PortBaseOut > > m_outputs
The out ports of the node (own by the node)
Definition Node.hpp:413
const std::string & model_name() const
Gets the model (type/class) name of the node.
Definition Node.hpp:443
virtual bool fromJsonInternal(const nlohmann::json &data)
Internal json representation of the Node.
Definition Node.cpp:91
auto port_by_name(const std::string &type, const std::string &name) const -> IndexAndPort< PortBaseRawPtr >
Get a port by its name.
Definition Node.cpp:72
nlohmann::json m_metadata
Additional data on the node, added by application or gui or ...
Definition Node.hpp:422
auto port_base(const PortCollection< PortPtr< PortType > > &ports, PortIndex idx) const -> PortRawPtr< PortType >
Gets the PortBase In or Out by its index.
Definition Node.hpp:538
PortBaseRawPtr port_by_index(const std::string &type, PortIndex index) const
Get a port by its index.
Definition Node.cpp:86
virtual void toJsonInternal(nlohmann::json &data) const
Internal json representation of the Node.
Definition Node.cpp:109
Base class for nodes' ports A port is a strongly typed extremity of connections between nodes.
Definition Port.hpp:30
hepler function to manage enum as underlying types in VariableSet
Definition Cage.cpp:4