[armedbear-cvs] r11382 - in trunk/j/examples/abcl: . interface_implementation_in_lisp javacall_from_lisp lispcall_from_java_simple lispcall_from_java_with_params_and_return

Ville Voutilainen vvoutilainen at common-lisp.net
Fri Nov 7 22:55:44 UTC 2008


Author: vvoutilainen
Date: Fri Nov  7 22:55:43 2008
New Revision: 11382

Log:
Examples for using abcl, initial commit.


Added:
   trunk/j/examples/abcl/
   trunk/j/examples/abcl/interface_implementation_in_lisp/
   trunk/j/examples/abcl/interface_implementation_in_lisp/Main.java
   trunk/j/examples/abcl/interface_implementation_in_lisp/MyInterface.java
   trunk/j/examples/abcl/interface_implementation_in_lisp/interface_implementation.lisp
   trunk/j/examples/abcl/javacall_from_lisp/
   trunk/j/examples/abcl/javacall_from_lisp/Main.java
   trunk/j/examples/abcl/javacall_from_lisp/lispfunctions.lisp
   trunk/j/examples/abcl/lispcall_from_java_simple/
   trunk/j/examples/abcl/lispcall_from_java_simple/Main.java
   trunk/j/examples/abcl/lispcall_from_java_simple/MainAlternative.java
   trunk/j/examples/abcl/lispcall_from_java_simple/lispfunction.lisp
   trunk/j/examples/abcl/lispcall_from_java_with_params_and_return/
   trunk/j/examples/abcl/lispcall_from_java_with_params_and_return/Main.java
   trunk/j/examples/abcl/lispcall_from_java_with_params_and_return/lispfunctions.lisp

Added: trunk/j/examples/abcl/interface_implementation_in_lisp/Main.java
==============================================================================
--- (empty file)
+++ trunk/j/examples/abcl/interface_implementation_in_lisp/Main.java	Fri Nov  7 22:55:43 2008
@@ -0,0 +1,49 @@
+import org.armedbear.lisp.*;
+
+public class Main
+{
+    /**
+     * This example loads a lisp file and gets two function symbols
+     * from it. The functions return implementations of MyInterface.
+     * The example gets two separate implementations and invokes
+     * the functions in the interface for both implementations.
+     */
+    public static void main(String[] argv)
+    {
+	try
+	    {
+		Interpreter interpreter = Interpreter.createInstance();
+		interpreter.eval("(load \"interface_implementation.lisp\")");
+		// the function is not in a separate package, thus the
+		// correct package is CL-USER. Symbol names are
+		// upper case. Package needs the prefix, because java
+		// also has a class named Package.
+		org.armedbear.lisp.Package defaultPackage = 
+		    Packages.findPackage("CL-USER");
+		Symbol interfacesym = 
+		    defaultPackage.findAccessibleSymbol("GET-INTERFACE");
+		Function interfaceFunction = 
+		    (Function) interfacesym.getSymbolFunction();
+		LispObject myinterface = interfaceFunction.execute();
+		MyInterface x = 
+		    (MyInterface) JavaObject.getObject(myinterface);
+		x.firstFunction();
+		x.secondFunction();
+		Symbol interfacesym2 = 
+		    defaultPackage.
+		    findAccessibleSymbol("GET-ANOTHER-INTERFACE");
+		Function interfaceFunction2 = 
+		    (Function) interfacesym2.getSymbolFunction();
+		LispObject myInterface2 = interfaceFunction2.execute();
+		MyInterface y = 
+		    (MyInterface) JavaObject.getObject(myInterface2);
+		y.firstFunction();
+		y.secondFunction();
+	    }
+	catch (Throwable t)
+	    {
+		System.out.println("abcl exception!");
+		t.printStackTrace();
+	    }
+    }
+}
\ No newline at end of file

Added: trunk/j/examples/abcl/interface_implementation_in_lisp/MyInterface.java
==============================================================================
--- (empty file)
+++ trunk/j/examples/abcl/interface_implementation_in_lisp/MyInterface.java	Fri Nov  7 22:55:43 2008
@@ -0,0 +1,8 @@
+/**
+ * Example interface, with two methods.
+ */
+public interface MyInterface
+{
+    public void firstFunction();
+    public void secondFunction();
+}
\ No newline at end of file

Added: trunk/j/examples/abcl/interface_implementation_in_lisp/interface_implementation.lisp
==============================================================================
--- (empty file)
+++ trunk/j/examples/abcl/interface_implementation_in_lisp/interface_implementation.lisp	Fri Nov  7 22:55:43 2008
@@ -0,0 +1,60 @@
+; first we define a class hierarchy. No slots defined,
+; we don't need them in the example.
+(defclass base ())
+
+(defclass derived1 (base))
+
+(defclass derived2 (base))
+
+; then a couple of generic methods
+(defgeneric invoke (param) (:documentation "Sample generic function"))
+
+(defgeneric invoke2 (param) (:documentation "Sample generic function"))
+
+; and their methods, for different classes
+(defmethod invoke ((param derived1))
+  (format t "in derived1 invoke~%"))
+
+(defmethod invoke ((param derived2))
+  (format t "in derived2 invoke~%"))
+
+(defmethod invoke2 ((param derived1))
+  (format t "in derived1 invoke2~%"))
+
+(defmethod invoke2 ((param derived2))
+  (format t "in derived2 invoke2~%"))
+
+; closure for interface implementation, closes
+; over a provided object and calls the invoke
+; method with the object. Thus the firstFunction()
+; in MyInterface will call the invoke method.
+(defun make-first-function (object)
+  (lambda () (invoke object)))
+
+; closure for interface implementation, closes
+; over a provided object and invokes the invoke2
+; method with the object. Thus the secondFunction()
+; in MyInterface will call the invoke2 method.
+(defun make-second-function (object)
+  (lambda () (invoke2 object)))
+
+; gets an interface implementation, uses an instance of
+; class derived1
+(defun get-interface ()
+  (let ((firstobject (make-instance 'derived1)))
+    (jinterface-implementation "MyInterface"
+			       "firstFunction" 
+			       (make-first-function firstobject)
+			       "secondFunction"
+			       (make-second-function firstobject))))
+
+; gets an interface implementation, uses an instance of
+; class derived2
+(defun get-another-interface ()
+  (let ((secondobject (make-instance 'derived2)))
+    (jinterface-implementation "MyInterface"
+			       "firstFunction" 
+			       (make-first-function secondobject)
+			       "secondFunction"
+			       (make-second-function secondobject))))
+						

Added: trunk/j/examples/abcl/javacall_from_lisp/Main.java
==============================================================================
--- (empty file)
+++ trunk/j/examples/abcl/javacall_from_lisp/Main.java	Fri Nov  7 22:55:43 2008
@@ -0,0 +1,42 @@
+import org.armedbear.lisp.*;
+
+public class Main
+{
+    /**
+     * This example creates an Interpreter instance, loads our
+     * lisp code from a file and then looks up a function defined
+     * in the loaded lisp file and executes the function. 
+     *
+     * The function takes a single parameter and invokes a java method
+     * on the object provided. We provide our Main object as the parameter.
+     *
+     */
+    public static void main(String[] argv)
+    {
+	try
+	    {
+		Main thisObject = new Main();
+		Interpreter interpreter = Interpreter.createInstance();
+		interpreter.eval("(load \"lispfunctions.lisp\")");
+		// the function is not in a separate package, thus the
+		// correct package is CL-USER. Symbol names are
+		// upper case. Package needs the prefix, because java
+		// also has a class named Package.
+		org.armedbear.lisp.Package defaultPackage = 
+		    Packages.findPackage("CL-USER");
+		Symbol voidsym = 
+		    defaultPackage.findAccessibleSymbol("VOID-FUNCTION");
+		Function voidFunction = (Function) voidsym.getSymbolFunction();
+		voidFunction.execute(new JavaObject(thisObject));
+	    }
+	catch (Throwable t)
+	    {
+		System.out.println("abcl exception!");
+		t.printStackTrace();
+	    }
+    }
+    public int addTwoNumbers(int a, int b)
+    {
+	return a + b;
+    }
+}
\ No newline at end of file

Added: trunk/j/examples/abcl/javacall_from_lisp/lispfunctions.lisp
==============================================================================
--- (empty file)
+++ trunk/j/examples/abcl/javacall_from_lisp/lispfunctions.lisp	Fri Nov  7 22:55:43 2008
@@ -0,0 +1,18 @@
+; we need to get the
+; 1) class (Main)
+; 2) classes of the parameters (int)
+; 3) method reference (getting that requires the class
+; of our object and the classes of the parameters
+
+; After that we can invoke the function with jcall,
+; giving the method reference, the object and the parameters.
+; The result is a lisp object (no need to do jobject-lisp-value), 
+; unless we invoke the method
+; with jcall-raw. 
+(defun void-function (param)
+  (let* ((class (jclass "Main"))
+	 (intclass (jclass "int"))
+	 (method (jmethod class "addTwoNumbers" intclass intclass))
+	 (result (jcall method param 2 4)))
+    (format t "in void-function, result of calling addTwoNumbers(2, 4): ~a~%" result)))
+

Added: trunk/j/examples/abcl/lispcall_from_java_simple/Main.java
==============================================================================
--- (empty file)
+++ trunk/j/examples/abcl/lispcall_from_java_simple/Main.java	Fri Nov  7 22:55:43 2008
@@ -0,0 +1,24 @@
+import org.armedbear.lisp.*;
+
+public class Main
+{
+    /**
+     * This example creates an Interpreter instance, loads our
+     * lisp code from a file and then evaluates a function defined
+     * in the loaded lisp file.
+     */
+    public static void main(String[] argv)
+    {
+	try
+	    {
+		Interpreter interpreter = Interpreter.createInstance();
+		interpreter.eval("(load \"lispfunction.lisp\")"); 
+		LispObject myInterface = interpreter.eval("(lispfunction)");
+	    }
+	catch (Throwable t)
+	    {
+		System.out.println("abcl exception!");
+		t.printStackTrace();
+	    }
+    }
+}
\ No newline at end of file

Added: trunk/j/examples/abcl/lispcall_from_java_simple/MainAlternative.java
==============================================================================
--- (empty file)
+++ trunk/j/examples/abcl/lispcall_from_java_simple/MainAlternative.java	Fri Nov  7 22:55:43 2008
@@ -0,0 +1,33 @@
+import org.armedbear.lisp.*;
+
+public class MainAlternative
+{
+    /**
+     * This example creates an Interpreter instance, loads our
+     * lisp code from a file and then looks up a function defined
+     * in the loaded lisp file and executes the function.
+     */
+    public static void main(String[] argv)
+    {
+	try
+	    {
+		Interpreter interpreter = Interpreter.createInstance();
+		interpreter.eval("(load \"lispfunction.lisp\")");
+		// the function is not in a separate package, thus the
+		// correct package is CL-USER. Symbol names are
+		// upper case. Package needs the prefix, because java
+		// also has a class named Package.
+		org.armedbear.lisp.Package defaultPackage = 
+		    Packages.findPackage("CL-USER");
+		Symbol sym = 
+		    defaultPackage.findAccessibleSymbol("LISPFUNCTION");
+		Function function = (Function) sym.getSymbolFunction();
+		function.execute();
+	    }
+	catch (Throwable t)
+	    {
+		System.out.println("abcl exception!");
+		t.printStackTrace();
+	    }
+    }
+}
\ No newline at end of file

Added: trunk/j/examples/abcl/lispcall_from_java_simple/lispfunction.lisp
==============================================================================
--- (empty file)
+++ trunk/j/examples/abcl/lispcall_from_java_simple/lispfunction.lisp	Fri Nov  7 22:55:43 2008
@@ -0,0 +1,2 @@
+(defun lispfunction ()
+  (format t "in lispfunction~%"))

Added: trunk/j/examples/abcl/lispcall_from_java_with_params_and_return/Main.java
==============================================================================
--- (empty file)
+++ trunk/j/examples/abcl/lispcall_from_java_with_params_and_return/Main.java	Fri Nov  7 22:55:43 2008
@@ -0,0 +1,51 @@
+import org.armedbear.lisp.*;
+
+public class Main
+{
+    /**
+     * This example creates an Interpreter instance, loads our
+     * lisp code from a file and then looks up two functions defined
+     * in the loaded lisp file and executes the functions. 
+     *
+     * The first function takes a single parameter and prints its value, 
+     * so we can provide any Object, so we use a String.
+     *
+     * The second function takes two numbers, adds them together, prints
+     * the parameters and the result, and returns the result. 
+     * We use two integers as parameters and just print the result
+     * from java side.
+     */
+    public static void main(String[] argv)
+    {
+	try
+	    {
+		Interpreter interpreter = Interpreter.createInstance();
+		interpreter.eval("(load \"lispfunctions.lisp\")");
+		// the function is not in a separate package, thus the
+		// correct package is CL-USER. Symbol names are
+		// upper case. Package needs the prefix, because java
+		// also has a class named Package.
+		org.armedbear.lisp.Package defaultPackage = 
+		    Packages.findPackage("CL-USER");
+
+		Symbol voidsym = 
+		    defaultPackage.findAccessibleSymbol("VOID-FUNCTION");
+		Function voidFunction = (Function) voidsym.getSymbolFunction();
+		voidFunction.execute(new JavaObject("String given from java"));
+
+		Symbol intsym = 
+		    defaultPackage.findAccessibleSymbol("INT-FUNCTION");
+		Function intFunction = (Function) intsym.getSymbolFunction();
+		LispObject result = 
+		    intFunction.execute(new JavaObject(1), 
+					new JavaObject(6));
+		System.out.print("The result on the java side: ");
+		System.out.println(result.intValue());
+	    }
+	catch (Throwable t)
+	    {
+		System.out.println("abcl exception!");
+		t.printStackTrace();
+	    }
+    }
+}
\ No newline at end of file

Added: trunk/j/examples/abcl/lispcall_from_java_with_params_and_return/lispfunctions.lisp
==============================================================================
--- (empty file)
+++ trunk/j/examples/abcl/lispcall_from_java_with_params_and_return/lispfunctions.lisp	Fri Nov  7 22:55:43 2008
@@ -0,0 +1,14 @@
+; param comes from java, so accessing it require
+; calling jobject-lisp-value on it
+(defun void-function (param)
+  (format t "in void-function, param: ~a~%" (jobject-lisp-value param)))
+
+; params come from java, so accessing them require
+; calling jobject-lisp-value on them
+(defun int-function (jparam1 jparam2)
+  (let* ((param1 (jobject-lisp-value jparam1))
+	 (param2 (jobject-lisp-value jparam2))
+	 (result (+ param1 param2)))
+    (format t "in int-function, params: ~a ~a~%result: ~a~%" 
+	    param1 param2 result) 
+    result))
\ No newline at end of file




More information about the armedbear-cvs mailing list