Sei sulla pagina 1di 10

Using Java And Jess Together

Part I: Scripting Java With Jess

Creating Java Objects


1. Data structures in Jess are very limited. But Jess supports access of all Java's data structures, and other classes. 2. Example: We could use a Jess list to store a list of groceries, but a hashtable would be more suitable for looking up prices of grocery items.
Jess> (bind ?prices (new java.util.HashMap)) <External-Address:java.util.HashMap>

Jess treats externally defined objects as having type "EXTERNAL-ADDRESS". Now you can work with ?prices as an ordinary HashMap, with this syntax: (?prices put "potatoes" 2.30)
(?prices put "bread" 1.50) (?prices put "soup" 2.00) (?prices get "bread")

3. Can also use import statements as in Java:


(import java.util.HashMap) (bind ?table (new HashMap))

Calling Java Methods


1. Parameters passed into Java methods, by way of Jess, have to be converted into Java types; return values from Java have to be converted into Jess types. 2. Some Jess ! Java conversions: RU.EXTERNAL_ADDRESS the symbol nil TRUE/FALSE a symbol (RU.ATOM) RU.STRING RU.INTEGER RU.FLOAT RU.LIST The wrapped object null reference String, boolean, or Boolean String, char or Character String or char or Character typically: int or Integer typically: double or Double Java array

3. Some Java ! Jess conversions null void return value String boolean or Boolean int or Integer double, float, Double, Float char or Character array anything else nil nil RU.STRING TRUE or FALSE RU.INTEGER RU.FLOAT RU.ATOM (a symbol) a list RU.EXTERNAL_ADDRESS

4. Calling static methods in Java: requires the use of the 'call' function in Jess: Example:
(call Math pow 2.0 3.0)

This calls the static method pow in the Math class (computes 23)

Using Java Arrays


1. Since the Jess-Java type conversion of a Jess list is a Java array, and conversely, it is often useful to perform conversions to and from arrays within Java 2. Example: Recall that Java HashMap has a keySet function (which returns a Set) and Set has a toArray method, that converts a Set to an array.
HashMap h = new HashMap(); //add some elements Set s = h.keySet(); Object[] arr = s.toArray();

This can be done in Jess:


(bind ; add (bind (bind ?h (new java.util.HashMap)) some elements ?s (?h keySet)) ?list (?s toArray))

Or in one step, like this:


(bind ?list ((?h keySet) toArray))

3. For larger arrays, conversions like this are inefficient this can be minimized by
creating the data structure in Java, then asking Java code to manipulate the data, and read the results into Jess after processing has completed.

Working With Exceptions


1. Jess notifies you when Java throws exceptions; Jess also supports try/catch blocks to handle such exceptions. 2. Example:
(import java.util.*) (import java.io.*) (bind ?file nil) (try (bind ?file (new BufferedReader (new java.io.FileReader "data.txt"))) (while (neq nil (bind ?line (?file readLine))) (printout t ?line crlf)) catch (printout t "Error processing file" crlf) finally (if (neq nil ?file) then (?file close)))

Part II: Running Jess Inside Java

Running Jess In A Java Application


1. Jess provides an API to access Jess funcitions from within a Java application. (See \jess_distribution\Jess61p4\docs\api.html) 2. All API methods reside in the Rete class, included in the jess.jar archive. Therefore, you must include jess.jar as an external jar file for your project. 3. A simple way to execute Jess code from within Java is with these steps: a. prepare your Jess code in a file b. in your Java code, create an instance of the Rete class c. invoke the Jess batch function using the Rete method executeCommand 4. Example
package jesscalldemo; import jess.Rete; public class Demo { public static void main(String[] args) { (new Demo()).callJessFile(); } public void callJessFile() { Rete engine = new Rete(); try { engine.executeCommand("(batch \"late-binding.clp\")"); } catch(Exception e){ e.printStackTrace(); } } }

5. Pieces of Jess code can be run separately in a more efficient way, using the API. More on this later.

6. Exercises
1. Access Java code from Jess to implement the following algorithm for removing duplicates from a list: Given the list origlist, start with an instance of a hashtable. For each element e in origlist, check to see if e is a key in the hashtable; if so, remove the element from origlist; if not, place the entry (e, e) in the hashtable. Your code should use a Java hashtable and a Jess list. The last line of your code should print out the list to the console. To test your code, use the following Jess list: [a b a c a d b f a c h b] 2. Jess does not provide a sorting algorithm for lists. Use the fact that the Java Collections API does provide an efficient sorting algorithm for lists to define a Jess function sort that accepts a list of Jess strings and returns a list of the same strings in sorted (dictionary) order. Test your Jess code on the following list [bob joe frank steve allen david bob joe ralph]; your code should print out the original list and the sorted list. 3. Write a Jess function that uses JDBC to read fname and lname fields from a database table called Customer, passing in the key custid. Assume that custid, fname and lname are the actual column names in the table. Test your function on the test database provided in the student directory. 4. Write a Jess function that computes the nth prime, for any positive integer n. Put your code in a file called primes.clp. Create a Java class called TestPrimeProg.java. In the main method, use the Rete class to test your primes.clp program; try compute the 12th and the 15th prime, and then print these to the console.

Potrebbero piacerti anche