In addition, you can extend the Java Class to allow Prolog to call methods in your Java code.
The sections of this document are:
To use the Java Class, you must make these files accessible to the calling environment:
javac Hello.javaThis will produce Hello.class which is simply run by typing:
java Hello
So, the Amzi! Java interface defines a Java class, LogicServer, whose methods are implemented by calls to C++ functions in a dynamic/shared libary that, in turn, calls the main Amzi! engine dynamic/shared library. This is illustrated in Figure 1.
Figure 1
Working right to left in the diagram, amzi.dll is the Amzi! Logic Server (Prolog engine). amzijni.dll/libamzijni.so is the interface library that implements the Java versions of Logic Server API functions. Those, in turn are wrapped in the Java classes, LogicServer and LSException.
There are a number of features of the Java class that were dictated by the distinct characteristics of Java. These are detailed in the following sections.
Figure 2 is an expanded architecture diagram that illustrates the package and its classes.
Figure 2
A fundamental data type for the Prolog interface is a Prolog term. Internally, a Prolog term is a pointer, but, since that pointer is not manipulated by the application, it can be stored as an integer. For Java, it is stored in a 64-bit integer so Java can support both 64- and 32-bit versions of the Amzi! Prolog engine.
Many Logic Server API (LSAPI) functions return error codes, and use argument pointers to pass values back and forth. The Java class, like other Amzi! lsapis, is designed to use exception handling for errors, with return values passed directly as return values, without using pointers.
There are a number of Java LSAPI functions that differ from the corresponding base LSAPI because of lack of pointers. These are discussed in the following sections.
For Java, the query LSAPI functions (Exec, ExecStr, Call, CallStr) return the term (a long) directly, instead of a true false. If the query fails, that is indicated by a zero (0) value returned. (Errors are indicated by LSExceptions.)
import amzi.ls.*;
From there you can either instantiate a new LogicServer object and invoke its methods, or you can define a new class that extends the LogicServer class adding new methods and variables.
The Java methods that implement extended predicates, must be declared as returning type boolean, and as public. They can be added one at a time using the API function AddPred, which adds a single predicate at a time. If your extended predicate is in a package, then the package name must be included in the class name, delimited by forward slashes, to AddPred as follows:
ls.AddPred("extpred", 1, "javapkg/jprolog", "extpred", this);
Note: Extended predicate definitions
must always be added after calling InitLS and before calling LoadXPL.
The sample in directory /samples/java/pets_callback is an example of how to implement an extended predicate in Java.
For AddPred, the Java method that implements the predicate is defined with two string parameters, giving the class and method names, and one Object reference for the particular instance of the object that will be used for the functions. (AddPred does not allow the use of static methods for extended predicates.) See the samples for an example of its use.
public native void Init(String ININame) throws LSException; public native void Init2(String INIParms) throws LSException; public native void InitLSX(long Arg) throws LSException; public native void AddLSX(String LSXName, long Arg) throws LSException; public native void AddPred(String PredName, int Arity, String Class, String Method, Object Obj) throws LSException; public native void Load(String XPLName) throws LSException; public native boolean Main() throws LSException; public native void Reset() throws LSException; public native void Close() throws LSException;
public native long Exec(long Term) throws LSException; public native long ExecStr(String Query) throws LSException; public native long Call(long Term) throws LSException; public native long CallStr(String Query) throws LSException; public native boolean Redo() throws LSException; public native void ClearCall() throws LSException;
public native void Asserta(long Term) throws LSException; public native void Assertz(long Term) throws LSException; public native long Retract(long Term) throws LSException; public native void AssertaStr(String TermStr) throws LSException; public native void AssertzStr(String TermStr) throws LSException; public native boolean RetractStr(String TermStr) throws LSException;
TermToStr is especially useful during development to display the results of a Prolog query.
public native String TermToStr(long Term, int Len) throws LSException; public native String TermToStrQ(long Term, int Len) throws LSException; public native long StrToTerm(String TermStr) throws LSException; public native int StrTermLen(long Term) throws LSException;
public native long MakeAtom(String AtomStr) throws LSException; public native long MakeStr(String Str) throws LSException; public native long MakeInt(int Num) throws LSException; public native long MakeFloat(double Num) throws LSException; public native int GetTermType(long Term) throws LSException; public native String GetStrTerm(long Term) throws LSException; public native int GetIntTerm(long Term) throws LSException; public native double GetFloatTerm(long Term) throws LSException;
public native String GetFunctor(long Term) throws LSException; public native int GetArity(long Term) throws LSException; public native long MakeFA(String Functor, int Arity) throws LSException; public native long GetArg(long Term, int Num) throws LSException; public native String GetStrArg(long Term, int Num) throws LSException; public native int GetIntArg(long Term, int Num) throws LSException; public native double GetFloatArg(long Term, int Num) throws LSException; public native long UnifyStrArg(long Term, int Num, String Str) throws LSException; public native long UnifyIntArg(long Term, int Num, int Val) throws LSException; public native long UnifyFloatArg(long Term, int Num, double Val) throws LSException; public native int GetArgType(long Term, int Num) throws LSException; public native int StrArgLen(long Term, int Num) throws LSException;
public native int GetParmType(int iarg) throws LSException; public native long GetParm(int iarg) throws LSException; public native String GetStrParm(int iarg) throws LSException; public native int GetIntParm(int iarg) throws LSException; public native double GetFloatParm(int iarg) throws LSException; public native boolean UnifyParm(int iarg, long Term) throws LSException; public native boolean UnifyStrParm(int iarg, String s) throws LSException; public native boolean UnifyIntParm(int iarg, int i) throws LSException; public native boolean UnifyFloatParm(int iarg, double f) throws LSException;
public native long MakeList() throws LSException; public native long PushList(long ListTerm, long Term) throws LSException; public native long GetHead(long ListTerm) throws LSException; public native String GetStrHead(long ListTerm) throws LSException; public native int GetIntHead(long ListTerm) throws LSException; public native double GetFloatHead(long ListTerm) throws LSException; public native long GetTail(long ListTerm) throws LSException;
public native int GetType(); public native int GetRC(); public native int GetLineno(); public native String GetMsg(); public native String GetReadFileName(); public native String GetReadBuffer(); public native String GetCallStack();
public native String GetVersion() throws LSException;
Copyright ©1995-2000 Amzi! inc. All Rights Reserved. info@amzi.com
Amzi! is a registered trademark and Adventure in Prolog, Logic Server, Logic Explorer, Adventure in Prolog, SupportKT, Subscription Plus and WebLS are trademarks of Amzi! inc.