DerivedMetric.java

Go to the documentation of this file.
00001 
00004 package edu.rice.cs.hpc.data.experiment.metric;
00005 
00006 import edu.rice.cs.hpc.data.experiment.Experiment;
00007 import edu.rice.cs.hpc.data.experiment.scope.*;
00008 
00009 //math expression
00010 import com.graphbuilder.math.*;
00011 
00016 public class DerivedMetric extends BaseMetric {
00017     //===================================================================================
00018     // DATA
00019     //===================================================================================
00020 
00021     // formula expression
00022     private Expression expression;
00023     // the total aggregate value
00024     private double dRootValue = 0.0;
00025     // map function
00026     private ExtFuncMap fctMap;
00027     // map variable 
00028     private MetricVarMap varMap;
00029 
00030     private Experiment experiment;
00031     
00032     //===================================================================================
00033     // CONSTRUCTORS
00034     //===================================================================================
00035     
00036 
00037     /*****
00038      * Create derived metric based on experiment data. We'll associate this metric with the root scope of CCT
00039      * <p/>
00040      * A metric should be independent to root scope. The root scope is only used to compute the percentage
00041      * 
00042      * @param experiment
00043      * @param e
00044      * @param sName
00045      * @param sID
00046      * @param index
00047      * @param annotationType
00048      * @param objType
00049      */
00050     public DerivedMetric(Experiment experiment, Expression e, String sName, String sID, int index, AnnotationType annotationType, MetricType objType) {
00051         
00052         // no root scope information is provided, we'll associate this metric to CCT root scope 
00053         // the partner of this metric is itself (derived metric has no partner)
00054         super(sID, sName, true, null, annotationType, index, index, objType);
00055         
00056         this.expression = e;
00057         this.experiment = experiment;
00058         
00059         // set up the functions
00060         this.fctMap = new ExtFuncMap();
00061         
00062         RootScope root = (RootScope) experiment.getRootScope().getSubscope(0);
00063         
00064         BaseMetric []metrics = experiment.getMetrics(); 
00065         this.fctMap.init(metrics);
00066 
00067         // set up the variables
00068         this.varMap = new MetricVarMap(experiment);
00069 
00070         // Bug fix: always compute the aggregate value 
00071         this.dRootValue = getAggregateMetrics(root);
00072         if(this.dRootValue == 0.0)
00073             this.annotationType = AnnotationType.NONE ;
00074     }
00075     
00076     /****
00077      * Set the new expression
00078      * 
00079      * @param expr : the new expression
00080      */
00081     public void setExpression( Expression expr ) {
00082         this.expression = expr;
00083         
00084         // new formula has been set, refresh the root value used for computing percent
00085         RootScope root = (RootScope) experiment.getRootScope().getSubscope(0);
00086         dRootValue = getAggregateMetrics(root);
00087     }
00088 
00089     // -----------------------------------------------------------------------
00090     //  For the time being, the aggregate value will be computed point-wise, just like
00091     //  the way scopes are computed.
00092     // -----------------------------------------------------------------------
00098     private double getAggregateMetrics(RootScope scopeRoot) {
00099         try {
00100             Double objSum = this.getDoubleValue(scopeRoot);
00101             if (objSum != null)
00102                 return objSum.doubleValue();
00103         } catch (Exception e) {
00104             // invalid metric ?
00105         }
00106         return Double.MIN_VALUE;
00107     }
00108     
00109     
00110     //===================================================================================
00111     // GET VALUE
00112     //===================================================================================
00118     public Double getDoubleValue(Scope scope) {
00119         Double objResult = null;
00120         this.varMap.setScope(scope);
00121         try {
00122             double dValue = this.expression.eval(this.varMap, this.fctMap);
00123             objResult = new Double(dValue);
00124         } catch(java.lang.Exception e) {
00125             // should throw an exception
00126         }
00127         return objResult;
00128     }
00129     
00134     @Override
00135     public MetricValue getValue(Scope scope) {
00136         double dVal;
00137         // if the scope is a root scope, then we return the aggregate value
00138         if(scope instanceof RootScope) {
00139             dVal = dRootValue;
00140         } else {
00141             // otherwise, we need to recompute the value again via the equation
00142             Double objVal = this.getDoubleValue(scope);
00143             if(objVal == null)
00144                 return MetricValue.NONE;    // the value is not available !
00145             dVal = objVal.doubleValue();
00146         }
00147         if(this.getAnnotationType() == AnnotationType.PERCENT){
00148             return new MetricValue(dVal, ((float) dVal/this.dRootValue));
00149         } else {
00150             return new MetricValue(dVal);
00151         }
00152     }
00153 
00154     /****
00155      * return the current expression formula
00156      * 
00157      * @return
00158      */
00159     public Expression getFormula() {
00160         return expression;
00161     }
00162 
00163     //@Override
00164     public BaseMetric duplicate() {
00165         return new DerivedMetric(experiment, expression, displayName, shortName, index, annotationType, metricType);
00166     }
00167     
00168 }

Generated on 5 May 2015 for HPCVIEWER by  doxygen 1.6.1