KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > fractal > jmx > julia > stat > ExponentialSmoothingStatController


1 /***
2  * Fractal JMX
3  * Copyright (C) 2003 France Telecom R&D
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library 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 GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * Contact: fractal@objectweb.org
20  */

21 package org.objectweb.fractal.jmx.julia.stat;
22
23 import org.objectweb.fractal.julia.loader.Tree;
24
25 /**
26  * Extends {@link BasicStatController} with an exponential smoothing method for the rate computation.
27  * The rate values returned by this implementation are computed as follows:
28  * <pre>
29  * S<sub>T</sub> = (alpha * x<sub>T</sub>) + ((1 - alpha) * S<sub>T-1</sub>)
30  * </pre>
31  *
32  * where:
33  * <ul>
34  * <li>S<sub>T</sub>: is the current smoothed rate value. It represents a weighted average of all past rate values
35  * returned by {@link BasicStatController}.
36  * <li>alpha: is the {@link #_alpha smoothing constant}.
37  * Its value must be in the interval [0-1] and can be fixed by {@link #initialize initialize}.
38  * <br>If alpha equals 1, the rate values returned by this implementation are similar to the values returned by
39  * {@link BasicStatController}.
40  * <li>x<sub>T</sub>: is the current actual rate value returned by {@link BasicStatController}.
41  * <li>S<sub>T-1</sub>: is the previous smoothed rate value.
42  * </ul>
43  *
44  *
45  * @version 0.1
46  */

47 public class ExponentialSmoothingStatController extends BasicStatController {
48     /**
49      * Smoothing constant (default value is 0.5).
50      * <p>Its value must be in the interval [0-1] and can be fixed by {@link #initialize initialize}.
51      */

52     protected double _alpha = 0.5;
53
54     private double _lastRateOfMethodCall;
55
56     private double _lastRateOfMethodSuccess;
57
58     // -------------------------------------------------------------------------
59
// overload BasicStatController
60
// -------------------------------------------------------------------------
61
/**
62      * Initializes this object with the given arguments.
63      * The first subtree (if any) must represent the value of {@link #_period period}.
64      * The second subtree (if any) must represent the value of the {@link #_alpha smoothing constant}.
65      *
66      * @param args the arguments to be used to initialize this object.
67      * @throws NumberFormatException
68      * if the first subtree is not a valid string representation of a long value,
69      * or if the second subtree is not a valid string representation of a double value.
70      * @throws IllegalStateException
71      * if the firts subtree does not represent a positive value
72      * or if the second subtree does not represent a value in the interval [0-1].
73      */

74     public void initialize(final Tree args) {
75         //System.out.println("-- " + args.toString() + ", period=" + period + ", alpha=" + alpha);
76
if (args.getSize() == 0)
77             return;
78         else if (args.getSize() == 1){
79             super.initialize(args);
80             return;
81         }
82         super.initialize(args);
83         _alpha = Double.valueOf(args.getSubTree(1).toString()).doubleValue();
84         if ((_alpha < 0) || (_alpha > 1))
85             throw new IllegalStateException JavaDoc("alpha=" + _alpha + " is invalid. The value must in the interval [0-1].");
86         //System.out.println("-- " + args.toString() + ", period=" + period + ", alpha=" + alpha);
87
}
88
89     public double getRateOfMethodCall() {
90         return _lastRateOfMethodCall = expSmoothing(super.getRateOfMethodCall(), _lastRateOfMethodCall);
91     }
92
93     public double getRateOfMethodSuccess() {
94         return _lastRateOfMethodSuccess = expSmoothing(super.getRateOfMethodSuccess(), _lastRateOfMethodSuccess);
95     }
96
97     public void reset() {
98         super.reset();
99         _lastRateOfMethodCall = 0;
100         _lastRateOfMethodSuccess = 0;
101     }
102
103     private double expSmoothing(double newRate, double lastRate) {
104         return (_alpha * newRate) + (1 - _alpha) * lastRate;
105     }
106 }
107
108
Popular Tags