[HepData-svn] r1550 - trunk/hepdata-webapp/src/main/java/cedar/hepdata/formats

blackhole at projects.hepforge.org blackhole at projects.hepforge.org
Tue Jun 26 13:47:42 BST 2012


Author: whalley
Date: Tue Jun 26 13:47:42 2012
New Revision: 1550

Log:
adding formatter for new input code

Added:
   trunk/hepdata-webapp/src/main/java/cedar/hepdata/formats/InputFormatter.java

Added: trunk/hepdata-webapp/src/main/java/cedar/hepdata/formats/InputFormatter.java
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ trunk/hepdata-webapp/src/main/java/cedar/hepdata/formats/InputFormatter.java	Tue Jun 26 13:47:42 2012	(r1550)
@@ -0,0 +1,292 @@
+package cedar.hepdata.formats;
+
+import cedar.hepdata.model.*;
+import cedar.hepdata.util.*;
+import cedar.hepdata.xml.*;
+import cedar.hepdata.db.*;
+
+import cedar.hepdata.webapp.components.*;
+
+import java.util.*;
+
+import org.antlr.stringtemplate.*;
+
+
+public class InputFormatter {
+
+    public static String format(Paper p) {
+        if(p == null) return null;
+	    StringBuffer s = new StringBuffer();
+	    s.append("*author: " + p.getAuthors().first() + "\n");
+
+        for (Reference ref : p.getReferences()){
+            s.append("*reference: "+ref.getDescription()+":"+ref.getDate()+"\n");
+        }
+
+        for (Modification mod : p.getModifications()){
+            s.append("*status: Encoded "+mod.getComment()+" by "+mod.getModifier()+"\n");
+        }
+
+        for (Experiment exp : p.getExperiments()){
+            s.append("*experiment: "+exp.getName()+"\n");
+            s.append("*detector: "+exp.getInformalName()+"\n");
+        }
+        if(p.getSpiresId()!=null) s.append("*spiresId: " + p.getSpiresId() + "\n");
+        if(p.getInspireId()!=null) s.append("*inspireId: " + p.getInspireId() + "\n");
+        if(p.getRedId()!=null) s.append("*durhamId: " + p.getRedId() + "\n");
+
+        for (String comment : p.getComments()){
+            s.append("*comment: "+comment+"\n");
+        }
+
+
+
+        for (Dataset ds : p.getDatasets()){
+        
+            s.append("\n*dataset:"+"\n");
+            if(ds.getTfpString() != null){ 
+                s.append(ds.getTfpString().replace("TFP = L","*l").replace(";","") + "\n"); 
+            }
+            for (String comment : ds.getComments()){
+                if(!comment.startsWith("Location")){
+                    s.append("*comment: "+comment+"\n");
+                }    
+            }
+            for (String re : ds.getDsReactions()){
+                s.append("*reackey: "+re+"\n");
+            }
+            for (String obs : ds.getDsObservables()){
+                s.append("*obskey: "+obs+"\n");
+            }
+            for (String plab : ds.getDsPlabs()){
+                s.append("*obskey: "+plab+"\n");
+            }
+        
+            for (DatasetError de : ds.getErrors()){
+                s.append("*Error: " + de.getPlus());
+                if(de.getNormType().toSymbol().equals("%")){s.append(" PCT");}
+                s.append(";\n");
+                s.append("ErrorComment: " + de.getComment() + ";\n");
+            }
+       
+
+            int numpoints = ds.getNumPoints(); 
+            int idmax = ds.getMaxPointId();             
+            int nyax = ds.getYAxes().size();
+            int nxax = ds.getXAxes().size();
+            
+         //   s.append("\nThis dataset has "+nxax+" x axes and "+nyax+" y axes with "+numpoints+" points\n");
+            if(nyax > 0 && nxax > 0) {
+                int nycomm = ds.getYAxis(1).getComments().size();
+                int nyprop = ds.getYAxis(1).getProperties().size();;
+
+                for ( Property prop : ds.getYAxis(1).getProperties()){
+                    if(prop.getName().startsWith("sqrts")){ nyprop--; }
+                }
+                
+               
+                for (Property prop : ds.getProperties()){
+                    s.append("*dsqual: "+prop.getName()+" :");
+                    if(prop.getLowValue().equals(prop.getHighValue())){
+                        s.append(" "+prop.getLowValue());  
+                    } else{
+                        s.append(" "+prop.getLowValue()+" - "+prop.getHighValue());
+                    }
+                    if(!prop.getUnit().toString().equals("")){
+                        s.append(" : "+prop.getUnit().toString());
+                    } 
+                    s.append("\n");
+                } 
+                
+                String[][] store = new String[nyax][nycomm+nyprop+1];
+                int nyhead=0;
+                for (int n=0; n<nyax; n++){
+                    List <String> storelist = new ArrayList();
+                    
+                    YAxis yax = ds.getYAxis(n+1);
+                    String yheader = "";
+                    if(yax.getHeader() != null){ 
+                        yheader = yax.getHeader(); 
+                        storelist.add("*yheader: " + yheader);
+                        nyhead=1;
+                    }
+                
+                   for (String comm : yax.getComments()){
+                       if(comm.contains(":")){
+                     	StringBuffer tempbuff = new StringBuffer();
+                        String left = comm.split("\\s*:\\s*")[0];
+                        String right = comm.split("\\s*:\\s*")[1];
+                        tempbuff.append("*qual: " + left + " : " + right);
+                        storelist.add(tempbuff.toString());
+                       } else{
+                           nycomm =- 1;
+                       }
+                    }
+                    for (Property prop : yax.getProperties()){
+                        if(!prop.getName().startsWith("sqrts")){
+                     	    StringBuffer tempbuff = new StringBuffer();
+                            tempbuff.append("*qual: " + prop.getName().trim());
+                            if(!prop.getUnit().toString().equals("num")) { tempbuff.append(" IN " + prop.getUnit().toString().toUpperCase()); } 
+                            if(prop.getLowValue().equals(prop.getHighValue())){
+                                tempbuff.append(" : " + prop.getLowValue());
+                            } else{
+                                tempbuff.append(" : " + prop.getLowValue() + " TO " + prop.getHighValue());
+                            } 
+                            storelist.add(tempbuff.toString());
+                        }    
+                    }
+                    Collections.sort(storelist);
+                    for (int i=0; i<storelist.size(); i++){
+                        store[n][i]=storelist.get(i);
+                    }
+                }
+                 // now check each property etc. for repetition across yaxes (very simple case first)
+                for (int i=0; i<nyprop+nycomm+nyhead; i++){
+                    Boolean same = true;
+                    if(nyax > 1){
+                        for (int ny=0; ny<nyax-1; ny++){
+                           if(!store[ny][i].equals(store[ny+1][i])) { same = false; }
+                        }
+                    }
+                    if(same) { 
+                        s.append(store[0][i]+"\n"); 
+                    }
+                    else{
+                        s.append(store[0][i]);
+                        for(int ny=1; ny<nyax; ny++){
+                           String right = store[ny][i].split("\\s\\:\\s")[1];
+                            s.append(" : "+right);
+                        }
+                        s.append("\n");
+                    }
+                }
+                
+                             
+                s.append("*xheader");
+                for(XAxis xax : ds.getXAxes()){
+                    s.append(": " + xax.getHeader() + " ");
+                }
+                s.append("\n");
+                s.append("*data");
+                for(XAxis xax : ds.getXAxes()){
+                s.append(": x ");
+                }
+                for(YAxis yx : ds.getYAxes()){
+                     s.append(": y ");
+                }     
+                s.append("\n");
+                
+                for (int i=0; i<numpoints; i++){
+///////////////////////// x axes/////////////////////////////////
+                    for( int nx=0;  nx<ds.getXAxes().size(); nx++){
+                        try{
+                            if(ds.getXAxis(nx+1).getBin(i+1).getRelation() != Relation.EQUALS){ 
+                                s.append(" " + ds.getXAxis(nx+1).getBin(i+1).getRelation().toString()); 
+                            }
+                            if(ds.getXAxis(nx+1).getBin(i+1).getFocus() != null){
+                                s.append(" " + ds.getXAxis(nx+1).getBin(i+1).getFocus());
+                                if(ds.getXAxis(nx+1).getBin(i+1).getLowValue() != null 
+                                   && ds.getXAxis(nx+1).getBin(i+1).getHighValue() != null 
+                                   && !ds.getXAxis(nx+1).getBin(i+1).getLowValue().equals(ds.getXAxis(nx+1).getBin(i+1).getHighValue())){
+                                       s.append(" (BIN=" + ds.getXAxis(nx+1).getBin(i+1).getLowValue() + " TO " + ds.getXAxis(nx+1).getBin(i+1).getHighValue() + ")");                   
+                               }
+                            } else {
+                                if(ds.getXAxis(nx+1).getBin(i+1).getLowValue() != null && ds.getXAxis(nx+1).getBin(i+1).getHighValue()==null){
+                                    s.append(" " + ds.getXAxis(nx+1).getBin(i+1).getLowValue());                   
+                                }
+                                else if(ds.getXAxis(nx+1).getBin(i+1).getLowValue() == null && ds.getXAxis(nx+1).getBin(i+1).getHighValue() != null){
+                                    s.append(" " + ds.getXAxis(nx+1).getBin(i+1).getHighValue());                   
+                                }
+                                else{
+                                    s.append(" " + ds.getXAxis(nx+1).getBin(i+1).getDescription());
+                                }
+                           }
+                        } catch(Exception e){
+                           s.append(" " + ds.getXAxis(nx+1).getBin(i+1).getDescription());
+                        }
+                        s.append(";");                   
+                    }
+////////////////////////////y-axes///////////////////////////////////////////
+                    for (int n=0; n<nyax; n++){
+                        YAxis yax = ds.getYAxis(n+1);
+                        try{
+                             if(yax.getPoint(i+1).getRelation() != Relation.EQUALS){ 
+                                s.append(" " + yax.getPoint(i+1).getRelation().toString()); 
+                             }
+                           String f_val = yax.getPoint(i+1).getValue().toString();
+                           if(yax.getPoint(i+1).getValueLength() != null) {
+                                int lsd = yax.getPoint(i+1).getValueLength();
+                                int msd = 0;
+                                 if(lsd > 0){
+                                    msd = f_val.indexOf(".");
+                                }  else{
+                                    msd = f_val.length() - 2;
+                                }
+                                f_val = Formats.forms(yax.getPoint(i+1).getValue(),msd,lsd);
+                            }
+                            if(f_val.contains("E-")){
+                                int lsd = Integer.parseInt(f_val.substring(f_val.indexOf("E")+2));
+                                int ldiff = f_val.length() + lsd;
+                                f_val = Formats.forms(yax.getPoint(i+1).getValue(),1,ldiff-5);
+                            }
+                            s.append(" " + f_val);
+//                            s.append(" " + yax.getPoint(i+1).getValue());
+                            int nerr=0;
+                            for(Uncertainty err : yax.getPoint(i+1).getErrors()){
+                                nerr++;
+                                String norm = "";
+                                if(err.getNormType() == ErrorNorm.PCT){  norm = " PCT"; } 
+                                String f_plus = err.getPlus().toString();
+                                if(f_plus.contains("E-")){
+                                    int lsd = Integer.parseInt(f_plus.substring(f_plus.indexOf("E")+2));
+                                    int ldiff = f_plus.length() + lsd;
+                                    f_plus = Formats.forms(err.getPlus(),1,ldiff-5);
+                                 }
+                                String f_minus = err.getMinus().toString();
+                                if(f_minus.contains("E-")){
+                                    int lsd = Integer.parseInt(f_minus.substring(f_minus.indexOf("E")+2));
+                                    int ldiff = f_minus.length() + lsd;
+                                    f_minus = Formats.forms(err.getMinus(),1,ldiff-5);
+                                 }
+                                
+                                if(nerr == 1) {
+                                    if(err.isSymmetric()){ 
+//                                        s.append(" +- " + err.getPlus() + norm); 
+                                        s.append(" +- " + f_plus + norm); 
+                                    }
+                                    else{
+//                                        s.append(" +" + err.getPlus() + norm + ",-" + err.getMinus() + norm);
+                                        s.append(" +" + f_plus + norm + ",-" + f_minus + norm);
+                                    }
+                                }
+                                else if(nerr == 2) { 
+                                    s.append(" (DSYS=");
+//                                    if(err.isSymmetric()) {s.append(err.getPlus() + norm);}
+//                                    else{s.append("+" + err.getPlus() + norm + ",-" + err.getMinus() + norm);}
+                                    if(err.isSymmetric()) {s.append(f_plus + norm);}
+                                    else{s.append("+" + f_plus + norm + ",-" + f_minus + norm);}
+                                } 
+                                else { 
+                                    s.append(",DSYS=");
+//                                    if(err.isSymmetric()) {s.append(err.getPlus() + norm);}
+//                                    else{s.append("+" + err.getPlus() + norm + ",-" + err.getMinus() + norm);}
+                                    if(err.isSymmetric()) {s.append(f_plus + norm);}
+                                    else{s.append("+" + f_plus + norm + ",-" + f_minus + norm);}
+                                } 
+                            }
+                            if(nerr > 1) { s.append(")");}
+                        }
+                        catch(Exception e){ s.append(" - "); }
+                        s.append(";");
+                    }
+                    s.append("\n");
+                }
+                s.append("*dataend:\n");
+            }
+    	} 
+
+        s.append("*E\n");
+        return s.toString();
+    }
+
+}


More information about the HepData-svn mailing list