Code Examples
A repository of 155 code examples for BeepBeep
SimpleFunction.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 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.QueueSource;
24 import ca.uqac.lif.cep.util.Booleans;
25 
26 /**
27  * Use the {@link ca.uqac.lif.cep.functions.ApplyFunction FunctionProcessor}
28  * to apply a function to each input event. Here, we apply the negation to
29  * every Boolean event. This can be represented grahpically as follows:
30  * <p>
31  * <img src="./doc-files/basic/SimpleFunction.png" alt="Processor graph">
32  * <p>
33  * For an input stream with the values <tt>false</tt>, <tt>true</tt>, <tt>true</tt>,
34  * <tt>false</tt>, <tt>true</tt>, the expected output should be:
35  * <pre>
36  * The event is: true
37  * The event is: false
38  * The event is: false
39  * The event is: true
40  * The event is: false
41  * </pre>
42  * @author Sylvain HallĂ©
43  * @difficulty Easy
44  */
45 public class SimpleFunction
46 {
47  /*
48  * In this example, we apply the Negation function to a trace of
49  * Boolean values.
50  */
51  public static void main (String[] args)
52  {
53  ///
54  QueueSource source = new QueueSource();
55  source.setEvents(false, true, true, false, true);
56  ApplyFunction not = new ApplyFunction(Booleans.not);
57  Connector.connect(source, not);
58  Pullable p = not.getPullableOutput();
59  for (int i = 0; i < 5; i++)
60  {
61  boolean x = (Boolean) p.pull();
62  System.out.println("The event is: " + x);
63  }
64  ///
65  }
66 }
Use the FunctionProcessor to apply a function to each input event.