/
FOLBCAsk.java
324 lines (294 loc) · 9.57 KB
/
FOLBCAsk.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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
package aima.core.logic.fol.inference;
import java.util.*;
import aima.core.logic.fol.Unifier;
import aima.core.logic.fol.inference.proof.*;
import aima.core.logic.fol.kb.FOLKnowledgeBase;
import aima.core.logic.fol.kb.data.Clause;
import aima.core.logic.fol.kb.data.Literal;
import aima.core.logic.fol.parsing.ast.AtomicSentence;
import aima.core.logic.fol.parsing.ast.Sentence;
import aima.core.logic.fol.parsing.ast.Term;
import aima.core.logic.fol.parsing.ast.Variable;
import aima.core.logic.propositional.kb.KnowledgeBase;
/**
* Artificial Intelligence A Modern Approach (3rd Edition): Figure 9.6, page
* 338.<br>
* <br>
*
* <pre>
* function FOL-BC-ASK(KB, query) returns a generator of substitutions
* return FOL-BC-OR(KB, query, { })
*
* generator FOL-BC-OR(KB, goal, θ) yields a substitution
* for each rule (lhs ⇒ rhs) in FETCH-RULES-FOR-GOAL(KB, goal) do
* (lhs, rhs) ← STANDARDIZE-VARIABLES((lhs, rhs))
* for each θ' in FOL-BC-AND(KB, lhs, UNIFY(rhs, goal, θ)) do
* yield θ'
*
* generator FOL-BC-AND(KB, goals, θ) yields a substitution
* if θ = failure then return
* else if LENGTH(goals) = 0 then yield θ
* else do
* first, rest ← FIRST(goals), REST(goals)
* for each θ' in FOL-BC-OR(KB, SUBST(θ, first), θ) do
* for each θ'' in FOL-BC-AND(KB, rest, θ') do
* yield θ'
* </pre>
*
* Figure 9.6 A simple backward-chaining algorithm for first-order knowledge bases.
*
* @author samagra
* @author Ritwik Sharma
* @author Ciaran O'Reilly
* @author Mike Stampone
*/
public class FOLBCAsk implements InferenceProcedure{
List<List<Literal>> finalAnswer;// to store the final result
List<Literal> substitutedLiterals;
List<HashMap<Variable,Term>> finalList;
BCASKHandler bcaskHandler = new BCASKHandler();
public boolean maybeFalse = false;
public FOLBCAsk() {
finalAnswer = new ArrayList<>();
substitutedLiterals = new ArrayList<>();
}
public List<Literal> getSubstitutedLiterals() {
return substitutedLiterals;
}
//
// START-InferenceProcedure
/**
* Returns a set of substitutions
* function FOL-BC-ASK(KB, query) returns a generator of substitutions
* @param kb
* a knowledge base
* @param query
* goals, a list of conjuncts forming a query
*
* @return a set of substitutions
*/
public List<HashMap<Variable,Term>> folBcAsk(FOLKnowledgeBase kb, Literal query){
//return FOL-BC-OR(KB, query, { })
return folBcOr(kb,query, new HashMap<>());
}
/**
* generator FOL-BC-OR(KB, goal, θ) yields a substitution
* @param kb
* The knowleadge base
* @param goal
* The goals at the or node to be achieved
* @param theta
* Substitution
* @return
* a list of substitutions
*/
private List<HashMap<Variable, Term>> folBcOr(FOLKnowledgeBase kb, Literal goal, HashMap<Variable, Term> theta) {
List<HashMap<Variable,Term>> result = new ArrayList<>();
finalAnswer.add(new ArrayList<>(Collections.singletonList(goal)));
HashMap<Variable,Term> temp;
if (fetchRulesForGoal(kb,goal).isEmpty())
maybeFalse = true;
// for each rule (lhs ⇒ rhs) in FETCH-RULES-FOR-GOAL(KB, goal) do
for (Clause rule :
fetchRulesForGoal(kb,goal)) {
//(lhs, rhs) ← STANDARDIZE-VARIABLES((lhs, rhs))
Clause tempClause = kb.standardizeApart(rule);
temp = new HashMap<>(theta);
Literal rhs = tempClause.getPositiveLiterals().get(0);
List<Literal> lhs = new ArrayList<>();
for (Literal literal :
tempClause.getNegativeLiterals() ){
lhs.add(new Literal(literal.getAtomicSentence(),!literal.isNegativeLiteral()));
}
//for each θ' in FOL-BC-AND(KB, lhs, UNIFY(rhs, goal, θ)) do
// yield θ'
result.addAll(folBcAnd(kb, lhs, new Unifier().unify(rhs.getAtomicSentence(), goal.getAtomicSentence(), temp)));
}
return result;
}
/**
* generator FOL-BC-AND(KB, goals, θ) yields a substitution
* @param kb
* @param goals
* @param theta
* @return
*/
private List<HashMap<Variable, Term>> folBcAnd(FOLKnowledgeBase kb, List<Literal> goals, Map<Variable, Term> theta) {
List<HashMap<Variable,Term>> result = new ArrayList<>();
finalAnswer.add(new ArrayList<>(goals));
// if θ = failure then return
if (theta==null)
return result;
// else if LENGTH(goals) = 0 then yield θ
else if (goals.size()==0){
result.add((HashMap<Variable, Term>) theta);
return result;
}
// else do
else {
// first, rest ← FIRST(goals), REST(goals)
Literal first = goals.get(0);
List<Literal> rest = new ArrayList<>(goals);
rest.remove(0);
// for each θ' in FOL-BC-OR(KB, SUBST(θ, first), θ) do
for (HashMap<Variable, Term> thetaPrime :
folBcOr(kb,kb.subst(theta,first),(HashMap<Variable,Term>)theta)) {
substitutedLiterals.add(kb.subst(theta,first));
// for each θ'' in FOL-BC-AND(KB, rest, θ') do
// yield θ'
result.addAll(folBcAnd(kb, rest, thetaPrime));
}
}
return result;
}
/**
* Fetches all those implication clauses whose rhs meet with the goal
* @param kb
* @param goal
* @return
*/
private List<Clause> fetchRulesForGoal(FOLKnowledgeBase kb, Literal goal){
List<Clause> result = new ArrayList<>();
for (Clause clause :
kb.getAllDefiniteClauseImplications()) {
Literal rhs = clause.getPositiveLiterals().get(0);
if (rhs.getAtomicSentence().getSymbolicName().equals(goal.getAtomicSentence().getSymbolicName())){
result.add(clause);
}
}
for (Clause clause :
kb.getAllClauses()) {
if (clause.isUnitClause()){
for (Literal l :
clause.getLiterals()) {
if (l.getAtomicSentence().getSymbolicName().equals(goal.getAtomicSentence().getSymbolicName())) {
result.add(clause);
}
}
}
}
for (Clause clause :
result) {
ProofStep step = new BCProofStep(clause,this.bcaskHandler.proofs.get(0).getSteps(),goal);
this.bcaskHandler.addProofStep(step);
}
return result;
}
public List<List<Literal>> getFinalAnswer() {
return finalAnswer;
}
@Override
public InferenceResult ask(FOLKnowledgeBase kb, Sentence query) {
Literal l = new Literal(((AtomicSentence) query));
List<HashMap<Variable, Term>> substitutes = this.folBcAsk(kb, l);
this.finalList = substitutes;
if (l.getAtomicSentence().getArgs().get(0) instanceof Variable) {
Variable x = (Variable) l.getAtomicSentence().getArgs().get(0);
for (HashMap<Variable, Term> subs :
substitutes) {
HashMap<Variable, Term> toadd = new HashMap<>();
toadd.put(new Variable(x.getValue()), subs.get(x));
Proof proof = new BCProof();
proof.replaceAnswerBindings(new HashMap<>(toadd));
((BCProof) proof).proofSteps = new ArrayList<>(this.bcaskHandler.proofs.get(0).getSteps());
this.bcaskHandler.proofs.add(proof);
}
}
if (this.bcaskHandler.proofs.size()>1)
this.bcaskHandler.proofs.remove(0);
return this.bcaskHandler;
}
class BCASKHandler implements InferenceResult{
private ProofStep stepFinal = null;
private List<Proof> proofs = new ArrayList<>();
public BCASKHandler(){
proofs.add(new BCProof());
}
@Override
public boolean isPossiblyFalse() {
return finalList.isEmpty();
}
@Override
public boolean isTrue() {
return (!finalList.isEmpty());
}
@Override
public boolean isUnknownDueToTimeout() {
return false;
}
@Override
public boolean isPartialResultDueToTimeout() {
return false;
}
@Override
public List<Proof> getProofs() {
return proofs;
}
public void addProofStep(ProofStep step){
((BCProof)this.proofs.get(0)).addProofStep(step);
}
}
class BCProof implements Proof{
List<ProofStep> proofSteps = new ArrayList<>();
Map<Variable, Term> answerBindings = new HashMap<>();
public BCProof(){
}
public void addProofStep(ProofStep step){
proofSteps.add(step);
}
@Override
public List<ProofStep> getSteps() {
return proofSteps;
}
@Override
public Map<Variable, Term> getAnswerBindings() {
return answerBindings;
}
@Override
public void replaceAnswerBindings(Map<Variable, Term> updatedBindings) {
answerBindings = updatedBindings;
}
}
class BCProofStep extends AbstractProofStep{
List<ProofStep> predecessors = new ArrayList<>();
Clause implication ;
Literal goal;
public BCProofStep(Clause implication, List<ProofStep> predecessors, Literal goal){
this.implication = implication;
this.predecessors = predecessors;
this.goal = goal;
this.setStepNumber(this.predecessors.size()+1);
}
@Override
public List<ProofStep> getPredecessorSteps() {
return predecessors;
}
@Override
public String getProof() {
return this.toString();
}
@Override
public String toString() {
StringBuilder result = new StringBuilder();
if (implication.getLiterals().size()>1){
int i =0;
for (Literal l :
implication.getNegativeLiterals()) {
result.append((new Literal(l.getAtomicSentence())).toString());
i++;
if (i<implication.getNegativeLiterals().size())
result.append(" AND ");
}
result.append(" => ");
result.append(implication.getPositiveLiterals().get(0));
return result.toString();
}
result.append(implication.getLiterals().toString());
return result.toString();
}
@Override
public String getJustification() {
return "To Prove Backwards :" + goal.toString();
}
}
}