Skip to content

Commit

Permalink
[dataflow] improve logging and error management
Browse files Browse the repository at this point in the history
  • Loading branch information
MathiasPaulin committed Oct 17, 2022
1 parent 17dda23 commit e8c3bd7
Show file tree
Hide file tree
Showing 22 changed files with 109 additions and 339 deletions.
252 changes: 50 additions & 202 deletions src/Dataflow/Core/DataflowGraph.cpp

Large diffs are not rendered by default.

5 changes: 1 addition & 4 deletions src/Dataflow/Core/DataflowGraph.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ class RA_DATAFLOW_API DataflowGraph : public Node
*/
DataflowGraph( const std::string& instanceName, const std::string& typeName );

void fromJsonInternal( const nlohmann::json& ) override;
bool fromJsonInternal( const nlohmann::json& data ) override;
void toJsonInternal( nlohmann::json& ) const override;

private:
Expand Down Expand Up @@ -193,9 +193,6 @@ class RA_DATAFLOW_API DataflowGraph : public Node
/// \param out The port to add.
bool addGetter( PortBase* out );

/// Loding status. Set tu true when starting loading, set to false if some nodes can't be laoded
bool m_loadStatus { true };

public:
static const std::string& getTypename();
};
Expand Down
42 changes: 17 additions & 25 deletions src/Dataflow/Core/Node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,49 +28,41 @@ void Node::generateUuid() {
}
/// Gets the UUID of the node as a string
std::string Node::getUuid() const {
if ( m_uuid.is_nil() ) {
// generates the uuid (need to remove const attribute) ...
const_cast<Node*>( this )->generateUuid();
}
std::string struuid = "{" + uuids::to_string( m_uuid ) + "}";
return struuid;
}

bool Node::setUuid( const std::string& uid ) {
if ( m_uuid.is_nil() ) {
auto id = uuids::uuid::from_string( uid );
if ( id ) {
m_uuid = id.value();
return true;
}
}
return false;
return std::string { "{" } + uuids::to_string( m_uuid ) + "}";
}

Node::Node( const std::string& instanceName, const std::string& typeName ) :
m_typeName { typeName }, m_instanceName { instanceName } {
generateUuid();
}

void Node::fromJson( const nlohmann::json& data ) {
bool Node::fromJson( const nlohmann::json& data ) {
if ( data.contains( "model" ) ) {
if ( data["model"].contains( "instance" ) ) { m_instanceName = data["model"]["instance"]; }
}
else {
LOG( logERROR ) << "Missing required model when loading a Dataflow::Node";
return false;
}
// get the common content of the Node from the json data
if ( data.contains( "id" ) ) {
std::string struuid = data["id"];
m_uuid = uuids::uuid::from_string( struuid ).value();
}
else {
generateUuid();
}
if ( data.contains( "model" ) ) {
if ( data["model"].contains( "instance" ) ) { m_instanceName = data["model"]["instance"]; }
// get the specific concrete node informations
const auto& datamodel = data["model"];
fromJsonInternal( datamodel );
LOG( logERROR ) << "Missing required uuid when loading node " << m_instanceName;
return false;
}

// get the specific concrete node information
const auto& datamodel = data["model"];
auto loaded = fromJsonInternal( datamodel );

// get the supplemental informations related to application/gui/...
for ( auto& [key, value] : data.items() ) {
if ( key != "id" && key != "model" ) { m_extraJsonData.emplace( key, value ); }
}
return loaded;
}

void Node::toJson( nlohmann::json& data ) const {
Expand Down
8 changes: 2 additions & 6 deletions src/Dataflow/Core/Node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,6 @@ class RA_DATAFLOW_API Node
/// \brief Gets the UUID of the node as a string
std::string getUuid() const;

/// \brief Sets the UUID of the node from a valid uuid string
/// \return true if the uuid is set, false if the node already have a valid uid or the string is
/// invalid
bool setUuid( const std::string& uid );
/// @}

/// \name Serialization of a node
Expand All @@ -122,7 +118,7 @@ class RA_DATAFLOW_API Node

/// \brief unserialized the content of the node.
/// Fill the node from its json representation
void fromJson( const nlohmann::json& data );
bool fromJson( const nlohmann::json& data );

/// \brief Add a metadata to the node to store application specific information.
/// used, e.g. by the node editor gui to save node position in the graphical canvas.
Expand All @@ -148,7 +144,7 @@ class RA_DATAFLOW_API Node
/// Must be implemented by inheriting classes.
/// Be careful with template specialization and function member overriding when implementing
/// this method.
virtual void fromJsonInternal( const nlohmann::json& ) = 0;
virtual bool fromJsonInternal( const nlohmann::json& ) = 0;

/// internal json representation of the Node.
/// Must be implemented by inheriting classes.
Expand Down
8 changes: 0 additions & 8 deletions src/Dataflow/Core/Node.inl
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,10 @@ namespace Dataflow {
namespace Core {

inline void Node::init() {
#ifdef GRAPH_CALL_TRACE
std::cout << "\e[34m\e[1m" << getTypeName() << "\e[0m \"" << m_instanceName
<< "\": initialization." << std::endl;
#endif
m_initialized = true;
}

inline void Node::destroy() {
#ifdef GRAPH_CALL_TRACE
std::cout << "\e[34m\e[1m" << getTypeName() << "\e[0m \"" << m_instanceName << "\": destroy."
<< std::endl;
#endif
m_interface.clear();
m_inputs.clear();
m_outputs.clear();
Expand Down
25 changes: 6 additions & 19 deletions src/Dataflow/Core/NodeFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,6 @@ Node* NodeFactory::createNode( std::string& nodeType,
if ( owningGraph != nullptr ) { owningGraph->addNode( node ); }
return node;
}
#if 0
else {
std::cerr << "NodeFactory: no defined node for type " << nodeType << "." << std::endl;
std::cerr << "Available nodes are : " << std::endl;
for ( const auto& e : m_nodesCreators ) {
std::cerr << "\t" << e.first << std::endl;
}
}
#endif
return nullptr;
}

Expand All @@ -47,11 +38,10 @@ bool NodeFactory::registerNodeCreator( std::string nodeType,
m_nodesCreators[nodeType] = { std::move( nodeCreator ), nodeCategory };
return true;
}
else {
std::cerr << "NodeFactory: trying to add an already existing node creator for type "
<< nodeType << "." << std::endl;
return false;
}
LOG( Ra::Core::Utils::logWARNING )
<< "NodeFactory: trying to add an already existing node creator for type " << nodeType
<< ".";
return false;
}

size_t NodeFactory::nextNodeId() {
Expand All @@ -70,11 +60,8 @@ Node* NodeFactorySet::createNode( std::string& nodeType,
auto node = it.second->createNode( nodeType, data, owningGraph );
if ( node ) { return node; }
}
std::cerr << "NodeFactorySet: unable to find constructor for " << nodeType
<< " in any of the following factories :" << std::endl;
for ( const auto& it : m_factories ) {
std::cerr << "\t" << it.first << std::endl;
}
LOG( Ra::Core::Utils::logERROR ) << "NodeFactorySet: unable to find constructor for "
<< nodeType << " in any managed factory.";
return nullptr;
}

Expand Down
3 changes: 2 additions & 1 deletion src/Dataflow/Core/Nodes/Functionals/BinaryOpNode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -216,9 +216,10 @@ class BinaryOpNode : public Node
<< "Unable to save data when serializing a " << getTypeName() << ".";
}

void fromJsonInternal( const nlohmann::json& ) override {
bool fromJsonInternal( const nlohmann::json& ) override {
LOG( Ra::Core::Utils::logDEBUG )
<< "Unable to read data when un-serializing a " << getTypeName() << ".";
return true;
}

private:
Expand Down
2 changes: 1 addition & 1 deletion src/Dataflow/Core/Nodes/Functionals/FilterNode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class FilterNode : public Node
UnaryPredicate predicate );

void toJsonInternal( nlohmann::json& data ) const override;
void fromJsonInternal( const nlohmann::json& data ) override;
bool fromJsonInternal( const nlohmann::json& ) override;

private:
UnaryPredicate m_predicate;
Expand Down
8 changes: 2 additions & 6 deletions src/Dataflow/Core/Nodes/Functionals/FilterNode.inl
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,6 @@ void FilterNode<coll_t, v_t>::execute() {
// m_elements.reserve( inData.size() ); // --> this is not a requirement of
// SequenceContainer
std::copy_if( inData.begin(), inData.end(), std::back_inserter( m_elements ), f );
#ifdef GRAPH_CALL_TRACE
std::cout << "\e[36m\e[1mFilterNode \e[0m \"" << m_instanceName << "\": execute, from "
<< input->getData().size() << " to " << m_elements.size() << " "
<< typeid( T ).name() << "." << std::endl;
#endif
}
}

Expand Down Expand Up @@ -73,9 +68,10 @@ void FilterNode<coll_t, v_t>::toJsonInternal( nlohmann::json& data ) const {
}

template <typename coll_t, typename v_t>
void FilterNode<coll_t, v_t>::fromJsonInternal( const nlohmann::json& ) {
bool FilterNode<coll_t, v_t>::fromJsonInternal( const nlohmann::json& ) {
LOG( Ra::Core::Utils::logWARNING ) // TODO make this logDEBUG
<< "Unable to read data when un-serializing a " << getTypeName() << ".";
return true;
}

} // namespace Functionals
Expand Down
2 changes: 1 addition & 1 deletion src/Dataflow/Core/Nodes/Functionals/ReduceNode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class ReduceNode : public Node
v_t initialValue );

void toJsonInternal( nlohmann::json& data ) const override;
void fromJsonInternal( const nlohmann::json& data ) override;
bool fromJsonInternal( const nlohmann::json& ) override;

private:
ReduceOperator m_operator;
Expand Down
7 changes: 2 additions & 5 deletions src/Dataflow/Core/Nodes/Functionals/ReduceNode.inl
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,6 @@ void ReduceNode<coll_t, v_t>::execute() {
if ( m_portIn->isLinked() ) {
const auto& inData = m_portIn->getData();
m_result = std::accumulate( inData.begin(), inData.end(), iv, f );
#ifdef GRAPH_CALL_TRACE
std::cout << "\e[36m\e[1mMReduceNode \e[0m \"" << m_instanceName << "\": execute, from "
<< input->getData().size() << " " << getTypename() << "." << std::endl;
#endif
}
}

Expand Down Expand Up @@ -78,9 +74,10 @@ void ReduceNode<coll_t, v_t>::toJsonInternal( nlohmann::json& data ) const {
}

template <typename coll_t, typename v_t>
void ReduceNode<coll_t, v_t>::fromJsonInternal( const nlohmann::json& ) {
bool ReduceNode<coll_t, v_t>::fromJsonInternal( const nlohmann::json& ) {
LOG( Ra::Core::Utils::logWARNING ) // TODO make this logDEBUG
<< "Unable to read data when un-serializing a " << getTypeName() << ".";
return true;
}

} // namespace Functionals
Expand Down
2 changes: 1 addition & 1 deletion src/Dataflow/Core/Nodes/Functionals/TransformNode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class TransformNode : public Node
TransformOperator op );

void toJsonInternal( nlohmann::json& data ) const override;
void fromJsonInternal( const nlohmann::json& data ) override;
bool fromJsonInternal( const nlohmann::json& ) override;

private:
TransformOperator m_operator;
Expand Down
8 changes: 2 additions & 6 deletions src/Dataflow/Core/Nodes/Functionals/TransformNode.inl
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,6 @@ void TransformNode<coll_t, v_t>::execute() {
// m_elements.reserve( inData.size() ); // --> this is not a requirement of
// SequenceContainer
std::transform( inData.begin(), inData.end(), std::back_inserter( m_elements ), f );
#ifdef GRAPH_CALL_TRACE
std::cout << "\e[36m\e[1mMapNode \e[0m \"" << m_instanceName << "\": execute, from "
<< input->getData().size() << " to " << m_elements.size() << " "
<< typeid( T ).name() << "." << std::endl;
#endif
}
}

Expand Down Expand Up @@ -70,9 +65,10 @@ void TransformNode<coll_t, v_t>::toJsonInternal( nlohmann::json& data ) const {
}

template <typename coll_t, typename v_t>
void TransformNode<coll_t, v_t>::fromJsonInternal( const nlohmann::json& ) {
bool TransformNode<coll_t, v_t>::fromJsonInternal( const nlohmann::json& ) {
LOG( Ra::Core::Utils::logWARNING ) // TODO make this logDEBUG
<< "Unable to read data when un-serializing a " << getTypeName() << ".";
return true;
}

} // namespace Functionals
Expand Down
2 changes: 1 addition & 1 deletion src/Dataflow/Core/Nodes/Private/SourcesNodeFactory.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include <Dataflow/Core/Nodes/Private/FunctionalsNodeFactory.hpp>
#include <Dataflow/Core/Nodes/Private/SourcesNodeFactory.hpp>
#include <Dataflow/Core/Nodes/Sources/CoreDataSources.hpp>

namespace Ra {
Expand Down
4 changes: 3 additions & 1 deletion src/Dataflow/Core/Nodes/Sinks/SinkNode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,11 @@ class SinkNode : public Node

protected:
void toJsonInternal( nlohmann::json& data ) const override;
void fromJsonInternal( const nlohmann::json& data ) override;
bool fromJsonInternal( const nlohmann::json& data ) override;

private:
/// \todo : allow user to specify where to store the data ? (i.e. make this a shared_ptr ?).
// If yes, add a method setDataStorage(std::shared_ptr<T> v)
T m_data;

/// @{
Expand Down
10 changes: 4 additions & 6 deletions src/Dataflow/Core/Nodes/Sinks/SinkNode.inl
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@ void SinkNode<T>::init() {
template <typename T>
void SinkNode<T>::execute() {
m_data = m_portIn->getData();
#ifdef GRAPH_CALL_TRACE
std::cout << "\e[33m\e[1m" << getTypename() << "\e[0m \"" << getInstanceName() << "\": execute."
<< std::endl;
#endif
}

template <typename T>
Expand All @@ -51,10 +47,12 @@ const std::string& SinkNode<T>::getTypename() {
}

template <typename T>
void SinkNode<T>::toJsonInternal( nlohmann::json& /*data*/ ) const {}
void SinkNode<T>::toJsonInternal( nlohmann::json& ) const {}

template <typename T>
void SinkNode<T>::fromJsonInternal( const nlohmann::json& /*data*/ ) {}
bool SinkNode<T>::fromJsonInternal( const nlohmann::json& ) {
return true;
}

} // namespace Sinks
} // namespace Core
Expand Down
6 changes: 4 additions & 2 deletions src/Dataflow/Core/Nodes/Sources/CoreDataSources.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,12 @@ DECLARE_COREDATA_SOURCES( Vector4ui, Vector4ui );
} \
\
template <> \
inline void SingleDataSourceNode<TYPE>::fromJsonInternal( const nlohmann::json& data ) { \
inline bool SingleDataSourceNode<TYPE>::fromJsonInternal( const nlohmann::json& data ) { \
if ( data.contains( #NAME ) ) { \
TYPE v = data[#NAME]; \
setData( v ); \
} \
return true; \
}

SPECIALIZE_EDITABLE_SOURCE( bool, boolean );
Expand All @@ -95,14 +96,15 @@ SingleDataSourceNode<Ra::Core::Utils::Color>::toJsonInternal( nlohmann::json& da
}

template <>
inline void
inline bool
SingleDataSourceNode<Ra::Core::Utils::Color>::fromJsonInternal( const nlohmann::json& data ) {
if ( data.contains( "color" ) ) {
std::array<Scalar, 3> c = data["color"];
auto v =
Ra::Core::Utils::Color::sRGBToLinearRGB( Ra::Core::Utils::Color( c[0], c[1], c[2] ) );
setData( v );
}
return true;
}

#undef SPECIALIZE_EDITABLE_SOURCE
Expand Down
4 changes: 2 additions & 2 deletions src/Dataflow/Core/Nodes/Sources/FunctionSource.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ class FunctionSourceNode : public Node
protected:
FunctionSourceNode( const std::string& instanceName, const std::string& typeName );

void fromJsonInternal( const nlohmann::json& ) override;
void toJsonInternal( nlohmann::json& ) const override;
bool fromJsonInternal( const nlohmann::json& ) override;
void toJsonInternal( nlohmann::json& data ) const override;

/// @{
/// The data provided by the node
Expand Down
7 changes: 2 additions & 5 deletions src/Dataflow/Core/Nodes/Sources/FunctionSource.inl
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@ void FunctionSourceNode<R, Args...>::execute() {
m_data = &m_localData;
}
m_portOut->setData( m_data );
#ifdef GRAPH_CALL_TRACE
std::cout << "\e[34m\e[1mFunctionSourceNode\e[0m \"" << m_instanceName << "\": execute."
<< std::endl;
#endif
}

template <class R, class... Args>
Expand Down Expand Up @@ -58,9 +54,10 @@ void FunctionSourceNode<R, Args...>::toJsonInternal( nlohmann::json& data ) cons
}

template <class R, class... Args>
void FunctionSourceNode<R, Args...>::fromJsonInternal( const nlohmann::json& ) {
bool FunctionSourceNode<R, Args...>::fromJsonInternal( const nlohmann::json& ) {
LOG( Ra::Core::Utils::logDEBUG )
<< "Unable to read data when un-serializing a " << getTypeName() << ".";
return true;
}

} // namespace Sources
Expand Down
Loading

0 comments on commit e8c3bd7

Please sign in to comment.