Code Examples
A repository of 155 code examples for BeepBeep
MaxSymbols.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 maximum number of distinct symbols
43  * in a stream. In this example, we compute the number of distinct symbols
44  * in a sliding window of width 4. That number, minus 3, must be less than
45  * or equal to 0. In other words, there cannot be 4 distinct symbols in
46  * any window.
47  * <p>
48  * The parameters of the <tt>TrendDistance</tt> processor in this example
49  * are as follows:
50  * <table>
51  * <tr><th>Parameter</th><th>Value</th></tr>
52  * <tr>
53  * <td><img src="./doc-files/mining/trenddistance/WidthParameter.png" alt="Window Width" title="The width of the window"></td>
54  * <td>4</td>
55  * </tr>
56  * <tr>
57  * <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>
58  * <td><img src="./doc-files/mining/trenddistance/PassthroughSlicer.png" alt="Processor chain"></td>
59  * </tr>
60  * <tr>
61  * <td><img src="./doc-files/mining/trenddistance/PatternParameter.png" alt="Reference Pattern" title="The reference pattern"></td>
62  * <td>3</td>
63  * </tr>
64  * <tr>
65  * <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>
66  * <td><img src="./doc-files/mining/trenddistance/Subtraction.png" alt="Distance Function"></td>
67  * </tr>
68  * <tr>
69  * <td><img src="./doc-files/mining/trenddistance/ComparisonFunction.png" alt="Comparison Function" title="The function that compares that distance with a given threshold"></td>
70  * <td><img src="./doc-files/mining/LessThanOrEqual.png" alt="&leq;"></td>
71  * </tr>
72  * <tr>
73  * <td><img src="./doc-files/mining/trenddistance/DistanceThreshold.png" alt="Distance Threshold" title="The distance threshold"></td>
74  * <td>1</td>
75  * </tr>
76 
77  * </table>
78  *
79  * @author Sylvain HallĂ©
80  *
81  */
82 public class MaxSymbols
83 {
84  @SuppressWarnings("rawtypes")
85  public static void main(String[] args)
86  {
87  ReadStringStream reader = new ReadStringStream(MaxSymbols.class.getResourceAsStream("SymbolDistribution.txt"));
88  FindPattern feeder = new FindPattern("(.*?),");
89  Connector.connect(reader, feeder);
90  GroupProcessor counter = new GroupProcessor(1, 1);
91  {
92  TurnInto one = new TurnInto(1);
93  counter.associateInput(INPUT, one, INPUT);
94  Cumulate sum_one = new Cumulate(new CumulativeFunction<Number>(Numbers.addition));
95  Connector.connect(one, sum_one);
96  counter.associateOutput(OUTPUT, sum_one, OUTPUT);
97  counter.addProcessors(one, sum_one);
98  }
99  Slice slicer = new Slice(new IdentityFunction(1), counter);
100  HashMap<Object,Object> pattern = MapDistance.createMap("a", 6, "b", 1, "c", 2);
101  TrendDistance<HashMap,Number,Number> alarm = new TrendDistance<HashMap,Number,Number>(pattern, 9, slicer, new FunctionTree(Numbers.absoluteValue,
102  new FunctionTree(MapDistance.instance, StreamVariable.X, StreamVariable.Y)), 2, Numbers.isLessThan);
103  Connector.connect(feeder, alarm);
104  Pullable p = alarm.getPullableOutput();
105  boolean b = true;
106  for (int i = 0; b && i < 10; i++)
107  {
108  b = (Boolean) p.pull();
109  System.out.println(b);
110  }
111  }
112 }
Trend distance based on the maximum number of distinct symbols in a stream.
Definition: MaxSymbols.java:82