Code Examples
A repository of 155 code examples for BeepBeep
FilterConditionComposite.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 import ca.uqac.lif.cep.Pullable;
26 import ca.uqac.lif.cep.functions.StreamVariable;
27 import ca.uqac.lif.cep.functions.Constant;
28 import ca.uqac.lif.cep.functions.ApplyFunction;
29 import ca.uqac.lif.cep.functions.FunctionTree;
30 import ca.uqac.lif.cep.tmf.Filter;
31 import ca.uqac.lif.cep.tmf.Fork;
32 import ca.uqac.lif.cep.tmf.QueueSource;
33 import ca.uqac.lif.cep.util.Booleans;
34 import ca.uqac.lif.cep.util.Numbers;
35 
36 /**
37  * Filter a trace by evaluating a compound condition on the events of
38  * that trace.
39  * <p>
40  * In this example, we wish to keep all events that are even <em>and</em>
41  * greater than 4, and discard the others.
42  * This example can be represented grahpically as:
43  * <p>
44  * <img src="./doc-files/basic/FilterConditionComposite.png" alt="Processor graph">
45  * @author Sylvain Hallé
46  * @difficulty Easy
47  */
49 {
50  public static void main(String[] args)
51  {
52  // Create a trace of dummy values
53  QueueSource source_values = new QueueSource();
54  source_values.setEvents(6, 5, 3, 8, 9, 2, 1, 7, 4, 5,
55  2, 4, 7, 6, 12, 8, 1);
56  // Fork the trace in two
57  Fork fork = new Fork(2);
58  connect(source_values, fork);
59  // Connect the first ("left") output of the fork into a filter
60  Filter filter = new Filter();
61  connect(fork, LEFT, filter, LEFT);
62  // Create the compound condition "is even and is greater than 4"
63  FunctionTree tree = new FunctionTree(Booleans.and,
64  Numbers.isEven,
65  new FunctionTree(Numbers.isGreaterThan,
66  StreamVariable.X,
67  new Constant(4)));
68  ApplyFunction condition = new ApplyFunction(tree);
69  // Connect its input to the second output of the fork
70  connect(fork, RIGHT, condition, INPUT);
71  // Connect the condition as the second input of our filter
72  connect(condition, OUTPUT, filter, RIGHT);
73  // Get a reference to the filter's output pullable
74  Pullable p = filter.getPullableOutput();
75  // Pull 4 events from p
76  for (int i = 0; i < 4; i++)
77  {
78  int x = (Integer) p.pull();
79  System.out.printf("Output event #%d is %d\n", i, x);
80  }
81  }
82 }
Filter a trace by evaluating a compound condition on the events of that trace.