Code Examples
A repository of 155 code examples for BeepBeep
WindowEven.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.functions.Cumulate;
24 import ca.uqac.lif.cep.functions.CumulativeFunction;
25 import ca.uqac.lif.cep.tmf.QueueSource;
26 import ca.uqac.lif.cep.tmf.Window;
27 import ca.uqac.lif.cep.util.Booleans;
28 import ca.uqac.lif.cep.util.Numbers;
29 
30 /**
31  * Use a {@link ca.uqac.lif.cep.tmf.Window Window} processor to perform a
32  * computation over a sliding window of events that are not numeric.
33  * The chain of processors in this example can be
34  * represented graphically as:
35  * <p>
36  * <img src="./doc-files/basic/WindowEven.png" alt="Processor graph">
37  * @author Sylvain HallĂ©
38  * @difficulty Easy
39  */
40 public class WindowEven
41 {
42  public static void main(String[] args)
43  {
44  /// Create a source of arbitrary numbers
45  QueueSource source = new QueueSource().setEvents(2, 7, 1, 8, 2, 8, 1, 8, 2, 8);
46 
47  // Check if each number is even
48  ApplyFunction is_even = new ApplyFunction(Numbers.isEven);
49  Connector.connect(source, is_even);
50 
51  // Create a cumulate processor
52  Cumulate sum = new Cumulate(
53  new CumulativeFunction<Boolean>(Booleans.or));
54 
55  // Create a window processor of width 3, using sum as the
56  // processor to be used on each window. Connect it to is_even.
57  Window win = new Window(sum, 3);
58  Connector.connect(is_even, win);
59 
60  // Pull events from the window
61  Pullable p = win.getPullableOutput();
62  for (int i = 0; i < 10; i++)
63  {
64  System.out.println("Window #" + i + ": " + p.pull());
65  }
66  ///
67  }
68 }
static void main(String[] args)
Definition: WindowEven.java:42
Use a Window processor to perform a computation over a sliding window of events that are not numeric...
Definition: WindowEven.java:40