JESS:Java Expert System Shell
JESS:Java Expert System Shell
JESS:Java Expert System Shell
System Shell
Course IFT6802
Student ZHENG ZHEN
UMontreal ift6802
Overview
What is Jess?
Jess is a tool for building a type of
intelligent software called Expert Systems.
An Expert System:
a set of rules can be repeatedly applied to
a collection of facts
Developed by Sandia Laboratories
March 12,
UMontreal ift6802
UMontreal ift6802
March 12,
UMontreal ift6802
How to start?
Start as command-line interface
java -classpath jess.jar jess.Main
March 12,
UMontreal ift6802
Introduction
Basics:
Atom letters, numbers and $*=+/<>_?#. case
sensitive Numbers, Strings, Comments (;)
Lists fundamental unit of syntax in Jess ( a b c),
variables (?) + atoms : ?x
multivariable $+ variable: $?y
(defrule example
(grocery-list $?list) => (printout t "I need to buy " $?list crlf))
Jess> (assert (grocery-list eggs milk bacon))
UMontreal ift6802
March 12,
UMontreal ift6802
Java reflection
Jess can create and manipulate Java objects
directly
1)
March 12,
UMontreal ift6802
37
42
'nil'
boolean/Boolean
'TRUE' ,'FALSE'
void
'nil'
RU.INTEGER
String
RU.STRING
RU.FLOAT
An array
multifield
Char/Character
RU.ATOM
long / Long
RU.LONG
anything else
RU.EXTERNAL_ADDRESS
March 12,
UMontreal ift6802
Facts
Facts is one of the most important parts of Jess
(1) ordered facts
Facts
(2) unordered facts
(3) definstance facts
(1): (assert (person "Bob Smith" Male 35) )
(2): using the deftemplate define the slots
(deftemplate person
(slot name )
(slot age )
(slot gender) )
after that assert unordered fact:
(assert (person (name "Bob Smith") (age 34) (gender
Male)))
March 12,
UMontreal ift6802
10
(3):
template similarity
Java Beans
slots similarity
properties
Jess provides:
defclass Java Beans
Jess template
definstance Bean representation
fact base
import java.io.Serializable;
public class ExampleBean implements Serializable {
private String m_name = "Bob";
public String getName() { return m_name; }
public void setName(String s) { m_name = s; }
}// end of class
March 12,
UMontreal ift6802
11
dynamic
f-0 (MAIN::simple
(class <External-address:java.lang.Class>)
(name "Bob")
(OBJECT <External-Address:ExampleBean>))
For a total of 1 facts
A fact representing the Bean appears in the
knowledge base
Examples:
March 12,
Facts
Console
UMontreal ift6802
12
Rules
Rules can take actions based on the contents of facts
It is similar to if then, but
Jess> (watch all)
Jess> (defrule do-change-baby
"If baby is wet, change baby's diaper.
(baby-is-wet) => (change-baby))
Jess> (deffunction change-baby ()
(printout t "Baby is now dry" crlf))
Jess> (assert (baby-is-wet))
Jess> (run)
FIRE 1 MAIN::do-change-baby f-1 Baby is now
dry
<== Focus MAIN 1
March 12,
UMontreal ift6802
13
Basic Patterns
Jess> (defrule example
(different ?d1 ?d2&~?d1)
(same ?s ?s)
(more-than-one-hundred ?m&:(> ?m 100))
(red-or-blue red|blue)
(one-more ?X =(+ ?X 1))
=>
(printout t "Found what I wanted!" crlf))
Which facts can fire this rule?
1. (different 100 11) 2. (same d d) 3. (red-or-blue
white)
4. (more-than-one-hundred 101) 5. (one-more 72
73)
March 12,
UMontreal ift6802
14
Pattern bindings
A pattern-binding variable used to retract /modify the fact
Jess> (defrule example-5 ?fact <- (a "retract me") => (retract ?
fact))
1. Jess> (assert (a "retract me"))
==> f-1 (MAIN::a "retract me")
==> Activation: MAIN::example-5 : f-1
<Fact-1>
If we try assert the fact again, Jess prompt FALSE
2. Jess> (assert (a "retract me"))
FALSE
3. Jess> (run)
FIRE 1 MAIN::example-5 f-1
<== f-1 (MAIN::a "retract me")
<== Focus MAIN
1
March 12,
UMontreal ift6802
15
4. Jess> (run)
0
5. Jess> (assert (a "retract me"))
==> f-2 (MAIN::a "retract me")
==> Activation: MAIN::example-5 : f-2
<Fact-2>
6. Jess> (facts)
f-0 (MAIN::initial-fact)
f-2 (MAIN::a "retract me")
For a total of 2 facts.
Where is the f-1?
UMontreal ift6802
16
Salience
Salience is a kind of rule priority, each rule has one
Salience high
Activated
Activated
Activated
.
.
Activated
Rule1
Rule2
Rule3
Rule n
be fired
if same salience
then conflict resolution strategy
Salience low
Salience values can be integers, global variables, or function calls
UMontreal ift6802
17
. (1)
. (2)
. (3)
March 12,
UMontreal ift6802
18
'not
Jess> (defrule forall-example (not (and (a ?x) (not (b ?
x)))) =>)
a => b ; a V b
March 12,
UMontreal ift6802
19
March 12,
UMontreal ift6802
20
'test'
Jess> (deftemplate person (slot age))
Jess> (defrule example-8
(person (age ?x))
(test (> ?x 60))
=>
(printout t ?x " is over 60!" crlf))
Jess> (assert (person (age 65)))
==> f-8 (MAIN::person (age 65))
==> Activation: MAIN::example-8 : f-8,
<Fact-8>
Jess> (run)
FIRE 1 MAIN::example-8 f-8,
65 is over 60!
March 12,
UMontreal ift6802
21
test CE is evaluated
as long as evaluated the preceding pattern
so
Jess> (defrule rule_1 (foo ?X)
(test (> ?X 3)) =>)
same as
Jess> (defrule rule_2 (foo ?X&:(> ?X 3)) =>)
therefor
IF a test CE is the first pattern on the LHS , or
the first pattern in a branch of an or CE
(initial-fact) must insert as the "preceding
pattern"
Why usually write (reset) before (run) in the Jess file?
March 12,
UMontreal ift6802
22
'logical'
logical CE specify logical dependencies among facts
Jess> (defrule rule-1 (logical (faucet-open))
=> (assert (water-flowing)))
Jess> (assert (faucet-open))
Jess> (run)
Jess> (facts)
f-0 (MAIN::faucet-open)
f-1 (MAIN::water-flowing)
For a total of 2 facts.
March 12,
UMontreal ift6802
23
'unique'
Jess> (deftemplate tax-form (slot social-security-number))
Jess> (deftemplate person (slot social-security-number)
(slot name))
Jess> (defrule unique-demo
(tax-form (social-security-number ?num))
(unique (person (social-security-number ?num)
(name ?name)))
=>
(printout t "Auditing " ?name "..." crlf))
March 12,
UMontreal ift6802
24
'exists'
Jess> (defrule exists-demo (exists (honest ?))
=>
(printout t "There is at least one honest
man!" crlf))
(exists (A)) same as (not (not (A))).
in the same pattern ,
exists may not be combined with a test
CE.
Combination of various CE can provide the
powerful flexible inference engine
March 12,
UMontreal ift6802
25
March 12,
UMontreal ift6802
26
Defqueries
defquery create a special kind of rule with no RHS
Jess> (defquery search (declare (variables ?X)) (foo ?
X ?Y))
Jess> (deffacts data (foo blue red) (bar blue green)
(foo blue pink) (foo red blue)
(foo blue blue) (foo orange yellow)
(bar blue purple))
Jess> (reset)
Jess> (bind ?it (run-query search blue))
Jess> (while (?it hasNext)
(bind ?token (call ?it next))
(bind ?fact (call ?token fact 1))
(bind ?slot (fact-slot-value ?fact __data))
(bind ?datum (nth$ 2 ?slot))
(printout t ?datum crlf))
red pink blue FALSE
March 12,
UMontreal ift6802
27
Defmodules
Modules divide rules and templates into distinct
groups
By default, current module is "MAIN::
Jess> (defmodule WORK)
Jess> (deftemplate WORK::job (slot
salary))
TRUE
Jess> (list-deftemplates WORK)
WORK::job
For a total of 1 deftemplates.
Jess> (get-current-module)
WORK
UMontreal ift6802
28
March 12,
UMontreal ift6802
29
March 12,
UMontreal ift6802
30
March 12,
UMontreal ift6802
31
March 12,
UMontreal ift6802
32
5. Value resolution
static values (atoms, numbers, strings)
jess.Value
dynamic values (variables, function calls)
dynamic values need
to be interpreted in a particular context before use
jess.Value.intValue(jess.Context) is self-resolving
jess.Value. type() return RU.VARIABLE for a jess.Value object
jess.Value. resolveValue() resolve the return value
March 12,
UMontreal ift6802
33
6. Transferring
On jess side :
(store <name> <value>)
(fetch <name>) (clear-storage)
March 12,
UMontreal ift6802
34
import jess.*;
public class ExFetch {
public static void main(String[] unused) throws
JessException {
Rete r = new Rete();
r.store("DIMENSION", new java.awt.Dimension(10, 10));
r.executeCommand("(defclass dimension
java.awt.Dimension)");
r.executeCommand("(definstance dimension
(fetchDIMENSION) static)");
r.executeCommand("(facts)");
}
}
C:\> java ExFetch
f-0 (MAIN::dimension (class <ExternalAddress:java.lang.Class>) (height 10.0) (size <ExternalAddress:java.awt.Dimension>) (width 10.0) (OBJECT
<External-Address:java.awt.Dimension>))
For a total of 1 facts.
March 12,
UMontreal ift6802
35
March 12,
UMontreal ift6802
36
March 12,
UMontreal ift6802
37
Protg:
Knowledge acquisition and ontology development tool
Developed by SMI, Stanford University
http://protege.stanford.edu/
UMontreal ift6802
38
March 12,
UMontreal ift6802
39
March 12,
UMontreal ift6802
40
March 12,
UMontreal ift6802
41
Agent frameworks
Jess + Jade + Protg = JadeJessProtg
FuzzyJess
PrologTab
JessTab
More
Jess
extension
s
FloraTab
Jess
Protg
JadeJessProtege
Your tab
JADE
More
Protg
plug-ins
Your system
March 12,
UMontreal ift6802
42
References
http://www.iro.umontreal.ca/~vaucher/ift6802/Guide.html
Obtain Jess
Download from http://herzberg.ca.sandia.gov/jess/
License required (commercial or free academic)
Compilation required
Get JessTab
Download from http://www.ida.liu.se/~her/JessTab/
Obtain Protg
Download from http://protege.stanford.edu/
March 12,
UMontreal ift6802
43
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies: