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