org/objectweb/proactive/benchmarks/timit/config/Serie.java

00001 /* 
00002  * ################################################################
00003  * 
00004  * ProActive: The Java(TM) library for Parallel, Distributed, 
00005  *            Concurrent computing with Security and Mobility
00006  * 
00007  * Copyright (C) 1997-2007 INRIA/University of Nice-Sophia Antipolis
00008  * Contact: proactive@objectweb.org
00009  * 
00010  * This library is free software; you can redistribute it and/or
00011  * modify it under the terms of the GNU Lesser General Public
00012  * License as published by the Free Software Foundation; either
00013  * version 2.1 of the License, or any later version.
00014  *  
00015  * This library is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018  * Lesser General Public License for more details.
00019  * 
00020  * You should have received a copy of the GNU Lesser General Public
00021  * License along with this library; if not, write to the Free Software
00022  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00023  * USA
00024  *  
00025  *  Initial developer(s):               The ProActive Team
00026  *                        http://www.inria.fr/oasis/ProActive/contacts.html
00027  *  Contributor(s): 
00028  * 
00029  * ################################################################
00030  */ 
00031 package org.objectweb.proactive.benchmarks.timit.config;
00032 
00033 import java.util.ArrayList;
00034 import java.util.Arrays;
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 Serie extends Tag {
00045 
00046     private ConfigChart[] charts;
00047 
00048     private Benchmark[] benchmarks;
00049     
00050     public Serie(Element eSerie) {
00051         super(eSerie);
00052 
00053         // Construct ConfigChart array (if needed)
00054         if( eSerie.getChild("charts") != null ) {
00055             List chartList = eSerie.getChild("charts").getChildren();
00056             this.charts = new ConfigChart[chartList.size()];
00057             for (int i = 0; i < this.charts.length; i++) {
00058                 this.charts[i] = new ConfigChart((Element) chartList.get(i));
00059             }
00060         }
00061 
00062         // Construct Benchmark array
00063         List benchList = eSerie.getChild("benchmarks").getChildren();
00064         this.benchmarks = Benchmark.toArray(benchList);
00065 
00066     }
00067 
00068     public String get(String name) {
00069         name = name.toLowerCase();
00070         String value = super.get(name);
00071 
00072         if (value != null) {
00073             return value;
00074         }
00075 
00076         if (name.equals("errorfile")) {
00077             return "error.log";
00078         }
00079 
00080         throw new RuntimeException("Variable benchmark.'" + name
00081                 + "' missing in configuration file");
00082     }
00083 
00084     public Benchmark[] getBenchmarks() {
00085         return this.benchmarks.clone();
00086     }
00087 
00088     public ConfigChart[] getCharts() {
00089         if( this.charts == null ) return null;
00090         return this.charts.clone();
00091     }
00092 
00093     public String toString() {
00094         return super.toString() + Arrays.toString(this.charts) + "  -  "
00095                 + Arrays.toString(this.benchmarks) + "\n";
00096     }
00097 
00098     public static Serie[] oldtoArray(List serieList) {
00099         int quantity = serieList.size();
00100         Serie[] result = new Serie[quantity];
00101 
00102         for (int i = 0; i < quantity; i++) {
00103             Element eSerie = (Element) serieList.get(i);
00104             result[i] = new Serie(eSerie);
00105         }
00106 
00107         return result;
00108     }
00109     public static Serie[] toArray(List serieList) {
00110         
00111         ArrayList<String> seqList;
00112         int quantity = serieList.size();
00113         ArrayList<Serie> result = new ArrayList<Serie>();
00114 
00115         Pattern p = Pattern.compile("[^\\x23\\x7B\\x7D]*\\x23\\x7B" + // *#{
00116                 "([^\\x7D]*)" + // A,B,C
00117                 "\\x7D[^\\x7D\\x23\\x7B]*"); // }*
00118 
00119         for (int i = 0; i < quantity; i++) {
00120             Element eSerie = (Element) serieList.get(i);
00121             seqList = new ArrayList<String>();
00122 
00123             // 1 : searching sequences in attributes
00124             Iterator itAttr = eSerie.getAttributes().iterator();
00125             while (itAttr.hasNext()) {
00126                 Attribute attr = (Attribute) itAttr.next();
00127                 Matcher m = p.matcher(attr.getValue());
00128                 while (m.find()) {
00129                     String sequence = m.group(1);
00130                     if (!seqList.contains(sequence)) {
00131                         seqList.add(sequence);
00132                     }
00133                 }
00134             }
00135             
00136             // 2 : expanding sequences (recursive call)
00137             if (seqList.size() > 0) {
00138                 expand(eSerie, seqList, 0, result);
00139             } else {
00140                 result.add(new Serie(eSerie));
00141             }
00142         }
00143 
00144         return result.toArray(new Serie[1]);
00145     }
00146 
00147     private static void expand(Element eSerie, ArrayList<String> seqList,
00148             int index, ArrayList<Serie> out) {
00149 
00150         String seq = seqList.get(index);
00151         String[] values = seq.split(",");
00152 
00153         for (String value : values) {
00154             Element eSerieClone = (Element) eSerie.clone();
00155             XMLHelper.replaceAll(
00156                     eSerieClone, "\\x23\\x7B" + seq + "\\x7D", // #{*}
00157                     value);
00158             Iterator itDesc = eSerieClone.getDescendants();
00159             while( itDesc.hasNext() ) {
00160                 Object eDesc = itDesc.next();
00161                 if( eDesc instanceof Element ) {
00162                 XMLHelper.replaceAll(
00163                         (Element) eDesc, "\\x23\\x7B" + seq + "\\x7D", // #{*},
00164                         value);
00165                 }
00166             }
00167 
00168             if (index + 1 < seqList.size()) {
00169                 expand(eSerieClone, seqList, index + 1, out);
00170             } else {
00171                 out.add(new Serie(eSerieClone));
00172             }
00173         }
00174     }
00175 }

Generated on Mon Jan 22 15:16:04 2007 for ProActive by  doxygen 1.5.1