public abstract class AbstractPrologEngine extends Object implements PrologEngine
| Modifier and Type | Field and Description |
|---|---|
String |
firstJavaMessageName
Name of first message sent to Java
|
boolean |
interPrologFileLoaded
Determines whether deterministicGoal machinery is in place...
|
String |
nl
Convenience for newline; value for the OS where Prolog is running (which may be different from the Java side).
|
String |
prologBinDirectoryOrCommand
File path to directory with Prolog machine, or command path; this may NOT have
options appended (Windows oblige), use specific constructors in subclasses for that
|
MAX_INT_VALUE, MIN_INT_VALUE, version| Constructor and Description |
|---|
AbstractPrologEngine(String prologBinDirectoryOrCommand,
boolean debug,
boolean loadFromJar)
Create a Prolog executor, possibly spawning it in a different process or loading it into memory, depending on the implementation by our subclass.
|
| Modifier and Type | Method and Description |
|---|---|
void |
abortTasks()
Do not invoke this.
|
boolean |
add_lib_dir(File D)
Adds a directory to Prolog's library search paths
|
void |
addPrologEngineListener(PrologEngineListener L) |
static void |
addSoftwareLibrary(File file)
Add a jar to the runtime classpath
Grabbed from http://stackoverflow.com/questions/1010919/adding-files-to-java-classpath-at-runtime?answertab=active#tab-top
|
static void |
addSoftwareLibrary(String file) |
static boolean |
assignableType(Class<?> left,
Class<?> right)
It is OK to assign an expression typed right to a variable typed left.
|
boolean |
command(ArrayList<String> commands) |
boolean |
command(String s)
Execute a Prolog "command"; except until the basic InterProlog is loaded,
this in fact delegates to deterministicGoal, which synchronizes at the end and returns a reliable result.
|
boolean |
command(String[] commands) |
boolean |
consultAbsolute(File f)
Consults a Prolog file
|
boolean |
consultAbsolute(String f) |
String |
consultFromPackage(String filename,
Object requester)
Extracts a Prolog file from the jar file or directory where the requester's class came from,
and asks Prolog process to reconsult it.
|
String |
consultFromPackage(String filename,
Object requester,
boolean firstTimeOnly)
Extracts a Prolog file from the jar file or directory where the requester's class came from,
and asks Prolog process to reconsult it.
|
String |
consultRelative(String filename,
Object requester)
Consults a Prolog file from the directory where the requester's class would come from if it
did not come from a jar file.
|
File |
createTempDirectory()
Create a new temporary directory
|
static double |
currentTimeSecs() |
static void |
deleteAll(File D) |
void |
deleteTempFiles()
If you don't trust the Java VM to execute earlier "deleteOnExit", call this
|
boolean |
deterministicGoal(String G)
A parameterless goal with no result other than success/failure.
|
Object[] |
deterministicGoal(String G,
String RVars)
Useful when you're constructing objects from Prolog, but don't need to pass any from Java.
|
boolean |
deterministicGoal(String G,
String OVar,
Object[] objectsP)
Useful when you want to pass objects to Prolog but don't need objects returned.
|
Object[] |
deterministicGoal(String G,
String OVar,
Object[] objectsP,
String RVars)
Synchronously calls a Prolog goal.
|
TermModel |
deterministicGoal(TermModel G)
Useful for inter-Prolog goal calling through Java.
|
String |
deterministicGoalJSON(String JSON,
int timestamp)
Dispatches goal encoded as a JSON object and returns a JSON response
Example:
ipPrologEngine(E), JSON='{"g":s("string:concat_atom([hello_,I],O)"),"i":s("I"),"o":s("O"),"x":s("miguel")}', java(E,string(R),deterministicGoalJSON(string(JSON),int(1))).
|
ResultFromJava |
doCallback(Object x)
Execute a Prolog->Java call
|
void |
endAllTasks(Exception e)
Do not invoke this
|
void |
exec(String S)
A command expected to always succeed; throws an IPException if it fails
|
boolean |
executingOnJavaSide() |
static Constructor<?> |
findConstructor(Class<?> targetClass,
Class<?>[] formalArguments)
Similar to findMethod(), but for constructors rather than regular methods
|
static Method |
findMethod(Class<?> targetClass,
String name,
Class<?>[] formalArguments)
An utility building on the functionality of getMethod(), to provide the javaMessage predicate with method argument
polimorphism.
|
void |
firstJavaMessage()
Dummy method, whose name is used to start the callback thread
|
PrologImplementationPeer |
getImplementationPeer() |
String |
getInterprologPath() |
File |
getJarDirectory()
Returns the directory containing the jar with the engine class
|
static File |
getJarDirectory(Class<? extends AbstractPrologEngine> aClass)
Returns the directory containing the jar with the given class
|
boolean |
getLoadFromJar() |
String |
getPrologBaseDirectory()
Returns the installation directory for the Prolog system, without the trailing separator char
|
String |
getPrologNumericVersion()
Returns the Prolog numeric version for this engine; useful for functionality that depends on features of a particular version.
|
String |
getPrologVersion()
Returns the Prolog system name and version for this engine
|
Object |
getRealJavaObject(int ID)
Same as getRealJavaObject(InvisibleObject), but accepts an integer ID as argument instead
|
Object |
getRealJavaObject(InvisibleObject o)
Get the object referred by the integer in a InvisibleObject wrapper.
|
Object |
getRealJavaObject(Object o)
Just returns the object, untouched (but "dereferenced" if called from Prolog).
|
PrologEngineListener |
getThePrologListener()
Kludgy method to be used only if a single listener exists
|
SolutionIterator |
goal(String G,
String RVars)
Useful when you're constructing objects from Prolog, but don't need to pass any from Java.
|
SolutionIterator |
goal(String G,
String OVar,
Object[] objectsP,
String RVars)
Similar to deterministicGoal, but rather than just the first solution returns an Iterator which lazily returns bindings for solutions.
|
Object |
handleCallback(Object x)
Handling of javaMessages and deterministicGoals.
|
boolean |
hasPrologExtension(String filename) |
boolean |
inPrologShell()
We know the engine to be in the Prolog shell, so Prolog goals do not need a postfix.
|
void |
interrupt()
Interrupt Prolog and make it return to its top level.
|
void |
interruptTasks()
Do not invoke this.
|
static boolean |
is64WindowsOS() |
boolean |
isAllowSimultaneousThreads() |
boolean |
isAvailable()
Present implementation is always available, so this always returns true.
|
boolean |
isDebug()
Debug messages are being written, both Java and Prolog side, cf.
|
boolean |
isIdle()
The engine is doing nothing: no pending Prolog goals nor Java callbacks
|
static boolean |
isLinuxOS() |
static boolean |
isMacOS() |
boolean |
isPaused()
To be used only if this engine has a EngineController (single) listener
|
boolean |
isProfiling() |
static boolean |
isSerializable(Object x)
Returns true if the object is Serializable or, being an array, its element type is primitive or Serializable
|
boolean |
isShutingDown()
The engine is in the process of shuting down
|
boolean |
isThreadedCallbacks() |
static boolean |
isWindowsOS()
whether we (Java side) are re under Windows -- isWindowsOS
|
boolean |
lastSolutionUndefined() |
boolean |
load_dynAbsolute(File f) |
String |
load_dynRelative(String filename,
Object requester) |
static void |
loop()
Infinite Java loop, for testing only!
|
void |
loop2() |
Object |
makeInvisible(Object x)
Register an object with this Engine, so it later can be referred from Prolog without serializing it, and returns
an InvisibleObject encapsulating the reference.
|
static String |
printAllStackTraces() |
static String |
printAllStackTraces(boolean quiet)
If !quite, does not print to the standard (error) output
|
static void |
printBindings(Object[] b)
Convenience for debugging deterministicGoal() messages
|
static void |
printStackTrace() |
void |
profilingMessage(String s)
Profiling aid
|
void |
progressMessage(String s)
Debugging aid
|
void |
progressMessage(String s,
Object X) |
String |
prologBinToBaseDirectory(String binDirectoryOrStartCommand)
Computes the installation directory for the Prolog system, without the trailing separator char
|
String |
prologCanWork()
Never call this directly; the Prolog side does call this automatically during timed calls
|
void |
prologEnteredBreak()
Never call this directly; the Prolog side does call this automatically if detectsPauses
|
void |
prologResumedComputation()
Never call this directly; the Prolog side does call this automatically if detectsPauses
|
void |
prologReturnedToTopLevel()
Never call this directly; the Prolog side does call this automatically if detectsPauses
|
abstract boolean |
realCommand(String s)
Implementation of a simple parameterless Prolog goal; does not support recursive nor multithreaded operation, use command instead
|
int |
registerJavaObject(Object x)
Register an object with this Engine, so it later can be referred from Prolog without serializing it.
|
void |
removePrologEngineListener(PrologEngineListener L) |
char |
serverFileSeparatorChar() |
boolean |
serverIsWindows()
Whether the Prolog machine is a Windows box
|
void |
setAllowSimultaneousThreads(boolean allowSimultaneousThreads) |
void |
setDebug(boolean d)
Show (or hide) debug messages, both Java and Prolog side, cf.
|
void |
setProfiling(boolean d) |
void |
setThreadedCallbacks(boolean yes)
If true, the Java execution of javaMessage predicates will happen in new threads (default);
if false, execution will be under the thread of the most recent deterministicGoal currently executing in Prolog
|
void |
setTimedCallIntervall(int ms)
If ms>0, all future deterministic goals will be executed under control so that the Java side is messaged (with @see#willWork messages)
every ms, with the possibility to abort the Prolog computation, by having the Prolog engine interrupt normal execution periodically;
otherwise the goal will run uninterrupted.
|
static String |
shortClassName(Class<?> c)
Returns just the name of the class, with no package information.
|
void |
shutdown()
Release Prolog engine resources, making it unusable
|
void |
stop()
Gracefully stop the engine computation
|
boolean |
teachMoreObjects(Object[] examples)
Same as #teachMoreObjects(ObjectExamplePair[]), but example pairs are constructed with (2) repeated examples for each object
|
boolean |
teachMoreObjects(ObjectExamplePair example) |
boolean |
teachMoreObjects(ObjectExamplePair[] examples)
Send an array of object example pairs to Prolog and generate ipObjectSpec facts.
|
boolean |
teachOneObject(Object example)
Same as #teachMoreObjects(ObjectExamplePair[]), but the single example pair is constructed repeating the object
|
String |
toString() |
String |
unescapedFilePath(String p)
Some Prologs use '\' as an escape character in atoms, which can affect file paths under Windows.
|
boolean |
unregisterJavaObject(int ID)
Removes reference to the object from the registry.
|
boolean |
unregisterJavaObject(Object obj)
Removes reference to the object from the registry.
|
boolean |
unregisterJavaObjects(Class<?> cls)
Removes references to objects of class
cls from the registry. |
void |
waitUntilAvailable()
This method blocks until
isAvailable() returns true. |
void |
waitUntilIdle()
Sleeps the current Java thread until this engine is idle.
|
public String prologBinDirectoryOrCommand
public String nl
public boolean interPrologFileLoaded
public final String firstJavaMessageName
public AbstractPrologEngine(String prologBinDirectoryOrCommand, boolean debug, boolean loadFromJar)
prologBinDirectoryOrCommand - File path to Prolog machine, see subclass docs for precise semanticsdebug - if true, print progress messages to aid debuggingloadFromJar - if true, startup files should be loaded from the jar file, rather than directly from the file system
IF prologBinDirectoryOrCommand is null then InterProlog needs to find values for one or more properties, XSB_BIN_DIRECTORY and/or others, depending on which Prolog(s) you're using.
First it looks for an 'interprolog.defs' file
(a Properties file in the format described in http://java.sun.com/j2se/1.4.2/docs/api/java/util/Properties.html,
in the directory where the jar file containing the InterProlog classes is).
If 'interprolog.defs' does not exist, an attempt is made
to get the property through System.getProperties(); in this scenario you can define the property by using the -D java (command) switch;
if it doesn't find it, it will try to find an environment variable
public void deleteTempFiles()
public String getInterprologPath()
public PrologImplementationPeer getImplementationPeer()
getImplementationPeer in interface PrologEnginepublic String getPrologVersion()
getPrologVersion in interface PrologEnginepublic String getPrologNumericVersion()
public String getPrologBaseDirectory()
getPrologBaseDirectory in interface PrologEnginepublic String prologBinToBaseDirectory(String binDirectoryOrStartCommand)
public boolean serverIsWindows()
public char serverFileSeparatorChar()
public static boolean isWindowsOS()
public static boolean is64WindowsOS()
public static boolean isMacOS()
public static boolean isLinuxOS()
public boolean hasPrologExtension(String filename)
public void shutdown()
shutdown in interface PrologEnginepublic boolean isShutingDown()
public File createTempDirectory()
public static void deleteAll(File D)
public File getJarDirectory()
public static File getJarDirectory(Class<? extends AbstractPrologEngine> aClass)
public String unescapedFilePath(String p)
unescapedFilePath in interface PrologEnginepublic String consultFromPackage(String filename, Object requester)
PrologEngineconsultFromPackage in interface PrologEnginefilename - The Prolog file name, including extension; if it has no extension, the Prolog file extensions are appended in turn until a file is foundrequester - Defines where the Prolog file residesPrologEngine.consultRelative(String, Object),
PrologEngine.load_dynRelative(String, Object)public String consultFromPackage(String filename, Object requester, boolean firstTimeOnly)
consultFromPackage in interface PrologEnginefilename - The Prolog file path relative to the requester's package, including extension; if it has no extension, the Prolog file extensions are appended in turn until a file is foundrequester - Defines where the Prolog file residesfirstTimeOnly - If true a second call will NOT repeat the consult on the Prolog endconsultRelative(String, Object),
load_dynRelative(String, Object)public boolean add_lib_dir(File D)
public boolean consultAbsolute(File f)
consultAbsolute in interface PrologEnginepublic boolean consultAbsolute(String f)
public boolean load_dynAbsolute(File f)
load_dynAbsolute in interface PrologEnginepublic final String consultRelative(String filename, Object requester)
consultRelative in interface PrologEnginefilename - The Prolog file name, including suffix; if a path it should use '/' as the separator, independently of the OSrequester - Defines where the Prolog file residespublic String load_dynRelative(String filename, Object requester)
load_dynRelative in interface PrologEnginepublic void interrupt()
PrologEngineinterrupt in interface PrologEnginepublic boolean command(String s)
command in interface PrologEnginepublic boolean command(String[] commands)
commands - to be executed as a single conjunction in order 0,1,...public void exec(String S)
public abstract boolean realCommand(String s)
command(String)public void progressMessage(String s)
public void profilingMessage(String s)
public boolean isDebug()
isDebug in interface PrologEnginepublic void setDebug(boolean d)
setDebug in interface PrologEnginepublic boolean isProfiling()
public void setProfiling(boolean d)
public boolean getLoadFromJar()
public static void printBindings(Object[] b)
public boolean teachOneObject(Object example)
teachOneObject in interface PrologEngineteachMoreObjects(ObjectExamplePair[])public boolean teachMoreObjects(Object[] examples)
teachMoreObjects in interface PrologEngineteachMoreObjects(ObjectExamplePair[])public boolean teachMoreObjects(ObjectExamplePair[] examples)
teachMoreObjects in interface PrologEngineexamples - The examplesObjectExamplePairpublic boolean teachMoreObjects(ObjectExamplePair example)
public String deterministicGoalJSON(String JSON, int timestamp)
JSON - : {"g":"some goal with var","i":"VarWithInputJSONstring","o":"VarWithOutputJSONString","x":JSONinput}timestamp - : some unique client identifier for this call; it may be in JSON, but the caller should extract itpublic Object[] deterministicGoal(String G, String OVar, Object[] objectsP, String RVars)
deterministicGoal in interface PrologEngineG - Prolog goal termOVar - Prolog variable that will be bound to objectsP arrayobjectsP - Array of Java objects to pass to Prolog goal; nonserializable objects are encapsulated in InvisibleObject references, changing this arrayRVars - Prolog list with object specifications, typically containing variables occurring in g.
If null a single binding will be returned, containing a TermModel object representing the goal term solutiondeterministicGoal(String),
deterministicGoal(String,String),
deterministicGoal(String,String,Object[])public boolean deterministicGoal(String G)
deterministicGoal in interface PrologEnginepublic Object[] deterministicGoal(String G, String RVars)
deterministicGoal in interface PrologEnginepublic boolean deterministicGoal(String G, String OVar, Object[] objectsP)
deterministicGoal in interface PrologEnginepublic TermModel deterministicGoal(TermModel G)
deterministicGoal in interface PrologEnginepublic SolutionIterator goal(String G, String OVar, Object[] objectsP, String RVars)
goal in interface PrologEnginepublic SolutionIterator goal(String G, String RVars)
goal in interface PrologEnginepublic boolean lastSolutionUndefined()
lastSolutionUndefined in interface PrologEnginepublic boolean isIdle()
isIdle in interface PrologEnginepublic boolean isPaused()
public boolean isAllowSimultaneousThreads()
public void setAllowSimultaneousThreads(boolean allowSimultaneousThreads)
allowSimultaneousThreads - canAcceptNewGoal(Thread)public void endAllTasks(Exception e)
public void abortTasks()
public void interruptTasks()
public boolean isAvailable()
isAvailable in interface PrologEnginepublic void waitUntilAvailable()
PrologEngineisAvailable() returns true.waitUntilAvailable in interface PrologEnginepublic void waitUntilIdle()
waitUntilIdle in interface PrologEnginepublic Object handleCallback(Object x)
x - Argument of the callback predicatepublic final void firstJavaMessage()
public ResultFromJava doCallback(Object x)
public static boolean isSerializable(Object x)
public static Method findMethod(Class<?> targetClass, String name, Class<?>[] formalArguments) throws NoSuchMethodException
NoSuchMethodExceptionpublic static Constructor<?> findConstructor(Class<?> targetClass, Class<?>[] formalArguments) throws NoSuchMethodException
NoSuchMethodExceptionpublic static boolean assignableType(Class<?> left, Class<?> right)
public static String shortClassName(Class<?> c)
foo.bar.Mumble were the class passed in, the
string "Mumble" would be returned. Similarly, if the class
that is passed in is a.b.Class$InnerClass the string
returned would be InnerClass.public static void addSoftwareLibrary(File file) throws Exception
Exceptionpublic static void addSoftwareLibrary(String file) throws Exception
Exceptionpublic int registerJavaObject(Object x)
registerJavaObject in interface PrologEnginex - Object to be registeredInvisibleObjectpublic Object makeInvisible(Object x)
makeInvisible in interface PrologEnginex - Object to be registeredInvisibleObjectpublic Object getRealJavaObject(InvisibleObject o)
getRealJavaObject in interface PrologEngineo - An InvisibleObjectInvisibleObjectpublic Object getRealJavaObject(int ID)
getRealJavaObject in interface PrologEnginepublic Object getRealJavaObject(Object o)
getRealJavaObject in interface PrologEnginepublic boolean unregisterJavaObject(int ID)
unregisterJavaObject in interface PrologEnginepublic boolean unregisterJavaObject(Object obj)
unregisterJavaObject in interface PrologEnginepublic boolean unregisterJavaObjects(Class<?> cls)
cls from the registry. This method should be
used with extreme caution since any further prolog calls to the unregistered objects by means
of reference to them in the registry might result in unpredictable behaviour.unregisterJavaObjects in interface PrologEnginepublic void setThreadedCallbacks(boolean yes)
setThreadedCallbacks in interface PrologEnginepublic boolean isThreadedCallbacks()
public static String printAllStackTraces(boolean quiet)
public static String printAllStackTraces()
public static void printStackTrace()
public boolean inPrologShell()
inPrologShell in interface PrologEnginepublic void addPrologEngineListener(PrologEngineListener L)
public void removePrologEngineListener(PrologEngineListener L)
public PrologEngineListener getThePrologListener()
public void stop()
public void setTimedCallIntervall(int ms)
public String prologCanWork()
public void prologEnteredBreak()
public void prologResumedComputation()
public void prologReturnedToTopLevel()
public static void loop()
throws InterruptedException
InterruptedExceptionpublic void loop2()
public boolean executingOnJavaSide()
public static double currentTimeSecs()