-
Notifications
You must be signed in to change notification settings - Fork 810
Expand file tree
/
Copy pathSentence.java
More file actions
241 lines (217 loc) · 6.82 KB
/
Copy pathSentence.java
File metadata and controls
241 lines (217 loc) · 6.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
package aima.core.logic.propositional.parsing.ast;
import java.util.Arrays;
import java.util.List;
import aima.core.logic.common.ParseTreeNode;
import aima.core.logic.propositional.parsing.PLVisitor;
import aima.core.util.Util;
/**
* Artificial Intelligence A Modern Approach (3rd Edition): page 244.<br>
* <br>
* The base of the knowledge representation language for propositional logic.
* Note: this class hierarchy defines the abstract syntax representation used
* for representing propositional logic.
*
* @author Ciaran O'Reilly
* @author Ravi Mohan
*
*/
public abstract class Sentence implements ParseTreeNode {
/**
*
* @return the logical connective associated with this sentence if it has
* one (i.e. is a ComplexSentence), null otherwise.
*/
public Connective getConnective() {
return null;
}
/**
*
* @return the number of simpler sentences contained in this sentence. Will
* only be > 0 if a Complex Sentence.
*/
public int getNumberSimplerSentences() {
return 0;
}
/**
* Get the simplified sentence, at the specified offset (starts at 0),
* contained by this Sentence if it is a Complex Sentence, null otherwise.
*
* @param offset
* the offset of the contained simplified sentence to retrieve.
* @return the simplified sentence, at the specified offset, contained by
* this sentence (if a complex sentence), null otherwise.
*/
public Sentence getSimplerSentence(int offset) {
return null;
}
/**
*
* @return true if a complex sentence with a Not connective, false
* otherwise.
*/
public boolean isNotSentence() {
return hasConnective(Connective.NOT);
}
/**
*
* @return true if a complex sentence with an And connective, false
* otherwise.
*/
public boolean isAndSentence() {
return hasConnective(Connective.AND);
}
/**
*
* @return true if a complex sentence with an Or connective, false
* otherwise.
*/
public boolean isOrSentence() {
return hasConnective(Connective.OR);
}
/**
*
* @return true if a complex sentence with an Implication connective, false
* otherwise.
*/
public boolean isImplicationSentence() {
return hasConnective(Connective.IMPLICATION);
}
/**
*
* @return true if a complex sentence with a Biconditional connective, false
* otherwise.
*/
public boolean isBiconditionalSentence() {
return hasConnective(Connective.BICONDITIONAL);
}
/**
*
* @return true if a proposition symbol, false otherwise.
*/
public boolean isPropositionSymbol() {
return getConnective() == null;
}
/**
*
* @return true if a complex sentence containing a single simpler sentence,
* false otherwise.
*/
public boolean isUnarySentence() {
return hasConnective(Connective.NOT);
}
/**
*
* @return true if a complex sentence containing two simpler sentences,
* false otherwise.
*/
public boolean isBinarySentence() {
return getConnective() != null && !hasConnective(Connective.NOT);
}
/**
* Allow a PLVisitor to walk over the abstract syntax tree represented by this
* Sentence.
*
* @param plv
* a Propositional Logic visitor.
* @param arg
* an optional argument for use by the visior.
* @return a result specific to the visitors behavior.
*/
public <A, R> R accept(PLVisitor<A, R> plv, A arg) {
R result = null;
if (isPropositionSymbol()) {
result = plv.visitPropositionSymbol((PropositionSymbol) this, arg);
} else if (isUnarySentence()) {
result = plv.visitUnarySentence((ComplexSentence) this, arg);
} else if (isBinarySentence()) {
result = plv.visitBinarySentence((ComplexSentence) this, arg);
}
return result;
}
/**
* Utility routine that will create a string representation of a given
* Sentence and place it inside brackets if it is a complex sentence that
* has lower precedence than this complex sentence.<br>
* <br>
* Note: this is a form of pretty printing, whereby we only add brackets in
* the concrete syntax representation as needed to ensure it can be parsed
* back again into an equivalent abstract syntax representation used here.
*
* @param parentConnective
* the connective of the parent sentence.
* @param childSentence
* a simpler child sentence.
* @return a String representation of the Sentence, bracketed if the parent
* based on its connective has higher precedence.
*/
public String bracketSentenceIfNecessary(Connective parentConnective,
Sentence childSentence) {
String result = null;
if (childSentence instanceof ComplexSentence) {
ComplexSentence cs = (ComplexSentence) childSentence;
if (cs.getConnective().getPrecedence() < parentConnective
.getPrecedence()) {
result = "(" + childSentence + ")";
}
}
if (result == null) {
result = childSentence.toString();
}
return result;
}
/**
* Create a disjunction of disjuncts.
* @param disjuncts
* the disjuncts from which to create the disjunction.
* @return a disjunction of the given disjuncts.
*/
public static Sentence newDisjunction(Sentence... disjuncts) {
return newDisjunction(Arrays.asList(disjuncts));
}
/**
* Create a disjunction of disjuncts.
* @param disjuncts
* the disjuncts from which to create the disjunction.
* @return a disjunction of the given disjuncts.
*/
public static Sentence newDisjunction(List<? extends Sentence> disjuncts) {
if (disjuncts.size() == 0) {
return PropositionSymbol.FALSE;
}
else if (disjuncts.size() == 1) {
return disjuncts.get(0);
}
return new ComplexSentence(Util.first(disjuncts), Connective.OR, newDisjunction(Util.rest(disjuncts)));
}
/**
* Create a conjunction of conjuncts.
* @param conjuncts
* the conjuncts from which to create the conjunction.
* @return a conjunction of the given conjuncts.
*/
public static Sentence newConjunction(Sentence... conjuncts) {
return newConjunction(Arrays.asList(conjuncts));
}
/**
* Create a conjunction of conjuncts.
* @param conjuncts
* the conjuncts from which to create the conjunction.
* @return a conjunction of the given conjuncts.
*/
public static Sentence newConjunction(List<? extends Sentence> conjuncts) {
if (conjuncts.size() == 0) {
return PropositionSymbol.TRUE;
}
else if (conjuncts.size() == 1) {
return conjuncts.get(0);
}
return new ComplexSentence(Util.first(conjuncts), Connective.AND, newConjunction(Util.rest(conjuncts)));
}
//
// PROTECTED
//
protected boolean hasConnective(Connective connective) {
// Note: can use '==' as Connective is an enum.
return getConnective() == connective;
}
}