Code Examples
A repository of 155 code examples for BeepBeep
AverageValueRelative.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.BOTTOM;
21 import static ca.uqac.lif.cep.Connector.INPUT;
22 import static ca.uqac.lif.cep.Connector.OUTPUT;
23 import static ca.uqac.lif.cep.Connector.TOP;
24 import ca.uqac.lif.cep.Connector;
25 import ca.uqac.lif.cep.GroupProcessor;
26 import ca.uqac.lif.cep.Pullable;
27 import ca.uqac.lif.cep.functions.CumulativeFunction;
28 import ca.uqac.lif.cep.functions.Cumulate;
29 import ca.uqac.lif.cep.functions.ApplyFunction;
30 import ca.uqac.lif.cep.functions.TurnInto;
31 import ca.uqac.lif.cep.peg.TrendDistance;
32 import ca.uqac.lif.cep.tmf.Fork;
33 import ca.uqac.lif.cep.tmf.QueueSource;
34 import ca.uqac.lif.cep.util.Numbers;
35 
36 /**
37  * Trend distance based on the average of values in a
38  * stream. This example is similar to {@link AverageValueAbsolute}.
39  * However, the distance function computes the <em>ratio</em> (instead
40  * of the difference) between the reference average and the observed
41  * average.
42  * <p>
43  * The parameters of the <tt>TrendDistance</tt> processor in this example
44  * are as follows:
45  * <table>
46  * <tr><th>Parameter</th><th>Value</th></tr>
47  * <tr>
48  * <td><img src="./doc-files/mining/trenddistance/WidthParameter.png" alt="Window Width" title="The width of the window"></td>
49  * <td>3</td>
50  * </tr>
51  * <tr>
52  * <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>
53  * <td><img src="./doc-files/mining/trenddistance/AverageProcessor.png" alt="Processor chain"></td>
54  * </tr>
55  * <tr>
56  * <td><img src="./doc-files/mining/trenddistance/PatternParameter.png" alt="Reference Pattern" title="The reference pattern"></td>
57  * <td>6</td>
58  * </tr>
59  * <tr>
60  * <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>
61  * <td><img src="./doc-files/mining/trenddistance/Division.png" alt="Distance Function"></td>
62  * </tr>
63  * <tr>
64  * <td><img src="./doc-files/mining/trenddistance/ComparisonFunction.png" alt="Comparison Function" title="The function that compares that distance with a given threshold"></td>
65  * <td><img src="./doc-files/mining/LessThanOrEqual.png" alt="&leq;"></td>
66  * </tr>
67  * <tr>
68  * <td><img src="./doc-files/mining/trenddistance/DistanceThreshold.png" alt="Distance Threshold" title="The distance threshold"></td>
69  * <td>½</td>
70  * </tr>
71  * </table>
72  *
73  * @author Sylvain Hallé
74  *
75  */
76 public class AverageValueRelative
77 {
78  public static void main(String[] args)
79  {
80  GroupProcessor average = new GroupProcessor(1, 1);
81  {
82  Fork fork = new Fork(2);
83  average.associateInput(INPUT, fork, INPUT);
84  Cumulate sum = new Cumulate(new CumulativeFunction<Number>(Numbers.addition));
85  Connector.connect(fork, TOP, sum, INPUT);
86  TurnInto one = new TurnInto(1);
87  Connector.connect(fork, BOTTOM, one, INPUT);
88  Cumulate sum_one = new Cumulate(new CumulativeFunction<Number>(Numbers.addition));
89  Connector.connect(one, sum_one);
90  ApplyFunction div = new ApplyFunction(Numbers.division);
91  Connector.connect(sum, OUTPUT, div, TOP);
92  Connector.connect(sum_one, OUTPUT, div, BOTTOM);
93  average.associateOutput(OUTPUT, div, OUTPUT);
94  average.addProcessors(fork, sum, one, sum_one, div);
95  }
96  TrendDistance<Number,Number,Number> alarm = new TrendDistance<Number,Number,Number>(6, 3, average, Numbers.division, 0.5, Numbers.isLessThan);
97  QueueSource source = new QueueSource();
98  source.setEvents(6.1, 5.9, 6, 6.7, 6.7, 6.7);
99  Connector.connect(source, alarm);
100  Pullable p = alarm.getPullableOutput();
101  boolean b = true;
102  for (int i = 0; b && i < 10; i++)
103  {
104  b = (Boolean) p.pull();
105  System.out.println(b);
106  }
107  }
108 }
Trend distance based on the average of values in a stream.