bc65580322583a746dde2ffbe6aa177f.ppt
- Количество слайдов: 43
JESS: Java Expert System Shell Course IFT 6802 Student ZHENG ZHEN March 12, 2003 UMontreal ift 6802 1
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, 2003 UMontreal ift 6802 2
facts in Jess look like: (person "Bob Smith" Male 35) or (person (name "Bob Smith") (gender Male) (age 34)) slots rules in Jess look like: A rule has two parts: LHS pattern => RHS action (defrule example-3 (not-b-and-c ? n 1&~b ? n 2&~c) (different ? d 1 ? d 2&~? d 1) => (printout t "Found what I wanted!" crlf)) March 12, 2003 UMontreal ift 6802 3
What are advantages? w Jess is a rule engine w Jess is a scripting environment for Java Jess call Java w Jess is faster than some popular expert system shells written in C w Jess is easy to extend with new commands w… March 12, 2003 UMontreal ift 6802 4
How to start? w Start as command-line interface java -classpath jess. jar jess. Main w Start as console-style GUIs java -classpath jess. jar jess. Console March 12, 2003 UMontreal ift 6802 5
Introduction w 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)) Global variable : ? *x* or ? *all-values* March 12, 2003 UMontreal ift 6802 6
Function: can be defined with two ways 1) define a function directly (deffunction max (? a ? b) (if (> ? a ? b) then ? a else ? b)) Function calls are simply lists Jess> (printout t "The greater of 3 and 5 is " (max 3 5) crlf) The greater of 3 and 5 is 5 2) "wrap" extra code around any Jess function (defadvice before + (bind $? argv (create$ $? argv 1))) Jess> (+ 2 2) 5 Variables Function Examples: March 12, 2003 UMontreal ift 6802 7
w Java reflection can create and manipulate Java objects directly 1) Jess (bind ? ht (new java. util. Hashtable)) Jess> (call ? ht put "key 1" “Apple") Jess> (? ht get "key 1") “Apple" 2) Jess access member variables of Java objects Jess> (bind ? pt (new java. awt. Point)) 37
w access static members by using the name of the class Jess> (get-member System out) w Jess can import either a whole package using (import java. io. * ) or a single class using (import java. awt. Button) w Type conversion (Rete Utilities, RU) null 'nil' boolean/Boolean 'TRUE' , 'FALSE' void 'nil' byte, short, int/wrappers RU. INTEGER String RU. STRING double, float /wrappers RU. FLOAT An array multifield Char/Character RU. ATOM long / Long RU. LONG anything else RU. EXTERNAL_ADDRESS March 12, 2003 UMontreal ift 6802 9
w 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, 2003 UMontreal ift 6802 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 Example. Bean implements Serializable { private String m_name = "Bob"; public String get. Name() { return m_name; } public void set. Name(String s) { m_name = s; } }// end of class Jess> (defclass simple Example. Bean) Jess> (ppdeftemplate simple) March 12, 2003 UMontreal ift 6802 11
Jess> (bind ? sb (new Example. Bean)) Jess> (definstance simple ? sb static) Jess> (facts) dynamic f-0 (MAIN: : simple (class
w 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, 2003 UMontreal ift 6802 13
w Basic Patterns Jess> (defrule example (different ? d 1 ? d 2&~? d 1) (same ? 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, 2003 UMontreal ift 6802 14
w 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
f-2 (MAIN: :" src="https://present5.com/presentation/bc65580322583a746dde2ffbe6aa177f/image-16.jpg" alt="4. Jess> (run) 0 5. Jess> (assert (a "retract me")) ==> f-2 (MAIN: :" />
4. Jess> (run) 0 5. Jess> (assert (a "retract me")) ==> f-2 (MAIN: : a "retract me") ==> Activation: MAIN: : example-5 : f-2
w Salience is a kind of rule priority, each rule has one Salience high Activated. . Activated Rule 1 Rule 2 Rule 3 Rule n be fired if same salience then conflict resolution strategy Salience low Salience values can be integers, global variables, or function calls Jess> (defrule example-6 (declare (salience -100)). . . => …. ) Or call (set-salience-evaluation. . ) March 12, 2003 UMontreal ift 6802 17
w Conditional Element (CE. ) w 'and' used to construct complex logical conditions with or and not w 'or' Jess> (defrule or-example-1 (or (a) (b) (c)) =>) MAIN: : or-example-1: =1+1+1+t ……. (1) MAIN: : or-example-1&1: +1+1+1+t ……. (2) MAIN: : or-example-1&2: +1+1+1+t ……. (3) TRUE Jess> (assert (a) (b) (c)) Jess> (printout t (run) crlf) FIRE 1 MAIN: : or-example-1 f-3 FIRE 2 MAIN: : or-example-1 f-2 FIRE 3 MAIN: : or-example-1 f-1 3 (1), (2) and (3) are 3 separate sub-rules March 12, 2003 UMontreal ift 6802 18
w 'not‘ Jess> (defrule forall-example (not (and (a ? x) (not (b ? x)))) =>) “ a => b ; a V b” cannot define any variables used in subsequent patterns only evaluated a fact matching it exists if a not CE is the first on a rule's LHS (not () …=>) the first in an and group (or (. . ) (not(. . )). . =>) the only pattern on a given branch of an or group then (initial-fact) is inserted as preceding pattern Why usually write (reset) before (run) in the Jess file? March 12, 2003 UMontreal ift 6802 19
Rearranging not CE together with and or Based on De. Morgan's rules for logical operations 1. (not (and (x) (y))) => (or (not (x)) (not (y))) “ (x y) => ( x) ( y)” 2. (not (or (x) (y))) => (and (not (x)) (not (y))) “ (x y) => ( x) ( y)” Two constraints of Jess rule LHS: 1. or CE must be at the top level 2. not CE must apply De. Morgan's rules Conjunctive Normal Form atom exclude ‘ ’ , conjunct with ‘ ‘ March 12, 2003 UMontreal ift 6802 20
w '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,
test CE is evaluated as long as evaluated the preceding pattern so Jess> (defrule_1 (foo ? X) (test (> ? X 3)) =>) same Jess> (defrule_2 (foo ? X&: (> ? X 3)) =>) as 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, 2003 UMontreal ift 6802 22
w 'logical' logical CE specify logical dependencies among facts Jess> (defrule-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. Jess> (watch facts) Jess> (retract (fact-id 0)) <== f-0 (MAIN: : faucet-open) <== f-1 (MAIN: : water-flowing) TRUE logical CE must be the first patterns in the rule March 12, 2003 UMontreal ift 6802 23
w '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)) unique CE hint to Jess that only one can have a given SSN so Jess don’t look further in the same patten, unique don’t combine with either test or not CEs unique is quite similar to Prolog’s ! (cut) March 12, 2003 UMontreal ift 6802 24
w 'exists' Jess> (defrule exists-demo (exists (honest ? )) => (printout t "There is at least one honest man!" crlf)) (exists (A)) same as (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, 2003 UMontreal ift 6802 25
w Forward and backward chaining 1) supports both forward and backward chaining 2) To use backward chaining in Jess, use like Jess> (do-backward-chaining factorial) Prolog is backwards chaining: given the rules human(Socrates). mortal(X) : - human(X) Jess, though, is forwards chaining. Here, you have Jess> (assert (human Socrates)) Jess> (defrule mortal (human ? X) => (assert (mortal ? X))) Jess> (watch facts) Jess> (run) ==> f-1 (MAIN: : mortal Socrates) 1 March 12, 2003 UMontreal ift 6802 26
w 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 orange yellow) (bar blue purple)) Jess> (reset) Jess> (bind ? it (run-query search blue)) Jess> (while (? it has. Next) (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, 2003 UMontreal ift 6802 27
w 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 The MAIN is global namespace for templates. March 12, 2003 UMontreal ift 6802 28
w Focus: only rules in the focus module will fire focus module independent from current module Jess> (defmodule DRIVING) Jess> (defrule get-in-car => (printout t "Ready to go!" crlf)) Jess> (reset) Jess> (run) 0 Why not? Which is current module? Jess> (focus DRIVING) MAIN Jess> (run) Ready to go! 1 March 12, 2003 UMontreal ift 6802 29
w Returning from a rule RHS return terminates the execution of RHS and focus module popped from the focus stack using focus call a module from a rule's RHS using return from the call like a subroutine March 12, 2003 UMontreal ift 6802 30
Jess vs. Java w use Jess library in Java import jess. *; public class Ex. Square { public static void main(String[] unused) { try { Rete r = new Rete(); r. execute. Command("(deffunction square (? n) (return (* ? n)))"); Value v = r. execute. Command("(square 3)"); // Prints '9' System. out. println(v. int. Value(r. get. Global. Context())); } catch (Jess. Exception ex) { System. err. println(ex); } } } C: > java Ex. Square 9 March 12, 2003 UMontreal ift 6802 31
w jess. Rete class : the rule engine itself 1. each jess. Rete object : an independent reasoning engine 2. jess. Rete object in a multithreaded environment assert or retract in a given jess. Rete object at a time 3. Call Jess functions directly in Java run(), reset(), clear(), assert. Fact(Fact), retract(int), and halt(). 4. Executing other Jess commands Rete class's execute. Command(String cmd) a parseable String returns the jess. Value object & interpreted in the global context March 12, 2003 UMontreal ift 6802 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. int. Value(jess. Context) is self-resolving jess. Value. type() return RU. VARIABLE for a jess. Value object jess. Value. resolve. Value() resolve the return value March 12, 2003 UMontreal ift 6802 33
6. Transferring values between Jess and Java On java side methods are available in the jess. Rete public Value store(String name, Value val); public Value store(String name, Object val); public Value fetch(String name); public void clear. Storage(); On jess side : (store
import jess. *; public class Ex. Fetch { public static void main(String[] unused) throws Jess. Exception { Rete r = new Rete(); r. store("DIMENSION", new java. awt. Dimension(10, 10)); r. execute. Command("(defclass dimension java. awt. Dimension)"); r. execute. Command("(definstance dimension (fetch. DIMENSION) static)"); r. execute. Command("(facts)"); } } C: > java Ex. Fetch f-0 (MAIN: : dimension (class
7. Adding new functions to the Jess by implements interface Rete. add. Userfunction() import jess. *; public class Ex. My. Upcase implements Userfunction { public String get. Name() { return "my-upcase"; } public Value call(Value. Vector vv, Context context) throws. Jess. Exception { return new Value(vv. get(1). string. Value(context). to. Upper. Case(), RU. STRING); } } C: > java Ex. My. Upcase Jess> (load-function Ex. My. Upcase) Jess> (my-upcase foo) "FOO" March 12, 2003 UMontreal ift 6802 36
Jess vs. Jade w Jade : Multi-agent platform w Jess + Jade : Multi-intelligent agent platform Jade call Jess = Java call Jess template ~ Jade ontology public class Basic. Jess. Behaviour extends Cyclic. Behaviour{ …. public class Jess. Send implements Userfunction { …. . jess. execute. Command(ACLJess. Template()); jess. execute. Command("(deftemplate My. Agent (slot name))"); jess. add. Userfunction(new Jess. Send(my. Agent, this)); jess. execute. Command("(deffacts My. Agent "All facts about this agent" (My. Agent (name " + my. Agent. get. Name() + ")))"); …. March 12, 2003 UMontreal ift 6802 37
Jess VS. Protege w Difficult manage large/complex ontologies – Ontology editors should be programmable w Difficult to integrate problem solving and ontology development – OO languages/shells need an graphical counterpart Protégé: – Knowledge acquisition and ontology development tool – Developed by SMI, Stanford University – http: //protege. stanford. edu/ Jess. Tab – Combining two popular systems Jess. Tab is a tab plug-in for running Jess inside Protégé March 12, 2003 UMontreal ift 6802 38
w Jess console window in Protégé March 12, 2003 UMontreal ift 6802 39
w Mirroring Jess definitions in Protégé knowledge bases March 12, 2003 UMontreal ift 6802 40
w Editing Jess definitions in Protégé March 12, 2003 UMontreal ift 6802 41
Agent frameworks w Jess + Jade + Protégé = Jade. Jess. Protégé Fuzzy. Jess Prolog. Tab Jess. Tab More Jess extension s Flora. Tab Jess Protégé Jade. Jess. Protege Your tab JADE More Protégé plug-ins Your system March 12, 2003 UMontreal ift 6802 42
References w http: //www. iro. umontreal. ca/~vaucher/ift 6802/Guide. html w Obtain Jess – Download from http: //herzberg. ca. sandia. gov/jess/ – License required (commercial or free academic) – Compilation required w Get Jess. Tab – Download from http: //www. ida. liu. se/~her/Jess. Tab/ w Obtain Protégé – Download from http: //protege. stanford. edu/ March 12, 2003 UMontreal ift 6802 43