-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathClingoExecutor.java
More file actions
169 lines (152 loc) · 3.87 KB
/
ClingoExecutor.java
File metadata and controls
169 lines (152 loc) · 3.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/**
*
*/
package utils;
import java.io.BufferedReader;
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;
/**
* @author Arpit Sharma
* @date Aug 4, 2017
*
*/
public class ClingoExecutor {
enum Enum_OS {
LINUX,
WINDOWS,
UNKNOWN;
}
private String command = "";
private String clingoPath = null;
private Enum_OS currentOS = getOS();
public ClingoExecutor(){}
public ClingoExecutor(String clingoPath){
this.clingoPath = clingoPath;
}
public Enum_OS getOS() {
String OS = System.getProperty("os.name").toLowerCase();
Enum_OS currentOS;
if (OS.contains("linux")) {
currentOS = Enum_OS.LINUX;
} else if (OS.contains("windows")) {
currentOS = Enum_OS.WINDOWS;
} else {
currentOS = Enum_OS.UNKNOWN;
System.err.println("Unknown Operating System");
}
return currentOS;
}
public ArrayList<String> callASPusingFilesList(ArrayList<String> listOfFiles) throws InterruptedException{// sentFile, String bkFile, String rulesFile){
String files = "";
String[] cmd;
for(String name : listOfFiles){
files = files + " " + name;
}
command = clingoPath + " " +files.trim();
ArrayList<String> result = null;
try {
switch (currentOS) {
case LINUX:
cmd = new String[]{
"/bin/sh",
"-c",
command
};
break;
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<String>();
}
line=stdInput.readLine();
if(!line.trim().equalsIgnoreCase("")){
result.add(line);
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
public ArrayList<String> callASPusingCmd(String cmmd) throws InterruptedException{// sentFile, String bkFile, String rulesFile){
command = cmmd;
ArrayList<String> result = null;
try {
switch (currentOS) {
case LINUX:
String[] cmd = {
"/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<String>();
}
line=stdInput.readLine();
if(!line.trim().equalsIgnoreCase("")){
result.add(line);
}
}
}
break;
default:
@SuppressWarnings("unused")
String[] cmd2 = {
"cmd",
"/c",
command
};
break;
}
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
public static void main(String[] args) throws IOException, InterruptedException{
String clingo = "/home/arpit/workspace/WinogradPhd/WebContent/WEB-INF/lib/Clingo/clingo";
ClingoExecutor cw = new ClingoExecutor(clingo);
String rules = "/home/arpit/workspace/WinogradPhd/WebContent/WEB-INF/lib/Clingo/reasoning/just_rules.txt";
ArrayList<String> listOfFiles = new ArrayList<String>();
listOfFiles.add(rules);
listOfFiles.add("1.txt");
ArrayList<String> res = cw.callASPusingFilesList(listOfFiles);
for(String s : res){
System.out.println(s);
}
}
}