1#include <catch2/catch_test_macros.hpp>
5#include <Core/Utils/StdFilesystem.hpp>
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>
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;
18 g.
add_metadata( { {
"extra", { {
"info",
"missing operators on functional node" } } } } );
20 REQUIRE( g.metadata().contains(
"extra" ) );
21 REQUIRE( g.metadata()[
"extra"].contains(
"info" ) );
22 REQUIRE( g.metadata()[
"extra"][
"info"] ==
"missing operators on functional node" );
25 auto a = g.input_node_port(
"a",
"from" );
27 auto b = g.input_node_port(
"b",
"from" );
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 {
35 auto op_unique = g.add_node<TestNode>(
"addition" );
36 op_unique->set_operator( add );
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" ) );
44 a->set_default_value( x );
46 b->set_default_value( y );
48 REQUIRE( g.execute() );
49 auto z = r->
data<DataType>();
50 REQUIRE( z == x + y );
54 std::filesystem::create_directories( tmpdir );
55 g.saveToJson( tmpdir +
"/GraphSerializationTest.json" );
58 REQUIRE( g.execute() );
62 REQUIRE( g1.loadFromJson( tmpdir +
"/GraphSerializationTest.json" ) );
65 auto addition = g1.node(
"addition" );
66 REQUIRE( addition !=
nullptr );
67 REQUIRE( addition->model_name() == Functionals::BinaryOpScalar::node_typename() );
69 REQUIRE( typedAddition !=
nullptr );
70 if ( typedAddition !=
nullptr ) { typedAddition->set_operator( add ); }
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 );
79 auto a_loaded = g1.input_node_port(
"a",
"from" );
80 auto b_loaded = g1.input_node_port(
"b",
"from" );
82 a_loaded->set_default_value( xp );
84 b_loaded->set_default_value( yp );
85 REQUIRE( g1.execute() );
86 REQUIRE( z_loaded == 5 );
92 loadedSource_a->set_data( newX );
94 REQUIRE( g1.execute() );
95 REQUIRE( z_loaded == 6 );
96 std::filesystem::remove_all( tmpdir );
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.
Base class for nodes that will store the result of a computation graph.
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)