Code Examples
A repository of 155 code examples for BeepBeep
WindowSimple.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.Cumulate;
23 import ca.uqac.lif.cep.functions.CumulativeFunction;
24 import ca.uqac.lif.cep.tmf.QueueSource;
25 import ca.uqac.lif.cep.tmf.Window;
26 import ca.uqac.lif.cep.util.Numbers;
27 
28 /**
29  * Use a {@link ca.uqac.lif.cep.tmf.Window Window} processor to perform a
30  * computation over a sliding window of events.
31  * The chain of processors in this example can be
32  * represented graphically as:
33  * <p>
34  * <img src="./doc-files/basic/WindowSimple.png" alt="Processor graph">
35  * @author Sylvain HallĂ©
36  * @difficulty Easy
37  */
38 public class WindowSimple
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  // Create a cumulate processor
46  Cumulate sum = new Cumulate(
47  new CumulativeFunction<Number>(Numbers.addition));
48 
49  // Create a window processor of width 3, using sum as the
50  // processor to be used on each window. Connect it to the source.
51  Window win = new Window(sum, 3);
52  Connector.connect(source, win);
53 
54  // Pull events from the window
55  Pullable p = win.getPullableOutput();
56  System.out.println("First window: " + p.pull());
57  System.out.println("Second window: " + p.pull());
58  System.out.println("Third window: " + p.pull());
59  ///
60  }
61 }
static void main(String[] args)
Use a Window processor to perform a computation over a sliding window of events.