aboutsummaryrefslogtreecommitdiffstats
path: root/host/tests/expert_test.cpp
blob: a23c9cbb800a83b46964395abfb26351c784fe47 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
//
// Copyright 2010-2011 Ettus Research LLC
// Copyright 2018 Ettus Research, a National Instruments Company
//
// SPDX-License-Identifier: GPL-3.0-or-later
//

#include <uhd/property_tree.hpp>
#include <uhdlib/experts/expert_container.hpp>
#include <uhdlib/experts/expert_factory.hpp>
#include <boost/format.hpp>
#include <boost/test/unit_test.hpp>
#include <fstream>
#include <memory>

using namespace uhd::experts;

class worker1_t : public worker_node_t
{
public:
    worker1_t(const node_retriever_t& db)
        : worker_node_t("A+B=C"), _a(db, "A/desired"), _b(db, "B"), _c(db, "C")
    {
        bind_accessor(_a);
        bind_accessor(_b);
        bind_accessor(_c);
    }

private:
    void resolve() override
    {
        _c = _a + _b;
    }

    data_reader_t<int> _a;
    data_reader_t<int> _b;
    data_writer_t<int> _c;
};

//=============================================================================

class worker2_t : public worker_node_t
{
public:
    worker2_t(const node_retriever_t& db)
        : worker_node_t("C*D=E"), _c(db, "C"), _d(db, "D"), _e(db, "E")
    {
        bind_accessor(_c);
        bind_accessor(_d);
        bind_accessor(_e);
    }

private:
    void resolve() override
    {
        _e.set(_c.get() * _d.get());
    }

    data_reader_t<int> _c;
    data_reader_t<int> _d;
    data_writer_t<int> _e;
};

//=============================================================================

class worker3_t : public worker_node_t
{
public:
    worker3_t(const node_retriever_t& db)
        : worker_node_t("-B=F"), _b(db, "B"), _f(db, "F")
    {
        bind_accessor(_b);
        bind_accessor(_f);
    }

private:
    void resolve() override
    {
        _f.set(-_b.get());
    }

    data_reader_t<int> _b;
    data_writer_t<int> _f;
};

//=============================================================================

class worker4_t : public worker_node_t
{
public:
    worker4_t(const node_retriever_t& db)
        : worker_node_t("E-F=G"), _e(db, "E"), _f(db, "F"), _g(db, "G")
    {
        bind_accessor(_e);
        bind_accessor(_f);
        bind_accessor(_g);
    }

private:
    void resolve() override
    {
        _g.set(_e.get() - _f.get());
    }

    data_reader_t<int> _e;
    data_reader_t<int> _f;
    data_writer_t<int> _g;
};

//=============================================================================

class worker5_t : public worker_node_t
{
public:
    worker5_t(const node_retriever_t& db, std::shared_ptr<int> output)
        : worker_node_t("Consume_G"), _g(db, "G"), _c(db, "C"), _output(output)
    {
        bind_accessor(_g);
        //        bind_accessor(_c);
    }

private:
    void resolve() override
    {
        *_output = _g;
    }

    data_reader_t<int> _g;
    data_writer_t<int> _c;

    std::shared_ptr<int> _output;
};

class worker6_t : public worker_node_t
{
public:
    worker6_t() : worker_node_t("null_worker") {}

private:
    void resolve() override {}
};

//=============================================================================

#define DUMP_VARS                                                                     \
    BOOST_TEST_MESSAGE(str(                                                           \
        boost::format(                                                                \
            "### State = {A=%d%s, B=%d%s, C=%d%s, D=%d%s, E=%d%s, F=%d%s, G=%d%s}\n") \
        % nodeA.get() % (nodeA.is_dirty() ? "*" : "") % nodeB.get()                   \
        % (nodeB.is_dirty() ? "*" : "") % nodeC.get() % (nodeC.is_dirty() ? "*" : "") \
        % nodeD.get() % (nodeD.is_dirty() ? "*" : "") % nodeE.get()                   \
        % (nodeE.is_dirty() ? "*" : "") % nodeF.get() % (nodeF.is_dirty() ? "*" : "") \
        % nodeG.get() % (nodeG.is_dirty() ? "*" : "")));

#define VALIDATE_ALL_DEPENDENCIES                          \
    BOOST_CHECK(!nodeA.is_dirty());                        \
    BOOST_CHECK(!nodeB.is_dirty());                        \
    BOOST_CHECK(!nodeC.is_dirty());                        \
    BOOST_CHECK(!nodeD.is_dirty());                        \
    BOOST_CHECK(!nodeE.is_dirty());                        \
    BOOST_CHECK(!nodeF.is_dirty());                        \
    BOOST_CHECK(!nodeG.is_dirty());                        \
    BOOST_CHECK(nodeC.get() == nodeA.get() + nodeB.get()); \
    BOOST_CHECK(nodeE.get() == nodeC.get() * nodeD.get()); \
    BOOST_CHECK(nodeF.get() == -nodeB.get());              \
    BOOST_CHECK(nodeG.get() == nodeE.get() - nodeF.get()); \
    BOOST_CHECK(nodeG.get() == *final_output);


BOOST_AUTO_TEST_CASE(test_experts)
{
    // Initialize container object
    expert_container::sptr container = expert_factory::create_container("example");
    uhd::property_tree::sptr tree    = uhd::property_tree::make();

    // Output of expert tree
    std::shared_ptr<int> final_output = std::make_shared<int>();

    // Add data nodes to container
    expert_factory::add_dual_prop_node<int>(
        container, tree, "A", 0, uhd::experts::AUTO_RESOLVE_ON_WRITE);
    expert_factory::add_prop_node<int>(container, tree, "B", 0);
    expert_factory::add_data_node<int>(container, "C", 0);
    expert_factory::add_data_node<int>(container, "D", 1);
    expert_factory::add_prop_node<int>(
        container, tree, "E", 0, uhd::experts::AUTO_RESOLVE_ON_READ);
    expert_factory::add_data_node<int>(container, "F", 0);
    expert_factory::add_data_node<int>(container, "G", 0);

    // Add worker nodes to container
    expert_factory::add_worker_node<worker1_t>(container, container->node_retriever());
    expert_factory::add_worker_node<worker2_t>(container, container->node_retriever());
    expert_factory::add_worker_node<worker3_t>(container, container->node_retriever());
    expert_factory::add_worker_node<worker4_t>(container, container->node_retriever());
    expert_factory::add_worker_node<worker5_t>(
        container, container->node_retriever(), final_output);
    expert_factory::add_worker_node<worker6_t>(container);

    // Once initialized, getting modify access to graph nodes is possible (by design) but
    // extremely red-flaggy! But we do it here to monitor things
    data_node_t<int>& nodeA =
        *(const_cast<data_node_t<int>*>(dynamic_cast<const data_node_t<int>*>(
            &container->node_retriever().lookup("A/desired"))));
    data_node_t<int>& nodeB = *(const_cast<data_node_t<int>*>(
        dynamic_cast<const data_node_t<int>*>(&container->node_retriever().lookup("B"))));
    data_node_t<int>& nodeC = *(const_cast<data_node_t<int>*>(
        dynamic_cast<const data_node_t<int>*>(&container->node_retriever().lookup("C"))));
    data_node_t<int>& nodeD = *(const_cast<data_node_t<int>*>(
        dynamic_cast<const data_node_t<int>*>(&container->node_retriever().lookup("D"))));
    data_node_t<int>& nodeE = *(const_cast<data_node_t<int>*>(
        dynamic_cast<const data_node_t<int>*>(&container->node_retriever().lookup("E"))));
    data_node_t<int>& nodeF = *(const_cast<data_node_t<int>*>(
        dynamic_cast<const data_node_t<int>*>(&container->node_retriever().lookup("F"))));
    data_node_t<int>& nodeG = *(const_cast<data_node_t<int>*>(
        dynamic_cast<const data_node_t<int>*>(&container->node_retriever().lookup("G"))));

    DUMP_VARS

    // Ensure init behavior
    BOOST_CHECK(nodeA.is_dirty());
    BOOST_CHECK(nodeB.is_dirty());
    BOOST_CHECK(nodeC.is_dirty());
    BOOST_CHECK(nodeD.is_dirty());
    BOOST_CHECK(nodeE.is_dirty());
    BOOST_CHECK(nodeF.is_dirty());
    BOOST_CHECK(nodeG.is_dirty());
    container->resolve_all();
    VALIDATE_ALL_DEPENDENCIES // Ensure a default resolve

        // Ensure basic node value propagation
        tree->access<int>("B")
            .set(3);
    BOOST_CHECK(nodeB.get() == 3); // Ensure value propagated
    BOOST_CHECK(nodeB.is_dirty()); // Ensure that nothing got resolved...
    container->resolve_all();
    VALIDATE_ALL_DEPENDENCIES

    nodeD.set(2); // Hack for testing

    // Ensure auto-resolve on write
    tree->access<int>("A").set(200);
    BOOST_CHECK(nodeC.get() == nodeA.get() + nodeB.get());
    BOOST_CHECK(nodeE.get() == nodeC.get() * nodeD.get());
    BOOST_CHECK(nodeG.get() == nodeE.get() - nodeF.get());
    container->resolve_all();
    VALIDATE_ALL_DEPENDENCIES

    container->resolve_all();
    VALIDATE_ALL_DEPENDENCIES

    // Ensure auto-resolve on read
    tree->access<int>("E").get();
    BOOST_CHECK(nodeC.get() == nodeA.get() + nodeB.get());
    BOOST_CHECK(nodeE.get() == nodeC.get() * nodeD.get());
    BOOST_CHECK(!nodeE.is_dirty());
    tree->access<int>("E").set(-10);
    container->resolve_all(true);
    VALIDATE_ALL_DEPENDENCIES

    // Resolve to/from
    tree->access<int>("A").set(-1);
    container->resolve_to("C");
    BOOST_CHECK(nodeC.get() == nodeA.get() + nodeB.get());
    BOOST_CHECK(!nodeC.is_dirty());
    container->resolve_to("Consume_G");
    VALIDATE_ALL_DEPENDENCIES
}