JESS : Java Expert System Shell

advertisement
JESS : Java Expert
System Shell
Mark Maslyn
mmaslyn@msn.com
Java Expert System Shell
Java Application
A “Rules-Engine”
Developed by Ernest Friedman-Hill at
Sandia National Laboratory
Processes “Jess” Scripting Language
Can by called by Java Programs or act as a
Java Command Interpreter
Java and JESS
Jess Calls
Java
Java Calls Jess
Java App
JESS
JESS Uses
Templates (data structure definitions)
Facts (templates with data values)
Rules (logic and program flow)
Commands (to the JESS shell)
Java Beans
"Hello World" in JESS
c:\>java jess.Main
Jess, the Java Expert System Shell
Copyright (C) 2001 E.J. Friedman Hill and the
Sandia Corporation
Jess Version 6.1p7 5/7/2004
Jess>(printout t "Hello World" crlf)
Hello World
Jess>
Jess Can Call Java Functions
(bind ?frame (new JFrame “Pumps Demo”))
(call (?frame getContentPane)
setLayout(new GridLayout 2 3))
Java Calling JESS
import jess.*;
...
Rete engine = new Rete( );
engine.executeCommand (
“(printout t \”Hello Word\” crlf)”);
Jess Value Types
●
RU.FLOAT
●
float, double
●
RU.INTEGER
●
long, short, int, byte, char
●
RU.LONG
●
Same
●
RU.LIST
●
A Java array
●
RU.STRING
●
String, char, Character
●
Wrapper classes
Returning Values
import jess.*;
...
Rete engine = new Rete( );
Value result = engine.executeCommand (
“(+ 2 2 )”);
System.out.println(result.intValue(null));
Steps to Solve A Problem With Jess
• Define Templates and Facts
• Write Rules
• Run Jess
Templates (Data Structures)
(deftemplate employee
“employees in the database”
(slot name)
(slot gender)
(slot job_title))
Facts (Structures with Values
(employee (name John Smith)
(gender male)
(job_title programmer))
Rule Syntax:
if (LHS) then RHS
defrule example_rule LHS =>
RHS
if (left hand side) conditions are true
then clauses on the right hand side are executed
Example Rule
rule name
(defrule change-baby-if-wet
?wet <- (baby-is-wet)
=> (change-baby)
(retract ?wet))
“if” (LHS)
“then” (RHS)
Rule Conditions Can’t Call Functions
NO
(defrule equals
(eq 1 1) =>
(printout t “1 and 1 are equal” crlf))
YES
(defrule equals
(test (eq 1 1) ) =>
(printout t “1 and 1 are equal” crlf))
Search Space Problems
Jess Can Solve Constraint or Search Space
Problems (e.g. Logic, Scheduling)
4 Golfers Logic Problem
●
Four Golfers: Fred, Joe, Bob and Tom
are in line at the tee
●
Golfer to Fred's right is wearing blue
pants
●
Joe is second in line
●
Bob is wearing plaid pants
●
Tom isn't in position 1 or 4
●
Tom is wearing orange pants
Step 1: Define Templates
(deftemplate pants-color
(slot person)
(slot color))
(deftemplate golfer-position
(slot person)
(slot position))
(pants-color (person Bob) (color red))
(golfer-position (person Bob) (position 2))
Step 2: Write JESS rules to generate
combinations as “facts”
(defrule generate-combos
=>
(foreach ?name (create$ Fred Joe Bob Tom)
(foreach ?color (create$ red blue plaid orange)
(assert (pants-color (person ?name)
(color ?color))))
(foreach ?golfer-position (create$ 1 2 3 4)
(assert (golfer-position (person ?name)
(position ?pos))))))
Step 3: Run JESS to find solution.
(defrule find-solution
;; The golfer to Fred's right is wearing blue pants.
(position (person Fred) (position ?p1))
(pants-color (person Fred) (color ?c1))
...
=>
(printout t Fred " " ?p1 " " ?c1 crlf)
(printout t Joe " " ?p2 " " ?c2 crlf)
(printout t Bob " " ?p3 " " ?c3 crlf)
(printout t Tom " " ?p4 " " ?c4 crlf)
)
Logic Problem Demo
Search Tree
Rules Engine
Jess Can Act As a “Rules Engine” For
Expert System Type Problems (e.g.
Diagnosis, Configuration, Personalization)
Rule Engines Contain
• Rule base
• Working memory
• Inference engine
Asimov's 3 Laws of Robotics
• 1) A robot may not injure a human being or,
through inaction allow a human being to come
to harm.
• 2) A robot must obey orders given it by human
beings except where such orders would conflict
with the First Law.
3 Laws (Continued)
• 3) A robot must protect its own existence as
long as such protection does not conflict with
the First or Second Laws.
Robots Existence Rule
(defrule third_law
(first_law_obeyed)
(second_law_obeyed)
=> protect_robots_existence)
Animals Expert System Demo
“Shadow” Facts and Java
Beans
JESS fact
Java
Bean
(employee
(name “John Smith”)
(gender Male)
(job_title programmer))
Shadow Facts Java Bean
public class dimmerSwitch
{
private int brightness = 0;
public int getBrightness( )
{ return brightness; }
public void setBrightness( int value )
{ brightness = value; }
}
Adding the Java Bean to Jess...
Step 1: Generate a Jess template with defclass
(defclass dimmer dimmerSwitch)
Step 2: Create and bind an instance of this class in Jess
(bind ?ds (new dimmerSwitch))
(definstance dimmer ?ds)
Calling the Java Bean...
Step 3: Call the Java Bean methods
(call ?ds setBrightness 10 ))
Customization and Filtering
Customer
Name, Address
Customer in DB?
Java App
Packages
Promos
DB
Credit Score
JESS
Web Service
Rules
Filtered Results
Lookup Customer and Credit Score
Customer
Name, Address
Customer in DB?
Java App
Packages
Promos
DB
Credit Score
JESS
Web Service
Rules
Filtered Results
Load Packages, Promotions and Rules
Customer
Name, Address
Customer in DB?
Java App
Packages
Promos
DB
Credit Score
JESS
Web Service
Rules
Filtered Results
Example Packages by Partner
Partner
Package
Price
Description
SBC
Free-For-All
49.95
TV with no equipment
to buy!
SBC
Digital Home
Advantage
America's Top 60
29.95
59.95
Sprint
Digital Home
Advantage
America's Top 100
Qwest
Free-For-All
39.95
Connect up to 2 TV's
free!
America's Top 60
Cable Channels!
Connect up to 2 TV's
free!
America's Top 100
Cable Channels!
TV with no equipment
to buy!
Sprint
Sprint
34.95
49.95
Packages and Promotions Templates
deftemplate package (slot partner)
(slot name)
(slot desc)
(slot price))
deftemplate promotion (slot partner)
(slot name)
(slot desc)
(slot amount))
Retrieve Packages By Partner Rule
(assert (package-partner Sprint)) ;; example partner name
…
;; find all packages with Sprint as the partner
(defrule get-packages
(prod-partner ?p)
(package (partner ?p)(name ?n) (desc ?d) (price ?pr))
=>
(assert (results ?p ?n ?d ?pr)))
Filter Packages By Credit Score Rule
(assert (credit-score 620))
…
(defrule filter-by-credit-score
(credit-score ?c)
?f<-(results (partner ?p)(name ?n)(desc ?d) (price ?pr))
(test (and (< ?c 650)(> ?pr 39.95)))
=>
(retract ?f))
Use Rules to Filter Packages and Promotions
Customer
Name, Address
Customer in DB?
Java App
Packages
Promos
DB
Credit Score
JESS
Web Service
Rules
Filtered Results
JESS Pros and Cons
Pros
• Very Flexible System
• Free for Government or Academic Use
• Source Code Available
• Very Fast Rete Algorithm
• Planned Integration with Eclipse
Cons
• Lisp – like syntax
• Limited GUI interface – New Interface in 7.0
Jess Resources
Jess Home Page
herzberg.ca.sandia.gov/jess/index.shtml
Jess In Action (Book)
www.manning.com/friedman-hill
My Website
www.sawatchsoftware.com (click on the
Jess link)
Download