Code Examples
A repository of 155 code examples for BeepBeep
MaxMiningFunction.java
1 /*
2  BeepBeep, an event stream processor
3  Copyright (C) 2008-2017 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 mining.extraction;
19 
20 import java.util.Set;
21 
22 import ca.uqac.lif.cep.functions.FunctionException;
23 import ca.uqac.lif.cep.peg.Sequence;
24 import ca.uqac.lif.cep.peg.ml.SetMiningFunction;
25 import mining.SequenceReader;
26 
27 /**
28  * Create a mining function that computes the maximum value of all sequences.
29  * @see MaxMiningFunctionProcessor
30  * @author Sylvain HallĂ©
31  */
32 public class MaxMiningFunction
33 {
34  public static void main(String[] args) throws FunctionException
35  {
36  /* First, we must get from somewhere a set of sequences. For the sake
37  * of this example, we just create a few dummy sequences of numbers
38  * from the contents of a file. */
39  Set<Sequence<Number>> sequences = SequenceReader.readNumericalSequences("numbers-1.csv");
40 
41  /* We then create an instance of our mining function. A mining function
42  * takes as input a set of sequences, and returns for its output some
43  * "pattern" extracted from this set of sequences. */
44  SumFunction a_f = new SumFunction();
45 
46  /* We then evaluate the function on our set of sequences. */
47  Object[] outputs = new Object[1];
48  a_f.evaluate(new Object[]{sequences}, outputs);
49 
50  /* In this case, the result of this function should be the sum of
51  * all values in all the input sequences. */
52  System.out.println(outputs[0]);
53  }
54 
55  public static class SumFunction extends SetMiningFunction<Number,Number>
56  {
57  public SumFunction()
58  {
59  /* The constructor must simply specify that the output type
60  * of this function is a number. */
61  super(Number.class);
62  }
63 
64  @Override
65  public Number mine(Set<Sequence<Number>> sequences) throws FunctionException
66  {
67  /* The mine() method's job is to go trough all input sequences
68  * in the set, and to compute the "pattern" to extract from them.
69  * Here, the simple pattern we want to compute is the maximum of
70  * all values in all sequences. */
71  float max = Float.MIN_VALUE;
72  for (Sequence<Number> seq : sequences)
73  {
74  for (Number n : seq)
75  {
76  max = Math.max(max, n.floatValue());
77  }
78  }
79  return max;
80  }
81  }
82 }
Extract patterns from input streams using data mining and statistical algorithms. ...
Create a mining function that computes the maximum value of all sequences.
Utility class that creates a set of sequences from a file.