Code Examples
A repository of 155 code examples for BeepBeep
FunctionUsage.java
1 /*
2  BeepBeep, an event stream processor
3  Copyright (C) 2008-2018 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 java.util.Set;
21 
22 import ca.uqac.lif.cep.Context;
23 import ca.uqac.lif.cep.EventTracker;
24 import ca.uqac.lif.cep.functions.Function;
25 import ca.uqac.lif.cep.util.Booleans;
26 import ca.uqac.lif.cep.util.Numbers;
27 
28 /**
29  * Show the basic usage of
30  * {@link ca.uqac.lif.cep.functions.Function Function} objects.
31  * {@link ca.uqac.lif.cep.functions.Function#evaluate(Object[], Object[])
32  * @author Sylvain HallĂ©
33  * @difficulty Easy
34  */
35 public class FunctionUsage
36 {
37  public static void main(String[] args)
38  {
39  /// We create an instance of the negation function. The class
40  // Booleans contains a static reference to an instance of that function
41  Function negation = Booleans.not;
42 
43  // To hold the return value of a function, we must give it an array
44  Object[] out = new Object[1];
45 
46  // Let us evaluate the negation of a Boolean value. This is done
47  // by calling evaluate on the function object.
48  negation.evaluate(new Object[]{true}, out);
49 
50  // We print the return value of the function
51  System.out.println("The return value of the function is: " + out[0]);
52  ///
53 
54  //* Let us now try the same thing with a function that takes
55  // two arguments.
56  Function addition = Numbers.addition;
57 
58  // Since addition takes two arguments, we must pass it an array of
59  // two objects
60  addition.evaluate(new Object[]{2, 3}, out);
61 
62  // We print the return value of the function
63  System.out.println("The return value of the function is: " + out[0]);
64  //*
65 
66  //# Finally, a function does not necessarily have an output arity of 1.
67  // In this example, we call a function that has both an input and an
68  // output arity of 2. From two numbers x and y, it outputs the
69  // quotient and the remainder of the division of x by y.
70  Function int_division = IntegerDivision.instance;
71  Object[] outs = new Object[2];
72  int_division.evaluate(new Object[]{14, 3}, outs);
73  System.out.println("14 divided by 3 equals " +
74  outs[0] + " remainder " + outs[1]);
75  //#
76  }
77 
78  /**
79  * A function that computes integer division. From two numbers x and y,
80  * it outputs the quotient and the remainder of the division of x by y.
81  * This function is used in the code example above to show that functions
82  * can have an output arity different from 1.
83  */
84  public static class IntegerDivision extends Function
85  {
86  /**
87  * A reference to a single publicly accessible instance of the
88  * function
89  */
90  public static final transient IntegerDivision instance = new IntegerDivision();
91 
92  /**
93  * The constructor is declared private on purpose
94  */
95  private IntegerDivision()
96  {
97  super();
98  }
99 
100  @Override
101  public Function duplicate(boolean with_state)
102  {
103  return instance;
104  }
105 
106  @Override
107  public void evaluate(Object[] inputs, Object[] outputs, Context c, EventTracker t)
108  {
109  int x = (Integer) inputs[0];
110  int y = (Integer) inputs[1];
111  outputs[0] = x / y;
112  outputs[1] = x % y;
113  }
114 
115  @Override
116  public int getInputArity()
117  {
118  return 2;
119  }
120 
121  @Override
122  public int getOutputArity()
123  {
124  return 2;
125  }
126 
127  @Override
128  public void reset()
129  {
130  super.reset();
131  }
132 
133  @Override
134  public void getInputTypesFor(Set<Class<?>> classes, int index)
135  {
136  classes.add(Number.class);
137  }
138 
139  @Override
140  public Class<?> getOutputTypeFor(int index)
141  {
142  return Integer.class;
143  }
144  }
145 }
static void main(String[] args)
Show the basic usage of Function objects.