Trivial Prolog in Java

01: package ca.draisey.free.tprolog;
02:
03:
04:
05:
06:
07: // -- the main wrapper --
08: public final class PrologCLI {
09:         public static void main( final String[] argv )
10:         {
11:                 // allocate the database singleton
12:                 final Database database = new Database();
13:
14:                 // the CLI
15:                 final java.io.PrintWriter pout = new java.io.PrintWriter( System.out, true );
16:                 final java.io.Reader pin = new java.io.InputStreamReader( System.in );
17:                 try {
18:                         Sentence query;
19:                         try {
20:                                 query = Parse.goal( new java.io.FileReader( argv[0] ) );
21:                         }
22:                         catch( java.io.FileNotFoundException x ) {
23:                                 query = Parse.goal( new java.io.StringReader( argv[0] ) );
24:                         }
25:
26:                         pout.println( "***GOAL***" );
27:                         pout.println( query.toString() );
28:
29:                         Parse.database( new java.io.FileReader( argv[1] ), database );
30:
31:                         pout.println( "***DATABASE***" );
32:                         database.listDatabase( pout );
33:                         pout.println( "***END***" );
34:
35:                         database.listQuery( query, pout, database.new ListableSuccession() {
36:                                 void next()
37:                                 {
38:                                         try {
39:                                                 pout.print( "..." );
40:                                                 pout.flush();
41:                                                 // block until user input
42:                                                 // if user input '.' then abort
43:                                                 if( pin.read() == '.' ) throw new MonitorException();
44:                                                 while( pin.ready() ) pin.read(); // flush
45:                                         }
46:                                         catch( java.io.IOException x ) {
47:                                         }
48:                                         finally {
49:                                                 pout.println();
50:                                                 pout.flush();
51:                                         }
52:                                 }
53:                         } );
54:                 }
55:                 catch( java.io.FileNotFoundException x ) {
56:                         pout.println( "***FILE NOT FOUND***" );
57:                 }
58:                 finally {
59:                         pout.flush();
60:                 }
61:         }
62: }
63:
64: // fin