00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 package org.objectweb.proactive.benchmarks.timit.config;
00032
00033 import java.util.ArrayList;
00034 import java.util.HashMap;
00035 import java.util.Iterator;
00036 import java.util.List;
00037 import java.util.regex.Matcher;
00038 import java.util.regex.Pattern;
00039
00040 import org.jdom.Attribute;
00041 import org.jdom.Element;
00042 import org.objectweb.proactive.benchmarks.timit.util.XMLHelper;
00043
00044 public class Benchmark extends Tag {
00045
00046 private HashMap<String, String> variables;
00047
00048 public Benchmark(Element eBench) {
00049 super(eBench);
00050 this.variables = new HashMap<String, String>();
00051 Iterator it = eBench.getChildren("descriptorVariable").iterator();
00052 while (it.hasNext()) {
00053 Element var = (Element) it.next();
00054 this.variables.put(var.getAttributeValue("name"), var
00055 .getAttributeValue("value"));
00056 }
00057 }
00058
00059 public String get(String name) {
00060 name = name.toLowerCase();
00061 String value = super.get(name);
00062
00063 if (value != null) {
00064 return value;
00065 }
00066
00067
00068 if (name.equals("run")) {
00069 return "1";
00070 }
00071 if (name.equals("warmup")) {
00072 return "0";
00073 }
00074 if (name.equals("timeout")) {
00075 return "10000";
00076 }
00077 if (name.equals("removeextremums")) {
00078 return "false";
00079 }
00080 if (name.equals("writeeveryrun")) {
00081 return "true";
00082 }
00083 if (name.equals("descriptorgenerated")) {
00084 return "";
00085 }
00086
00087 throw new RuntimeException("Variable benchmark.'" + name
00088 + "' missing in configuration file");
00089 }
00090
00091 public HashMap<String, String> getVariables() {
00092 return this.variables;
00093 }
00094
00095 public static Benchmark[] toArray(List benchmarkList) {
00096 ArrayList<String> seqList;
00097 int quantity = benchmarkList.size();
00098 ArrayList<Benchmark> result = new ArrayList<Benchmark>();
00099
00100 Pattern p = Pattern.compile("[^\\x23\\x7B\\x7D]*\\x23\\x7B" +
00101 "([^\\x7D]*)" +
00102 "\\x7D[^\\x7D\\x23\\x7B]*");
00103
00104 for (int i = 0; i < quantity; i++) {
00105 Element eBench = (Element) benchmarkList.get(i);
00106 seqList = new ArrayList<String>();
00107
00108
00109 searchSequences( eBench, p, seqList );
00110 Iterator itVars = eBench.getChildren().iterator();
00111 while (itVars.hasNext()) {
00112 searchSequences( (Element) itVars.next(), p, seqList );
00113 }
00114
00115
00116 if (seqList.size() > 0) {
00117 expand(eBench, seqList, 0, result);
00118 } else {
00119 result.add(new Benchmark(eBench));
00120 }
00121 }
00122
00123 return result.toArray(new Benchmark[1]);
00124 }
00125
00126 private static void searchSequences(
00127 Element elt, Pattern p, ArrayList<String> seqList ) {
00128 Iterator itAttr = elt.getAttributes().iterator();
00129 while (itAttr.hasNext()) {
00130 Attribute attr = (Attribute) itAttr.next();
00131 Matcher m = p.matcher(attr.getValue());
00132 while (m.find()) {
00133 String sequence = m.group(1);
00134 if (!seqList.contains(sequence)) {
00135 seqList.add(sequence);
00136 }
00137 }
00138 }
00139 }
00140
00141 private static void expand(Element eBench, ArrayList<String> seqList,
00142 int index, ArrayList<Benchmark> out) {
00143
00144 String seq = seqList.get(index);
00145 String[] values = seq.split(",");
00146
00147 for (String value : values) {
00148 Element eBenchClone = (Element) eBench.clone();
00149 XMLHelper.replaceAll(
00150 eBenchClone, "\\x23\\x7B" + seq + "\\x7D",
00151 value);
00152 Iterator itDesc = eBenchClone.getDescendants();
00153 while( itDesc.hasNext() ) {
00154 Object eDesc = itDesc.next();
00155 if( eDesc instanceof Element ) {
00156 XMLHelper.replaceAll(
00157 (Element) eDesc, "\\x23\\x7B" + seq + "\\x7D",
00158 value);
00159 }
00160 }
00161
00162 if (index + 1 < seqList.size()) {
00163 expand(eBenchClone, seqList, index + 1, out);
00164 } else {
00165 out.add(new Benchmark(eBenchClone));
00166 }
00167 }
00168 }
00169 }