20 import static ca.uqac.lif.cep.Connector.BOTTOM;
21 import static ca.uqac.lif.cep.Connector.INPUT;
22 import static ca.uqac.lif.cep.Connector.LEFT;
23 import static ca.uqac.lif.cep.Connector.OUTPUT;
24 import static ca.uqac.lif.cep.Connector.RIGHT;
25 import static ca.uqac.lif.cep.Connector.TOP;
27 import ca.uqac.lif.cep.Connector;
28 import ca.uqac.lif.cep.GroupProcessor;
29 import ca.uqac.lif.cep.Pullable;
30 import ca.uqac.lif.cep.UtilityMethods;
31 import ca.uqac.lif.cep.functions.CumulativeFunction;
32 import ca.uqac.lif.cep.functions.ApplyFunction;
33 import ca.uqac.lif.cep.functions.Cumulate;
34 import ca.uqac.lif.cep.functions.TurnInto;
35 import ca.uqac.lif.cep.tmf.Fork;
36 import ca.uqac.lif.cep.tmf.QueueSource;
37 import ca.uqac.lif.cep.tmf.Window;
38 import ca.uqac.lif.cep.util.Numbers;
51 public static void main(String[] args)
57 QueueSource numbers =
new QueueSource(1);
58 numbers.setEvents(
new Object[]{2, 7, 1, 8, 2, 8, 1, 8, 2, 8,
59 4, 5, 9, 0, 4, 5, 2, 3, 5, 3, 6, 0, 2, 8, 7});
62 GroupProcessor group =
new GroupProcessor(1, 1);
64 Fork fork =
new Fork(2);
65 Cumulate sum_proc =
new Cumulate(
66 new CumulativeFunction<Number>(Numbers.addition));
67 Connector.connect(fork, TOP, sum_proc, INPUT);
72 TurnInto ones =
new TurnInto(1);
73 Connector.connect(fork, BOTTOM, ones, INPUT);
74 Cumulate counter =
new Cumulate(
75 new CumulativeFunction<Number>(Numbers.addition));
76 Connector.connect(ones, OUTPUT, counter, INPUT);
80 ApplyFunction division =
new ApplyFunction(Numbers.division);
81 Connector.connect(sum_proc, OUTPUT, division, LEFT);
82 Connector.connect(counter, OUTPUT, division, RIGHT);
83 group.addProcessors(fork, sum_proc, ones, counter, division);
84 group.associateInput(0, fork, 0);
85 group.associateOutput(0, division, 0);
88 Window win =
new Window(group, 3);
89 Connector.connect(numbers, win);
93 Pullable p = win.getPullableOutput();
94 System.out.println(
"The cumulative average is...");
95 for (
int i = 0; i < 20; i++)
97 float value = (Float) p.pull();
98 System.out.print(value +
", ");
99 UtilityMethods.pause(1000);
101 System.out.println(
"done!");
Compute the cumulative average of a list of numbers over a sliding window.