AggregateMetric.java
Go to the documentation of this file.00001 package edu.rice.cs.hpc.data.experiment.metric;
00002
00003 import com.graphbuilder.math.Expression;
00004 import com.graphbuilder.math.ExpressionParseException;
00005 import com.graphbuilder.math.ExpressionTree;
00006
00007 import edu.rice.cs.hpc.data.experiment.BaseExperimentWithMetrics;
00008 import edu.rice.cs.hpc.data.experiment.Experiment;
00009 import edu.rice.cs.hpc.data.experiment.scope.Scope;
00010 import com.graphbuilder.math.FuncMap;
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 public class AggregateMetric extends BaseMetric {
00023
00024 static final public char FORMULA_COMBINE = 'c';
00025 static final public char FORMULA_FINALIZE = 'f';
00026
00027
00028 private Expression formulaCombine, formulaFinalize;
00029
00030
00031
00032 private FuncMap fctMap;
00033
00034 private MetricVarMap finalizeVarMap;
00035 private CombineAggregateMetricVarMap combineVarMap;
00036
00037
00041 public AggregateMetric(String sID, String sDisplayName, boolean displayed, String format,
00042 AnnotationType annotationType, int index, int partner, MetricType type) {
00043
00044 super( sID, sDisplayName, displayed, format, annotationType, index, partner, type);
00045
00046 this.fctMap = new FuncMap();
00047 this.fctMap.loadDefaultFunctions();
00048
00049
00050 this.finalizeVarMap = new MetricVarMap();
00051 this.combineVarMap = new CombineAggregateMetricVarMap();
00052 }
00053
00054
00055
00056
00057
00058
00059
00060 public void setFormula(char type, String sFormula) {
00061 assert (type == FORMULA_COMBINE || type == FORMULA_FINALIZE);
00062
00063 try {
00064 if (type == FORMULA_COMBINE) {
00065 formulaCombine = ExpressionTree.parse(sFormula);
00066 } else {
00067 formulaFinalize = ExpressionTree.parse(sFormula);
00068 }
00069 } catch (ExpressionParseException e) {
00070 e.printStackTrace();
00071 }
00072 }
00073
00074
00075
00076
00077
00078
00079
00080
00081 public void init(BaseExperimentWithMetrics exp) {
00082 this.finalizeVarMap.setExperiment((Experiment)exp);
00083 this.combineVarMap.setExperiment((Experiment)exp);
00084 }
00085
00086
00087
00088
00089
00090
00091
00092 public void finalize(Scope scope) {
00093 Expression exp = this.formulaFinalize;
00094
00095 if (exp != null) {
00096 this.finalizeVarMap.setScope(scope);
00097 this.setScopeValue(exp, this.finalizeVarMap, scope);
00098 }
00099 }
00100
00101
00102
00103
00104
00105
00106
00107
00108 public void combine(Scope s_source, Scope s_target) {
00109 MetricValue value = s_target.getMetricValue(this);
00110 if (MetricValue.isAvailable(value)) {
00111
00112
00113
00114 Expression expression = this.formulaCombine;
00115 if (expression != null) {
00116 this.combineVarMap.setScopes(s_source, s_target);
00117 this.setScopeValue(expression, this.combineVarMap, s_target);
00118 }
00119 } else {
00120
00121
00122
00123 MetricValue v_source = s_source.getMetricValue(this);
00124 s_target.setMetricValue(index, v_source);
00125 }
00126 }
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136 private void setScopeValue(Expression expression, MetricVarMap var_map, Scope scope) {
00137 MetricValue mv;
00138 try {
00139 double dValue = expression.eval(var_map, this.fctMap);
00140 mv = new MetricValue(dValue);
00141 } catch(java.lang.Exception e) {
00142 mv = MetricValue.NONE;
00143 e.printStackTrace();
00144 }
00145 scope.setMetricValue(this.index, mv);
00146 }
00147
00148
00149 @Override
00150
00151
00152
00153
00154 public MetricValue getValue(Scope s) {
00155 MetricValue mv = s.getMetricValue(this.index);
00156 return mv;
00157 }
00158
00159 @Override
00160
00161
00162
00163
00164 public BaseMetric duplicate() {
00165 return new AggregateMetric(shortName, displayName, displayed,
00166 null, annotationType, index, partner_index, metricType);
00167 }
00168 }