Jess and Java
Jess and Java
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.
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.
When calling a java method, Jess converts jess data types to java types.
HashMap.put associates a key with a value. HashMap.get allows you to look up a value
associated with a key.
Jess>
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)
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>
This creates a collection for the keys contained in the HashMap.
Jess>
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)
)
)
E.g.
(if (instanceof ?ERROR NotBoundException) then….
The jess throw function allows you to throw java exceptions from jess code.
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)
Change the text in the question text area and repaint the frame:
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)
)
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.*)
;; 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))
;; 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)
)