Re: Java front-end for PowerLoom?
Andrew n marshall <[email protected]>
| Newsgroups | gmane.comp.ai.powerloom |
|---|---|
| Message-ID | <[email protected]> |
I did this for a small Powerloom text adventure, using it as an in-game debugging window. The files I'm including are ripped out of another project, so I'm not sure they work 100%. At least its a start. Anm [email protected] wrote: > I am exploring various options for getting up and running with Stella & > PowerLoom. I don't ask much of a usable interactive facility, and am > satisfied just to be able to pop up a REPL (read-eval-print loop) window > if nothing else. This is enough to give a great deal of access to the > internal state of an application. So all of the CL features are > wonderful, but at the moment I can't devote a lot of time to getting up > and running with Stella & PowerLoom in a Linux environment. > > > > Has anyone tried writing a simple facility of this sort for the Java > version of Stella? Since Stella does have some debug/trace capabilities > of its own (the goal/subgoal-trace feature, at least), should it not be > pretty straightforward to include these features in a Java-only > implementation, rather than relying on the underlying Lisp environment > for that purpose? > > > > Please share any thoughts along these lines... > > > > Kyle > > > > > ------------------------------------------------------------------------ > > _______________________________________________ > powerloom-forum mailing list > [email protected] > http://mailman.isi.edu/mailman/listinfo/powerloom-forum _______________________________________________ powerloom-forum mailing list [email protected] http://mailman.isi.edu/mailman/listinfo/powerloom-forum
PowerLoomUtilities.java
(text/plain, 2.2 KB)
/*
* Created on Oct 24, 2003
*/
package ips;
import java.io.*;
import us.ca.la.anm.io.IndentedWriter;
import edu.isi.powerloom.PlIterator;
import edu.isi.powerloom.logic.*;
import edu.isi.stella.Cons;
import edu.isi.stella.Stella;
import edu.isi.stella.Stella_Object;
import edu.isi.stella.Vector;
/**
* @author amarshal
*/
public class PowerLoomUtilities {
public static void printCons( Cons cons ) {
PrintWriter pw = new PrintWriter( System.out );
printCons( cons, pw );
pw.close();
}
public static void printCons( Cons cons, PrintWriter out ) {
if( cons == null || cons == Stella.NIL ) {
out.println( cons );
return;
}
PrintWriter nextOut = new IndentedWriter( out, 2 );
out.print( "( " );
while( cons != Stella.NIL ) {
print( cons.value, nextOut );
cons = cons.rest;
}
nextOut.close();
out.println( ")" );
}
public static void printProposition( Proposition prop ) {
PrintWriter pw = new PrintWriter( System.out );
printProposition( prop, pw );
pw.close();
}
public static void printProposition( Proposition prop, PrintWriter out ) {
Vector args = prop.arguments;
if( args == null || args.arraySize == 0 ) {
out.println( prop.operator );
return;
}
PrintWriter nextOut = new IndentedWriter( out, 2 );
out.println( "( Proposition"+prop.kind+" "+prop.operator );
for( int i=0; i<args.length(); i++)
print( args.nth(i), nextOut );
nextOut.close();
out.println( ")" );
}
public static void print( Stella_Object obj ) {
PrintWriter pw = new PrintWriter( System.out );
print( obj, pw );
pw.flush();
}
public static void print( Stella_Object obj, PrintWriter out ) {
if( obj instanceof PlIterator )
printCons( ((PlIterator)obj).consify(), out );
if( obj instanceof Cons )
printCons( (Cons) obj, out );
else if( obj instanceof Proposition )
printProposition( (Proposition) obj, out );
else
out.println( "("+obj.getClass().getName()+") "+obj );
}
}
PowerloomInterpreter.java
(text/plain, 2.6 KB)
/*
* Created on Oct 23, 2003
*/
package ips;
import java.lang.*;
import java.util.*;
import edu.isi.powerloom.*;
import edu.isi.powerloom.logic.*;
import edu.isi.stella.*;
import edu.isi.stella.javalib.*;
/**
* @author amarshal
*/
public class PowerloomInterpreter implements CommandInterpreter {
private static final boolean DEBUG_OUTPUT_CLASS = true;
protected String module = null;
protected final Map opToReturnClass;
public PowerloomInterpreter() {
if( DEBUG_OUTPUT_CLASS )
opToReturnClass = new HashMap();
else
opToReturnClass = null;
}
public String getCurrentModule() {
return module;
}
public void setCurrentModule( String module ) {
this.module = module;
}
/**
* @see adventure.CommandInterpreter#interpret(java.lang.String, adventure.Console)
*/
public void interpret(String command, Console console) {
String op = command; // to avoid NullPointerExceptions (however unliekly)
StringTokenizer st = new StringTokenizer( command );
if( st.hasMoreTokens() )
op = st.nextToken().toLowerCase(); // lowercase form of first command word
Stella_Object so = PLI.sEvaluate( command, getCurrentModule(), null );
if( DEBUG_OUTPUT_CLASS && so != null ) {
Object returnClass = opToReturnClass.get( op );
if( returnClass == null ) {
returnClass = so.getClass();
opToReturnClass.put( op, returnClass );
System.out.println( "Operator \""+op+"\" returned a "+returnClass );
}
}
// Change Module...
if( ( op.equalsIgnoreCase("(in-module") ||
op.equalsIgnoreCase("(cc") ) &&
so instanceof Module )
setCurrentModule( PLI.objectToParsableString( (Module) Stella.$MODULE$.get() ) );
// Specialized Output formats...
if( op.equalsIgnoreCase("(all-facts-of") &&
so instanceof Cons ) {
Cons cons = (Cons) so;
if( cons == Stella.NIL ) {
console.println( cons );
return;
}
console.print( "( "+cons.value );
while( cons.rest != Stella.NIL ) {
console.println();
cons = cons.rest;
console.print( " "+cons.value );
}
console.println( " ) " );
} else {
// General output format
console.println( Native.stringify(so) );
}
}
}
Console.java
(text/plain, 3.7 KB)
/*
* Created on Oct 23, 2003
*/
package ips;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**
* @author amarshal
*/
public class Console extends JPanel {
static boolean DEBUG_SCROLL = false;
private static final int HISTORY_SIZE = 50;
protected final JScrollPane scroll = new JScrollPane();
protected final JTextArea output = new JTextArea();
protected final JTextField input = new JTextField();
protected CommandInterpreter ci;
protected String[] history = new String[HISTORY_SIZE];
protected int histTop = 0;
protected int histBottom = 0;
protected int histCurrent = histBottom;
protected boolean echo = true;
public Console( CommandInterpreter ci ) {
buildGui();
setCommandInterpreter( ci );
}
public void setCommandInterpreter( CommandInterpreter ci ) {
this.ci = ci;
if( ci == null ) {
input.setEditable( false );
input.setText("");
} else {
input.setEditable( true );
}
}
public void print( Object object ) {
_print( object );
output.revalidate();
SwingUtilities.invokeLater( scrollUpdater );
}
public void println() {
_println();
output.revalidate();
SwingUtilities.invokeLater( scrollUpdater );
}
public void println( Object object ) {
_println( object );
output.revalidate();
SwingUtilities.invokeLater( scrollUpdater );
}
protected void _print( Object object ) {
output.append( object.toString() );
}
protected void _println() {
output.append("\n");
}
protected void _println( Object object ) {
_print( object );
_println();
}
protected void buildGui() {
setLayout( new BorderLayout() );
output.setEditable( false );
output.setFocusable( false );
output.setLineWrap( true );
scroll.setViewportView( output );
add( scroll, BorderLayout.CENTER );
add( input, BorderLayout.SOUTH );
input.addActionListener( new ActionListener() {
public void actionPerformed( ActionEvent event ) {
String command = input.getText().trim();
if( echo )
println( "> "+input.getText() );
input.setText( "" );
if( command != null && !"".equals(command) ) {
try {
ci.interpret( command, Console.this );
} catch( Throwable error ) {
error.printStackTrace( System.err );
println( error );
}
pushHistory( command );
}
histCurrent = histTop;
}
});
setPreferredSize( new Dimension( 500, 350 ) );
}
protected void pushHistory( String command ) {
int newTop = (histTop+1)%HISTORY_SIZE;
if( newTop == histBottom ) {
++histBottom;
histBottom %= HISTORY_SIZE;
}
histTop = newTop;
history[histTop] = command;
}
protected Runnable scrollUpdater = new Runnable() {
public void run() {
Dimension outputSize = output.getSize();
Rectangle rect = new Rectangle( 0, outputSize.height, 0, 0 );
if( DEBUG_SCROLL ) {
System.out.println( "DEBUG: scrollUpdater: outputSize="+outputSize );
System.out.println( " rect="+rect );
}
output.scrollRectToVisible( rect );
}
};
}
CommandInterpreter.java
(text/plain, 180 B)
/*
* Created on Oct 23, 2003
*/
package ips;
/**
* @author amarshal
*/
public interface CommandInterpreter {
public void interpret( String command, Console console );
}