3 - Scripting Java With Jess
3 - Scripting Java With Jess
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. 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)
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.
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.