Code Examples
A repository of 155 code examples for BeepBeep
SumTwo.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 basic;
19 
20 import ca.uqac.lif.cep.Connector;
21 import ca.uqac.lif.cep.Pullable;
22 import ca.uqac.lif.cep.functions.ApplyFunction;
23 import ca.uqac.lif.cep.tmf.Fork;
24 import ca.uqac.lif.cep.tmf.QueueSource;
25 import ca.uqac.lif.cep.tmf.Trim;
26 import ca.uqac.lif.cep.util.Numbers;
27 
28 /**
29  * Use the {@link ca.uqac.lif.cep.tmf.Trim Trim} and
30  * {@link ca.uqac.lif.cep.tmf.Fork Fork} processors to compute the sum of
31  * two successive events. The chain of processors in this example can be
32  * represented graphically as:
33  * <p>
34  * <img src="./doc-files/basic/SumTwo.png" alt="Processor graph">
35  * @author Sylvain HallĂ©
36  * @difficulty Easy
37  */
38 public class SumTwo
39 {
40  public static void main (String[] args)
41  {
42  /// Create a source of arbitrary numbers
43  QueueSource source = new QueueSource().setEvents(1, 2, 3, 4, 5, 6);
44 
45  // Fork the stream in two and connect it to the source
46  Fork fork = new Fork(2);
47  Connector.connect(source, fork);
48 
49  // Create an addition processor. Connect its first input to the
50  // first output of the fork.
51  ApplyFunction add = new ApplyFunction(Numbers.addition);
52  Connector.connect(fork, 0, add, 0);
53 
54  /* Create an instance of the Trim processor. This processor is
55  * instructed to discard the first 1 events it receives. Then
56  * connect this to the second output of the fork, and to the
57  * second input of add. */
58  Trim trim = new Trim(1);
59  Connector.connect(fork, 1, trim, 0);
60  Connector.connect(trim, 0, add, 1);
61 
62  /* Let us now print what we receive by pulling on the output of
63  * add. */
64  Pullable p = add.getPullableOutput();
65  for (int i = 0; i < 6; i++)
66  {
67  float x = (Float) p.pull();
68  System.out.println("The event is: " + x);
69  }
70  ///
71  }
72 }
static void main(String[] args)
Definition: SumTwo.java:40
Use the Trim and Fork processors to compute the sum of two successive events.
Definition: SumTwo.java:38