Code Examples
A repository of 155 code examples for BeepBeep
Average.java
1 /*
2  BeepBeep, an event stream processor
3  Copyright (C) 2008-2016 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 basic;
19 
20 import static ca.uqac.lif.cep.Connector.INPUT;
21 import static ca.uqac.lif.cep.Connector.LEFT;
22 import static ca.uqac.lif.cep.Connector.OUTPUT;
23 import static ca.uqac.lif.cep.Connector.RIGHT;
24 
25 import ca.uqac.lif.cep.Connector;
26 import ca.uqac.lif.cep.Pullable;
27 import ca.uqac.lif.cep.UtilityMethods;
28 import ca.uqac.lif.cep.functions.CumulativeFunction;
29 import ca.uqac.lif.cep.functions.ApplyFunction;
30 import ca.uqac.lif.cep.functions.Cumulate;
31 import ca.uqac.lif.cep.tmf.QueueSource;
32 import ca.uqac.lif.cep.util.Numbers;
33 
34 /**
35  * Compute the cumulative average of a list of numbers. The cumulative average
36  * is the average of all the numbers processed so far.
37  * <p>
38  * Consider for example the stream of numbers 2, 7, 1, 8, &hellip;. After
39  * reading the first event, the cumulative average is 2&nbsp;&div;&nbsp;1 = 2.
40  * After reading the second event, the average is (2 + 7)&nbsp;&div;&nbsp;2,
41  * and after reading the third, the average is (2 + 7 + 1)&nbsp;&div;&nbsp;3
42  * = 3.33 --and so on.
43  * <p>
44  * This example illustrates the use of the {@link Cumulate} and
45  * {@link CumulativeFunction} objects.
46  * <p>
47  * We will compute this average by computing the cumulative sum of a stream
48  * of numbers, and dividing it by the value of a counter that increments by
49  * 1 each time it is pulled.
50  * Represented graphically, this example corresponds to the following chain
51  * of processors:
52  * <p>
53  * <img src="./doc-files/basic/Average.png" alt="Processor graph">
54  * <p>
55  * The output of this program should look like this:
56  * <pre>
57  * The cumulative average is...
58  * 2.0, 4.5, 3.3333333, 4.5, 4.0, 4.6666665, 4.142857, 4.625, 4.3333335,
59 * </pre>
60  * @author Sylvain HallĂ©
61  * @difficulty Easy
62  */
63 public class Average
64 {
65 
66  public static void main(String[] args)
67  {
68  /* Hello! */
69  UtilityMethods.printGreeting();
70 
71  /* Let us first create a source of arbitrary numbers. This is done
72  * by instantiating a {@link QueueSource} object, and giving to it
73  * an array of numbers. */
74  ///
75  QueueSource numbers = new QueueSource(1);
76  numbers.setEvents(new Object[]{2, 7, 1, 8, 2, 8, 1, 8, 2, 8,
77  4, 5, 9, 0, 4, 5, 2, 3, 5, 3, 6, 0, 2, 8, 7});
78 
79  /* We pipe the output of this processor to a cumulative processor.
80  * Such a processor applies a function with two arguments: the first
81  * is the event that is being received, and the second is the output
82  * value of the cumulative function at the previous step. In our
83  * case, the function we use is addition over numbers; hence the
84  * output is the cumulative sum of all numbers received so far. */
85  Cumulate sum_proc = new Cumulate(
86  new CumulativeFunction<Number>(Numbers.addition));
87  Connector.connect(numbers, OUTPUT, sum_proc, INPUT);
88 
89  /* Now we create a source of numbers acting as a counter. */
90  QueueSource counter = new QueueSource().setEvents(1, 2, 3, 4, 5, 6, 7);
91 
92 
93  /* Divide one trace by the other; the output is the cumulative average
94  * of all numbers seen so far. */
95  ApplyFunction division = new ApplyFunction(Numbers.division);
96  Connector.connect(sum_proc, OUTPUT, division, LEFT);
97  Connector.connect(counter, OUTPUT, division, RIGHT);
98  ///
99 
100  /* Extract the first 20 events from that pipe and print them. */
101  Pullable p = division.getPullableOutput();
102  System.out.println("The cumulative average is...");
103  for (int i = 0; i < 20; i++)
104  {
105  float value = (Float) p.pull();
106  System.out.print(value + ", ");
107  UtilityMethods.pause(1000);
108  }
109  System.out.println("done!");
110  }
111 }
Compute the cumulative average of a list of numbers.
Definition: Average.java:63