Trivial Prolog in Java

001: package ca.draisey.free.tprolog;
002:
003:
004:
005:
006:
007: // I've added a vary simple GUI to this just for the practice.
008: // That, and I wanted to try out swing!
009:
010:
011:
012:
013:
014: import javax.swing.tree.TreeNode;
015: import javax.swing.tree.MutableTreeNode;
016: import javax.swing.tree.DefaultMutableTreeNode;
017:
018: final class GUIDatabase extends ListableDatabase implements Consultable {
019:         // Consultable methods
020:         public final void abolishDatabase()
021:         {
022:                 // clear dictionary AND viewable dictionary tree
023:                 dictionary.clear();
024:                 dictroot.removeAllChildren();
025:         }
026:         public final void appendzDatabase( final Sentence rulez )
027:         {
028:                 final String key = rulez.getkey();
029:                 if( dictionary.containsKey( key ) ) {
030:                         // append to end of existing rules for key
031:                         ( (DefaultMutableTreeNode) dictionary.get( key ) ).add(
032:                                 new DefaultMutableTreeNode( rulez, false )
033:                         );
034:                 }
035:                 else {
036:                         // add new entry to dictionary AND to viewable dictionary tree
037:                         final DefaultMutableTreeNode entrynode = new DefaultMutableTreeNode( key );
038:                         entrynode.add( new DefaultMutableTreeNode( rulez, false ) );
039:                         dictionary.put( key, entrynode );
040:                         dictroot.add( entrynode );
041:                 }
042:         }
043:
044:         // view methods
045:         // view result of query
046:         public final void viewableQuery( final Sentence query, final GUISuccession gui )
047:         {
048:                 final Term goal = query.dequantifyTerm();
049:                 final Variables goals = query.instantiateVariables();
050:                 try {
051:                         queryDatabase( goal, goals, gui.new SuccessionSuccessor() {
052:                                 int successCount = 0;
053:                                 void succeed()
054:                                 {
055:                                         ++successCount;
056:                                         final DefaultMutableTreeNode successNode = new DefaultMutableTreeNode( "SUCCEED " + successCount );
057:                                         for( final java.util.Iterator i = query.iterListedVariables( goals, gui ); i.hasNext(); ) {
058:                                                 successNode.add( new DefaultMutableTreeNode( i.next(), false ) );
059:                                         }
060:                                         gui.out( successCount, successNode );
061:                                         gui.next();
062:                                 }
063:                         } );
064:                         gui.out( 0, new DefaultMutableTreeNode( "FAIL", false ) );
065:                 }
066:                 catch( MonitorException x ) {
067:                         gui.out( 0, new DefaultMutableTreeNode( "ABORT", false ) );
068:                 }
069:                 catch( StackOverflowError x ) {
070:                         gui.out( 0, new DefaultMutableTreeNode( "OVERFLOW", false ) );
071:                 }
072:                 catch( OutOfMemoryError x ) {
073:                         gui.out( 0, new DefaultMutableTreeNode( "OUT-OF-MEMORY", false ) );
074:                 }
075:         }
076:
077:         // -- the required hook for an asynchronous print to GUI tree stream type function --
078:         abstract class GUISuccession extends ListableSuccession {
079:                 // return the goods
080:                 abstract void out( int s, DefaultMutableTreeNode snode );
081:         }
082:
083:         // view database methods
084:         // keys to dictionary entry object
085:     protected java.util.Set< String > dictionaryKeySet()
086:         {
087:                 return dictionary.keySet();
088:         }
089:         // view all the definitions in a viewable dictionary tree       
090:         public final DefaultMutableTreeNode viewDatabase()
091:         {
092:                 return dictroot;
093:         }
094:         // view the entry tree for a single key
095:         public final DefaultMutableTreeNode viewDatabase( final String key )
096:         {
097:                 return dictionary.get( key );
098:         }
099:
100:         // protected private helper method to access the container
101:         final java.util.Iterator< Sentence > matching( final String key )
102:         {
103:                 final MutableTreeNode matches = (MutableTreeNode) dictionary.get( key );
104:                 if( matches != null ) return new java.util.Iterator< Sentence >() {
105:                         java.util.Enumeration i = matches.children();   // inner Enumeration
106:                         public boolean hasNext() { return i.hasMoreElements(); }
107:                         public Sentence next()
108:                         {
109:                                 return (Sentence) ( (DefaultMutableTreeNode) i.nextElement() ).getUserObject();
110:                         }
111:                         public void remove() { throw new UnsupportedOperationException(); }
112:                 };
113:                 else return NOTMATCHING;
114:         }
115:
116:         // instance map of the key to dictionary entry object
117:         private final java.util.Map< String, DefaultMutableTreeNode > dictionary = new java.util.HashMap< String, DefaultMutableTreeNode >();
118:
119:         // the viewable tree instance -- we inherit the dictionary map from ListableDatabase
120:         private final DefaultMutableTreeNode dictroot = new DefaultMutableTreeNode();
121: }
122:
123: // fin