1#include <Core/Utils/Log.hpp>
2#include <Dataflow/Core/Node.hpp>
9using namespace Ra::Core::Utils;
13 m_model_name { typeName }, m_instance_name { instanceName }, m_display_name { instanceName } {}
17 if ( data.empty() ) {
return true; }
19 auto it_instance = data.find(
"instance" );
22 LOG( logERROR ) <<
"Missing required instance name when loading node " <<
m_instance_name;
28 auto it_model = data.find(
"model" );
29 if ( it_model != data.end() ) {
32 const auto& datamodel = *it_model;
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 ); }
40 LOG( logERROR ) <<
"Missing required model when loading a Dataflow::Node";
44 for (
auto& [key, value] : data.items() ) {
45 if ( key !=
"instance" && key !=
"model" ) {
m_metadata.emplace( key, value ); }
60 data.emplace(
"model", model );
63 for (
auto& [key, value] :
m_metadata.items() ) {
64 if ( key !=
"instance" && key !=
"model" ) { data.emplace( key, value ); }
74 if ( type ==
"in" ) {
return port_by_name( m_inputs, name ); }
75 return port_by_name( m_outputs, name );
79 return port_by_name( m_inputs, name );
83 return port_by_name( m_outputs, name );
92 LOG( Ra::Core::Utils::logDEBUG )
94 if (
const auto& ports = data.find(
"inputs" ); ports != data.end() ) {
95 for (
const auto&
port : *ports ) {
96 int index =
port[
"port_index"];
100 if (
const auto& ports = data.find(
"outputs" ); ports != data.end() ) {
101 for (
const auto&
port : *ports ) {
102 int index =
port[
"port_index"];
113 for (
size_t i = 0; i <
m_inputs.size(); ++i ) {
117 port[
"port_index"] = i;
118 port[
"type"] = Ra::Core::Utils::simplifiedDemangledType( p->type() );
119 data[
"inputs"].push_back(
port );
121 for (
size_t i = 0; i <
m_outputs.size(); ++i ) {
125 port[
"port_index"] = i;
126 port[
"type"] = Ra::Core::Utils::simplifiedDemangledType( p->type() );
127 data[
"outputs"].push_back(
port );
129 LOG( Ra::Core::Utils::logDEBUG ) << message;
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.
const std::string & instance_name() const
Gets the instance name of the node.
bool fromJson(const nlohmann::json &data)
Unserialized the content 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.
void toJson(nlohmann::json &data) const
Serialize the content of the node.
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)
auto port(const PortCollection< PortPtr< PortType > > &ports, PortIndex index) const
Gets a port in a collection by its index.
void add_metadata(const nlohmann::json &data)
Add a metadata to the node to store application specific information.
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)
const std::string & model_name() const
Gets the model (type/class) name of the node.
virtual bool fromJsonInternal(const nlohmann::json &data)
Internal json representation of the Node.
auto port_by_name(const std::string &type, const std::string &name) const -> IndexAndPort< PortBaseRawPtr >
Get a port by its name.
nlohmann::json m_metadata
Additional data on the node, added by application or gui or ...
auto port_base(const PortCollection< PortPtr< PortType > > &ports, PortIndex idx) const -> PortRawPtr< PortType >
Gets the PortBase In or Out by its index.
PortBaseRawPtr port_by_index(const std::string &type, PortIndex index) const
Get a port by its index.
virtual void toJsonInternal(nlohmann::json &data) const
Internal json representation of the Node.
Base class for nodes' ports A port is a strongly typed extremity of connections between nodes.
hepler function to manage enum as underlying types in VariableSet