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

Model.java

Go to the documentation of this file.
00001 package cedar.jetweb.model;
00002 
00003 import java.util.Vector;
00004 import java.util.HashMap;
00005 import java.util.Map;
00006 import java.util.Enumeration;
00007 import java.util.Date;
00008 import java.util.List;
00009 import java.util.ArrayList;
00010 import java.util.Comparator;
00011 import java.util.Collections;
00012 
00013 
00014 import cedar.jetweb.Storeable;
00015 import cedar.jetweb.model.fit.Fit;
00016 import cedar.jetweb.model.plots.DataPlot;
00017 
00018 import cedar.jetweb.job.LogFile;
00019 import cedar.jetweb.db.DBManager;
00020 import cedar.jetweb.JetWebException;
00021 import cedar.jetweb.generator.Generator;
00022 import cedar.jetweb.job.JobUtils;
00023 import cedar.jetweb.html.HTMLUtils;
00024 
00025 
00033 public class Model extends ResultSearchPattern implements Storeable {
00034 
00035     private Integer modelId=-1;
00036     private int runSeriesCollectionId;
00037     private RunSeriesCollection runSeriesCollection;
00038 
00042     public Model() throws JetWebException {
00043 
00044     // Make a Model.
00045     super();
00046 
00047     }
00048 
00057     public Model(ResultSearchPattern pattern) throws JetWebException {
00058 
00059     // Make a Model.
00060     super();
00061 
00062     // Reset any of the parameters from the pattern which were set 
00063     // (nulls & blanks are ignored).
00064 
00065     setFrom(pattern);
00066 
00067     // Set the remaining unset parameters to default values.
00068     setSoftDefaults();
00069 
00070     Vector<Model> existingModels = pattern.getConsistentModels();
00071 
00072     HashMap<Integer, RunSeriesCollection> existingRSCs = 
00073         new HashMap<Integer, RunSeriesCollection>();
00074         
00075     for(Model model: existingModels){
00076 
00077         if(this.matches(model)){
00078 
00079         RunSeriesCollection rsc = model.getRunSeriesCollection();
00080 
00081         if(!existingRSCs.containsKey(rsc.getId())){
00082             existingRSCs.put(rsc.getId(), rsc);
00083         }
00084         }
00085     }
00086 
00087     Vector<RunSeriesCollection> compatibleRSCs = 
00088         new Vector<RunSeriesCollection>();
00089 
00090     for(Map.Entry<Integer, RunSeriesCollection> entry:
00091         existingRSCs.entrySet()){
00092         boolean compatible = true;
00093         for(RunSeries rs: entry.getValue().getRunSeriesList()){
00094         if(compatible){
00095             compatible = rs.matches(this);
00096             
00097         }
00098         }
00099 
00100         if(compatible){
00101         compatibleRSCs.add(entry.getValue());
00102         }
00103 
00104     }
00105 
00106     if(compatibleRSCs.size()==0){
00107 
00108         // Add a new runSeriesCollection for this model.
00109         runSeriesCollection = new RunSeriesCollection();
00110 
00111     }else if(compatibleRSCs.size()==1){
00112         runSeriesCollection = compatibleRSCs.get(0);
00113 
00114     }else{
00115         //shouldn't ever get here I think!
00116         runSeriesCollection = new RunSeriesCollection();
00117 
00118         for(RunSeriesCollection rsc: compatibleRSCs){
00119         for(RunSeries rs: rsc.getRunSeriesList()){
00120             runSeriesCollection.add(rs);
00121         }
00122         }
00123     }
00124 
00125     if(pattern instanceof RunSeries){
00126         runSeriesCollection.add((RunSeries)pattern);
00127     }
00128 
00129     }
00130 
00131 
00138     public Model(int newmodelId) throws JetWebException {
00139     super();
00140     System.out.println("Making new Model by id = "+newmodelId);
00141     modelId=newmodelId;
00142     retrieve();
00143 
00144     System.out.println("rsc Id = "+runSeriesCollection.getId());
00145 
00146     }
00147 
00148 
00159     public boolean store() throws JetWebException {
00160 
00161     // First the runSeriesCollection.
00162     System.out.println("Model: storing rsc for model "+getId());
00163 
00164     System.out.println("model.store(): rsc id = "+runSeriesCollection.getId());
00165 
00166     DBManager.selectId(runSeriesCollection);
00167     boolean added = DBManager.addToDB(this);
00168     runSeriesCollection.store();
00169     return added;
00170 
00171     }
00172 
00173 
00174     public boolean retrieve() throws JetWebException {
00175     DBManager.selectFromDB(this);
00176     return true;
00177     }
00178 
00184     public int getId(){ 
00185     
00186 
00187     if(modelId.equals(-1)){
00188         try{
00189         modelId = -2;
00190         modelId = DBManager.getId(this);
00191         }catch(JetWebException err){
00192         System.out.println(err.getMessage());
00193         }
00194     }
00195     if(modelId<0) modelId=-1;
00196     return(modelId);
00197     
00198     }
00199 
00200 
00206     public Date getDate(){
00207     return runSeriesCollection.getDate();
00208     }
00209 
00216     public Vector<RunSeries> getRunSeries(MCProcessType proc) 
00217     throws JetWebException {
00218     if(runSeriesCollection==null){
00219         return new Vector<RunSeries>();
00220     }
00221 
00222     return runSeriesCollection.getRunSeriesList(proc);
00223     }
00224 
00230     public RunSeriesCollection getRunSeriesCollection(){
00231     return runSeriesCollection;
00232     }
00233 
00237     public void setRunSeriesCollection(RunSeriesCollection rsc){
00238     runSeriesCollection = rsc;
00239     }
00240 
00241     public void setId(int id){
00242     modelId=id;
00243     }
00248     public boolean predicts(DataPlot plot) throws JetWebException {
00249     return runSeriesCollection.contains(plot);
00250     }
00251 
00256     public void zero(MCProcessType proc){
00257 
00258     Enumeration<RunSeries> rsList = runSeriesCollection.getRunSeriesList(proc).elements();
00259     while (rsList.hasMoreElements()){
00260         RunSeries rs = rsList.nextElement();
00261         Enumeration<LogFile> logs = rs.getLogFiles().elements();
00262         JobUtils.moveLogs(logs,"run/edit");
00263         DBManager.zero(rs);
00264     }
00265     }
00266 
00267 
00275     public void addAllRunSeries() throws JetWebException {
00276 
00277     // Loop over all valid processTypes (i.e. those which can be
00278     // generated solo, in a single run series.
00279     Vector<MCProcessType> procList = MCProcessType.getAll(true);
00280 
00281     for (MCProcessType proc : procList){
00282 
00283         Vector<RunSeries> rsList = DBManager.getConsistentRunSeries(proc,this);
00284 
00285         //System.out.println("Model: rsList = "  + rsList);
00286 
00287         System.out.println("model.addAllRunSeries(): rsc Id = "+
00288                    runSeriesCollection.getId());
00289 
00290         for (RunSeries runSeries : rsList) {
00291         System.out.println("Model.addAllRunSeries(): Adding runseries "
00292                    + runSeries.getId());
00293         runSeriesCollection.add(runSeries);
00294         
00295         System.out.println("model.addAllRunSeries(): now rsc Id = "+
00296                    runSeriesCollection.getId());
00297 
00298         }
00299     }
00300     }
00301 
00302 
00303 
00304     private void setFrom(ResultSearchPattern pattern) throws JetWebException {
00305 
00306     generatorList = new Vector<Generator>();
00307     protonpdfList = new Vector<PDF>();
00308     photonpdfList = new Vector<PDF>();
00309 
00310     Generator gen = pattern.getGenerator();
00311     PDF protPDF = pattern.getProtonPDF();
00312     PDF photPDF = pattern.getPhotonPDF();
00313 
00314     if(gen!=null)     generatorList.add(pattern.getGenerator());
00315     if(protPDF!=null) protonpdfList.add(pattern.getProtonPDF());
00316     if(photPDF!=null) photonpdfList.add(pattern.getPhotonPDF());
00317 
00318     description_ = pattern.getDescription();
00319 
00320     }
00321 
00322 
00332     public static List<Model> getModels( final ResultSearchPattern pattern, 
00333                 final String sort ) throws JetWebException {
00334 
00335     List<Model> list = new ArrayList<Model>();
00336 
00337     System.out.println("Model: Looking for models for "+pattern.getGenerator());
00338 
00339     // Build a list of Models which are consistent with this ResultSearchPattern
00340         list.addAll(pattern.getConsistentModels());
00341     
00342     //sort the fitList according to the "sort" input
00343     Comparator listSort = new Comparator() {
00344         public int compare(Object o1, Object o2) {
00345             Model m1 = (Model)o1; 
00346             Model m2 = (Model)o2; 
00347             int sortResult=0;
00348             try {
00349             if (sort.equals(HTMLUtils.PTHAT)){
00350                 sortResult = m2.getPthat().compareTo(m1.getPthat());
00351 
00352             } else if (sort.equals(HTMLUtils.DATE)){
00353                 sortResult = m1.getDate().compareTo(m2.getDate());
00354 
00355                 /*
00356 
00357             } else if (sort.equals(HTMLUtils.CHI2ALL)){
00358             CutBand band1 = (CutBand)j1.bands.get(CutBand.ALL);
00359             CutBand band2 = (CutBand)j2.bands.get(CutBand.ALL);
00360             sortResult = (new Double(band2.getChi2Total()/band2.getDOFTotal()))
00361                 .compareTo((new Double(band1.getChi2Total()/band1.getDOFTotal())));
00362             } else if (sort.equals(HTMLUtils.CHI2HIGH)){
00363             CutBand band1 = (CutBand)j1.bands.get(CutBand.HI);
00364             CutBand band2 = (CutBand)j2.bands.get(CutBand.HI);
00365             sortResult = (new Double(band2.getChi2Total()/band2.getDOFTotal()) )
00366                 .compareTo((new Double(band1.getChi2Total()/band1.getDOFTotal())));
00367             } else if (sort.equals(HTMLUtils.CHI2LOW)){
00368             CutBand band1 = (CutBand)j1.bands.get(CutBand.LO);
00369             CutBand band2 = (CutBand)j2.bands.get(CutBand.LO);
00370             sortResult = (new Double(band2.getChi2Total()/band2.getDOFTotal()) )
00371                 .compareTo((new Double(band1.getChi2Total()/band1.getDOFTotal())));
00372             } else if (sort.equals(HTMLUtils.CHI2SHAPE)){
00373             CutBand band1 = (CutBand)j1.bands.get(CutBand.SHAPE);
00374             CutBand band2 = (CutBand)j2.bands.get(CutBand.SHAPE);
00375             sortResult = (new Double(band2.getChi2Total()/band2.getDOFTotal()) )
00376                 .compareTo((new Double(band1.getChi2Total()/band1.getDOFTotal())));
00377             } else if (sort.equals(HTMLUtils.CHI2CHARM)){
00378             CutBand band1 = (CutBand)j1.bands.get(CutBand.CHARM);
00379             CutBand band2 = (CutBand)j2.bands.get(CutBand.CHARM);
00380             sortResult = (new Double(band2.getChi2Total()/band2.getDOFTotal()) )
00381                 .compareTo((new Double(band1.getChi2Total()/band1.getDOFTotal())));
00382 
00383             } else if (sort.equals(HTMLUtils.CHI2ALLEP)){
00384             CutBand band1 = (CutBand)j1.bands.get(CutBand.ALL);
00385             CutBand band2 = (CutBand)j2.bands.get(CutBand.ALL);
00386             sortResult = (new Double(band2.getChi2Ep()/band2.getDOFEp()))
00387                 .compareTo((new Double(band1.getChi2Ep()/band1.getDOFEp())));
00388             } else if (sort.equals(HTMLUtils.CHI2HIGHEP)){
00389             CutBand band1 = (CutBand)j1.bands.get(CutBand.HI);
00390             CutBand band2 = (CutBand)j2.bands.get(CutBand.HI);
00391             sortResult = (new Double(band2.getChi2Ep()/band2.getDOFEp()) )
00392                 .compareTo((new Double(band1.getChi2Ep()/band1.getDOFEp())));
00393             } else if (sort.equals(HTMLUtils.CHI2LOWEP)){
00394             CutBand band1 = (CutBand)j1.bands.get(CutBand.LO);
00395             CutBand band2 = (CutBand)j2.bands.get(CutBand.LO);
00396             sortResult = (new Double(band2.getChi2Ep()/band2.getDOFEp()) )
00397                 .compareTo((new Double(band1.getChi2Ep()/band1.getDOFEp())));
00398             } else if (sort.equals(HTMLUtils.CHI2SHAPEEP)){
00399             CutBand band1 = (CutBand)j1.bands.get(CutBand.SHAPE);
00400             CutBand band2 = (CutBand)j2.bands.get(CutBand.SHAPE);
00401             sortResult = (new Double(band2.getChi2Ep()/band2.getDOFEp()) )
00402                 .compareTo((new Double(band1.getChi2Ep()/band1.getDOFEp())));
00403             } else if (sort.equals(HTMLUtils.CHI2CHARMEP)){
00404             CutBand band1 = (CutBand)j1.bands.get(CutBand.CHARM);
00405             CutBand band2 = (CutBand)j2.bands.get(CutBand.CHARM);
00406             sortResult = (new Double(band2.getChi2Ep()/band2.getDOFEp()) )
00407                 .compareTo((new Double(band1.getChi2Ep()/band1.getDOFEp())));
00408 
00409             } else if (sort.equals(HTMLUtils.CHI2ALLEE)){
00410             CutBand band1 = (CutBand)j1.bands.get(CutBand.ALL);
00411             CutBand band2 = (CutBand)j2.bands.get(CutBand.ALL);
00412             sortResult = (new Double(band2.getChi2Ee()/band2.getDOFEe()))
00413                 .compareTo((new Double(band1.getChi2Ee()/band1.getDOFEe())));
00414             } else if (sort.equals(HTMLUtils.CHI2HIGHEE)){
00415             CutBand band1 = (CutBand)j1.bands.get(CutBand.HI);
00416             CutBand band2 = (CutBand)j2.bands.get(CutBand.HI);
00417             sortResult = (new Double(band2.getChi2Ee()/band2.getDOFEe()) )
00418                 .compareTo((new Double(band1.getChi2Ee()/band1.getDOFEe())));
00419             } else if (sort.equals(HTMLUtils.CHI2LOWEE)){
00420             CutBand band1 = (CutBand)j1.bands.get(CutBand.LO);
00421             CutBand band2 = (CutBand)j2.bands.get(CutBand.LO);
00422             sortResult = (new Double(band2.getChi2Ee()/band2.getDOFEe()) )
00423                 .compareTo((new Double(band1.getChi2Ee()/band1.getDOFEe())));
00424             } else if (sort.equals(HTMLUtils.CHI2SHAPEEE)){
00425             CutBand band1 = (CutBand)j1.bands.get(CutBand.SHAPE);
00426             CutBand band2 = (CutBand)j2.bands.get(CutBand.SHAPE);
00427             sortResult = (new Double(band2.getChi2Ee()/band2.getDOFEe()) )
00428                 .compareTo((new Double(band1.getChi2Ee()/band1.getDOFEe())));
00429             } else if (sort.equals(HTMLUtils.CHI2CHARMEE)){
00430             CutBand band1 = (CutBand)j1.bands.get(CutBand.CHARM);
00431             CutBand band2 = (CutBand)j2.bands.get(CutBand.CHARM);
00432             sortResult = (new Double(band2.getChi2Ee()/band2.getDOFEe()) )
00433                 .compareTo((new Double(band1.getChi2Ee()/band1.getDOFEe())));
00434 
00435             } else if (sort.equals(HTMLUtils.CHI2ALLPP)){
00436             CutBand band1 = (CutBand)j1.bands.get(CutBand.ALL);
00437             CutBand band2 = (CutBand)j2.bands.get(CutBand.ALL);
00438             sortResult = (new Double(band2.getChi2Pp()/band2.getDOFPp()))
00439                 .compareTo((new Double(band1.getChi2Pp()/band1.getDOFPp())));
00440             } else if (sort.equals(HTMLUtils.CHI2HIGHPP)){
00441             CutBand band1 = (CutBand)j1.bands.get(CutBand.HI);
00442             CutBand band2 = (CutBand)j2.bands.get(CutBand.HI);
00443             sortResult = (new Double(band2.getChi2Pp()/band2.getDOFPp()) )
00444                 .compareTo((new Double(band1.getChi2Pp()/band1.getDOFPp())));
00445             } else if (sort.equals(HTMLUtils.CHI2LOWPP)){
00446             CutBand band1 = (CutBand)j1.bands.get(CutBand.LO);
00447             CutBand band2 = (CutBand)j2.bands.get(CutBand.LO);
00448             sortResult = (new Double(band2.getChi2Pp()/band2.getDOFPp()) )
00449                 .compareTo((new Double(band1.getChi2Pp()/band1.getDOFPp())));
00450             } else if (sort.equals(HTMLUtils.CHI2SHAPEPP)){
00451             CutBand band1 = (CutBand)j1.bands.get(CutBand.SHAPE);
00452             CutBand band2 = (CutBand)j2.bands.get(CutBand.SHAPE);
00453             sortResult = (new Double(band2.getChi2Pp()/band2.getDOFPp()) )
00454                 .compareTo((new Double(band1.getChi2Pp()/band1.getDOFPp())));
00455             } else if (sort.equals(HTMLUtils.CHI2CHARMPP)){
00456             CutBand band1 = (CutBand)j1.bands.get(CutBand.CHARM);
00457             CutBand band2 = (CutBand)j2.bands.get(CutBand.CHARM);
00458             sortResult = (new Double(band2.getChi2Pp()/band2.getDOFPp()) )
00459                 .compareTo((new Double(band1.getChi2Pp()/band1.getDOFPp())));
00460             */
00461 
00462             }
00463             } catch (NumberFormatException e){
00464             System.out.println("Model: number format exception in sorting of results by " 
00465                        + sort);
00466             } catch (JetWebException e1){
00467             System.out.println("Model: JetWebException thrown " 
00468                        + sort);
00469             e1.printStackTrace(System.out);
00470             } catch (NullPointerException e2){
00471             System.out.println("Model:NullPointerException in sorting of results by " 
00472                        + sort);
00473             e2.printStackTrace(System.out);
00474             }
00475             return -1*sortResult;
00476         }
00477     };
00478     
00479     Collections.sort(list,listSort);
00480     return list;
00481 
00482     }
00483 
00484 
00485 
00486 }
00487 
00488 
00489 
00490 
00491 
00492 
00493 
00494 
00495 
00496 
00497 
00498 
00499 
00500 
00501 
00502 
00503 

Generated Wed Jan 17 09:14:27 GMT 2007