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