Code Examples
A repository of 155 code examples for BeepBeep
DecimationSum.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 import static ca.uqac.lif.cep.Connector.connect;
25 
26 import ca.uqac.lif.cep.Pullable;
27 import ca.uqac.lif.cep.UtilityMethods;
28 import ca.uqac.lif.cep.functions.ApplyFunction;
29 import ca.uqac.lif.cep.tmf.CountDecimate;
30 import ca.uqac.lif.cep.tmf.Fork;
31 import ca.uqac.lif.cep.tmf.QueueSource;
32 import ca.uqac.lif.cep.tmf.Trim;
33 import ca.uqac.lif.cep.util.Numbers;
34 
35 /**
36  * Compute the sum of every pair of successive events. Here we are
37  * interested in computing the sum of events at position 0-1, 2-3, 4-5, etc.
38  * To do so, we compute first the sum of every two successive events,
39  * and then keep every other event of the resulting trace.
40  * Graphically, this chain of processors can be represented as:
41  * <p>
42  * <img src="./doc-files/basic/Decimation.png" alt="Processor graph">
43  * <p>
44  * On the input stream 6, 5, 3, 8, 9, 2, 1, 7, 4, 5, &hellip;,
45  * the expected output of this program is:
46  * <pre>
47  * Event #0 is: 11
48  * Event #1 is: 11
49  * Event #2 is: 11
50  * Event #3 is: 8
51  * Event #4 is: 9
52  * Event #5 is: 6
53  * Event #6 is: 13
54  * Event #7 is: 20
55  * Event #8 is: 7
56  * </pre>
57  *
58  * @author Sylvain HallĂ©
59  * @difficulty Easy
60  */
61 public class DecimationSum
62 {
63  public static void main(String[] args)
64  {
65  /* Create a stream of dummy values. */
66  QueueSource source_values = new QueueSource();
67  source_values.setEvents(6, 5, 3, 8, 9, 2, 1, 7, 4, 5,
68  2, 4, 7, 6, 12, 8, 1);
69 
70  /* Duplicate this stream into two paths. */
71  Fork fork = new Fork(2);
72  connect(source_values, fork);
73 
74  /* The first path is plugged directly as the first argument of
75  * an adding processor. */
76  ApplyFunction sum = new ApplyFunction(Numbers.addition);
77  connect(fork, LEFT, sum, LEFT);
78 
79  /* Along the second path, we start by removing the first event of
80  * the stream. This is done using the Trim processor. */
81  Trim trim = new Trim(1);
82  connect(fork, RIGHT, trim, INPUT);
83 
84  /* We then plug the output of this Trim processor as the second
85  * argument of the adding processor. This has for effect that the
86  * sum processor will add events of the input stream at positions
87  * i and i+1 (for i = 0, 1, 2, etc.). From the stream of numbers
88  * defined above, the output of this processor will be:
89  * 6+5, 5+3, 3+8, 8+9, etc. */
90  connect(trim, OUTPUT, sum, RIGHT);
91 
92  /* Since we want to keep only the sums of events at positions 0-1,
93  * 2-3, 4-5, etc., we have to remove from this output every other
94  * event. This is done with the CountDecimate processor, which is
95  * instructed to keep every <i>n</i>-th event it receives; here, n=2. */
96  CountDecimate decimate = new CountDecimate(2);
97  connect(sum, OUTPUT, decimate, INPUT);
98 
99  /* Get the Pullable of this last processor, and show the first few
100  * values it outputs. */
101  Pullable p = decimate.getPullableOutput();
102  for (int i = 0; i < 9; i++)
103  {
104  int v = ((Number) p.pull()).intValue();
105  System.out.printf("Event #%d is: %d\n", i, v);
106  // Pause between each so you have time to read!
107  UtilityMethods.pause(1000);
108  }
109  }
110 }
Compute the sum of every pair of successive events.