-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathForwardChain.java
More file actions
259 lines (188 loc) · 5.82 KB
/
Copy pathForwardChain.java
File metadata and controls
259 lines (188 loc) · 5.82 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
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
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
import java.util.Stack;
import java.util.Iterator;
public class ForwardChain {
static File file;
static BufferedReader reader;
public static void main(String[] args) {
// ForwardChain fc = new ForwardChain();
//
// Literal q = new Literal("Q",false);
// DefClauseKB KB = new DefClauseKB();
//
// // add a clause to kb
// DefiniteClause c1 = new DefiniteClause(new Literal("Q", false));
// c1.getPremises().add(new Literal("P", false));
//
// KB.getDefClauses().add(c1);
//
// // add a clause to kb
// c1 = new DefiniteClause(new Literal("P", false));
// c1.getPremises().add(new Literal("L", false));
// c1.getPremises().add(new Literal("M", false));
//
// KB.getDefClauses().add(c1);
//
// // add a clause to kb
// c1 = new DefiniteClause(new Literal("M", false));
// c1.getPremises().add(new Literal("B", false));
// c1.getPremises().add(new Literal("L", false));
//
// KB.getDefClauses().add(c1);
//
// // add a clause to kb
// c1 = new DefiniteClause(new Literal("L", false));
// c1.getPremises().add(new Literal("A", false));
// c1.getPremises().add(new Literal("P", false));
//
// KB.getDefClauses().add(c1);
//
// // add a clause to kb
// c1 = new DefiniteClause(new Literal("L", false));
// c1.getPremises().add(new Literal("A", false));
// c1.getPremises().add(new Literal("B", false));
//
// KB.getDefClauses().add(c1);
//
// // add a clause to kb
// c1 = new DefiniteClause(new Literal("A", false));
//
// KB.getDefClauses().add(c1);
//
// // add a clause to kb
// c1 = new DefiniteClause(new Literal("B", false));
//
// KB.getDefClauses().add(c1);
//
// KB.print();
// System.out.print("We want to prove: ");
// q.print();
// System.out.println("\n");
//
// boolean entails = fc.PlFcEntails(KB, q);
//
// if(entails) {
// System.out.print("Literal ");
// q.print();
// System.out.print(" has been proven to be true.");
// } else {
// System.out.print("Couldn't infer ");
// q.print();
// System.out.print(" from the given knowledge base.");
// }
ForwardChain fc = new ForwardChain();
DefClauseKB KB = readFile("src\\defClauses.txt");
KB.print();
System.out.println("Please give the literal to prove: ");
Scanner input = new Scanner(System.in);
Literal l = new Literal(input.nextLine(),false);
input.close();
boolean entails = fc.PlFcEntails(KB, l);
if(entails) {
System.out.print("Literal ");
l.print();
System.out.print(" has been proven to be true.");
} else {
System.out.print("Couldn't infer ");
l.print();
System.out.print(" from the given knowledge base.");
}
}
private static DefClauseKB readFile(String filepath){
try{
file = new File(filepath);
}catch (Exception e){
e.printStackTrace();
}
try {
reader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
}catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
String line = reader.readLine();
return parse(line);
} catch (IOException e) {
}
return null;
}
public static DefClauseKB parse(String s){
DefClauseKB kb = new DefClauseKB();
DefiniteClause defcl;
String subclauses[] = s.split(",+(?![^\\(]*\\))", -1);
for (String c:subclauses){
c = c.replaceAll("[()]", "");
String tokens [] = c.split("=>");
if (tokens.length==1) {
defcl = new DefiniteClause(new Literal(tokens[0],false));
}else{
defcl = new DefiniteClause(new Literal(tokens[1],false));
String literals []= tokens[0].split(",");
for(String x:literals){
defcl.getPremises().add(new Literal(x,false));
}
}
kb.getDefClauses().add(defcl);
}
return kb;
}
public Stack<Literal> initAgenda(DefClauseKB KB) {
Stack<Literal> agenda = new Stack<Literal>();
Iterator<DefiniteClause> iter = KB.getIter();
while(iter.hasNext()) {
DefiniteClause next = iter.next();
next.initCount(); // initialize the count
if(next.isFact()) {
agenda.push(next.getHead());
}
}
return agenda;
}
/**
* propositional logic - forward chaining
* @return true if the knowledge base entails the literal q
* false otherwise
*/
public boolean PlFcEntails(DefClauseKB KB, Literal q) {
// initialize the agenda with the symbols that are known to be true
Stack<Literal> agenda = initAgenda(KB);
// if the literal to prove is already in the agenda
if(agenda.contains(q)) {
System.out.println("The given statement is a fact of the knowledge base.");
return true;
}
while(!agenda.isEmpty()) {
Literal p = agenda.pop();
if(!p.isInferred()) { // if p is not inferred yet
p.setInferred();
// for each Horn clause in KB
Iterator<DefiniteClause> iter = KB.getIter();
while(iter.hasNext()) {
DefiniteClause c = iter.next();
if(c.getPremises().contains(p)) { // in whose p appears
c.decrCount();
if(c.getCount() == 0) { // rule trigger
// print
System.out.println("Rule triggered: ");
c.print();
//print
System.out.print("\nNew fact: ");
c.getHead().print();
System.out.println();
if(c.getHead().equals(q))
return true;
agenda.push(c.getHead());
}
}
}
}
}
return false;
}
}