00001
00002
00003
00004
00005
00006
00007
00008 package com.xsb.interprolog;
00009
00010 import com.declarativa.interprolog.*;
00011 import com.declarativa.interprolog.util.*;
00012 import java.io.*;
00013
00016 public class NativeEngine extends PrologEngine{
00017 static { System.out.println("\nLoading xsb library .....\n\n");}
00018 static { System.loadLibrary("xsb"); }
00019
00020 private ByteArrayOutputStream bao = new ByteArrayOutputStream();
00021
00023 private native void xsb_setDebug(boolean debug);
00024 protected native int xsb_init_internal(String jXSBPath);
00025 protected native int xsb_command_string(String jCommandString);
00026 protected native int xsb_close_query();
00028 protected native int put_bytes(byte[] b, int size, int args, String jStr);
00029 protected native byte[] get_bytes();
00031 protected native void xsb_interrupt();
00032
00033 public NativeEngine(String startPrologCommand){
00034 this(startPrologCommand,false);
00035 }
00036
00041 public NativeEngine(String XSB_DIR, boolean debug) {
00042 super(XSB_DIR,debug);
00043
00044 if (numberOfInstances > 1)
00045 throw new IPException("Can't have more than one instance of NativeEngine");
00046
00047 int ret = xsb_init_internal(XSB_DIR);
00048 if (ret != 0)
00049 throw new IPException("XSB Initialization error");
00050 try {
00051 progressMessage("Loading initial files...");
00052 command("assert(library_directory('"+tempDirectory.getAbsolutePath()+"'))");
00053
00054 consultFromPackage("interprolog.xwam", PrologEngine.class);
00055 progressMessage("Teaching examples to XSB...");
00056 ByteArrayOutputStream serializedTemp = new ByteArrayOutputStream();
00057 ObjectOutputStream bootObjects = new ObjectOutputStream(serializedTemp);
00058 teachIPobjects(bootObjects);
00059 teachBasicObjects(bootObjects);
00060 bootObjects.flush();
00061 byte[] b = serializedTemp.toByteArray();
00062
00063
00064 if (put_bytes(b, b.length, 1, "ipLearnExamples")!=0)
00065 throw new IPException("ipLearnExamples failed");
00066 progressMessage("Initial examples taught.");
00067 xsb_close_query();
00068
00069 if (!command("ipObjectSpec('InvisibleObject',E,["+registerJavaObject(this)+"],_), assert(ipPrologEngine(E))"))
00070 throw new IPException("assert of ipPrologEngine/1 failed");;
00071 } catch (Exception e){
00072 throw new IPException("Could not initialize XSB:"+e);
00073 }
00074 startTopGoal();
00075 }
00076
00079 protected void startTopGoal(){
00080 Thread top = new Thread(){
00081 public void run(){
00082 boolean ended = false;
00083 while(!ended){
00084 progressMessage("Calling "+firstJavaMessageName+"...");
00085 progressMessage("Will call xsb_command_string");
00086
00087 int rc = xsb_command_string("ipPrologEngine(E), javaMessage(E,"+firstJavaMessageName+").");
00088
00089 progressMessage("xsb_command_string done ...");
00090 if (rc==1 && interrupting) {
00091 interruptTasks();
00092 } else if (rc==1){
00093 System.err.println("Prolog execution aborted and restarted");
00094 abortTasks();
00095 }else {
00096 ended=true;
00097 System.out.println("NativeEngine ending abnormally, rc=="+rc);
00098 }
00099 }
00100 }
00101 };
00102 topGoalHasStarted = true;
00103 top.setName("Prolog handler");
00104 top.start();
00105 }
00106
00107 public void setDebug(boolean d){
00108 super.setDebug(d);
00109 xsb_setDebug(d);
00110 }
00111
00112 public void shutdown(){
00113 System.err.println("NO SHUTDOWN YET!!!");
00114 }
00115
00116 protected void doInterrupt(){
00117 xsb_interrupt();
00118 }
00119
00120 protected boolean realCommand(String s){
00121 int result = xsb_command_string(s+".");
00122 if (result==0)
00123 return true;
00124 else if (result==1)
00125 return false;
00126 else
00127 throw new IPException("Problem executing Prolog command");
00128 }
00129
00130 public Object[] deterministicGoal(String G, String OVar, Object[] objectsP, String RVars){
00131 if (!topGoalHasStarted)
00132 throw new IPException("Premature invocation of deterministicGoal");
00133 return super.deterministicGoal(G, OVar, objectsP, RVars);
00134 }
00135
00137 protected byte[] callback(byte[] in) {
00138 progressMessage("entering callback(byte[])");
00139 byte[] out;
00140 Object x;
00141 try{
00142 ByteArrayInputStream bai = new ByteArrayInputStream(in);
00143 ObjectInputStream ios = new ObjectInputStream(bai);
00144 x = ios.readObject();
00145 ios.close();
00146 } catch (ClassNotFoundException e){
00147 x = e;
00148 } catch (IOException e){
00149 throw new IPException("Bad exception before callback handling:"+e);
00150 }
00151 Object y = handleCallback(x);
00152 try {
00153 synchronized(this){
00154 bao.reset();
00155 ObjectOutputStream oos = new ObjectOutputStream(bao);
00156 oos.writeObject(y); oos.flush();
00157 out = bao.toByteArray();
00158 }
00159 } catch (IOException e){
00160 throw new IPException("Bad exception after callback handling:"+e);
00161 }
00162 return out;
00163 }
00164
00165 public boolean isIdle(){
00166 return super.isIdle();
00167 }
00168 }