00001
00002
00003
00004
00005
00006
00007
00008
00009 abstract class LogicTree
00010 {
00011
00012
00013
00014
00015 static LogicTree C0 = new False();
00016
00017
00018
00019
00020
00021
00022 static LogicTree C1 = new True();
00023
00024
00025
00026
00027
00028
00029
00030 static String additionalIndentation = " ";
00031
00032
00033
00034
00035
00036
00037 public String toString()
00038 {
00039 StringBuffer buffer = new StringBuffer();
00040 toStringBuffer("", buffer);
00041 return buffer.toString();
00042 }
00043
00044
00045
00046
00047
00048
00049 abstract public void toStringBuffer(String indentation, StringBuffer buffer);
00050
00051
00052
00053
00054
00055
00056
00057 public boolean isFailure()
00058 {
00059 return this instanceof Failure;
00060 }
00061
00062
00063
00064
00065
00066
00067 public static LogicTree iff(LogicTree leftSubtree, LogicTree rightSubtree)
00068 {
00069 return new Iff(leftSubtree, rightSubtree);
00070 }
00071
00072
00073
00074
00075
00076
00077 public static LogicTree implies(LogicTree leftSubtree, LogicTree rightSubtree)
00078 {
00079 return new Implies(leftSubtree, rightSubtree);
00080 }
00081
00082
00083
00084
00085
00086
00087 public static LogicTree or(LogicTree leftSubtree, LogicTree rightSubtree)
00088 {
00089 return new Or(leftSubtree, rightSubtree);
00090 }
00091
00092
00093
00094
00095
00096
00097 public static LogicTree and(LogicTree leftSubtree, LogicTree rightSubtree)
00098 {
00099 return new And(leftSubtree, rightSubtree);
00100 }
00101
00102
00103
00104
00105
00106
00107 public static LogicTree not(LogicTree subtree)
00108 {
00109 return new Not(subtree);
00110 }
00111
00112
00113
00114
00115
00116
00117 public static LogicTree var(char name)
00118 {
00119 return new Var(name);
00120 }
00121
00122
00123
00124
00125
00126
00127 public static LogicTree failure(String cause)
00128 {
00129 return new Failure(cause);
00130 }
00131
00132
00133
00134
00135
00136
00137 public static void main(String arg[])
00138 {
00139 System.out.println("Tree for: (a+b)' = a'*b'");
00140 System.out.println(
00141 iff(
00142 not(or(var('a'), var('b'))),
00143 and(not(var('a')), not(var('b')))
00144 ) );
00145
00146 System.out.println("Tree for: c > 1");
00147 System.out.println(
00148 implies(var('c'), C1)
00149 );
00150
00151 System.out.println("Tree for: 0 > d");
00152 System.out.println(
00153 implies(C0, var('d'))
00154 );
00155
00156 System.out.println("Tree for: a + a'*b = a + b");
00157 System.out.println(
00158 iff(
00159 or(var('a'), and(not(var('a')), var('b'))),
00160 or(var('a'), var('b')))
00161 );
00162 }
00163 }
00164
00165
00166
00167
00168
00169
00170 class BinaryTree extends LogicTree
00171 {
00172
00173
00174
00175
00176 String op;
00177
00178
00179
00180
00181
00182 LogicTree leftSubtree;
00183
00184
00185
00186
00187
00188
00189 LogicTree rightSubtree;
00190
00191
00192
00193
00194
00195
00196 BinaryTree(String op, LogicTree leftSubtree, LogicTree rightSubtree)
00197 {
00198 this.op = op;
00199 this.leftSubtree = leftSubtree;
00200 this.rightSubtree = rightSubtree;
00201 }
00202
00203 public void toStringBuffer(String indentation, StringBuffer buffer)
00204 {
00205 buffer.append(indentation);
00206 buffer.append(op);
00207 buffer.append("\n");
00208 leftSubtree.toStringBuffer(indentation + additionalIndentation, buffer);
00209 rightSubtree.toStringBuffer(indentation + additionalIndentation, buffer);
00210 }
00211 }
00212
00213
00214
00215
00216
00217
00218 class UnaryTree extends LogicTree
00219 {
00220
00221
00222
00223
00224 String op;
00225
00226
00227
00228
00229
00230
00231 LogicTree subtree;
00232
00233
00234
00235
00236
00237
00238 UnaryTree(String op, LogicTree subtree)
00239 {
00240 this.op = op;
00241 this.subtree = subtree;
00242 }
00243
00244 public void toStringBuffer(String indentation, StringBuffer buffer)
00245 {
00246 buffer.append(indentation);
00247 buffer.append(op);
00248 buffer.append("\n");
00249 subtree.toStringBuffer(indentation + additionalIndentation, buffer);
00250 }
00251 }
00252
00253
00254
00255
00256
00257 class Unit extends LogicTree
00258 {
00259
00260
00261
00262
00263 String value;
00264
00265
00266
00267
00268
00269
00270 Unit(String value)
00271 {
00272 this.value = value;
00273 }
00274
00275 public void toStringBuffer(String indentation, StringBuffer buffer)
00276 {
00277 buffer.append(indentation);
00278 buffer.append(value);
00279 buffer.append("\n");
00280 }
00281 }
00282
00283
00284
00285
00286
00287
00288
00289 class Failure extends LogicTree
00290 {
00291
00292
00293
00294
00295 String cause;
00296
00297
00298
00299
00300
00301
00302 Failure(String cause)
00303 {
00304 this.cause = cause;
00305 }
00306
00307 public void toStringBuffer(String indentation, StringBuffer buffer)
00308 {
00309 buffer.append(indentation);
00310 buffer.append("*** error: " + cause);
00311 buffer.append("\n");
00312 }
00313 }
00314
00315
00316
00317
00318
00319
00320 class Iff extends BinaryTree
00321 {
00322 Iff(LogicTree leftSubtree, LogicTree rightSubtree)
00323 {
00324 super("=", leftSubtree, rightSubtree);
00325 }
00326 }
00327
00328
00329
00330
00331
00332
00333 class Implies extends BinaryTree
00334 {
00335 Implies(LogicTree leftSubtree, LogicTree rightSubtree)
00336 {
00337 super(">", leftSubtree, rightSubtree);
00338 }
00339 }
00340
00341
00342
00343
00344
00345
00346 class Or extends BinaryTree
00347 {
00348 Or(LogicTree leftSubtree, LogicTree rightSubtree)
00349 {
00350 super("+", leftSubtree, rightSubtree);
00351 }
00352 }
00353
00354
00355
00356
00357
00358
00359 class And extends BinaryTree
00360 {
00361 And(LogicTree leftSubtree, LogicTree rightSubtree)
00362 {
00363 super("*", leftSubtree, rightSubtree);
00364 }
00365 }
00366
00367
00368
00369
00370
00371
00372 class Not extends UnaryTree
00373 {
00374 Not(LogicTree subtree)
00375 {
00376 super("'", subtree);
00377 }
00378 }
00379
00380
00381
00382
00383
00384
00385
00386 class False extends Unit
00387 {
00388 False()
00389 {
00390 super("0");
00391 }
00392 }
00393
00394
00395
00396
00397
00398
00399 class True extends Unit
00400 {
00401 True()
00402 {
00403 super("1");
00404 }
00405 }
00406
00407
00408
00409
00410
00411
00412 class Var extends Unit
00413 {
00414 Var(char name)
00415 {
00416 super("" + name);
00417 }
00418 }
00419