Main Page | Packages | Class Hierarchy | Class List | Directories | File List | Class Members | Related Pages

ResultSearchPattern.java

Go to the documentation of this file.
00001 package cedar.jetweb.model;
00002 
00003 import java.util.Vector;
00004 import java.util.Enumeration;
00005 import java.util.HashMap;
00006 import java.util.Set;
00007 import java.util.Map;
00008 import java.util.Map.Entry;
00009 import java.util.Enumeration;
00010 
00011 import cedar.jetweb.*;
00012 import cedar.jetweb.util.Match;
00013 import cedar.jetweb.job.LogFile;
00014 import cedar.jetweb.model.fit.Fit;
00015 import cedar.jetweb.db.DBManager;
00016 
00017 import cedar.jetweb.generator.Generator;
00018 
00033 public class ResultSearchPattern implements Cloneable {
00034 
00035     protected String description_;
00036 
00040     protected Vector<Generator> generatorList;
00044     protected Vector<PDF> protonpdfList;
00048     protected Vector<PDF> photonpdfList;
00049 
00054     protected Vector<Match> notMatching = new Vector<Match>();
00055 
00060     public Enumeration<Match> getNonMatchingParameters(){
00061     return notMatching.elements();
00062     }
00063 
00069     public Vector<PDF> getProtonPDFList(){ 
00070     if (protonpdfList==null) { protonpdfList = new Vector<PDF>(); }
00071     return protonpdfList; 
00072     }
00078     public PDF getProtonPDF(int i) throws JetWebException { 
00079     if (protonpdfList!=null && i < protonpdfList.size()) {
00080         try {
00081         return protonpdfList.elementAt(i); 
00082         } catch (ArrayIndexOutOfBoundsException a){
00083         throw new JetWebException(a,"Index was "+i+":");
00084         }
00085     } else {
00086         return null;
00087     }
00088     }
00094     public PDF getProtonPDF() throws JetWebException {
00095     return getProtonPDF(0); 
00096     }
00097 
00103     public Vector<PDF> getPhotonPDFList() { 
00104     if (photonpdfList==null) { photonpdfList = new Vector<PDF>(); }
00105     return photonpdfList; 
00106     }
00112     public PDF getPhotonPDF(int i) throws JetWebException {
00113  
00114     if (photonpdfList!=null && i<photonpdfList.size()) {
00115         try {
00116         return photonpdfList.elementAt(i); 
00117         } catch (ArrayIndexOutOfBoundsException a){
00118         throw new JetWebException(a,"Index was "+i+":");
00119         }
00120     } else {
00121         return null;
00122     }
00123     }
00129     public PDF getPhotonPDF() throws JetWebException { 
00130     return getPhotonPDF(0); 
00131     }
00132 
00138     public Vector<Generator> getGeneratorList(){ 
00139     if (generatorList==null) { generatorList = new Vector<Generator>(); }
00140     return generatorList; 
00141     }
00147     public Generator getGenerator(int i) throws JetWebException { 
00148     if (generatorList!=null) {
00149         try {
00150         return (Generator)generatorList.elementAt(i); 
00151         } catch (ArrayIndexOutOfBoundsException a){
00152         throw new JetWebException(a,"Index was "+i+":");
00153         }
00154     } else {
00155         return null;
00156     }
00157     }
00162     public void setGenerator(Generator gen){
00163     if (generatorList==null){
00164         generatorList = new Vector<Generator>();
00165     } else {
00166         generatorList.clear();
00167     }
00168     generatorList.add(gen);
00169     }
00170     
00175     public String getDescription(){
00176     return description_;
00177     }
00178 
00179     public ResultSearchPattern setDescription(String description){
00180     description_ = description;
00181     return this;
00182     }
00183 
00188     public Generator getGenerator() throws JetWebException { 
00189     return getGenerator(0);
00190     }
00191 
00200     public Double getPthat() throws JetWebException {   
00201     return getGenerator().getPthat();
00202     }
00203 
00208     public ResultSearchPattern (){
00209     // do nothing.
00210     }
00211 
00212     /*
00213      * Make copy of pattern. 
00214      * Does not copy the generators. 
00215      */
00216     public Object clone() {
00217     ResultSearchPattern newPattern = new ResultSearchPattern();
00218     newPattern.generatorList = 
00219         (this.generatorList==null) ? null : 
00220         new Vector<Generator>(this.generatorList);
00221     newPattern.protonpdfList = 
00222         (this.protonpdfList==null) ? null : 
00223         new Vector<PDF>(this.protonpdfList);
00224     newPattern.photonpdfList = 
00225         (this.photonpdfList==null) ? null : 
00226         new Vector<PDF>(this.photonpdfList);
00227     newPattern.notMatching = 
00228         (this.notMatching==null) ? null : 
00229         new Vector<Match>(this.notMatching);
00230 
00231     return newPattern;
00232     }
00233 
00239     private boolean compare
00240     (String paramName, Vector<Object> val1, Vector<Object> val2){
00241     boolean match = false;
00242 
00243     if(val1.size()==0 ||val2.size()==0){
00244         match = true;
00245     }else if(val1.contains(val2.elementAt(0))){
00246         match = true;
00247     }else{
00248         notMatching.add
00249         (new Match(paramName, val1.elementAt(0), val2.elementAt(0)));
00250                       
00251     }
00252     return match;
00253     }
00254 
00257     private boolean compare(String paramName, String val1, String val2){
00258     boolean match = false;
00259 
00260     if (val1.trim().length()==0 || val2.trim().length()==0) { 
00261         match = true; 
00262     } else {
00263         if (val1.equals(val2)){
00264         match = true;
00265         } else {
00266         //System.out.println("Failed"+ paramName+ ":" + val1+","+val2);
00267         notMatching.add(new Match(paramName,val1,val2));
00268         }
00269     }
00270 
00271     return match;
00272     }
00273 
00274     // compares two HashMaps of parameters, if non-matching, record in
00275     // notMatching collection.
00276     /*
00277     private boolean compare
00278     (String name,HashMap<?,?> map1,HashMap<?,?> map2, Generator gen){
00279 
00280     boolean match = true;
00281 
00282     Set<? extends Map.Entry<?,?>> list = map1.entrySet();
00283 
00284     for (Map.Entry<?,?> entry : list){
00285         Object key = entry.getKey();
00286         if (gen.inModel(name, (Integer)key) && map2.containsKey(key)){
00287         Object val1 = map2.get(key);
00288         Object val2 = entry.getValue();
00289         if (val1!=null && val2 !=null && !val1.equals(val2)){
00290             match = false;
00291             if (val1 instanceof Double) {
00292             match = Match.compareDouble((Double)val1,(Double)val2);
00293             val1 = JetWebConfig.getFormat().format(val1);
00294             val2 = JetWebConfig.getFormat().format(val2);
00295             } 
00296             if (!match) {
00297             notMatching.add
00298                 (new Match(name+":"+key.toString(), val1, val2));
00299             }
00300         }
00301         }
00302     }
00303     return match;
00304     }
00305     */
00306     private <T1, T2> boolean compare
00307     (String name, HashMap<T1, T2> map1, HashMap<T1, T2> map2, 
00308      Generator gen){
00309 
00310     boolean match = true;
00311 
00312     for(Map.Entry<T1, T2> entry: map1.entrySet()){
00313         T1 key = entry.getKey();
00314         boolean inModel = true;
00315         if(key instanceof Integer){
00316         inModel = gen.inModel(name, (Integer)key);
00317         }
00318         if(inModel && map2.containsKey(key)){
00319         T2 val1 = map2.get(key);
00320         T2 val2 = entry.getValue();
00321 
00322         if(val1!=null && val2 !=null &&
00323            !Match.compare(val1, val2)){
00324             match = false;
00325             if(!match){
00326             //if(val1 instanceof Double){
00327             //  val1 = (T2)JetWebConfig.getFormat().format(val1);
00328             //  val2 = (T2)JetWebConfig.getFormat().format(val2);
00329             //}
00330             System.out.println("Failed comparison on " + 
00331                        name + ": " + key + "." + 
00332                        val1 + "!=" + val2);
00333 
00334             notMatching.add
00335                 (new Match(name+":"+key.toString(), val1, val2));
00336             }
00337         }
00338         }
00339     }
00340 
00341     return match;
00342     }
00343 
00344 
00355     public boolean matches(RunSeries runSeries) throws JetWebException {
00356     return matches(runSeries,runSeries.getMCProcessType());
00357     }
00358 
00369     public boolean matches(Model model) throws JetWebException {
00370     return matches(model,null);
00371     }
00372 
00382     public boolean matches(ResultSearchPattern pattern) throws JetWebException {
00383     return matches(pattern,null);
00384     }
00385 
00397     private boolean matches(ResultSearchPattern pattern, MCProcessType proc) 
00398     throws JetWebException {
00399 
00400     boolean match = true;
00401 
00402     String tmp;
00403 
00404     // clear down the record of comparison of this pattern with any other.
00405     notMatching.clear();
00406 
00407     match = match && compare(Match.GEN,getGenerator().getName(),
00408                  pattern.getGenerator().getName());
00409     match = match && compare(Match.GEN,getGenerator().getVersion(),
00410                  pattern.getGenerator().getVersion());
00411 
00412     if(!match){ 
00413         System.out.println("Failed compare on generator name and version" );
00414         return match;
00415     }
00416 
00417     // Got to do all the generator parameters too!!
00418     match = match && compare("Integer: ",getGenerator().getIntParameters(),
00419                  pattern.getGenerator().getIntParameters(), pattern.getGenerator());
00420 
00421     HashMap<String, Integer> intParams1 = new HashMap<String, Integer>();
00422     HashMap<String, Integer> intParams2 = new HashMap<String, Integer>();
00423     
00424     for(Map.Entry<String, Integer> entry: 
00425         getGenerator().getIntParameters().entrySet()){
00426         String key = entry.getKey();
00427 
00428         if(getGenerator().inModel(key) || 
00429            getGenerator().inProcessType(key)){
00430         intParams1.put(key, entry.getValue());
00431         }
00432 
00433     }
00434 
00435     for(Map.Entry<String, Integer> entry:
00436         pattern.getGenerator().getIntParameters().entrySet()){
00437         String key = entry.getKey();
00438         if(pattern.getGenerator().inModel(key) ||
00439            pattern.getGenerator().inProcessType(key)){
00440         intParams2.put(key, entry.getValue());
00441         }
00442     }
00443 
00444     match = match && compare("Integer: ",intParams1, intParams2,
00445                  pattern.getGenerator());
00446 
00447     if(!match) return match;
00448 
00449     HashMap<String, Double> realParams1 = new HashMap<String, Double>();
00450     HashMap<String, Double> realParams2 = new HashMap<String, Double>();
00451     for(Map.Entry entry: getGenerator().getRealParameters().entrySet()){
00452         String key = (String)entry.getKey();
00453         if(getGenerator().inModel(key) || 
00454            getGenerator().inProcessType(key)){
00455         realParams1.put(key, (Double)entry.getValue());
00456         }
00457     }
00458 
00459     for(Map.Entry entry: 
00460         pattern.getGenerator().getRealParameters().entrySet()){
00461         String key = (String)entry.getKey();
00462         if(pattern.getGenerator().inModel(key)||
00463            pattern.getGenerator().inProcessType(key)){
00464         realParams2.put(key, (Double)entry.getValue());
00465         }
00466     }
00467 
00468     match = match && 
00469         compare("Real   : ", realParams1, realParams2, 
00470             pattern.getGenerator());
00471 
00472     //  match = match && compare("Real   : ",getGenerator().getRealParameters(),
00473     //       pattern.getGenerator().getRealParameters());
00474 
00475     if(!match) return match;
00476 
00477     // Now the integer arrays.
00478     for (Map.Entry<String,HashMap<Integer,Integer>> entry : 
00479          getGenerator().getIntParameterArrays().entrySet()){
00480         
00481         String arrayName = entry.getKey(); 
00482         if(pattern.getGenerator().inModel(arrayName)){
00483         HashMap<Integer,Integer> map2 = 
00484             pattern.getGenerator().getIntParameterArrays().
00485             get(arrayName);
00486         match = match && 
00487             compare(arrayName,entry.getValue(),map2, 
00488                 pattern.getGenerator());
00489         }
00490     }   
00491 
00492     if(!match) return match;
00493 
00494     // Now the real arrays.
00495     for (Map.Entry<String,HashMap<Integer,Double>> entry : 
00496          getGenerator().getRealParameterArrays().entrySet()){
00497         
00498             String arrayName = entry.getKey(); 
00499         if(pattern.getGenerator().inModel(arrayName)){
00500         HashMap<Integer,Double> map2 = 
00501             pattern.getGenerator().getRealParameterArrays().
00502             get(arrayName);
00503         match = match && 
00504             compare(arrayName,entry.getValue(),map2, 
00505                 pattern.getGenerator());
00506         }
00507     }
00508     /*
00509     if(photonpdfList != null &&
00510        pattern.getPhotonPDFList()!=null){
00511         match = match && compare(Match.PHPDF, 
00512                      PDF2Object(photonpdfList),
00513                      PDF2Object(pattern.getPhotonPDFList()));
00514     }
00515     */
00516 
00517     if(getPhotonPDF() !=null &&
00518        pattern.getPhotonPDF() !=null){
00519         match = match && getPhotonPDF().equals(pattern.getPhotonPDF());
00520     }
00521 
00522     if(!match) return match;
00523     /*
00524     if(protonpdfList != null &&
00525        pattern.getProtonPDFList()!=null){
00526         match = match && compare(Match.PRPDF,
00527                      PDF2Object(protonpdfList),
00528                      PDF2Object(pattern.getProtonPDFList()));
00529     }
00530     */
00531     if(getProtonPDF() != null &&
00532        pattern.getProtonPDF() != null){
00533         match = match && getProtonPDF().equals(pattern.getProtonPDF());
00534     }
00535 
00536     return match;   
00537     }
00538 
00539     private Vector<Object> PDF2Object(Vector<PDF> pdfVec){
00540     Vector<Object> objVec = new Vector<Object>(pdfVec.size());
00541     for(PDF pdf: pdfVec){
00542         objVec.add(pdf);
00543     }
00544     return objVec;
00545     }
00546 
00550     public void dumpParms(){
00551 
00552     System.out.println(getParms()); 
00553     
00554     }
00555 
00561     public String getParms(){
00562     
00563     StringBuffer b = new StringBuffer("----------------------------");  
00564 
00565     b.append("\nGenerator "+this.getGeneratorList());   
00566     b.append("\nPhoton "+this.getPhotonPDFList());  
00567     b.append("\nProton "+this.getProtonPDFList());  
00568     return b.toString();
00569 
00570     }
00571 
00572 
00576     public void setSoftDefaults() throws JetWebException {
00577 
00578     if (getProtonPDFList().size()==0) 
00579         getProtonPDFList().add(JetWebConfig.defaultProtonPDF);
00580     if (getPhotonPDFList().size()==0) 
00581         getPhotonPDFList().add(JetWebConfig.defaultPhotonPDF);
00582 
00583     // Default generator
00584     if (getGeneratorList().size()==0) {
00585         // add a default generator
00586         Generator gen = Generator.Maker();
00587         gen.setDefaults();
00588         getGeneratorList().add(gen);
00589     } else {
00590         for (Generator gen : generatorList)
00591         gen.setSoftDefaults();
00592     }
00593     }
00597     public void setDefaults() throws JetWebException {
00598 
00599     getProtonPDFList().clear(); 
00600     getProtonPDFList().add(JetWebConfig.defaultProtonPDF);
00601 
00602     getPhotonPDFList().clear(); 
00603         getPhotonPDFList().add(JetWebConfig.defaultPhotonPDF);
00604 
00605     // Default generator
00606     getGeneratorList().clear();
00607         Generator gen = Generator.Maker();
00608     gen.setDefaults();
00609     generatorList.add(gen);
00610 
00611     }
00612 
00616     public Vector<Model> getConsistentModels() throws JetWebException {
00617     return DBManager.getConsistentModels(this);
00618     }
00619 
00620 }
00621 
00622 
00623 
00624 

Generated Wed Jan 17 09:14:27 GMT 2007