Loading [MathJax]/jax/input/TeX/config.js
Radium Engine  1.5.29
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
serialization.cpp
1#include <catch2/catch_test_macros.hpp>
2
3#include <string>
4
5#include <Core/Utils/StdFilesystem.hpp>
6
7#include <Dataflow/Core/DataflowGraph.hpp>
8#include <Dataflow/Core/Functionals/Types.hpp>
9#include <Dataflow/Core/Sinks/Types.hpp>
10#include <Dataflow/Core/Sources/Types.hpp>
11
12TEST_CASE( "Dataflow/Core/DataflowGraph/Serialization",
13 "[unittests][Dataflow][Core][DataflowGraph]" ) {
14 SECTION( "Execution and modification of a graph" ) {
15 using namespace Ra::Dataflow::Core;
16 using DataType = Scalar;
17 DataflowGraph g { "original graph" };
18 g.add_metadata( { { "extra", { { "info", "missing operators on functional node" } } } } );
19
20 REQUIRE( g.metadata().contains( "extra" ) );
21 REQUIRE( g.metadata()["extra"].contains( "info" ) );
22 REQUIRE( g.metadata()["extra"]["info"] == "missing operators on functional node" );
23
24 auto source_a = g.add_node<Sources::SingleDataSourceNode<DataType>>( "a" );
25 auto a = g.input_node_port( "a", "from" );
26 auto source_b = g.add_node<Sources::SingleDataSourceNode<DataType>>( "b" );
27 auto b = g.input_node_port( "b", "from" );
28 auto sink = g.add_node<Sinks::SinkNode<DataType>>( "r" );
29 auto r = g.output_node_port( "r", "data" );
31 TestNode::BinaryOperator add = []( TestNode::Arg1_type pa,
32 TestNode::Arg2_type pb ) -> TestNode::Res_type {
33 return pa + pb;
34 };
35 auto op_unique = g.add_node<TestNode>( "addition" );
36 op_unique->set_operator( add );
37
38 REQUIRE( g.add_link( source_a, "to", op_unique, "a" ) );
39 REQUIRE( g.add_link( op_unique, "result", sink, "from" ) );
40 REQUIRE( g.add_link( source_b, "to", op_unique, "b" ) );
41
42 // execution of the original graph
43 DataType x { 1_ra };
44 a->set_default_value( x );
45 DataType y { 2_ra };
46 b->set_default_value( y );
47 // Execute initial graph";
48 REQUIRE( g.execute() );
49 auto z = r->data<DataType>();
50 REQUIRE( z == x + y );
51
52 // Save the graph
53 std::string tmpdir { "tmpDir4Tests" };
54 std::filesystem::create_directories( tmpdir );
55 g.saveToJson( tmpdir + "/GraphSerializationTest.json" );
56 g.destroy();
57 // this does nothing as g was destroyed
58 REQUIRE( g.execute() );
59
60 // Create a new graph and load from the saved graph
61 DataflowGraph g1 { "loaded graph" };
62 REQUIRE( g1.loadFromJson( tmpdir + "/GraphSerializationTest.json" ) );
63
64 // Setting the unserializable data on nodes (functions)
65 auto addition = g1.node( "addition" );
66 REQUIRE( addition != nullptr );
67 REQUIRE( addition->model_name() == Functionals::BinaryOpScalar::node_typename() );
68 auto typedAddition = std::dynamic_pointer_cast<Functionals::BinaryOpScalar>( addition );
69 REQUIRE( typedAddition != nullptr );
70 if ( typedAddition != nullptr ) { typedAddition->set_operator( add ); }
71
72 // Execute loaded graph
73 // Data delivered by the source nodes are the one saved by the original graph
74 REQUIRE( g1.execute() );
75 auto r_loaded = g1.output_node_port( "r", "data" );
76 auto& z_loaded = r_loaded->data<DataType>();
77 REQUIRE( z_loaded == z );
78
79 auto a_loaded = g1.input_node_port( "a", "from" );
80 auto b_loaded = g1.input_node_port( "b", "from" );
81 DataType xp { 2_ra };
82 a_loaded->set_default_value( xp );
83 DataType yp { 3_ra };
84 b_loaded->set_default_value( yp );
85 REQUIRE( g1.execute() );
86 REQUIRE( z_loaded == 5 );
87
88 // change the data delivered by a
89 auto loadedSource_a =
91 Scalar newX = 3_ra;
92 loadedSource_a->set_data( newX );
93
94 REQUIRE( g1.execute() );
95 REQUIRE( z_loaded == 6 );
96 std::filesystem::remove_all( tmpdir );
97 }
98}
Represent a set of connected nodes that define a Direct Acyclic Computational Graph Ownership of node...
Apply a binary operation on its input.
void add_metadata(const nlohmann::json &data)
Add a metadata to the node to store application specific information.
Definition Node.cpp:68
Base class for nodes that will store the result of a computation graph.
Definition SinkNode.hpp:17
Base class for nodes that will give access to some input data to the graph. This class can be used to...
Quaternion add(const Quaternion &q1, const Quaternion &q2)
Returns the sum of two quaternions.
T dynamic_pointer_cast(T... args)