Skip to content

Instantly share code, notes, and snippets.

@sergio-castro
Last active November 24, 2018 00:50
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sergio-castro/5141949 to your computer and use it in GitHub Desktop.
Save sergio-castro/5141949 to your computer and use it in GitHub Desktop.
This class shows an easy way to configure Logtalk in SWI or YAP using the Jpl library, together with examples of simple invocations of Logtalk methods from Java. You need to have the jpl jar in your classpath to compile and execute this file.
import jpl.Atom;
import jpl.Compound;
import jpl.JPL;
import jpl.Query;
import jpl.Term;
/**
* This class shows how to configure Logtalk in SWI or YAP using the Jpl library.
* You need to have the jpl jar in your classpath to compile and execute this file.
* In this example, we explicitly set the directory where the native part of the JPL library is located
* (with calls to JPL.setNativeLibraryDir)
* Note that this step is not necessary if java is invoked with the parameter java.library.path, like:
* java -Djava.library.path="the_jpl_path"
* Or, if the path to the Jpl native library is already part of the default search path of libraries in your operative system
* (e.g., if it is included in DYLD_LIBRARY_PATH in OSX, or LD_LIBRARY_PATH in Linux)
* @author sergioc
*
*/
public class LogtalkLoader {
public static void main(String[] args) {
LogtalkLoader logtalkLoader = new LogtalkLoader();
logtalkLoader.loadLogtalkInYap();
//logtalkLoader.loadLogtalkInSwi();
logtalkLoader.test();
}
public void loadLogtalkInYap() {
String jplPath = "/opt/local/lib/Yap"; //the directory where the native part of the jpl library for YAP is located in your machine. In my machine the library is here: "/opt/local/lib/Yap/libjpl.dylib"
String logtalkIntegrationScript = "/opt/local/share/logtalk/integration/logtalk_yap.pl"; //the Logtalk integration script for YAP
JPL.setNativeLibraryDir(jplPath);
if(new Query("consult('" + logtalkIntegrationScript +"')").hasSolution())
System.out.println("Logtalk loaded!");
else
System.out.println("Error");
}
public void loadLogtalkInSwi() {
String jplPath = "/opt/local/lib/swipl/lib/x86_64-darwin11.4.2"; //the directory where the native part of the jpl library for SWI is located in your machine. In my machine the library is here: "/opt/local/lib/swipl/lib/x86_64-darwin11.4.2/libjpl.dylib"
String logtalkIntegrationScript = "/opt/local/share/logtalk/integration/logtalk_swi.pl"; //the Logtalk integration script for SWI
JPL.setNativeLibraryDir(jplPath);
if(new Query("consult('" + logtalkIntegrationScript +"')").hasSolution())
System.out.println("Logtalk loaded!");
else
System.out.println("Error");
}
public void test() {
//once Logtalk is loaded, you can call logtalk methods as you would write a normal query in Jpl, for example:
System.out.println(new Query("logtalk::current_object(user)").hasSolution());
//or (writing the term to query by hand)
System.out.println(new Query(new Compound("::", //the Logtalk message operator
new Term[]{new Atom("logtalk"), //the object receiving the message
new Compound("current_object", new Term[]{new Atom("logtalk")}) //the message
})).hasSolution());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment