Code Examples
A repository of 155 code examples for BeepBeep
FixedInterval.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 functions;
19 
20 import ca.uqac.lif.cep.functions.StreamVariable;
21 import ca.uqac.lif.cep.functions.Constant;
22 import ca.uqac.lif.cep.functions.FunctionException;
23 import ca.uqac.lif.cep.functions.FunctionTree;
24 import ca.uqac.lif.cep.util.Booleans;
25 import ca.uqac.lif.cep.util.Numbers;
26 
27 /**
28  * Creates a compound function (a
29  * {@link ca.uqac.lif.cep.functions.FunctionTree FunctionTree})
30  * that checks if a number lies between two fixed bounds.
31  * <p>
32  * In this example, the bounds of the interval are fixed in advance;
33  * more precisely, we evaluate if a number is between 0 and 2. You may also
34  * want to look at the {@link Interval} example, where the bounds of the
35  * interval are replaced by variables.
36  *
37  * @author Sylvain HallĂ©
38  *
39  */
40 public class FixedInterval
41 {
42  public static void main(String[] args) throws FunctionException
43  {
44  /*
45  * A FunctionTree is a function object created by
46  * composing multiple functions together. Here we create
47  * a function f(x) that returns true if x lies between
48  * 0 and 2. The TracePlaceholder objects are used to
49  * refer to the arguments, with the first argument starting at 0.
50  */
51  FunctionTree in_interval = new FunctionTree(Booleans.and,
52  new FunctionTree(Numbers.isGreaterThan,
53  StreamVariable.X,
54  Constant.ZERO), // x > 0
55  new FunctionTree(Numbers.isLessThan,
56  StreamVariable.X,
57  new Constant(2) // x < 2
58  ));
59 
60  /* Likewise, a function always returns an array of objects. Most
61  * functions (like this one) return a single object, so the output
62  * array is also of size 1. */
63  Object[] value = new Object[1];
64  in_interval.evaluate(new Integer[]{3}, value); // = {false}
65  System.out.printf("Return value of the function: %b\n", value[0]);
66  in_interval.evaluate(new Integer[]{1}, value);
67  System.out.printf("Return value of the function: %b\n", value[0]);
68  }
69 }
Creates a compound function (a FunctionTree) that checks if a number lies between two fixed bounds...