/* These java class files use Microsoft's JDirect (part of its Java SDK) to implement versions of the LogicServer and LSException classes which are compatible with Microsoft's Visual J++ and Java VM. JDirect is basically an easier-to-use alternative to the RNI for accessing the functions of Amzi4.dll. (Note: Microsoft's Java implementation does not support JNI, which is the native interface Amzi uses for accessing the Amzi4.dll functions from java code; so Visual J++ code cannot call methods of the Amzi-supplied LogicServer.class.) Most, but not all, of the methods defined in Amzi's LogicServer.java class file are defined here. No ascii versions of those methods are defined here, rather only the unicode versions. These implementations have been only partially tested. You will need to download and install the Microsoft Java SDK (3.0 is currently the latest version) before compiling this code. If you are using Visual J++, make sure you compile using the jvc.exe (which in turn uses msjvc.dll and jps.dll) file in the Java SDK \bin directory (it's not automatically installed in the Vj\bin directory). For more information on JDirect, see the \Docs\htm\jdirect.htm file in the documentation download for the Java SDK. */ /** @dll.import("Amzi4",unicode) */ class LogicServer { private native static int lsInitW(int engid[], String s); private native static int lsInit2W(int engid[], String s); private native static int lsInitLSX(int engid, int ptr[]); private native static int lsAddLSXW(int engid, String s, int ptr[]); // private native static int lsAddPredA(int engid, String s, int arity, ExtPred, int ptr[]); // private native static int lsInitPredsA(int engid, PRED_INITAptr); private native static int lsLoadW(int engid, String s); private native static int lsMain(int engid); private native static int lsReset(int engid); private native static int lsClose(int engid); /* function and predicate parameters */ private native static int lsGetParm(int engid, int param, int ctype, int ptr[]); private native static int lsGetParmType(int engid, int param); private native static int lsStrParmLen(int engid, int param); private native static int lsUnifyParm(int engid, int param, int ctype, int ptr[]); /* Calling Prolog from C */ private native static int lsExec(int engid, int term[]); private native static int lsExecStrW(int engid, int term[], String s); private native static int lsCall(int engid, int term[]); private native static int lsCallStrW(int engid, int term[], String s); private native static int lsRedo(int engid); private native static int lsClearCall(int engid); /* Asserting and retracting */ private native static int lsAsserta(int engid, int term); private native static int lsAssertz(int engid, int term); private native static int lsRetract(int engid, int term); private native static int lsAssertaStrW(int engid, String s); private native static int lsAssertzStrW(int engid, String s); private native static int lsRetractStrW(int engid, String s); /* String/Term conversion functions */ private native static int lsTermToStrW(int engid, int term, StringBuffer s, int max); private native static int lsTermToStrQW(int engid, int term, StringBuffer s, int max); private native static int lsStrToTermW(int engid, int term[], String s); /* Making Prolog types */ private native static int lsMakeAtomW(int engid, int term[], String s); private native static int lsMakeStrW(int engid, int term[], String s); private native static int lsMakeInt(int engid, int term[], int n); private native static int lsMakeFloat(int engid, int term[], double f); private native static int lsMakeAddr(int engid, int term[], int ptr[]); /* Getting C values from Prolog terms */ private native static int lsGetTermType(int engid, int term); private native static int lsGetTerm(int engid, int term, int ctype, int ptr[]); private native static int lsGetTerm(int engid, int term, int ctype, double ptr[]); private native static int lsStrTermLen(int engid, int term); /* Structure hacking functions */ private native static int lsGetFAW(int engid, int term, StringBuffer s, int arity[]); private native static int lsMakeFAW(int engid, int term[], String s, int arity); private native static int lsUnifyArg(int engid, int term[], int num, int ctype, int ptr[]); private native static int lsGetArg(int engid, int term, int num, int ctype, int ptr[]); private native static int lsGetArg(int engid, int term, int num, int ctype, double ptr[]); private native static int lsGetArgType(int engid, int term, int num); private native static int lsStrArgLen(int engid, int term, int num); private native static int lsUnify(int engid, int term1, int term2); /* List hacking functions */ private native static int lsMakeList(int engid, int term[]); private native static int lsPushList(int engid, int term[], int trm); private native static int lsPopList(int engid, int term[], int ctype, int ptr[]); private native static int lsGetHead(int engid, int term, int type, int ptr[]); private native static int lsGetTail(int engid, int term); private int Term[]; private int Engid; private int RC; private StringBuffer Buffer = null; public static final int STR_BUFFER_LEN = 4095; public static final int TRUE = 1; public static final int FALSE = 0; public static final int NOTOK = -1; public static final int pERR = -1; public static final int pVAR = 8; public static final int cAATOM = 0; public static final int cASTR = 1; public static final int cINT = 2; public static final int cLONG = 3; public static final int cSHORT = 4; public static final int cFLOAT = 5; public static final int cDOUBLE = 6; public static final int cADDR = 7; public static final int cTERM = 8; public static final int cWSTR = 9; public static final int cWATOM = 10; LogicServer() { Buffer = new StringBuffer(STR_BUFFER_LEN); Term = new int[1]; Engid = 0; } protected void finalize() { if (Engid == 0) return; RC = lsClose(Engid); // if (RC != 0) throw new LSException(Engid); } public void Init(String ini_name) throws LSException { int m_eng[] = {0}; RC = lsInitW(m_eng, ini_name); if (RC != 0) throw new LSException(m_eng[0]); Engid = m_eng[0]; } public void Init2(String ini_params) throws LSException { int m_eng[] = {0}; RC = lsInit2W(m_eng, ini_params); if (RC != 0) throw new LSException(m_eng[0]); Engid = m_eng[0]; } public void Load(String xpl_file) throws LSException { RC = lsLoadW(Engid, xpl_file); if (RC != 0) throw new LSException(Engid); } public boolean Main() throws LSException { int tf = lsMain(Engid); return (tf == TRUE) ? true : false; } public void Reset() throws LSException { RC = lsReset(Engid); if (RC != 0) throw new LSException(Engid); } public void Close() throws LSException { RC = lsClose(Engid); if (RC != 0) throw new LSException(Engid); } /* Calling Prolog from Java */ public int Exec(int t) { Term[0] = t; int tf = lsExec(Engid, Term); if (tf == TRUE) return Term[0]; else return 0; } public int Call(int t) { Term[0] = t; int tf = lsCall(Engid, Term); if (tf == TRUE) return Term[0]; else return 0; } public int ExecStr(String s) { int tf = lsExecStrW(Engid, Term, s); if (tf == TRUE) return Term[0]; else return 0; } public int CallStr(String s) { int tf = lsCallStrW(Engid, Term, s); if (tf == TRUE) return Term[0]; else return 0; } public boolean Redo() { int tf = lsRedo(Engid); return (tf == TRUE) ? true : false; } public void ClearCall() throws LSException { RC = lsClearCall(Engid); if (RC != 0) throw new LSException(Engid); } /* Asserting and retracting */ public void Asserta(int t) throws LSException { RC = lsAsserta(Engid, t); if (RC != 0) throw new LSException(Engid); } public void Assertz(int t) throws LSException { RC = lsAssertz(Engid, t); if (RC != 0) throw new LSException(Engid); } public void AssertaStr(String s) throws LSException { RC = lsAssertaStrW(Engid, s); if (RC != 0) throw new LSException(Engid); } public void AssertzStr(String s) throws LSException { RC = lsAssertzStrW(Engid, s); if (RC != 0) throw new LSException(Engid); } public boolean RetractStr(String str) throws LSException { int tf = lsRetractStrW(Engid, str); if (tf != TRUE && tf != FALSE) throw new LSException(Engid); return (tf == TRUE) ? true : false; } public boolean Retract(int term) throws LSException { int tf = lsRetract(Engid, term); if (tf != TRUE && tf != FALSE) throw new LSException(Engid); return (tf == TRUE) ? true : false; } /* String/Term conversion functions */ public String TermToStr(int term, int n) throws LSException { StringBuffer sb = new StringBuffer(n); RC = lsTermToStrW(Engid, term, sb, n); if (RC != 0) throw new LSException(Engid); return sb.toString(); } public String TermToStrQ(int term, int n) throws LSException { StringBuffer sb = new StringBuffer(n); RC = lsTermToStrQW(Engid, term, sb, n); if (RC != 0) throw new LSException(Engid); return sb.toString(); } public int StrToTerm(String str) throws LSException { RC = lsStrToTermW(Engid, Term, str); if (RC != 0) throw new LSException(Engid); return Term[0]; } /* Making Prolog types */ public int MakeAtom(String str) throws LSException { RC = lsMakeAtomW(Engid, Term, str); if (RC != 0) throw new LSException(Engid); return Term[0]; } public int MakeStr(String str) throws LSException { RC = lsMakeStrW(Engid, Term, str); if (RC != 0) throw new LSException(Engid); return Term[0]; } public int MakeInt(int n) throws LSException { RC = lsMakeInt(Engid, Term, n); if (RC != 0) throw new LSException(Engid); return Term[0]; } public int MakeFloat(double f) throws LSException { RC = lsMakeFloat(Engid, Term, f); if (RC != 0) throw new LSException(Engid); return Term[0]; } public int MakeAddr(int a[]) throws LSException { RC = lsMakeAddr(Engid, Term, a); if (RC != 0) throw new LSException(Engid); return Term[0]; } /* Getting Java values from Prolog terms */ public int GetTermType(int term) throws LSException { if (Engid == 0 || term == 0) { LSException err = new LSException(Engid); err.RaiseError("invalid Engid or Term"); throw err; } return lsGetTermType(Engid, term); } public String GetStrTerm(int term) throws LSException { RC = lsTermToStrW(Engid, term, Buffer, STR_BUFFER_LEN); if (RC != 0) throw new LSException(Engid); return Buffer.toString(); } public int GetIntTerm(int term) throws LSException { int n[] = {0}; RC = lsGetTerm(Engid, term, cINT, n); if (RC != 0) throw new LSException(Engid); return n[0]; } public double GetFloatTerm(int term) throws LSException { double f[] = {0.0}; RC = lsGetTerm(Engid, term, cDOUBLE, f); if (RC != 0) throw new LSException(Engid); return f[0]; } /* Structure hacking functions */ public String GetFunctor(int term) throws LSException { int arity[] = {0}; RC = lsGetFAW(Engid, term, Buffer, arity); if (RC != 0) throw new LSException(Engid); return Buffer.toString(); } public int GetArity(int term) throws LSException { int arity[] = {0}; RC = lsGetFAW(Engid, term, Buffer, arity); if (RC != 0) throw new LSException(Engid); return arity[0]; } public int MakeFA(String func, int arity) throws LSException { RC = lsMakeFAW(Engid, Term, func, arity); if (RC != 0) throw new LSException(Engid); return Term[0]; } public int GetArg(int term, int num) throws LSException { RC = lsGetArg(Engid, term, num, cTERM, Term); if (RC != 0) throw new LSException(Engid); return Term[0]; } public String GetStrArg(int term, int num) throws LSException { RC = lsGetArg(Engid, term, num, cTERM, Term); if (RC != 0) throw new LSException(Engid); RC = lsTermToStrW(Engid, Term[0], Buffer, STR_BUFFER_LEN); if (RC != 0) throw new LSException(Engid); return Buffer.toString(); } public int GetIntArg(int term, int num) throws LSException { int n[] = {0}; RC = lsGetArg(Engid, term, num, cINT, n); if (RC != 0) throw new LSException(Engid); return n[0]; } public double GetFloatArg(int term, int num) throws LSException { double f[] = {0.0}; RC = lsGetArg(Engid, term, num, cDOUBLE, f); if (RC != 0) throw new LSException(Engid); return f[0]; } public int UnifyArg(int term, int num, int term_arg) throws LSException { int term_ptr[] = {term}; Term[0] = term_arg; int tf = lsUnifyArg(Engid, term_ptr, num, cTERM, Term); if (tf != TRUE && tf != FALSE) throw new LSException(Engid); if (tf == FALSE) return 0; else return term_ptr[0]; } public int UnifyStrArg(int term, int num, String str) throws LSException { if (str != null) { int str_term[] = {0}; RC = lsMakeStrW(Engid, str_term, str); if (RC != 0) throw new LSException(Engid); return UnifyArg(term, num, str_term[0]); } else if (lsGetArgType(Engid, term, num) == pVAR) return term; return 0; } public int UnifyIntArg(int term, int num, int val) throws LSException { int n[] = {val}; Term[0] = term; int tf = lsUnifyArg(Engid, Term, num, cINT, n); if (tf != TRUE && tf != FALSE) throw new LSException(Engid); if (tf == FALSE) return 0; else return Term[0]; } public int UnifyFloatArg(int term, int num, double val) throws LSException { int flt_term[] = {0}; RC = lsMakeFloat(Engid, flt_term, val); if (RC != 0) throw new LSException(Engid); return UnifyArg(term, num, flt_term[0]); } public int GetArgType(int term, int num) throws LSException { int pt = lsGetArgType(Engid, term, num); if (pt == pERR) throw new LSException(Engid); return pt; } public int StrArgLen(int term, int num) throws LSException { int len = lsStrArgLen(Engid, term, num); if (len == NOTOK) throw new LSException(Engid); return len; } public boolean Unify(int term1, int term2) throws LSException { int tf = lsUnify(Engid, term1, term2); if (tf != TRUE && tf != FALSE) throw new LSException(Engid); return (tf == TRUE) ? true : false; } /* List hacking functions */ public int MakeList() throws LSException { RC = lsMakeList(Engid, Term); if (RC != 0) throw new LSException(Engid); return Term[0]; } public int PushList(int list_term, int push_term) throws LSException { Term[0] = list_term; RC = lsPushList(Engid, Term, push_term); if (RC != 0) throw new LSException(Engid); return Term[0]; } public int GetHead(int list_term) throws LSException { RC = lsGetHead(Engid, list_term, cTERM, Term); if (RC != 0) throw new LSException(Engid); return Term[0]; } public String GetStrHead(int list_term) throws LSException { RC = lsGetHead(Engid, list_term, cTERM, Term); if (RC != 0) throw new LSException(Engid); lsTermToStrW(Engid, Term[0], Buffer, STR_BUFFER_LEN); return Buffer.toString(); } public int GetIntHead(int list_term) throws LSException { int n[] = {0}; RC = lsGetHead(Engid, list_term, cINT, n); if (RC != 0) throw new LSException(Engid); return n[0]; } public double GetFloatHead(int list_term) throws LSException { double f[] = {0}; RC = lsGetTerm(Engid, list_term, cDOUBLE, f); if (RC != 0) throw new LSException(Engid); return f[0]; } public int GetTail(int list_term) throws LSException { int tail = lsGetTail(Engid, list_term); if (tail == 0) throw new LSException(Engid); return tail; } } /* Error handling */ /** @dll.import("Amzi4",unicode) */ class LSException extends Throwable { private native static int lsErrRaiseW(int Engid, String s); private native static int lsGetExceptType(int Engid); private native static int lsGetExceptRC(int Engid); private native static int lsGetExceptLineno(int Engid); private native static void lsGetExceptMsgW(int Engid, StringBuffer s, int max); private native static void lsGetExceptCallStackW(int Engid, StringBuffer s, int max); public static final int ERRMSG_BUFFER_LEN = 1023; private int Engid; private StringBuffer Buffer = null; LSException(int eid) { Engid = eid; Buffer = new StringBuffer(ERRMSG_BUFFER_LEN); } public void RaiseError(String msg) { lsErrRaiseW(Engid, msg); } public int GetRC() { return lsGetExceptRC(Engid); } public String GetMsg() { lsGetExceptMsgW(Engid, Buffer, ERRMSG_BUFFER_LEN); return Buffer.toString(); } int GetType() { return lsGetExceptType(Engid); } String GetCallStack() { lsGetExceptCallStackW(Engid, Buffer, ERRMSG_BUFFER_LEN); return Buffer.toString(); } int GetReadLineno() { return lsGetExceptLineno(Engid); } }