Code Examples
A repository of 155 code examples for BeepBeep
PatternMiningFunctionProcessor.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;
19 
20 import java.util.Set;
21 
22 import ca.uqac.lif.cep.Connector;
23 import static ca.uqac.lif.cep.Connector.BOTTOM;
24 import static ca.uqac.lif.cep.Connector.INPUT;
25 import static ca.uqac.lif.cep.Connector.OUTPUT;
26 import static ca.uqac.lif.cep.Connector.TOP;
27 import ca.uqac.lif.cep.GroupProcessor;
28 import ca.uqac.lif.cep.functions.ApplyFunction;
29 import ca.uqac.lif.cep.functions.CumulativeFunction;
30 import ca.uqac.lif.cep.functions.Cumulate;
31 import ca.uqac.lif.cep.functions.FunctionException;
32 import ca.uqac.lif.cep.functions.TurnInto;
33 import ca.uqac.lif.cep.peg.Sequence;
34 import ca.uqac.lif.cep.peg.ml.ProcessorMiningFunction;
35 import ca.uqac.lif.cep.tmf.Filter;
36 import ca.uqac.lif.cep.tmf.Fork;
37 import ca.uqac.lif.cep.tmf.Trim;
38 import ca.uqac.lif.cep.util.Equals;
39 import ca.uqac.lif.cep.util.Numbers;
40 
41 /**
42  * Create a mining function from BeepBeep processors.
43  * @see AverageMiningFunction
44  * @author Sylvain HallĂ©
45  * @difficulty Medium
46  */
48 {
49  public static void main(String[] args) throws FunctionException
50  {
51  /* First, we must get from somewhere a set of sequences. For the sake
52  * of this example, we just create a few dummy sequences of numbers
53  * from the contents of a file. */
54  Set<Sequence<Number>> sequences = SequenceReader.readNumericalSequences("numbers-1.csv");
55 
56  /* We create a processor that computes the number of times a value
57  * is followed by the same value. Look out! This processor returns
58  * nothing for a sequence where no value appears twice in a row
59  * (and will cause a NullPointerException). Exercise: could you rewrite
60  * this processor to avoid this corner case? */
61  GroupProcessor total_same = new GroupProcessor(1, 1);
62  {
63  Fork fork = new Fork(3);
64  Trim trim = new Trim(1);
65  Connector.connect(fork, 0, trim, INPUT);
66  ApplyFunction equals = new ApplyFunction(Equals.instance);
67  Connector.connect(fork, 1, equals, BOTTOM);
68  Connector.connect(trim, OUTPUT, equals, TOP);
69  TurnInto ones = new TurnInto(1);
70  Connector.connect(fork, 2, ones, INPUT);
71  Filter filter = new Filter();
72  Connector.connect(ones, OUTPUT, filter, TOP);
73  Connector.connect(equals, OUTPUT, filter, BOTTOM);
74  Cumulate sum = new Cumulate(new CumulativeFunction<Number>(Numbers.addition));
75  Connector.connect(filter, sum);
76  total_same.associateInput(INPUT, fork, INPUT);
77  total_same.associateOutput(OUTPUT, sum, OUTPUT);
78  total_same.addProcessors(fork, sum, trim, ones, equals, filter);
79  }
80 
81  /* We then create an instance of our mining function. A mining function
82  * takes as input a set of sequences, and returns for its output some
83  * "pattern" extracted from this set of sequences. */
84  ProcessorMiningFunction<Number,Number> a_f = new ProcessorMiningFunction<Number,Number>(total_same, new Cumulate(new CumulativeFunction<Number>(Numbers.maximum)), 0);
85 
86  /* We then evaluate the function on our set of sequences. */
87  Object[] outputs = new Object[1];
88  a_f.evaluate(new Object[]{sequences}, outputs);
89 
90  /* In this case, the result of this function should be the maximum
91  * number of times the same value has been seen twice in a single
92  * input sequence. */
93  System.out.println(outputs[0]);
94  }
95 }
Utility class that creates a set of sequences from a file.
Create a mining function from BeepBeep processors.