/
ComplexSentence.java
147 lines (132 loc) · 4.03 KB
/
ComplexSentence.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
package aima.core.logic.propositional.parsing.ast;
/**
* Artificial Intelligence A Modern Approach (3rd Edition): page 244.<br>
* <br>
* <b>Complex Sentence:</b> are constructed from simpler sentences, using
* parentheses (and square brackets) and logical connectives.
*
* @author Ciaran O'Reilly
* @author Ravi Mohan
*/
public class ComplexSentence extends Sentence {
private Connective connective;
private Sentence[] simplerSentences;
// Lazy initialize these values.
private int cachedHashCode = -1;
private String cachedConcreteSyntax = null;
/**
* Constructor.
*
* @param connective
* the complex sentence's connective.
* @param sentences
* the simpler sentences making up the complex sentence.
*/
public ComplexSentence(Connective connective, Sentence... sentences) {
// Assertion checks
assertLegalArguments(connective, sentences);
this.connective = connective;
simplerSentences = new Sentence[sentences.length];
System.arraycopy(sentences, 0, simplerSentences, 0, sentences.length);
}
/**
* Convenience constructor for binary sentences.
*
* @param sentenceL
* the left hand sentence.
* @param binaryConnective
* the binary connective.
* @param sentenceR
* the right hand sentence.
*/
public ComplexSentence(Sentence sentenceL, Connective binaryConnective, Sentence sentenceR) {
this(binaryConnective, sentenceL, sentenceR);
}
@Override
public Connective getConnective() {
return connective;
}
@Override
public int getNumberSimplerSentences() {
return simplerSentences.length;
}
@Override
public Sentence getSimplerSentence(int offset) {
return simplerSentences[offset];
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if ((o == null) || (this.getClass() != o.getClass())) {
return false;
}
boolean result = false;
ComplexSentence other = (ComplexSentence) o;
if (other.hashCode() == this.hashCode()) {
if (other.getConnective().equals(this.getConnective())
&& other.getNumberSimplerSentences() == this
.getNumberSimplerSentences()) {
// connective and # of simpler sentences match
// assume match and then test each simpler sentence
result = true;
for (int i = 0; i < this.getNumberSimplerSentences(); i++) {
if (!other.getSimplerSentence(i).equals(
this.getSimplerSentence(i))) {
result = false;
break;
}
}
}
}
return result;
}
@Override
public int hashCode() {
if (cachedHashCode == -1) {
cachedHashCode = 17 * getConnective().hashCode();
for (Sentence s : simplerSentences) {
cachedHashCode = (cachedHashCode * 37) + s.hashCode();
}
}
return cachedHashCode;
}
@Override
public String toString() {
if (cachedConcreteSyntax == null) {
if (isUnarySentence()) {
cachedConcreteSyntax = getConnective()
+ bracketSentenceIfNecessary(getConnective(), getSimplerSentence(0));
} else if (isBinarySentence()) {
cachedConcreteSyntax = bracketSentenceIfNecessary(getConnective(), getSimplerSentence(0))
+ " "
+ getConnective()
+ " "
+ bracketSentenceIfNecessary(getConnective(), getSimplerSentence(1));
}
}
return cachedConcreteSyntax;
}
//
// PRIVATE
//
private void assertLegalArguments(Connective connective, Sentence...sentences) {
if (connective == null) {
throw new IllegalArgumentException("Connective must be specified.");
}
if (sentences == null) {
throw new IllegalArgumentException("> 0 simpler sentences must be specified.");
}
if (connective == Connective.NOT) {
if (sentences.length != 1) {
throw new IllegalArgumentException("A not (~) complex sentence only take 1 simpler sentence not "+sentences.length);
}
}
else {
if (sentences.length != 2) {
throw new IllegalArgumentException("Connective is binary ("+connective+") but only "+sentences.length + " simpler sentences provided");
}
}
}
}