00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 import java.util.Hashtable;
00031 import jpl.Query;
00032 import jpl.*;
00033
00034 public class Test
00035 {
00036 public static void
00037 main( java.lang.String argv[] )
00038 {
00039
00040
00041 run_tests();
00042 }
00043
00044 static void
00045 run_tests()
00046 {
00047 test_0();
00048 test_1();
00049 test_2();
00050 test_3();
00051 test_4();
00052 test_5();
00053 test_6();
00054 test_7();
00055 test_8();
00056 test_9();
00057 test_10();
00058 test_11();
00059
00060 test_101();
00061 }
00062
00063 static void
00064 test_0()
00065 {
00066 System.out.print( "test 0..." );
00067
00068 Query query =
00069 new Query("consult('test.pl')");
00070
00071 if ( !query.hasSolution() ){
00072 System.out.println( "consult('test.pl') failed" );
00073 System.exit( 1 );
00074 }
00075 System.out.println( "passed." );
00076 }
00077
00078 static Term a =
00079 new Atom( "a" );
00080 static Term b =
00081 new Atom( "b" );
00082 static Term f_a =
00083 new Compound(
00084 "f",
00085 new Term[] {a}
00086 );
00087 static Term pair_a_b =
00088 new Compound(
00089 "-",
00090 new Term[] {a,b}
00091 );
00092
00093 static void
00094 test_1()
00095 {
00096 System.out.print( "test 1..." );
00097 Query query =
00098 new Query("p(a)");
00099
00100 if ( !query.hasSolution() ){
00101 System.out.println( "p(a) failed" );
00102 System.exit( 1 );
00103 }
00104 System.out.println( "passed." );
00105 }
00106
00107 static void
00108 test_2()
00109 {
00110 System.out.print( "test 2..." );
00111 Query query =
00112 new Query(
00113 "p",
00114 new Term[] {f_a}
00115 );
00116
00117 if ( !query.hasSolution() ){
00118 System.out.println( "p(f(a)) failed" );
00119 System.exit( 1 );
00120 }
00121 System.out.println( "passed." );
00122 }
00123
00124 static void
00125 test_3()
00126 {
00127 System.out.print( "test 3..." );
00128 Query query =
00129 new Query(
00130 "p",
00131 new Term[] {pair_a_b}
00132 );
00133
00134 if ( !query.hasSolution() ){
00135 System.out.println( "p( a-b ) failed" );
00136 System.exit( 1 );
00137 }
00138 System.out.println( "passed." );
00139 }
00140
00141 static void
00142 test_4()
00143 {
00144 System.out.print( "test 4..." );
00145 Variable X = new Variable("X");
00146 Query query =
00147 new Query(
00148 "p",
00149 new Term[] {X}
00150 );
00151
00152 Term[] target = new Term[] {a,f_a,pair_a_b,new Variable("_")};
00153
00154 Hashtable[] solutions = query.allSolutions();
00155
00156 if ( solutions.length != 4 ){
00157 System.out.println( "p(X) failed:" );
00158 System.out.println( "\tExpected: 4 solutions" );
00159 System.out.println( "\tGot: " + solutions.length );
00160 System.exit( 1 );
00161 }
00162
00163 for ( int i = 0; i < solutions.length-1; ++i ){
00164 Term binding = (Term)solutions[i].get( "X" );
00165 if ( ! binding.equals( target[i] ) ){
00166 System.out.println( "p(X) failed" );
00167 System.out.println( "\tExpected: " + target[i]);
00168 System.out.println( "\tGot: " + binding);
00169 System.exit( 1 );
00170 }
00171 }
00172
00173 System.out.println( "passed." );
00174 }
00175
00176 static void
00177 test_5()
00178 {
00179 System.out.print( "test 5..." );
00180 Variable X = new Variable("X");
00181 Variable Y = new Variable("Y");
00182 Query query =
00183 new Query(
00184 "p",
00185 new Term[] {X,Y}
00186 );
00187
00188 Term[] x_target = new Term[] {a,a};
00189 Term[] y_target = new Term[] {a,b};
00190
00191 Hashtable[] solutions = query.allSolutions();
00192
00193 if ( solutions.length != 2 ){
00194 System.out.println( "p(X, Y) failed:" );
00195 System.out.println( "\tExpected: 2 solutions" );
00196 System.out.println( "\tGot: " + solutions.length );
00197 System.exit( 1 );
00198 }
00199
00200 for ( int i = 0; i < solutions.length; ++i ){
00201 Object x_binding = solutions[i].get("X");
00202 if ( ! x_binding.equals( x_target[i] ) ){
00203 System.out.println( "p(X, Y) failed:" );
00204 System.out.println( "\tExpected: " + x_target[i] );
00205 System.out.println( "\tGot: " + x_binding );
00206 System.exit( 1 );
00207 }
00208 Object y_binding = solutions[i].get("Y");
00209 if ( ! y_binding.equals( y_target[i] ) ){
00210 System.out.println( "p( X, Y ) failed:" );
00211 System.out.println( "\tExpected: " + y_target[i] );
00212 System.out.println( "\tGot: " + y_binding );
00213 System.exit( 1 );
00214 }
00215 }
00216 System.out.println( "passed." );
00217 }
00218
00219 static void
00220 test_6()
00221 {
00222 System.out.print( "test 6..." );
00223 Variable X = new Variable("X");
00224 Query query =
00225 new Query(
00226 "p",
00227 new Term[] {X,X}
00228 );
00229
00230 Term[] x_target = new Term[] {a};
00231
00232 Hashtable[] solutions = query.allSolutions();
00233
00234 if ( solutions.length != 1 ){
00235 System.out.println( "p(X, X) failed:" );
00236 System.out.println( "\tExpected: 1 solution" );
00237 System.out.println( "\tGot: " + solutions.length );
00238 System.exit( 1 );
00239 }
00240
00241 for ( int i = 0; i < solutions.length; ++i ){
00242 Object x_binding = solutions[i].get("X");
00243 if ( ! x_binding.equals( x_target[i] ) ){
00244 System.out.println( "p(X, X) failed:" );
00245 System.out.println( "\tExpected: " + x_target[i] );
00246 System.out.println( "\tGot: " + x_binding );
00247 System.exit( 1 );
00248 }
00249 }
00250 System.out.println( "passed." );
00251 }
00252
00253 static void
00254 test_7()
00255 {
00256 System.out.print( "test 7..." );
00257 Variable X = new Variable("X");
00258 Variable Y = new Variable("Y");
00259 Query query =
00260 new Query(
00261 "r",
00262 new Term[] {
00263 new Compound(
00264 "f",
00265 new Term[] {X,X}
00266 ),
00267 Y
00268 }
00269 );
00270
00271 Hashtable[] solutions = query.allSolutions();
00272
00273 if ( solutions.length != 2 ){
00274 System.out.println( "r(f(X,X), Y) failed:" );
00275 System.out.println( "\tExpected: 2 solutions" );
00276 System.out.println( "\tGot: " + solutions.length );
00277 System.exit( 1 );
00278 }
00279
00280 Object x_binding, y_binding;
00281
00282 x_binding = solutions[0].get("X");
00283 y_binding = solutions[0].get("Y");
00284 if ( x_binding != y_binding ){
00285 System.out.println( "r(f(X,X), Y) failed:" );
00286 System.out.println( Util.toString( solutions[0] ) );
00287 System.out.println( "\tThe variables to which X and Y are bound in the first solution should be identical." );
00288 System.exit( 1 );
00289 }
00290
00291 x_binding = solutions[1].get("X");
00292 y_binding = solutions[1].get("Y");
00293 if ( x_binding == y_binding ){
00294 System.out.println( "r(f(X,X), Y) failed:" );
00295 System.out.println( Util.toString( solutions[1] ) );
00296 System.out.println( "\tThe variables to which X and Y are bound in the second solution should be distinct." );
00297 System.exit( 1 );
00298 }
00299 if ( x_binding.equals( y_binding ) ){
00300 System.out.println( "r(f(X,X), Y) failed:" );
00301 System.out.println( Util.toString( solutions[1] ) );
00302 System.out.println( "\tThe variables to which X and Y are bound in the second solution should not be \"equal\"." );
00303 System.exit( 1 );
00304 }
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315 System.out.println( "passed." );
00316 }
00317
00318 static void
00319 test_8()
00320 {
00321 System.out.print( "test 8..." );
00322 Variable X = new Variable("X");
00323 Query query =
00324 new Query(
00325 "r",
00326 new Term[] {
00327 new Compound(
00328 "f",
00329 new Term[] {X,X}
00330 ),
00331 X
00332 }
00333 );
00334
00335 Hashtable[] solutions = query.allSolutions();
00336
00337 if ( solutions.length != 2 ){
00338 System.out.println( "r( f( X, X ), X ) failed:" );
00339 System.out.println( "\tExpected: 2 solutions" );
00340 System.out.println( "\tGot: " + solutions.length );
00341 System.exit( 1 );
00342 }
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353 System.out.println( "passed." );
00354 }
00355
00356
00357 static Term test_9_solution =
00358 Util.termArrayToList(
00359 new Term[] {
00360 new Compound( "-", new Term[] {a,a}),
00361 new Compound( "-", new Term[] {a,b})
00362 }
00363 );
00364
00365 static void
00366 test_9()
00367 {
00368 System.out.print( "test 9..." );
00369 Variable X = new Variable("X");
00370 Variable Y = new Variable("Y");
00371 Variable XYs = new Variable("XYs");
00372 Query query =
00373 new Query(
00374 "bagof",
00375 new Term[] {
00376 new Compound(
00377 "-",
00378 new Term[] {X,Y}
00379 ),
00380 new Compound(
00381 "p",
00382 new Term[] {X,Y}
00383 ),
00384 XYs
00385 }
00386 );
00387
00388 Hashtable[] solutions = query.allSolutions();
00389
00390 if ( solutions.length != 1 ){
00391 System.out.println( "bagof(X-Y, p(X,Y), XYs) failed:" );
00392 System.out.println( "\tExpected: 1 solution" );
00393 System.out.println( "\tGot: " + solutions.length );
00394 System.exit( 1 );
00395 }
00396
00397 Term term = (Term) solutions[0].get("XYs");
00398
00399
00400 if ( ! (term instanceof Compound && ".".equals(((Compound)term).name()) && ((Compound)term).arity()==2) ){
00401 System.out.println( "bagof(X-Y, p(X,Y), XYs) failed:" );
00402 System.out.println( "\tExpected: XYs to be a List" );
00403 System.out.println( "\tGot: " + term );
00404 System.exit( 1 );
00405 }
00406
00407 if ( ! term.equals( test_9_solution ) ){
00408 System.out.println( "bagof(X-Y, p(X,Y), XYs) failed:" );
00409 System.out.println( "\tExpected: " + test_9_solution );
00410 System.out.println( "\tGot: " + term );
00411 System.exit( 1 );
00412 }
00413
00414 System.out.println( "passed." );
00415 }
00416
00417 static void
00418 test_10()
00419 {
00420 System.out.print( "test 10..." );
00421 Query query =
00422 new Query( "t" );
00423
00424 try {
00425 boolean b = query.hasSolution();
00426 System.out.println( "t failed:" );
00427 System.out.println( "\tExpected: JPLException" );
00428 System.out.println( "\tGot: " + b );
00429 System.exit( 1 );
00430 } catch ( PrologException e ){
00431 }
00432
00433 System.out.println( "passed." );
00434 }
00435
00436 static void
00437 test_11()
00438 {
00439 System.out.print( "test 11..." );
00440 Term tuple =
00441 new Compound(
00442 "t",
00443 new Term[]{
00444 new Atom( "a" ),
00445 new Atom( "b" ),
00446 new Atom( "c" ),
00447 new Atom( "d" ),
00448 new Atom( "e" )
00449 }
00450 );
00451
00452 try {
00453 Variable X = new Variable("X");
00454 Query query = new Query( "tuple", new Term[] {X} );
00455
00456 java.util.Hashtable solution = query.oneSolution();
00457
00458 Term result = (Term) solution.get("X");
00459
00460 if ( result == null || ! result.equals( tuple ) ){
00461 System.out.println( "failed:" );
00462 System.out.println( "\tresult: " + result );
00463 System.out.println( "\ttuple: " + tuple );
00464 System.exit( 1 );
00465 }
00466
00467 Term term;
00468
00469 term = new Atom( "a" );
00470 if ( ((Compound)result).arg( 1 ) == null || !((Compound)result).arg( 1 ).equals( term ) ){
00471 System.out.println( "failed:" );
00472 System.out.println( "\t((Compound)result).arg( 1 ): " + ((Compound)result).arg( 1 ) );
00473 System.out.println( "\tterm : " + term );
00474 System.exit( 1 );
00475 }
00476 term = new Atom( "b" );
00477 if ( ((Compound)result).arg( 2 ) == null || !((Compound)result).arg( 2 ).equals( term ) ){
00478 System.out.println( "failed:" );
00479 System.out.println( "\t((Compound)result).arg( 2 ): " + ((Compound)result).arg( 2 ) );
00480 System.out.println( "\tterm : " + term );
00481 System.exit( 1 );
00482 }
00483 term = new Atom( "e" );
00484 if ( ((Compound)result).arg( 5 ) == null || !((Compound)result).arg( 5 ).equals( term ) ){
00485 System.out.println( "failed:" );
00486 System.out.println( "\t((Compound)result).arg( 5 ): " + ((Compound)result).arg( 5 ) );
00487 System.out.println( "\tterm : " + term );
00488 System.exit( 1 );
00489 }
00490
00491
00492
00493
00494
00495
00496
00497 } catch ( PrologException e ){
00498 System.out.println( "failed" );
00499 e.printStackTrace();
00500 System.exit( 1 );
00501 }
00502
00503 System.out.println( "passed." );
00504 }
00505
00506 static void
00507 test_101()
00508 {
00509 System.out.print( "test 101..." );
00510 Thread[] threads = new Thread[10];
00511
00512 for ( int i = 0; i < threads.length; ++i ){
00513 threads[i] = new QueryThread( i );
00514 }
00515 for ( int i = 0; i < threads.length; ++i ){
00516 threads[i].start();
00517 }
00518 for ( int i = 0; i < threads.length; ++i ){
00519 try {
00520 threads[i].join();
00521 } catch ( InterruptedException ie ){
00522 ie.printStackTrace();
00523 System.exit( 1 );
00524 }
00525 }
00526 System.out.println( "passed." );
00527 }
00528
00529 private static class
00530 QueryThread extends Thread
00531 {
00532 private int id_ = -1;
00533
00534 public
00535 QueryThread( int id )
00536 {
00537 this.id_ = id;
00538 }
00539
00540 public java.lang.String
00541 toString()
00542 {
00543 return "(QueryThread id=" + id_ + ")";
00544 }
00545
00546
00547 public void
00548 run()
00549 {
00550 Query query =
00551 new Query(
00552 "p",
00553 new Term[] {
00554 new Atom("a"),
00555 new Atom("a")
00556 }
00557 );
00558
00559 for ( int i = 0; i < 10; ++i ){
00560 try {
00561 query.hasSolution();
00562 } catch ( jpl.JPLException e ){
00563 System.out.println( "Threaded p( a, a ) threw exception: " + e);
00564 System.exit( 1 );
00565 }
00566 System.out.print( id_ );
00567 Thread.yield();
00568 }
00569 for ( int i = 0; i < 10; ++i ){
00570
00571 try {
00572 while ( query.hasMoreSolutions() ){
00573 Thread.yield();
00574 query.nextSolution();
00575 }
00576 } catch ( jpl.JPLException e ){
00577 System.out.println( "Threaded p( a, a ) threw exception: " + e);
00578 System.exit( 1 );
00579 }
00580 System.out.print( id_ );
00581
00582 }
00583 }
00584 }
00585
00586
00587
00588 }