00001 00004 package edu.rice.cs.hpc.data.experiment.metric; 00005 00006 import com.graphbuilder.math.Expression; 00007 import com.graphbuilder.math.ExpressionTree; 00008 import com.graphbuilder.math.FuncMap; 00009 import com.graphbuilder.math.VarMap; 00010 import com.graphbuilder.math.func.Function; 00011 00012 import edu.rice.cs.hpc.data.experiment.Experiment; 00013 import edu.rice.cs.hpc.data.experiment.scope.RootScope; 00014 import edu.rice.cs.hpc.data.experiment.scope.Scope; 00015 00020 public class MetricVarMap extends VarMap { 00021 00022 private Experiment experiment; 00023 private Scope scope; 00024 00028 public MetricVarMap() { 00029 super(false); 00030 } 00031 00032 00033 public MetricVarMap(Experiment exp) { 00034 super(false); 00035 this.experiment = exp; 00036 } 00037 00038 public MetricVarMap(Scope s, Experiment exp) { 00039 super(false); 00040 this.scope = s; 00041 this.experiment = exp; 00042 } 00043 00047 public MetricVarMap(boolean caseSensitive) { 00048 super(caseSensitive); 00049 } 00050 00051 //=========================== 00052 00053 00054 public void setExperiment(Experiment exp) { 00055 this.experiment = exp; 00056 } 00057 00062 public void setScope(Scope s) { 00063 this.scope = s; 00064 } 00065 00070 public double getValue(String varName) { 00071 assert(experiment != null); 00072 00073 if(varName.startsWith("$")) { 00074 // Metric variable 00075 String sIndex = varName.substring(1); 00076 BaseMetric metric = this.experiment.getMetric(sIndex); 00077 if (metric == null) 00078 throw new RuntimeException("metric doesn't exist: " + sIndex); 00079 if (scope != null) { 00080 MetricValue value = metric.getValue(scope); 00081 if(MetricValue.isAvailable(value)) 00082 return MetricValue.getValue(value); 00083 } 00084 return 0.0; 00085 00086 } else if (varName.startsWith("@")) { 00087 //--------------------------------------------------------- 00088 // 2011.02.08: new interpretation of the symbol "@x" where x is the metric ID 00089 // @x returns the aggregate value of metric x 00090 //--------------------------------------------------------- 00091 String sIndex = varName.substring(1); 00092 00093 try{ 00094 BaseMetric metric = this.experiment.getMetric(sIndex); 00095 if (metric == null) 00096 throw new RuntimeException("Unrecognize metric ID: " + varName); 00097 00098 RootScope root = (RootScope) this.experiment.getRootScopeChildren()[0]; 00099 return MetricValue.getValue(metric.getValue(root)); 00100 00101 } catch (java.lang.Exception e) { 00102 throw new RuntimeException("Unrecognize variable: " + varName); 00103 } 00104 } else 00105 return super.getValue(varName); 00106 } 00107 00108 00113 public static void main(String[] args) { 00114 String s = "@1*r^2"; 00115 Expression x = ExpressionTree.parse(s); 00116 00117 MetricVarMap vm = new MetricVarMap(false /* case sensitive */); 00118 vm.setValue("r", 5); 00119 00120 FuncMap fm = new FuncMap(); // no functions in expression 00121 fm.loadDefaultFunctions(); 00122 System.out.println(x); 00123 System.out.println(x.eval(vm, fm)); 00124 00125 vm.setValue("r", 10); 00126 System.out.println(x.eval(vm, fm)); 00127 Function []fs = fm.getFunctions(); 00128 for( int i=0; i<fs.length ; i++) { 00129 System.out.println("\t<tr><td>" + " <code> " + fs[i].toString() + " </code> </td> <td></td> </tr>"); 00130 } 00131 } 00132 00133 }