00001 package DisplayGUI;
00002 import java.io.*;
00003 import guiUtilities.DaveUtils;
00004
00005 public class RxThresholdData {
00006 private static RxThresholdData instance = new RxThresholdData();
00007 ConfigurationInterface config=null;
00008
00009 java.util.regex.Pattern modulePattern = java.util.regex.Pattern.compile(".*<module\\s+id=\"(\\d+)\"\\s+group=\"\\d+\">(\\d{14})</module>.*");
00010 java.util.regex.Pattern murPattern = java.util.regex.Pattern.compile(".*<MUR.*id=\"(\\d+)\".*>.*");
00011 java.util.regex.Pattern channelIDPattern = java.util.regex.Pattern.compile(".*<channel\\s+id=\"(\\d+)\">.*");
00012 java.util.regex.Pattern streamDataPattern = java.util.regex.Pattern.compile("(.*<stream)(0|1)(\\s+threshold=\")([0-9a-fA-FxX]+)(\".*)");
00013 java.util.regex.Pattern hrefPattern = java.util.regex.Pattern.compile(".*<xi:include\\s+href=\"(.*\\.xml)\"/>.*");
00014 java.util.regex.Pattern commentPattern = java.util.regex.Pattern.compile("(.*)<!--(.*)");
00015 java.util.regex.Pattern endOfCommentPattern = java.util.regex.Pattern.compile("(.*)-->(.*)");
00016 java.util.regex.Pattern filePattern = java.util.regex.Pattern.compile("(.*)\\.xml");
00017
00018 java.util.regex.Matcher matcher;
00019 java.util.Vector xmlfileList;
00020 java.util.Map murMap;
00021 java.util.Map snMap;
00022 File xmlfile;
00023 String errorString=null;
00024 String thisID=null;
00025 String thisMUR=null;
00026
00027 boolean DEBUG=false;
00028
00029 public static RxThresholdData getInstance() {
00030 return instance;
00031 }
00032
00033 private RxThresholdData() {
00034 clearParams();
00035 }
00036 private void clearParams() {
00037 xmlfileList = new java.util.Vector();
00038 murMap = new java.util.HashMap();
00039 snMap = new java.util.HashMap();
00040 xmlfile=null;
00041 }
00042 public void refresh() {
00043 clearParams();
00044
00045
00046 String dataFile = System.getProperty("SCTDAQ_ROD_CONFIGURATION_PATH", null);
00047 if(dataFile==null) {
00048 errorString = new String("Env variable SCTDAQ_ROD_CONFIGURATION_PATH not passed to sctGUI. Check rungui script?");
00049 xmlfile=null;
00050 return;
00051 }
00052 xmlfile = new File(dataFile);
00053 if(!xmlfile.exists()) {
00054 errorString = new String("File "+xmlfile.getAbsolutePath()+" does not exist.");
00055 return;
00056 }
00057 getlmtFileList();
00058
00059 }
00060
00061 public String getStreamFileData(String sn,int stream) {
00062 if(!snMap.containsKey(sn)) {
00063 System.err.println("XML file does not seem to contain "+sn+", but its in the current configuration!");
00064 return null;
00065 }
00066 String murID = (String)snMap.get(sn);
00067 if(!murMap.containsKey(murID)) {
00068 System.err.println("murMap does not contain mur "+murID);
00069 return null;
00070 }
00071 MURInfo murInfo = (MURInfo)murMap.get(murID);
00072 return murInfo.getRxThrData(sn,stream,false);
00073 }
00074
00075
00076 public String getErrorString() {
00077 return errorString;
00078 }
00079
00080 private void getlmtFileList() {
00081 boolean comment=false;
00082 boolean readLines=false;
00083 File pathname = xmlfile.getParentFile();
00084 if(DEBUG) System.out.println("Opening "+xmlfile.getAbsolutePath());
00085 String line;
00086 try {
00087 BufferedReader in = new BufferedReader(new FileReader(xmlfile));
00088 while((line=in.readLine())!=null) {
00089
00090 matcher=commentPattern.matcher(line);
00091 if(matcher.matches()) {
00092 String preComment = line.substring(matcher.start(1),matcher.end(1));
00093 String postComment = line.substring(matcher.start(2),matcher.end(2));
00094 line=preComment;
00095 comment=true;
00096 java.util.regex.Matcher matcher2 = endOfCommentPattern.matcher(postComment);
00097 if(matcher2.matches()) {
00098 comment=false;
00099 line += postComment.substring(matcher2.start(2),matcher2.end(2));
00100 }
00101 }
00102 matcher=endOfCommentPattern.matcher(line);
00103 if(matcher.matches()) {
00104 comment=false;
00105 line=line.substring(matcher.start(2),matcher.end(2));
00106 }
00107
00108 if(comment) {
00109 if(DEBUG) System.out.println("Comment: "+line);
00110 continue;
00111 }
00112 if(line.matches(".*<partition.*")) readLines=true;
00113
00114 if(!readLines) continue;
00115
00116 if(DEBUG) System.out.println("line>"+line);
00117
00118 if(line.matches(".*</partition.*")) break;
00119
00120 checkLine(line,xmlfile);
00121
00122 matcher = hrefPattern.matcher(line);
00123 if(matcher.matches()) {
00124 if(DEBUG) System.out.println("File references "+line.substring(matcher.start(1),matcher.end(1)));
00125 xmlfileList.addElement(line.substring(matcher.start(1),matcher.end(1)));
00126 }
00127 }
00128 if(DEBUG) System.out.println("out of loop");
00129 in.close();
00130 for(int i=0;i<xmlfileList.size();i++) {
00131 String href =(String)xmlfileList.elementAt(i);
00132 File file = new File(href);
00133 if(!file.exists()) file = new File(pathname,href);
00134 if(!file.exists()) {
00135 System.err.println("Referenced file "+file.getAbsolutePath()+" does not exist.");
00136 errorString = new String(xmlfile.getName()+" references file "+file.getAbsolutePath()+" which does not exist.");
00137 continue;
00138 }
00139 if(DEBUG) System.out.println("Opening "+file.getAbsolutePath());
00140 in = new BufferedReader(new FileReader(file));
00141 while((line=in.readLine())!=null) {
00142 matcher=commentPattern.matcher(line);
00143 if(matcher.matches()) {
00144 String preComment = line.substring(matcher.start(1),matcher.end(1));
00145 String postComment = line.substring(matcher.start(2),matcher.end(2));
00146 line=preComment;
00147 comment=true;
00148 java.util.regex.Matcher matcher2 = endOfCommentPattern.matcher(postComment);
00149 if(matcher2.matches()) {
00150 comment=false;
00151 line += postComment.substring(matcher2.start(2),matcher2.end(2));
00152 }
00153 }
00154 matcher=endOfCommentPattern.matcher(line);
00155 if(matcher.matches()) {
00156 comment=false;
00157 line=line.substring(matcher.start(2),matcher.end(2));
00158 }
00159
00160 if(comment) {
00161 if(DEBUG) System.out.println("Comment: "+line);
00162 continue;
00163 }
00164
00165 if(DEBUG) System.out.println("line>"+line);
00166
00167 checkLine(line,file);
00168 }
00169 }
00170 in.close();
00171 }catch(Exception e) {javax.swing.JOptionPane.showMessageDialog(null,"Exception parsing config file(s): "+e.toString());}
00172 }
00173
00174 public String getMurID(String sn) {
00175 return (String)snMap.get(sn);
00176 }
00177 public String getModuleID(String sn) {
00178 if(!snMap.containsKey(sn)) return null;
00179 String murID = (String)snMap.get(sn);
00180 if(!murMap.containsKey(murID)) return null;
00181 MURInfo murInfo = (MURInfo)murMap.get(murID);
00182 return murInfo.getModuleID(sn);
00183 }
00184
00185
00186 private void checkLine(String line, File file) {
00187
00188 if(DEBUG) System.out.println("checking line "+line);
00189
00190 if(line.matches(".*</channel>.*")) {
00191 thisID=null;
00192 return;
00193 }
00194 if(line.matches(".*</MUR>.*")) {
00195 thisMUR=null;
00196 return;
00197 }
00198 matcher = murPattern.matcher(line);
00199 if(matcher.matches()) {
00200 thisMUR=line.substring(matcher.start(1),matcher.end(1));
00201 if(DEBUG) System.out.println("MUR "+thisMUR);
00202 return;
00203 }
00204
00205 if(thisMUR==null) return;
00206
00207 MURInfo murInfo = (murMap.containsKey(thisMUR)) ? (MURInfo)murMap.get(thisMUR) : new MURInfo(thisMUR,file);
00208
00209 matcher = modulePattern.matcher(line);
00210 if(matcher.matches()) {
00211
00212 String id = line.substring(matcher.start(1),matcher.end(1));
00213 String sn = line.substring(matcher.start(2),matcher.end(2));
00214 if(DEBUG) System.out.println("mapped id "+id+" to serial no "+sn);
00215 murInfo.addModule(id,sn);
00216 snMap.put(sn,thisMUR);
00217 }
00218 matcher = channelIDPattern.matcher(line);
00219 if(matcher.matches()) thisID = line.substring(matcher.start(1),matcher.end(1));
00220
00221 matcher = streamDataPattern.matcher(line);
00222 if(matcher.matches() && thisID!=null && murInfo.containsModule(thisID)) {
00223 int stream = Integer.valueOf(line.substring(matcher.start(2),matcher.end(2))).intValue();
00224 murInfo.addThr(thisID,stream,line.substring(matcher.start(4),matcher.end(4)),false);
00225 }
00226 murMap.put(thisMUR,murInfo);
00227 }
00228
00229 public void updateXMLfiles(java.util.Vector tableList, int threshold) {
00230 int murCount=0;
00231 int fileCount=0;
00232 int lineCount=0;
00233 java.util.Set fileSet = new java.util.HashSet();
00234
00235 for(int i=1;i<tableList.size();i++) {
00236 java.util.Vector theLine = (java.util.Vector)tableList.elementAt(i);
00237
00238
00239 String sn = (String)theLine.elementAt(0);
00240 Integer stream = (Integer)theLine.elementAt(1);
00241 String thisMUR= (String)theLine.elementAt(2);
00242 String thisID = (String)theLine.elementAt(3);
00243 String newThr = (String)theLine.elementAt(4);
00244 Integer diff = (Integer)theLine.elementAt(6);
00245 if(DEBUG) System.out.println(sn+"-"+stream.toString()+"-"+thisMUR+"-"+thisID+"-"+newThr+"-"+diff);
00246 if(!murMap.containsKey(thisMUR)) {
00247 System.err.println("No key for MUR "+thisMUR);
00248 continue;
00249 }
00250 if(Math.abs(diff.intValue())>threshold) {
00251 MURInfo murInfo = (MURInfo) murMap.get(thisMUR);
00252 murInfo.addThr(thisID,stream.intValue(),newThr,true);
00253 fileSet.add(murInfo.getXMLFile());
00254 }
00255 }
00256
00257
00258 boolean fileError=false;
00259 for (java.util.Iterator i=fileSet.iterator(); i.hasNext(); ) {
00260 File murFile = (File)i.next();
00261
00262 fileCount++;
00263
00264
00265 try {
00266 String original_file_name = murFile.getAbsolutePath();
00267 File original_file_backup=null;
00268 matcher = filePattern.matcher(original_file_name);
00269 if(matcher.matches()) {
00270 String fname = original_file_name.substring(matcher.start(1),matcher.end(1));
00271 fname += "_original.xml";
00272 original_file_backup = new File(fname);
00273 if(!murFile.renameTo(original_file_backup)) {
00274 errorString = "FAILED to rename mur file from "+original_file_name+" to "+fname;
00275 }
00276 else System.out.println("Copied "+original_file_name+" to "+fname);
00277 }
00278 else errorString = "file not recognised: "+original_file_name;
00279 if(errorString!=null) break;
00280 if(original_file_backup==null) continue;
00281
00282
00283 BufferedReader in = new BufferedReader(new FileReader(original_file_backup));
00284 BufferedWriter out = new BufferedWriter(new FileWriter(murFile));
00285 System.out.println("Opening "+murFile.getAbsolutePath()+" for update(s)...");
00286 String line;
00287 String serialno=null;
00288 MURInfo murInfo=null;
00289 while((line=in.readLine())!=null) {
00290
00291 matcher = murPattern.matcher(line);
00292 if(matcher.matches()) {
00293 thisMUR=line.substring(matcher.start(1),matcher.end(1));
00294 if(!murMap.containsKey(thisMUR)) {
00295 System.err.println("Found unrecognised MUR "+thisMUR+" in "+original_file_name);
00296 errorString="Found unrecognised MUR "+thisMUR+" in "+original_file_name;
00297 break;
00298 }
00299 murInfo = (MURInfo)murMap.get(thisMUR);
00300 murCount++;
00301 }
00302 if(line.matches(".*</MUR.*")) murInfo=null;
00303
00304
00305
00306 matcher = channelIDPattern.matcher(line);
00307 if(matcher.matches()) {
00308 if(murInfo==null) {
00309 System.err.println("Found channel declaration outside of MUR declaration in "+original_file_name+" : "+line);
00310 errorString="Found channel declaration outside of MUR declaration in "+original_file_name+" : "+line;
00311 break;
00312 }
00313 String chanID = line.substring(matcher.start(1),matcher.end(1));
00314 serialno = murInfo.getSerialno(chanID);
00315 }
00316 if(line.matches(".*</channel.*")) serialno=null;
00317
00318
00319 matcher = streamDataPattern.matcher(line);
00320 if(matcher.matches()) {
00321 if(serialno!=null) {
00322 String firstPart = line.substring(matcher.start(1),matcher.end(1));
00323 String streamS = line.substring(matcher.start(2),matcher.end(2));
00324 String middlePart = line.substring(matcher.start(3),matcher.end(3));
00325 String dataS = line.substring(matcher.start(4),matcher.end(4));
00326 String lastPart = line.substring(matcher.start(5),matcher.end(5));
00327 int stream = Integer.valueOf(streamS).intValue();
00328 String newData = murInfo.getRxThrData(serialno,stream,true);
00329 if(newData!=null) {
00330 line=firstPart+streamS+middlePart+newData+lastPart;
00331
00332
00333 System.out.println(murFile.getName()+" : "+serialno+" stream "+streamS+" "+dataS+" -> "+newData);
00334 lineCount++;
00335 }
00336 }
00337 }
00338 out.write(line);
00339 out.newLine();
00340
00341 }
00342 in.close();
00343 out.close();
00344 System.out.println("Closed "+murFile.getAbsolutePath()+" after update(s).");
00345 }catch(Exception eIO) {errorString = "Exception "+eIO.toString(); break;}
00346
00347 }
00348 if(lineCount>0) javax.swing.JOptionPane.showMessageDialog(null,"Updated "+lineCount+" lines within "+murCount+" MURs in "+fileCount+" files. See console for details.");
00349 clearParams();
00350
00351 }
00352
00353 }
00354
00355