Professional Documents
Culture Documents
JESS, which stands for Java Expert System Shell, is a powerful system that allows the solution of rule-
based problems. Jess is written entirely in Java and as a result, it is possible to run any Java code inside
of it, with the exception of defining new classes. Jess uses a Common LISP (CLISP) type syntax and
familiarity with CLISP is recommended.
java jess.Main
Jess will display information about itself, then gives the prompt Jess>. From the prompt, a program can
be entered in line-by-line, or else pre-made program file can be run.
to run myJessProgram.clp.
Variables:
There are two types of variables in Jess, local and global. Local variables are created when first
referenced and are destroyed by (reset), while globals must be declared in advance and are not
destroyed by (reset).
Local Variables:
A Jess variable can hold any type of value, including integers, strings, and external Java objects.
Global Variables:
Global variables have the form ?*variable*, and must be declared with (defglobal).
Functions:
Jess has a number of built in functions and has the ability to let users design their own. The
simplest are the arithmetic functions, +, - , *, and /:
Jess> (+ 2 3)
Jess> (/ 4 8)
0.5
Jess> (> 3 4)
FALSE
Jess> (<= 3 4)
TRUE
To design a custom function in Jess, use (deffunction). The following code calculates ?a / ?b + ?c
TRUE
Jess> (myMath 1 2 3)
3.5
Facts:
As said in the Jess Manual in section 2.7, A rule-based system maintains a collection of
knowledge nuggets called facts. This collection is known as the knowledge base. It is somewhat akin to a
relational database, especially in that the facts must have a specific structure. The two most useful kinds
of facts are ordered and unordered. An ordered fact is simply a list, while an unordered fact is a
collection of fields with values.
Ordered facts:
Jess> (reset)
TRUE
<Fact-1>
<Fact-2>
View all facts with (facts) and retract a single fact with (retract):
TRUE
Unordered facts:
Unordered facts in Jess are similar to structs in C. Templates for unordered facts are defined
using (deftemplate). Each field can hold either one or several pieces of data, using either the slot or
multislot option, respectively.
A college course
(slot course_name)
(slot course_number)
(multislot textbooks))
TRUE
<Fact-1>
Rules:
Rules are the methods that read and alter the knowledge base. Rules have two parts, the first is
the circumstance that the rule can be used, and the second is the result. As an example, a hungry agent
can set up a goal to eat.
Jess> (reset)
TRUE
(hungry)
=>
(assert (plan-to-eat)))
TRUE
<Fact-1>
Jess> (run)
Jess> (facts)
f-0 (MAIN::initial-fact)
f-1 (MAIN::hungry)
f-2 (MAIN::plan-to-eat)
Jess> (reset)
TRUE
(slot hungry))
TRUE
(food-available)
=>
TRUE
<Fact-1>
<Fact-2>
Jess> (run)
Jess> (facts)
f-0 (MAIN::initial-fact)
f-1 (MAIN::food-available)
Several things happen in the rule. A pointer to the entire agent is held in ?agent, while the agents name
is stored in ?name. There are two prerequisites, one that there is a hungry agent, and one that food is
available. To change the hungry attribute, (modify) is used on the pointer to the agent.
The boolean relations and, or, and not can be used in rules, such as (and (a) (not (b)). Test can perform
numeric comparisons, such as (test (= ?a ?b)), which will compare the temporary variables ?a and ?b and
return TRUE if they are equal. Be careful not to use just (= ?a ?b) as this will simply search the facts for
something like (= 1 4).
Although it is possible to do almost anything to do with Java within Jess, it is often easier to
write a separate Java class and interface with it. The following example calculates the nth fibonacci
number with a Java method.
easy_java.clp
(defglobal ?*external-class* = 0)
(deffunction initialize ()
(initialize)
(printout t "The 6th fibonacci number is: " (?*external-class* fibonacci 6) crlf)
easy_java.java
Just as in standard Java, an instance of the class must be declared. The first (bind) acts the same as the
Java code easy_java external_class = new easy_java();. Jess doesn't require periods or parenthesis to call
methods, so the call (?*external-class* fibonacci 6) is the same as external_class.fibonacci(6);.