Code Examples
A repository of 155 code examples for BeepBeep
SymbolDistribution.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.trenddistance;
19 
20 import static ca.uqac.lif.cep.Connector.INPUT;
21 import static ca.uqac.lif.cep.Connector.OUTPUT;
22 
23 import java.util.HashMap;
24 
25 import ca.uqac.lif.cep.Connector;
26 import ca.uqac.lif.cep.GroupProcessor;
27 import ca.uqac.lif.cep.Pullable;
28 import ca.uqac.lif.cep.functions.StreamVariable;
29 import ca.uqac.lif.cep.functions.CumulativeFunction;
30 import ca.uqac.lif.cep.functions.Cumulate;
31 import ca.uqac.lif.cep.functions.FunctionTree;
32 import ca.uqac.lif.cep.functions.IdentityFunction;
33 import ca.uqac.lif.cep.functions.TurnInto;
34 import ca.uqac.lif.cep.io.ReadStringStream;
35 import ca.uqac.lif.cep.peg.MapDistance;
36 import ca.uqac.lif.cep.peg.TrendDistance;
37 import ca.uqac.lif.cep.tmf.Slice;
38 import ca.uqac.lif.cep.util.Numbers;
39 import ca.uqac.lif.cep.util.FindPattern;
40 
41 /**
42  * Trend distance based on the statistical distribution of symbols in a
43  * stream.
44  * <p>
45  * The parameters of the <tt>TrendDistance</tt> processor in this example
46  * are as follows:
47  * <table>
48  * <tr><th>Parameter</th><th>Value</th></tr>
49  * <tr>
50  * <td><img src="./doc-files/mining/trenddistance/WidthParameter.png" alt="Window Width" title="The width of the window"></td>
51  * <td>9</td>
52  * </tr>
53  * <tr>
54  * <td><img src="./doc-files/mining/trenddistance/BetaProcessor.png" alt="Beta processor" title="The processor that computes the pattern over the current input stream"></td>
55  * <td><img src="./doc-files/mining/trenddistance/SymbolDistribution-PatternProcessor.png" alt="Processor chain"></td>
56  * </tr>
57  * <tr>
58  * <td><img src="./doc-files/mining/trenddistance/PatternParameter.png" alt="Reference Pattern" title="The reference pattern"></td>
59  * <td>A map that associates each symbol with a number of occurrences. The map is:<table><tr><th>a</th><td>6</td></tr><tr><th>b</th><td>1</td></tr><tr><th>c</th><td>2</td></tr></table></td>
60  * </tr>
61  * <tr>
62  * <td><img src="./doc-files/mining/trenddistance/DistanceFunction.png" alt="Distance Function" title="The function that computes the distance with respect to the reference pattern"></td>
63  * <td><img src="./doc-files/mining/MapDistance.png" alt="Distance Function"> ({@link ca.uqac.lif.cep.peg.MapDistance MapDistance})</td>
64  * </tr>
65  * <tr>
66  * <td><img src="./doc-files/mining/trenddistance/ComparisonFunction.png" alt="Comparison Function" title="The function that compares that distance with a given threshold"></td>
67  * <td><img src="./doc-files/mining/LessThanOrEqual.png" alt="&leq;"></td>
68  * </tr>
69  * <tr>
70  * <td><img src="./doc-files/mining/trenddistance/DistanceThreshold.png" alt="Distance Threshold" title="The distance threshold"></td>
71  * <td>2</td>
72  * </tr>
73 
74  * </table>
75  *
76  * @author Sylvain HallĂ©
77  *
78  */
79 public class SymbolDistribution
80 {
81  @SuppressWarnings("rawtypes")
82  public static void main(String[] args)
83  {
84  ReadStringStream reader = new ReadStringStream(SymbolDistribution.class.getResourceAsStream("SymbolDistribution.txt"));
85  FindPattern feeder = new FindPattern("(.*?),");
86  Connector.connect(reader, feeder);
87  GroupProcessor counter = new GroupProcessor(1, 1);
88  {
89  TurnInto one = new TurnInto(1);
90  counter.associateInput(INPUT, one, INPUT);
91  Cumulate sum_one = new Cumulate(new CumulativeFunction<Number>(Numbers.addition));
92  Connector.connect(one, sum_one);
93  counter.associateOutput(OUTPUT, sum_one, OUTPUT);
94  counter.addProcessors(one, sum_one);
95  }
96  Slice slicer = new Slice(new IdentityFunction(1), counter);
97  HashMap<Object,Object> pattern = MapDistance.createMap("a", 6, "b", 1, "c", 2);
98  TrendDistance<HashMap,Number,Number> alarm = new TrendDistance<HashMap,Number,Number>(pattern, 9, slicer, new FunctionTree(Numbers.absoluteValue,
99  new FunctionTree(MapDistance.instance, StreamVariable.X, StreamVariable.Y)), 2, Numbers.isLessThan);
100  Connector.connect(feeder, alarm);
101  Pullable p = alarm.getPullableOutput();
102  boolean b = true;
103  for (int i = 0; b && i < 10; i++)
104  {
105  b = (Boolean) p.pull();
106  System.out.println(b);
107  }
108  }
109 }
Trend distance based on the statistical distribution of symbols in a stream.