Code Examples
A repository of 155 code examples for BeepBeep
Average.java
1 /*
2  BeepBeep, an event stream processor
3  Copyright (C) 2008-2018 Sylvain HallĂ©
4 
5  This program is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published
7  by the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 package widgets;
19 
20 import ca.uqac.lif.cep.Connector;
21 import static ca.uqac.lif.cep.Connector.BOTTOM;
22 import static ca.uqac.lif.cep.Connector.INPUT;
23 import static ca.uqac.lif.cep.Connector.OUTPUT;
24 import static ca.uqac.lif.cep.Connector.TOP;
25 import ca.uqac.lif.cep.GroupProcessor;
26 import ca.uqac.lif.cep.functions.Cumulate;
27 import ca.uqac.lif.cep.functions.CumulativeFunction;
28 import ca.uqac.lif.cep.functions.TurnInto;
29 import ca.uqac.lif.cep.functions.ApplyFunction;
30 import ca.uqac.lif.cep.tmf.Fork;
31 import ca.uqac.lif.cep.util.Numbers;
32 
33 /**
34  * Group processor that computes the cumulative average of a stream of
35  * numbers. This processor is similar to the {@link basic.Average Average}
36  * examples of the {@link basic} section. Graphically, it corresponds to the
37  * following chain of processors:
38  * <p>
39  * <img
40  * src="./doc-files/widgets/Average.png"
41  * alt="Processor graph">
42  * <p>
43  * Since we encapsulate this chain into a
44  * {@link ca.uqac.lif.cep.GroupProcessor GroupProcessor}, in other processor
45  * graphs that use it, it will be drawn as this:
46  * <p>
47  * <img
48  * src="./doc-files/widgets/Average-box.png"
49  * alt="Processor graph">
50  *
51  * @author Sylvain HallĂ©
52  * @see basic.Average Average
53  * @difficulty Easy
54  */
55 public class Average extends GroupProcessor
56 {
57 
58  public Average()
59  {
60  super(1, 1);
61  Fork fork = new Fork(2);
62  associateInput(0, fork, 0);
63  TurnInto one = new TurnInto(1);
64  Connector.connect(fork, BOTTOM, one, INPUT);
65  Cumulate count = new Cumulate(new CumulativeFunction<Number>(Numbers.addition));
66  Connector.connect(one, count);
67  Cumulate sum = new Cumulate(new CumulativeFunction<Number>(Numbers.addition));
68  Connector.connect(fork, TOP, sum, INPUT);
69  ApplyFunction div = new ApplyFunction(Numbers.division);
70  Connector.connect(sum, OUTPUT, div, TOP);
71  Connector.connect(count, OUTPUT, div, BOTTOM);
72  associateOutput(0, div, 0);
73  addProcessors(fork, one, count, sum, div);
74  }
75 }
Group processor that computes the cumulative average of a stream of numbers.
Definition: Average.java:55