diff --git a/resources/Clingo/clingo.exe b/resources/Clingo/clingo.exe new file mode 100755 index 0000000..db7df91 Binary files /dev/null and b/resources/Clingo/clingo.exe differ diff --git a/src/main/KnowledgeExtractor.java b/src/main/KnowledgeExtractor.java index 7dcc79d..7ce13eb 100644 --- a/src/main/KnowledgeExtractor.java +++ b/src/main/KnowledgeExtractor.java @@ -7,8 +7,10 @@ import helper.KnowledgeObject; import java.io.BufferedReader; +import java.io.BufferedWriter; import java.io.File; import java.io.FileReader; +import java.io.FileWriter; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; @@ -59,13 +61,21 @@ public HashMap> extractKnowledge(File textFile } @SuppressWarnings("unchecked") - public void runOnServer(){ - HashSet setOfDoneFiles = getSetOfDoneFiles(); + public void runOnServer() throws IOException{ + String fileNamesFilePath = Configurations.getProperty("knowextfrom"); int counter = 1; ArrayList kparseFile = Utilities.listFilesForFolder(new File(savedKparsesdirPath), false); + BufferedWriter bw = null; + bw = new BufferedWriter(new FileWriter(fileNamesFilePath, true)); + HashSet setOfDoneFiles = getSetOfDoneFiles(fileNamesFilePath); + java.util.Collections.sort(kparseFile); for(String listFile : kparseFile){ - System.out.println(listFile); if(!setOfDoneFiles.contains(listFile)){ + System.out.println("Processing file "+listFile); + bw.write(listFile); + bw.newLine(); + bw.flush(); + ArrayList listOfParses = (ArrayList) Utilities.load(savedKparsesdirPath+listFile); for(GraphPassingNode gpn : listOfParses){ ArrayList kObjList; @@ -84,14 +94,19 @@ public void runOnServer(){ // e.printStackTrace(); } } + } else { + System.out.println("skipping file " + listFile); } } + + bw.close(); } - public HashSet getSetOfDoneFiles(){ + public HashSet getSetOfDoneFiles(String fileNamesFilePath) throws IOException{ HashSet result = new HashSet(); - String fileNamesFilePath = Configurations.getProperty("knowextfrom"); - try(BufferedReader br = new BufferedReader(new FileReader(fileNamesFilePath))){ + BufferedReader br = null; + try{ + br = new BufferedReader(new FileReader(fileNamesFilePath)); String line = null; while((line=br.readLine())!=null){ result.add(line); @@ -99,6 +114,9 @@ public HashSet getSetOfDoneFiles(){ }catch(IOException e){ e.printStackTrace(); } + finally { + br.close(); + } return result; } diff --git a/src/utils/ClingoExecutor.java b/src/utils/ClingoExecutor.java index 00cd333..d0fa69a 100644 --- a/src/utils/ClingoExecutor.java +++ b/src/utils/ClingoExecutor.java @@ -7,6 +7,7 @@ import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; +import java.util.concurrent.TimeUnit; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -49,6 +50,7 @@ public Enum_OS getOS() { public ArrayList callASPusingFilesList(ArrayList listOfFiles) throws InterruptedException{// sentFile, String bkFile, String rulesFile){ String files = ""; + String[] cmd; for(String name : listOfFiles){ files = files + " " + name; } @@ -57,39 +59,44 @@ public ArrayList callASPusingFilesList(ArrayList listOfFiles) th try { switch (currentOS) { case LINUX: - String[] cmd = { + cmd = new String[]{ "/bin/sh", "-c", command }; - Process process; - process = Runtime.getRuntime().exec(cmd); - process.waitFor(); - - BufferedReader stdInput = new BufferedReader(new InputStreamReader(process.getInputStream())); - String line = null; - while((line=stdInput.readLine())!=null){ - Pattern p = Pattern.compile(".*Answer.*"); - Matcher m = p.matcher(line); - if(m.find()){ - if(result==null){ - result=new ArrayList(); - } - line=stdInput.readLine(); - if(!line.trim().equalsIgnoreCase("")){ - result.add(line); - } - } - } break; - default: - @SuppressWarnings("unused") - String[] cmd2 = { + case WINDOWS: + cmd = new String[]{ "cmd", "/c", command }; break; + default: + throw new IOException("Unknown Operating System"); + } + + Process process; + process = Runtime.getRuntime().exec(cmd); + if(!process.waitFor(5, TimeUnit.SECONDS)) { + System.err.println("Clingo timed out"); + return null; + } + + BufferedReader stdInput = new BufferedReader(new InputStreamReader(process.getInputStream())); + String line = null; + while((line=stdInput.readLine())!=null){ + Pattern p = Pattern.compile(".*Answer.*"); + Matcher m = p.matcher(line); + if(m.find()){ + if(result==null){ + result=new ArrayList(); + } + line=stdInput.readLine(); + if(!line.trim().equalsIgnoreCase("")){ + result.add(line); + } + } } } catch (IOException e) { e.printStackTrace();