/
KnowledgeBase.java
135 lines (117 loc) · 3.58 KB
/
KnowledgeBase.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
package aima.core.logic.propositional.kb;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import aima.core.logic.propositional.inference.TTEntails;
import aima.core.logic.propositional.kb.data.Clause;
import aima.core.logic.propositional.kb.data.ConjunctionOfClauses;
import aima.core.logic.propositional.parsing.PLParser;
import aima.core.logic.propositional.parsing.ast.PropositionSymbol;
import aima.core.logic.propositional.parsing.ast.Sentence;
import aima.core.logic.propositional.transformations.ConvertToConjunctionOfClauses;
import aima.core.logic.propositional.transformations.SymbolCollector;
/**
* @author Ravi Mohan
* @author Mike Stampone
*/
public class KnowledgeBase {
private List<Sentence> sentences = new ArrayList<>();
private ConjunctionOfClauses asCNF = new ConjunctionOfClauses(Collections.emptySet());
private Set<PropositionSymbol> symbols = new LinkedHashSet<>();
private PLParser parser = new PLParser();
/**
* Adds the specified sentence to the knowledge base.
*
* @param aSentence
* a fact to be added to the knowledge base.
*/
public void tell(String aSentence) {
tell((Sentence) parser.parse(aSentence));
}
/**
* Adds the specified sentence to the knowledge base.
*
* @param aSentence
* a fact to be added to the knowledge base.
*/
public void tell(Sentence aSentence) {
if (!(sentences.contains(aSentence))) {
sentences.add(aSentence);
asCNF = asCNF.extend(ConvertToConjunctionOfClauses.apply(aSentence).getClauses());
symbols.addAll(SymbolCollector.getSymbolsFrom(aSentence));
}
}
/**
* Each time the agent program is called, it TELLS the knowledge base what
* it perceives.
*
* @param percepts
* what the agent perceives
*/
public void tellAll(String[] percepts) {
for (String percept : percepts) {
tell(percept);
}
}
/**
* Returns the number of sentences in the knowledge base.
*
* @return the number of sentences in the knowledge base.
*/
public int size() {
return sentences.size();
}
/**
* Returns the list of sentences in the knowledge base chained together as a
* single sentence.
*
* @return the list of sentences in the knowledge base chained together as a
* single sentence.
*/
public Sentence asSentence() {
return Sentence.newConjunction(sentences);
}
/**
*
* @return a Conjunctive Normal Form (CNF) representation of the Knowledge Base.
*/
public Set<Clause> asCNF() {
return asCNF.getClauses();
}
/**
*
* @return a unique set of the symbols currently contained in the Knowledge Base.
*/
public Set<PropositionSymbol> getSymbols() {
return symbols;
}
/**
* Returns the answer to the specified question using the TT-Entails
* algorithm.
*
* @param queryString
* a question to ASK the knowledge base
*
* @return the answer to the specified question using the TT-Entails
* algorithm.
*/
public boolean askWithTTEntails(String queryString) {
PLParser parser = new PLParser();
Sentence alpha = parser.parse(queryString);
return new TTEntails().isEntailed(this, alpha);
}
@Override
public String toString() {
return sentences.isEmpty() ? "" : asSentence().toString();
}
/**
* Returns the list of sentences in the knowledge base.
*
* @return the list of sentences in the knowledge base.
*/
public List<Sentence> getSentences() {
return sentences;
}
}