Code Examples
A repository of 155 code examples for BeepBeep
AverageValueAbsolute.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.StreamVariable;
28 import ca.uqac.lif.cep.functions.CumulativeFunction;
29 import ca.uqac.lif.cep.functions.Cumulate;
30 import ca.uqac.lif.cep.functions.ApplyFunction;
31 import ca.uqac.lif.cep.functions.FunctionTree;
32 import ca.uqac.lif.cep.functions.TurnInto;
33 import ca.uqac.lif.cep.peg.TrendDistance;
34 import ca.uqac.lif.cep.tmf.Fork;
35 import ca.uqac.lif.cep.tmf.QueueSource;
36 import ca.uqac.lif.cep.util.Numbers;
37 
38 /**
39  * Trend distance based on the average of values in a
40  * stream. In this example, we consider a stream of numerical values.
41  * The reference trend <tt>P</tt> is a single number, representing an
42  * average. The <tt>TrendProcessor</tt> emits an alarm when the
43  * <em>difference</em> between <tt>P</tt> and the average of values over a
44  * sliding window of width 3 is greater than ½. Note that here, the distance
45  * is measured as the absolute difference between the reference average
46  * and the observed average. One can also compute the distance in
47  * terms of percentages; see {@link AverageValueRelative}.
48  * <p>
49  * The parameters of the <tt>TrendDistance</tt> processor in this example
50  * are as follows:
51  * <table>
52  * <tr><th>Parameter</th><th>Value</th></tr>
53  * <tr>
54  * <td><img src="./doc-files/mining/trenddistance/WidthParameter.png" alt="Window Width" title="The width of the window"></td>
55  * <td>3</td>
56  * </tr>
57  * <tr>
58  * <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>
59  * <td><img src="./doc-files/mining/trenddistance/AverageProcessor.png" alt="Processor chain"></td>
60  * </tr>
61  * <tr>
62  * <td><img src="./doc-files/mining/trenddistance/PatternParameter.png" alt="Reference Pattern" title="The reference pattern"></td>
63  * <td>6</td>
64  * </tr>
65  * <tr>
66  * <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>
67  * <td><img src="./doc-files/mining/trenddistance/AbsoluteDifference.png" alt="Distance Function"></td>
68  * </tr>
69  * <tr>
70  * <td><img src="./doc-files/mining/trenddistance/ComparisonFunction.png" alt="Comparison Function" title="The function that compares that distance with a given threshold"></td>
71  * <td><img src="./doc-files/mining/LessThanOrEqual.png" alt="&leq;"></td>
72  * </tr>
73  * <tr>
74  * <td><img src="./doc-files/mining/trenddistance/DistanceThreshold.png" alt="Distance Threshold" title="The distance threshold"></td>
75  * <td>½</td>
76  * </tr>
77  * </table>
78  * Consider for example the input stream that starts with:
79  * <pre>
80  * 6.1, 5.9, 6, 6.7, 6.7, 6.7, &hellip;
81  * </pre>
82  * On the first window of width 3 (6.1, 5.9, 6), the average of the values is
83  * 6, and |6-6| &leq; ½. This average lies between 6-½ and 6+½ for windows 2
84  * and 3 as well. On the 4th window of width 3 (6.7, 6.7, 6.7), the
85  * average is 6.7, and |6-6.7| &gt; ½; this time, the <tt>TrendDistance</tt>
86  * processor returns false.
87  *
88  * @author Sylvain Hallé
89  *
90  */
91 public class AverageValueAbsolute
92 {
93  public static void main(String[] args)
94  {
95  GroupProcessor average = new GroupProcessor(1, 1);
96  {
97  Fork fork = new Fork(2);
98  average.associateInput(INPUT, fork, INPUT);
99  Cumulate sum = new Cumulate(new CumulativeFunction<Number>(Numbers.addition));
100  Connector.connect(fork, TOP, sum, INPUT);
101  TurnInto one = new TurnInto(1);
102  Connector.connect(fork, BOTTOM, one, INPUT);
103  Cumulate sum_one = new Cumulate(new CumulativeFunction<Number>(Numbers.addition));
104  Connector.connect(one, sum_one);
105  ApplyFunction div = new ApplyFunction(Numbers.division);
106  Connector.connect(sum, OUTPUT, div, TOP);
107  Connector.connect(sum_one, OUTPUT, div, BOTTOM);
108  average.associateOutput(OUTPUT, div, OUTPUT);
109  average.addProcessors(fork, sum, one, sum_one, div);
110  }
111  TrendDistance<Number,Number,Number> alarm = new TrendDistance<Number,Number,Number>(6, 3, average, new FunctionTree(Numbers.absoluteValue,
112  new FunctionTree(Numbers.subtraction, StreamVariable.X, StreamVariable.Y)), 0.5, Numbers.isLessThan);
113  QueueSource source = new QueueSource();
114  source.setEvents(6.1, 5.9, 6, 6.7, 6.7, 6.7);
115  Connector.connect(source, alarm);
116  Pullable p = alarm.getPullableOutput();
117  boolean b = true;
118  for (int i = 0; b && i < 10; i++)
119  {
120  b = (Boolean) p.pull();
121  System.out.println(b);
122  }
123  }
124 }
Trend distance based on the average of values in a stream.