-
Notifications
You must be signed in to change notification settings - Fork 0
/
CheckTrueFalse.java
305 lines (250 loc) · 8.75 KB
/
CheckTrueFalse.java
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
import java.io.*;
import java.util.*;
public class CheckTrueFalse {
static Set<String> symbolList = new HashSet<String>();
public static void main(String[] args) {
if (args.length != 3) {
System.out.println("Usage: "+ args[0]+ " [wumpus-rules-file] [additional-knowledge-file] [input_file]\n");
exit_function(0);
}
String buffer;
BufferedReader inputStream;
LogicalExpression kb_plus_rules = new LogicalExpression();
LogicalExpression statement1 = new LogicalExpression();
LogicalExpression statement2 = new LogicalExpression();
TTEntailsAlgorithm ttea = new TTEntailsAlgorithm();
TTEntailsAlgorithm.Model m = ttea.new Model();
try {
inputStream = new BufferedReader(new FileReader(args[0]));
kb_plus_rules.setConnective("and");
while ((buffer = inputStream.readLine()) != null) {
if (!(buffer.startsWith("#") || (buffer.equals("")))) {
LogicalExpression subExpression = readExpression(buffer);
kb_plus_rules.setSubexpression(subExpression);
}
}
inputStream.close();
} catch (Exception e) {
System.out.println("failed to open " + args[0]);
e.printStackTrace();
exit_function(0);
}
try {
inputStream = new BufferedReader(new FileReader(args[1]));
while ((buffer = inputStream.readLine()) != null) {
if (!(buffer.startsWith("#") || (buffer.equals("")))) {
String temp = buffer;
if (temp.contains("not")) {
String split[] = temp.split(" ");
split[1] = split[1].substring(0, split[1].length() - 1);
m.h.put(split[1], false);
} else {
temp = temp.trim();
m.h.put(temp, true);
}
LogicalExpression subExpression = readExpression(buffer);
kb_plus_rules.setSubexpression(subExpression);
}
}
inputStream.close();
} catch (Exception e) {
System.out.println("failed to open " + args[1]);
e.printStackTrace();
exit_function(0);
}
if (!valid_expression(kb_plus_rules)) {
System.out.println("invalid knowledge base");
exit_function(0);
}
String alpha1 = "";
String alpha2 = "";
try {
inputStream = new BufferedReader(new FileReader(args[2]));
getSymbols(kb_plus_rules);
Set<String> uniqueSymbolset = symbolList;
while ((buffer = inputStream.readLine()) != null) {
if (!buffer.startsWith("#")) {
if (buffer.contains("not")) {
alpha1 = buffer;
String split[] = buffer.split(" ");
alpha2 = split[1].substring(split[1].length() - 1);
} else {
alpha1 = buffer;
alpha2 = "(not " + buffer + ")";
}
statement1 = readExpression(alpha1);
statement2 = readExpression(alpha2);
if (valid_expression(statement1)&& !isValidInput(alpha1, uniqueSymbolset)) {
System.out.println("invalid statement");
return;
}
break;
}
}
inputStream.close();
} catch (Exception e) {
System.out.println("failed to open " + args[2]);
e.printStackTrace();
exit_function(0);
}
if (!valid_expression(statement1)) {
System.out.println("invalid statement");
exit_function(0);
}
boolean output1 = ttea.ttEntails(kb_plus_rules, m, statement1);
boolean output2 = ttea.ttEntails(kb_plus_rules, m, statement2);
printOutput(output1, output2);
}
private static void printOutput(boolean output1, boolean output2) {
try {
BufferedWriter writer;
writer = new BufferedWriter(new FileWriter(new File("result.txt")));
if (output1 != output2) {
System.out.println("definitely " + output1);
writer.write("definitely " + output1);
} else if (output1 == output2 && output1 == false) {
System.out.println("possibly true, possibly false");
writer.write("possibly true, possibly false");
} else if (output1 == output2 && output1 == true) {
System.out.println("both true and false");
writer.write("both true and false");
}
writer.close();
} catch (IOException e) {
System.out.println("Error message : " + e.getMessage());
e.printStackTrace();
}
}
public static LogicalExpression readExpression(String input_string) {
LogicalExpression result = new LogicalExpression();
input_string = input_string.trim();
if (input_string.startsWith("(")) {
String symbolString = "";
symbolString = input_string.substring(1);
if (!symbolString.endsWith(")")) {
System.out.println("missing ')' !!! - invalid expression! - readExpression():-" + symbolString);
exit_function(0);
} else {
symbolString = symbolString.substring(0,
(symbolString.length() - 1));
symbolString.trim();
symbolString = result.setConnective(symbolString);
}
result.setSubexpressions(read_subexpressions(symbolString));
} else {
result.setUniqueSymbol(input_string);
}
return result;
}
public static Vector<LogicalExpression> read_subexpressions(String input_string) {
Vector<LogicalExpression> symbolList = new Vector<LogicalExpression>();
LogicalExpression newExpression;
String newSymbol = new String();
input_string.trim();
while (input_string.length() > 0) {
newExpression = new LogicalExpression();
if (input_string.startsWith("(")) {
int parenCounter = 1;
int matchingIndex = 1;
while ((parenCounter > 0)&& (matchingIndex < input_string.length())) {
if (input_string.charAt(matchingIndex) == '(') {
parenCounter++;
} else if (input_string.charAt(matchingIndex) == ')') {
parenCounter--;
}
matchingIndex++;
}
newSymbol = input_string.substring(0, matchingIndex);
newExpression = readExpression(newSymbol);
symbolList.add(newExpression);
input_string = input_string.substring(newSymbol.length(),input_string.length());
} else {
if (input_string.contains(" ")) {
newSymbol = input_string.substring(0,input_string.indexOf(" "));
input_string = input_string.substring((newSymbol.length() + 1), input_string.length());
} else {
newSymbol = input_string;
input_string = "";
}
newExpression.setUniqueSymbol(newSymbol);
symbolList.add(newExpression);
}
input_string.trim();
if (input_string.startsWith(" ")) {
input_string = input_string.substring(1);
}
}
return symbolList;
}
public static boolean valid_expression(LogicalExpression expression) {
if (!(expression.getUniqueSymbol() == null)
&& (expression.getConnective() == null)) {
return valid_symbol(expression.getUniqueSymbol());
}
if ((expression.getConnective().equalsIgnoreCase("if")) || (expression.getConnective().equalsIgnoreCase("iff"))) {
if (expression.getSubexpressions().size() != 2) {
System.out.println("error: connective \"" + expression.getConnective() + "\" with " + expression.getSubexpressions().size() + " arguments\n");
return false;
}
}
else if (expression.getConnective().equalsIgnoreCase("not")) {
if (expression.getSubexpressions().size() != 1) {
System.out.println("error: connective \""+ expression.getConnective() + "\" with "+ expression.getSubexpressions().size()+ " arguments\n");
return false;
}
}
else if ((!expression.getConnective().equalsIgnoreCase("and"))
&& (!expression.getConnective().equalsIgnoreCase("or"))
&& (!expression.getConnective().equalsIgnoreCase("xor"))) {
System.out.println("error: unknown connective " + expression.getConnective() + "\n");
return false;
}
for (
Enumeration<LogicalExpression> e = expression.getSubexpressions().elements(); e.hasMoreElements();) {
LogicalExpression testExpression = (LogicalExpression) e.nextElement();
if (!valid_expression(testExpression)) {
return false;
}
}
return true;
}
public static boolean valid_symbol(String symbol) {
if (symbol == null || (symbol.length() == 0)) {
return false;
}
for (int counter = 0; counter < symbol.length(); counter++) {
if ((symbol.charAt(counter) != '_')
&& (!Character.isLetterOrDigit(symbol.charAt(counter)))) {
System.out.println("String: " + symbol + " is invalid! Offending character:---"+ symbol.charAt(counter) + "---\n");
return false;
}
}
return true;
}
private static void exit_function(int value) {
System.out.println("exiting from checkTrueFalse");
System.exit(value);
}
static boolean isValidInput(String s, Set<String> set) {
Iterator<String> it = set.iterator();
boolean b = false;
while (it.hasNext()) {
if (it.next().equals(s))
b = true;
}
if (s.contains("(or") || s.contains("(and") || s.contains("(xor") || s.contains("(not") || s.contains("(if") || s.contains("(iff"))
b = true;
return b;
}
static void getSymbols(LogicalExpression le) {
if (le.getUniqueSymbol() != null)
symbolList.add(le.getUniqueSymbol());
else
for (int i = 0; i < le.getSubexpressions().size(); i++) {
LogicalExpression lle = (LogicalExpression) le.getSubexpressions().get(i);
getSymbols(lle);
if (lle.getUniqueSymbol() != null)
symbolList.add(lle.getUniqueSymbol());
}
}
}