001: package ca.draisey.free.tprolog;
002:
003:
004:
005:
006:
007:
008:
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:
020: public final void abolishDatabase()
021: {
022:
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:
031: ( (DefaultMutableTreeNode) dictionary.get( key ) ).add(
032: new DefaultMutableTreeNode( rulez, false )
033: );
034: }
035: else {
036:
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:
045:
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:
078: abstract class GUISuccession extends ListableSuccession {
079:
080: abstract void out( int s, DefaultMutableTreeNode snode );
081: }
082:
083:
084:
085: protected java.util.Set< String > dictionaryKeySet()
086: {
087: return dictionary.keySet();
088: }
089:
090: public final DefaultMutableTreeNode viewDatabase()
091: {
092: return dictroot;
093: }
094:
095: public final DefaultMutableTreeNode viewDatabase( final String key )
096: {
097: return dictionary.get( key );
098: }
099:
100:
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();
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:
117: private final java.util.Map< String, DefaultMutableTreeNode > dictionary = new java.util.HashMap< String, DefaultMutableTreeNode >();
118:
119:
120: private final DefaultMutableTreeNode dictroot = new DefaultMutableTreeNode();
121: }
122:
123:
Prolog in Java
A trivial prolog interpreter written in java.
Copyright 2004, M.E.J.Draisey
This file is part of tprologinjava.
tprologinjava is free software; you can redistribute it and/or modify it under
the terms of the GNU General
Public License as published by the Free
Software Foundation; either version 2 of the License, or (at your option)
any later version.
tprologinjava is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with tprologinjava; if not, write to the Free Software Foundation, Inc.,
59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Formatted with
GNU source-highlight:
http:// www.gnu.org/ software/ src-highlite