Repository navigation
Expand file tree
/
Copy pathFOLBCAsk.java
More file actions
244 lines (204 loc) · 7.37 KB
/
Copy pathFOLBCAsk.java
File metadata and controls
244 lines (204 loc) · 7.37 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
package aima.core.logic.fol.inference;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import aima.core.logic.fol.inference.proof.Proof;
import aima.core.logic.fol.inference.proof.ProofFinal;
import aima.core.logic.fol.inference.proof.ProofStepBwChGoal;
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;
/**
* Artificial Intelligence A Modern Approach (2nd Edition): Figure 9.6, page
* 288.<br>
* <br>
*
* <pre>
* function FOL-BC-ASK(KB, goals, theta) returns a set of substitutions
* input: KB, a knowledge base
* goals, a list of conjuncts forming a query (theta already applied)
* theta, the current substitution, initially the empty substitution {}
* local variables: answers, a set of substitutions, initially empty
*
* if goals is empty then return {theta}
* qDelta <- SUBST(theta, FIRST(goals))
* for each sentence r in KB where STANDARDIZE-APART(r) = (p1 ˆ ... ˆ pn => q)
* and thetaDelta <- UNIFY(q, qDelta) succeeds
* new_goals <- [p1,...,pn|REST(goals)]
* answers <- FOL-BC-ASK(KB, new_goals, COMPOSE(thetaDelta, theta)) U answers
* return answers
* </pre>
*
* Figure 9.6 A simple backward-chaining algorithm.
*
* @author Ciaran O'Reilly
* @author Mike Stampone
*/
public class FOLBCAsk implements InferenceProcedure {
public FOLBCAsk() {
}
//
// START-InferenceProcedure
/**
* Returns a set of substitutions
*
* @param KB
* a knowledge base
* @param query
* goals, a list of conjuncts forming a query
*
* @return a set of substitutions
*/
public InferenceResult ask(FOLKnowledgeBase KB, Sentence query) {
// Assertions on the type queries this Inference procedure
// supports
if (!(query instanceof AtomicSentence)) {
throw new IllegalArgumentException(
"Only Atomic Queries are supported.");
}
List<Literal> goals = new ArrayList<Literal>();
goals.add(new Literal((AtomicSentence) query));
BCAskAnswerHandler ansHandler = new BCAskAnswerHandler();
List<List<ProofStepBwChGoal>> allProofSteps = folbcask(KB, ansHandler,
goals, new HashMap<Variable, Term>());
ansHandler.setAllProofSteps(allProofSteps);
return ansHandler;
}
// END-InferenceProcedure
//
//
// PRIVATE METHODS
//
/**
* <code>
* function FOL-BC-ASK(KB, goals, theta) returns a set of substitutions
* input: KB, a knowledge base
* goals, a list of conjuncts forming a query (theta already applied)
* theta, the current substitution, initially the empty substitution {}
* </code>
*/
private List<List<ProofStepBwChGoal>> folbcask(FOLKnowledgeBase KB,
BCAskAnswerHandler ansHandler, List<Literal> goals,
Map<Variable, Term> theta) {
List<List<ProofStepBwChGoal>> thisLevelProofSteps = new ArrayList<List<ProofStepBwChGoal>>();
// local variables: answers, a set of substitutions, initially empty
// if goals is empty then return {theta}
if (goals.isEmpty()) {
thisLevelProofSteps.add(new ArrayList<ProofStepBwChGoal>());
return thisLevelProofSteps;
}
// qDelta <- SUBST(theta, FIRST(goals))
Literal qDelta = KB.subst(theta, goals.get(0));
// for each sentence r in KB where
// STANDARDIZE-APART(r) = (p1 ^ ... ^ pn => q)
for (Clause r : KB.getAllDefiniteClauses()) {
r = KB.standardizeApart(r);
// and thetaDelta <- UNIFY(q, qDelta) succeeds
Map<Variable, Term> thetaDelta = KB.unify(r.getPositiveLiterals()
.get(0).getAtomicSentence(), qDelta.getAtomicSentence());
if (null != thetaDelta) {
// new_goals <- [p1,...,pn|REST(goals)]
List<Literal> newGoals = new ArrayList<Literal>(
r.getNegativeLiterals());
newGoals.addAll(goals.subList(1, goals.size()));
// answers <- FOL-BC-ASK(KB, new_goals, COMPOSE(thetaDelta,
// theta)) U answers
Map<Variable, Term> composed = compose(KB, thetaDelta, theta);
List<List<ProofStepBwChGoal>> lowerLevelProofSteps = folbcask(
KB, ansHandler, newGoals, composed);
ansHandler.addProofStep(lowerLevelProofSteps, r, qDelta,
composed);
thisLevelProofSteps.addAll(lowerLevelProofSteps);
}
}
// return answers
return thisLevelProofSteps;
}
// Artificial Intelligence A Modern Approach (2nd Edition): page 288.
// COMPOSE(delta, tau) is the substitution whose effect is identical to
// the effect of applying each substitution in turn. That is,
// SUBST(COMPOSE(theta1, theta2), p) = SUBST(theta2, SUBST(theta1, p))
private Map<Variable, Term> compose(FOLKnowledgeBase KB,
Map<Variable, Term> theta1, Map<Variable, Term> theta2) {
Map<Variable, Term> composed = new HashMap<Variable, Term>();
// So that it behaves like:
// SUBST(theta2, SUBST(theta1, p))
// There are two steps involved here.
// See: http://logic.stanford.edu/classes/cs157/2008/notes/chap09.pdf
// for a detailed discussion:
// 1. Apply theta2 to the range of theta1.
for (Variable v : theta1.keySet()) {
composed.put(v, KB.subst(theta2, theta1.get(v)));
}
// 2. Adjoin to delta all pairs from tau with different
// domain variables.
for (Variable v : theta2.keySet()) {
if (!theta1.containsKey(v)) {
composed.put(v, theta2.get(v));
}
}
return cascadeSubstitutions(KB, composed);
}
// See:
// http://logic.stanford.edu/classes/cs157/2008/miscellaneous/faq.html#jump165
// for need for this.
private Map<Variable, Term> cascadeSubstitutions(FOLKnowledgeBase KB,
Map<Variable, Term> theta) {
for (Variable v : theta.keySet()) {
Term t = theta.get(v);
theta.put(v, KB.subst(theta, t));
}
return theta;
}
class BCAskAnswerHandler implements InferenceResult {
private List<Proof> proofs = new ArrayList<Proof>();
public BCAskAnswerHandler() {
}
//
// START-InferenceResult
public boolean isPossiblyFalse() {
return proofs.size() == 0;
}
public boolean isTrue() {
return proofs.size() > 0;
}
public boolean isUnknownDueToTimeout() {
return false;
}
public boolean isPartialResultDueToTimeout() {
return false;
}
public List<Proof> getProofs() {
return proofs;
}
// END-InferenceResult
//
public void setAllProofSteps(List<List<ProofStepBwChGoal>> allProofSteps) {
for (List<ProofStepBwChGoal> steps : allProofSteps) {
ProofStepBwChGoal lastStep = steps.get(steps.size() - 1);
Map<Variable, Term> theta = lastStep.getBindings();
proofs.add(new ProofFinal(lastStep, theta));
}
}
public void addProofStep(
List<List<ProofStepBwChGoal>> currentLevelProofSteps,
Clause toProve, Literal currentGoal,
Map<Variable, Term> bindings) {
if (currentLevelProofSteps.size() > 0) {
ProofStepBwChGoal predecessor = new ProofStepBwChGoal(toProve,
currentGoal, bindings);
for (List<ProofStepBwChGoal> steps : currentLevelProofSteps) {
if (steps.size() > 0) {
steps.get(0).setPredecessor(predecessor);
}
steps.add(0, predecessor);
}
}
}
}
}