In this example, we set up a graph with three generators. Each of these generators operate on the same timer. Every time the timer fires, the generators produce a packet of data. The data they produce is dictated by the functors given to them at construction time. In this case, the functors are a reference to a random number generator.
The produced packets are then fed to a transformer defined locally. This transformer takes its inputs (in terms of T), multiplies them using *=
, then outputs the multiplication expression including the product as a string. For example, given the inputs of 3 and 4, it outputs the string "3 * 4 = 12".
Finally, the transformer's output is connected to an ostreamer. This node simply streams the data packets it receives to a std::ostream of our choice, std::cout in this case.
#include <algorithm>
#include <chrono>
#include <functional>
#include <iostream>
#include <random>
#include <sstream>
#include <string>
#include <thread>
using namespace std;
template<typename T>
{
public:
multiplication_expressifier(
size_t ins = 2,
const string& name_r =
"multiplication_expressifier") : flow::node(name_r), flow::
transformer<T, string>(name_r, ins, 1) {}
virtual ~multiplication_expressifier() {}
virtual void ready(size_t n)
{
{
{
return;
}
}
vector<unique_ptr<flow::packet<T>>> terms;
{
}
T product(terms[0]->data());
for_each(terms.begin() + 1, terms.end(), [&product](const unique_ptr<flow::packet<T>>& packet_up_r){
product *= packet_up_r->data();
});
stringstream ss;
ss << terms[0]->data();
for_each(terms.begin() + 1, terms.end(), [&ss](const unique_ptr<flow::packet<T>>& packet_up_r){
ss << " * " << packet_up_r->data();
});
ss << " = " << product;
}
};
int main()
{
random_device rd;
default_random_engine engine(rd());
uniform_int_distribution<size_t> uniform(0, 10);
auto generator = bind(uniform, ref(engine));
g.
add(make_shared<multiplication_expressifier<int>>(3),
"me1");
g.
connect<
string>(
"me1", 0,
"o1", 0);
thread mt_t(ref(mt));
char c;
cin >> c;
mt.stop();
mt_t.join();
return 0;
}
Here's the output of a run of about 30 seconds:
4 * 4 * 0 = 0
10 * 3 * 9 = 270
9 * 5 * 8 = 360
2 * 7 * 0 = 0
8 * 7 * 10 = 560
1 * 5 * 5 = 25
8 * 0 * 10 = 0
6 * 3 * 2 = 36
1 * 7 * 3 = 21
3 * 10 * 0 = 0