0% found this document useful (0 votes)
48 views

Jess and Java

This document discusses how to: 1. Create Java objects and call methods from within Jess using functions like new, call, import, and get/set-member. 2. Handle exceptions, create graphical user interfaces with AWT/Swing, add event handlers for components, and interact with users through a GUI frame. 3. Dynamically change GUI elements like replacing a text field with a combo box and button in response to user input.

Uploaded by

olivukovic
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views

Jess and Java

This document discusses how to: 1. Create Java objects and call methods from within Jess using functions like new, call, import, and get/set-member. 2. Handle exceptions, create graphical user interfaces with AWT/Swing, add event handlers for components, and interact with users through a GUI frame. 3. Dynamically change GUI elements like replacing a text field with a combo box and button in response to user input.

Uploaded by

olivukovic
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 6

Scripting Java with Jess

Creating Java Objects


You’ve already learnt how to create lists in Jess but they are quite difficult to manipulate. You
can use the HashMap to allow you to create a list and perform lookups. To create a new java
object from within Jess you use the new function.

E.g.

Jess> (reset)
Jess> (bind ?prices (new java.util.HashMap)
<External-Address:java.util.HashMap>

Rather than using fully qualified package names Jess has an import function that allows you
to implicitly import the entire package.

Jess>(bind ?prices nil)


Jess> (import java.util.*)
Jess>(bind ?prices (new HashMap))
<External-Address:java.util.HashMap>

You can also pass arguments to the constructor of a class but rather than enclosing them in
brackets as in Java, list them after the class name and separate by spaces.

Jess>(bind ?prices nil)


Jess> (import java.util.*)
Jess>(bind ?prices (new HashMap 20 5))
<External-Address:java.util.HashMap>

When calling a java method, Jess converts jess data types to java types.

Jess type Java Type


Nil Null reference
TRUE or FALSE String, java.lang.Boolean
RU.FLOAT Float,double
RU.INTEGER Long,short,int,byte,char
RU.LONG Long,short,int,byte,char
RU.LIST Java array

Calling Java Methods


You can invoke any of an objects methods using the call function.

HashMap.put associates a key with a value. HashMap.get allows you to look up a value
associated with a key.

Using the example of a shopping list

Jess> (call ?prices put milk 1.00)


Jess> (call ?prices put bread 1.50)
Jess> (call ?prices put tea 2.40)
Jess> (call ?prices put coffee 1.50)
Jess> (call ?prices get milk)
1.0

Jess>

You can omit the word call.


Jess> (?prices put apples 1.00)
Jess> (?prices get coffee)
1.5

Jess>

You can obviously embed these calls within rules, bind the results to variables etc.

When you are calling static functions you must use the call function.

E.g.
Jess> (call Thread sleep 1000)

Calling Set and Get Methods

Jess> (bind ?b (new javax.swing.JButton))


<External-Address:javax.swing.JButton>

Jess> (?b setText "OK")


Jess> (get ?b text)
"OK"Jess> (set ?b text "CANCEL")
Jess> (get ?b text)
"CANCEL"

Jess>

Note you can only use the second method if good java standards have been employed in the
relevant class. By convention getters are named getProperty or setProperty where Property is
the name of the property of the class to be amended. If this convention is not employed the
second method won’t work since jess builds the call based on the property name to be
amended included in the function call.

Arrays
You could use an array rather than a HashMap to handle the shopping list.

You can get the HashMap to convert itself to an array first to make life easier.

Jess> (bind ?list ((?prices keySet) toArray))


("bread" "tea" "apples" "coffee" "milk")

Jess>
This creates a collection for the keys contained in the HashMap.

You can then create a combo box from this list.

Jess> (import javax.swing.JComboBox)


TRUE

Jess> (bind ?jcb nil)

Jess> (bind ?jcb (new JComboBox ?list))


<External-Address:javax.swing.JComboBox>

Jess>

Accessing Java Members


Jess provides two functions get-member and set-member to allow you to access member
variables of a class.

Exceptions
By default if jess catches a java or jess exception, it prints a detailed message with stack
traces on the console. This is fine for developers but not for users. Regardless of whether you
are interacting with java or not, you should provide a handler to be executed in response to
errors.

Similar to java, jess provides a try function which can have a catch or finally block.

E.g.

Create a file called data.txt on your F drive. This code will open it, read all records and print to
the screen.

(import java.io.*)
(bind ?file nil)
(try
(bind ?file
(new BufferedReader
(new java.io.FileReader "f:\\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)
)
)

As with java a catch or finally block is required for every try.


Note: you can only have one catch block in jess.
To distinguish between exception types you can use the ?ERROR variable and the instanceof
function.

E.g.
(if (instanceof ?ERROR NotBoundException) then….

The jess throw function allows you to throw java exceptions from jess code.

Jess> (throw (new Exception “new jess exception))

Graphical user interfaces


You’ve already seen that you can interact with the swing package. You can also interact with
awt.

To create a window in which graphical elements appear, you need to create a frame.

E.g. to create a frame, size it, make it visible and keep a reference to it in a global variable

(import javax.swing.*)
(import java.awt.*)
(import java.awt.event.*)
(defglobal ?*frame* = (new JFrame "Sample Application"))
(?*frame* setSize 520 140)
(?*frame* setVisible TRUE)

You need now to control interaction with the user through the frame. At its simplest this
requires the creation of a text area in which you can display questions inside a scroll pane to
allow sizing of the text.

(import javax.swing.*)
(import java.awt.*)
(import java.awt.event.*)
(defglobal ?*frame* = (new JFrame "Sample Application"))
(?*frame* setSize 520 140)

(bind ?scroll nil)


(defglobal ?*qfield* = (new JTextArea 5 40))
(bind ?scroll (new JScrollPane ?*qfield*))
((?*frame* getContentPane) add ?scroll)
(?*qfield* setText "Testing...")
(?*frame* repaint)
(?*frame* setVisible TRUE)

Change the text in the question text area and repaint the frame:

(?*qfield* setText "ksksksk")


(?*frame* repaint)

Asking the user for input


Need to add an input area to the application window.

defglobal ?*apanel* = (new JPanel))


(defglobal ?*afield* = (new JTextField 40))
(defglobal ?*afield-ok* = (new JButton OK))
(?*apanel* add ?*afield*)
(?*apanel* add ?*afield-ok*)
((?*frame* getContentPane) add ?*apanel* (get-member BorderLayout SOUTH))
(?*frame* validate)
(?*frame* repaint)

Add behaviour to components like buttons and text fields by writing event handlers.

;; attach event handler – Simply accepts text in input and when user presses ok displays this
text in area where question was previously displayed
(deffunction read-input (?EVENT)
(bind ?text (sym-cat (?*afield* getText)))
(?*qfield* setText ?text)
)

(bind ?handler (new jess.awt.ActionListener read-input (engine)))


(?*afield* addActionListener ?handler)
(?*afield-ok* addActionListener ?handler)
(?*frame* repaint)

Adding Combo Boxes

Need to be able to accept multiple choices.


Set up a Combo-box and an associated OK button.

Code achieves much the same as before. Have single input text box and ok button.
When the ok button is clicked, the field and ok button are removed and a combo box and
associated button are added.
Clicking the ok button results in the choice selected been printed on the question text box.

(import javax.swing.*)
(import java.awt.*)
(import java.awt.event.*)

(defglobal ?*frame* = (new JFrame "Diagnostic Assistant"))


(?*frame* setDefaultCloseOperation (get-member JFrame EXIT_ON_CLOSE))
(?*frame* setSize 520 140)
(?*frame* setVisible TRUE)

;; Question field
(defglobal ?*qfield* = (new JTextArea 5 40))
(bind ?scroll (new JScrollPane ?*qfield*))
((?*frame* getContentPane) add ?scroll)
(?*qfield* setText "Please wait...")

;; Answer area
(defglobal ?*apanel* = (new JPanel))
(defglobal ?*afield* = (new JTextField 40))
(defglobal ?*afield-ok* = (new JButton OK))

(defglobal ?*acombo* = (new JComboBox (create$ "yes" "no")))


(defglobal ?*acombo-ok* = (new JButton OK))

(?*apanel* add ?*afield*)


(?*apanel* add ?*afield-ok*)
((?*frame* getContentPane) add ?*apanel* (get-member BorderLayout SOUTH))
(?*frame* validate)
(?*frame* repaint)

;; When text is read from text field, add combo box and button to panel and repaint
(deffunction read-input (?EVENT)
"An event handler for the user input field"
(bind ?ctext (sym-cat (?*afield* getText)))

(?*apanel* removeAll)
(?*apanel* add ?*acombo*)
(?*apanel* add ?*acombo-ok*)
(?*apanel* validate)
(?*apanel* repaint)
)

;; associate handlers with OK button


(bind ?handler (new jess.awt.ActionListener read-input (engine)))
(?*afield* addActionListener ?handler)
(?*afield-ok* addActionListener ?handler)

(deffunction combo-input (?EVENT)


"An event handler for the combo box"
(bind ?ctext (sym-cat (?*acombo* getSelectedItem)))
(?*qfield* setText ?ctext)
(?*apanel* repaint))
;; associate handler with combo box
(bind ?handler (new jess.awt.ActionListener combo-input (engine)))
(?*acombo-ok* addActionListener ?handler)

You might also like