private static void handleCommands(String fileNamePath, File outFilePath, String mode) throws IOException { // Purpose: Process file that contains commands for this process (e.g. STOP, ENQ etc.) System.out.println("Handling Command"); TStringList sl = new TStringList(); sl.ReadFromFile(fileNamePath); for (int j = 0; j < sl.Count(); j++) { handleOneCommand(sl.getString(j), outFilePath, mode); } // File cmdFile = new File(fileNamePath); // cmdFile.delete(); }
private String[] loadKBInfo(String mode, String parentPath) { // Results: [0] = kbFilePath // [1] = kbID String[] results = new String[2]; results[0] = ""; results[1] = ""; String configFilePath = parentPath + "config/"; File configFileDir = new File(configFilePath); if (configFileDir.isDirectory() == false) { System.out.println(configFileDir.getName() + " directory is missing. Exiting"); System.exit(0); } String configFName = parentPath + "config/KB MAP.txt"; TStringList sl = new TStringList(); try { sl.ReadFromFile(configFName); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } for (int i = 0; i < sl.Count(); i++) { String s = sl.getString(i); String lbl = s.split("=")[0]; if (lbl.equals("")) continue; String[] lblParts = lbl.split(":"); if (lblParts.length < 2) continue; if (lblParts[0].equals(mode) == false) continue; if (lblParts[1].equals("KB")) { String kbFilePath = parentPath + "kbs/" + s.split("=")[1].trim(); results[0] = kbFilePath; } else if (lblParts[1].equals("ID")) { results[1] = s.split("=")[1].trim(); // kb ID string } } return results; }
public static void main(String[] argv) throws InterruptedException { // Arguments: // 1st = Mode // 2nd = Parent Path -- (Parent path must have children /data and /output System.out.println("+----------------------------------+"); System.out.println("| Running Athena Pallas Processor |"); System.out.println("| 9/12/12 |"); System.out.println("+----------------------------------+"); Athena_Server_Obj = new ATHENA_Server_Local(); BasicConfigurator.configure(); Logger.getRootLogger().setLevel(Level.WARN); // Get and verify parameters if (argv.length < 2) { System.out.println("Not enough parameters."); System.out.println("Expected 2 parameters: 1st is MODE, 2nd is path to working directory."); System.out.println("Exiting."); System.exit(0); } String mode = argv[0]; String parentPath = argv[1]; File inFilePath = new File(parentPath + "astronautdata/"); File outFilePath = new File(parentPath + "output/"); File kbFilePath = new File(parentPath + "kbs/"); if (mode.equals("")) { System.out.println("Parameter 1 is empty. Should be RUNMODE (e.g. 'HTN'). Exiting"); System.exit(0); } if (inFilePath.isDirectory() == false) { System.out.println(inFilePath.getName() + " directory is missing. Exiting"); System.exit(0); } if (outFilePath.isDirectory() == false) { System.out.println(outFilePath.getName() + " directory is missing. Exiting"); System.exit(0); } if (kbFilePath.isDirectory() == false) { System.out.println(kbFilePath.getName() + " directory is missing. Exiting"); System.exit(0); } String kbID = ""; TStringList IgnoredFileNames = new TStringList(); // Main Loop while (mainLoopRunning) { if (Verbose == true) { // System.out.println("New Loop"); } fileProcessed = false; if (Verbose == true) { // System.out.println("Ignoring #" + IgnoredFileNames.Count() + " files."); } // (re)load if needed if (loadKB == true) { String[] kbInfo = Athena_Server_Obj.loadKBInfo(mode, parentPath); String kbFilenamePath = kbInfo[0]; kbID = kbInfo[1]; Athena_Server_Obj.loadKB(kbFilenamePath, kbID); loadKB = false; } File[] listOfFiles = inFilePath.listFiles(); // for (File child : inFilePath.listFiles()) { for (int i = 0; i < listOfFiles.length; i++) { File child = listOfFiles[i]; if (".".equals(child.getName()) || "..".equals(child.getName())) { continue; } if (child.getName().toUpperCase().equals(mode + "-CMD.TXT")) { try { handleCommands(child.getPath(), outFilePath, mode); // fileProcessed = true; if (child.delete() == false) { System.out.println("Unable to delete: " + child.getPath()); Thread.sleep(1000); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } // continue; } if (IgnoredFileNames.IndexOf(child.getName()) > -1) { // if (Verbose == true) {System.out.print("~");} continue; } if (Verbose == true) { System.out.println("Considering file: [" + child.getName() + "]"); } String[] namePieces = child.getName().split("_"); // (should only open filenames with format of <MODE>_<DFN>.xml) if (Verbose == true) { System.out.println("File Name Is: " + child.getName()); } String fileExt = GetFileExtension(child.getName()); if (Verbose == true) { System.out.println("File Ext Is: " + fileExt); } // At times, there seems to be a race condition, where this java process // tries to load in the file before the mump process is done writting // So I have modified the mumps process to that it outputs TWO files. // 1st the <MODE>_<DFN>.xml file // 2nd, after finished with above, it outputs <MODE>_<DFN>_READY.TXT // So the 2nd file must exist before opening the <MODE>_<DFN>.xml // The 2nd file need not be opened or processed. Just the fact that // it exists on in the directory is signal that it is OK to process. if (fileExt.equals("xml") == false) { if (Verbose == true) { System.out.println("Skipping file due to wrong extension."); } IgnoredFileNames.Add(child.getName()); continue; } if (((namePieces.length > 1) && (namePieces[0].equals(mode))) == false) { if (Verbose == true) { System.out.println("Skipping file due to wrong mode."); } IgnoredFileNames.Add(child.getName()); continue; } Boolean finishFileExists = finishFileExist(child.getPath()); if (Verbose == true) { System.out.println("Ready File Exists?: " + finishFileExists); } // NOTE: after processing the <MODE>_<DFN>.xml, both files should be deleted // NOTE: in java, one can't use '=' to compare strings -- that just compares a pointer to // the string. Must use ".equals()" if (finishFileExists == true) { System.out.println("Found File to Process: [" + child.getName() + "]"); fileProcessed = true; String patientID = namePieces[1]; patientID = patientID.split("\\.")[0]; File outFileName = new File(outFilePath.getPath() + "/" + mode + "_" + patientID + ".xml"); String htmlFile = outFilePath.getPath() + "/" + mode + "_" + patientID + ".html"; System.out.println("SENDING TO: [" + outFileName.getPath() + "]"); String readyFileName; readyFileName = outFileName.getPath(); try { processFile(patientID, child, outFileName, kbID, htmlFile); } catch (Throwable t) { try { FileWriter fwrite = new FileWriter(outFileName); fwrite.write( "-1^CDSS/Clinical Decision Support Engine(Athena) Error. " + t.getMessage()); fwrite.flush(); fwrite.close(); } catch (IOException e3) { System.out.println("Could not create error message"); } System.out.println("Error Processing Recommendation: " + t.getMessage()); // return; } System.out.println("Ready.TXT File Name Is: " + readyFileName); // Remove File readyFile = new File(readyFileName.replace(".xml", "_READY.TXT")); try { readyFile.createNewFile(); FileWriter fstream = new FileWriter(readyFile); BufferedWriter out = new BufferedWriter(fstream); out.write("DONE"); out.close(); System.out.println("****SUCCESS****" + readyFile.getPath()); } catch (java.io.IOException e0) { System.out.println("Error creating READY file"); } if (child.delete() == false) { System.out.println("Unable to delete: " + child.getName()); Thread.sleep(1000); } // Delete completed file File f = new File(getFinishFileName(child.getPath())); if (f.delete() == false) { System.out.println("Unable to delete: " + f.getName()); Thread.sleep(1000); } } } if (fileProcessed == false) { Thread.sleep(1000); if (Verbose == true) { System.out.print("."); } } else { if (Verbose == true) { System.out.println("Found process, so won't sleep..."); } } mainLoopRunning = false; } // for loop System.out.println("Finished with run."); System.exit(0); } // Main routine