-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLogicalExpression.java
167 lines (130 loc) · 5.33 KB
/
LogicalExpression.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
import java.util.*;
public class LogicalExpression {
private String uniqueSymbol = null; // null if sentence is a more complex expression
private String connective = null; // null if sentence is a _UNIQUE_ symbol
private Vector<LogicalExpression> subexpressions = null; // a vector of LogicalExpressions ( basically a vector of unique symbols and subexpressions )
//constructor
public LogicalExpression()
{
//these need to stay null if they're empty
//this.uniqueSymbol = "0";
//this.connective = "0";
this.subexpressions = new Vector<LogicalExpression>();
}
// another constructor that will ( or is supposed to ) create
// a new logical Expression, essentially making a copy
public LogicalExpression( LogicalExpression oldExpression ) {
if( oldExpression.getUniqueSymbol() == null) {
this.uniqueSymbol = oldExpression.getUniqueSymbol();
} else {
// create a new logical expression from the one passed to it
this.connective = oldExpression.getConnective();
// now get all of the subExpressions
// hint, enumerate over the subexpression vector of newExpression
for(
Enumeration<LogicalExpression> e = oldExpression.getSubexpressions().elements(); e.hasMoreElements(); ) {
LogicalExpression nextExpression = (LogicalExpression)e.nextElement();
this.subexpressions.add( nextExpression );
}
}
}
/* this method replaces _part_ of read_word()
* this method sets the symbol for the LogicalExpression
* it checks to make sure that it starts with one of the appropriate letters,
* then checks to make sure that the rest of the string is either digits or '_'
*/
public void setUniqueSymbol( String newSymbol )
{
boolean valid = true;
//remove the leading whitespace
newSymbol.trim();
if( this.uniqueSymbol != null )
{
System.out.println("setUniqueSymbol(): - this LE already has a unique symbol!!!" +
"\nswapping :->" + this.uniqueSymbol + "<- for ->" + newSymbol +"<-\n");
}
else if( valid )
{
this.uniqueSymbol = newSymbol;
//testing
//System.out.println(" setUniqueSymbol() - added-" + newSymbol + "- to the LogicalExpression! ");
}
}
/* this method replaces _part_ of read_word() from the example code
* it sets the connective for this LogicalExpression
*
* and returns the rest of the string to collect the symbols for it
*/
public String setConnective( String inputString ) {
String connect;
//testing
//System.out.println("setConnective() - beginning -" + inputString + "-");
// trim the whitespace at the beginning of the string if there is any
// there shouldn't be
inputString.trim();
// if the first character of the inputString is a '('
// - remove the ')' and the ')' and any whitespace after it.
if( inputString.startsWith("(") ) {
inputString = inputString.substring( inputString.indexOf('('), inputString.length() );
//trim the whitespace
inputString.trim();
}
//testing
//System.out.println("here: setConnective1- inputString:" + inputString + "--");
if( inputString.contains( " " ) ) {
// remove the connective out of the string
connect = inputString.substring( 0, inputString.indexOf( " " )) ;
inputString = inputString.substring( ( connect.length() + 1 ), inputString.length() );
//inputString.trim();
//testing
//System.out.println("I have the connective -" + connect + "- and i've got symbols -" + inputString + "-");
} else {
// just set to get checked and empty the inputString
// huh?
connect = inputString;
inputString = "";
}
// if connect is a proper connective
if ( connect.equalsIgnoreCase( "if" ) ||
connect.equalsIgnoreCase( "iff" ) ||
connect.equalsIgnoreCase( "and" ) ||
connect.equalsIgnoreCase("or") ||
connect.equalsIgnoreCase("xor") ||
connect.equalsIgnoreCase( "not" ) ) {
// ok, first word in the string is a valid connective
// set the connective
this.connective = connect;
//testing
//System.out.println( "setConnective(): I have just set the connective\n->" + connect + "<-\nand i'm returning\n->" + inputString + "<-" );
return inputString;
} else {
System.out.println( "unexpected character!!! : invalid connective!! - setConnective():-" + inputString );
LogicalExpression.exit_function( 0 );
}
// invalid connective - no clue who it would get here
System.out.println(" invalid connective! : setConnective:-" + inputString );
return inputString;
}
public void setSubexpression( LogicalExpression newSub ) {
this.subexpressions.add(newSub);
}
public void setSubexpressions( Vector<LogicalExpression> symbols ) {
this.subexpressions = symbols;
}
public String getUniqueSymbol(){
return this.uniqueSymbol;
}
public String getConnective() {
return this.connective;
}
public LogicalExpression getNextSubexpression() {
return this.subexpressions.lastElement();
}
public Vector<LogicalExpression> getSubexpressions() {
return this.subexpressions;
}
private static void exit_function(int value) {
System.out.println("exiting from LogicalExpression");
System.exit(value);
}
}