JESS:Java%20Expert%20System%20Shell - PowerPoint PPT Presentation

About This Presentation
Title:

JESS:Java%20Expert%20System%20Shell

Description:

Jess is faster than some popular expert system shells written in C ... Jess (printout t 'The greater of 3 and 5 is ' (max 3 5) crlf) The greater of 3 and 5 is ... – PowerPoint PPT presentation

Number of Views:227
Avg rating:3.0/5.0
Slides: 44
Provided by: iroUmo
Category:

less

Transcript and Presenter's Notes

Title: JESS:Java%20Expert%20System%20Shell


1
JESSJava Expert System Shell
  • Course IFT6802
  • Student ZHENG ZHEN

2
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

3
  • 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 gt RHS
    action
  • (defrule example-3
  • (not-b-and-c ?n1b ?n2c)
  • (different ?d1 ?d2?d1)
  • gt
  • (printout t "Found what I wanted!" crlf))

4
  • What are advantages?
  • Jess is a rule engine
  • Jess is a scripting environment for Java
  • Jess call Java
  • Jess is faster than some popular expert system
    shells written in C
  • Jess is easy to extend with new commands

5
  • How to start?
  • Start as command-line interface
  • java -classpath jess.jar jess.Main
  • Start as console-style GUIs
  • java -classpath jess.jar jess.Console

6
Introduction
  • Basics
  • Atom letters, numbers and /ltgt_?. 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) gt (printout t "I need to
    buy " ?list crlf))
  • Jessgt (assert (grocery-list eggs milk
    bacon))
  • Global variable ?x or ?all-values

7
  • Function can be defined with two ways
  • 1) define a function directly
  • (deffunction max (?a ?b)
  • (if (gt ?a ?b) then ?a else ?b))
  • Function calls are simply lists
  • Jessgt (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)))
  • Jessgt ( 2 2)
  • 5
  • Examples

Function
Variables
8
  • Java reflection
  • 1) Jess can create and manipulate Java
    objects directly
  • (bind ?ht (new java.util.Hashtable))
  • Jessgt (call ?ht put "key1" Apple")
  • Jessgt (?ht get "key1") Apple"
  • 2) Jess access member variables of Java objects
  • Jessgt (bind ?pt (new java.awt.Point))
  • ltExternal-Addressjava.awt.Pointgt
  • Jessgt (set-member ?pt x 37) 37
  • Jessgt (set-member ?pt y 42) 42
  • Jessgt (get-member ?pt x) 37

9
  • access static members by using the name of the
    class Jessgt (get-member System out)
  • Jess can import either a whole package
  • using (import java.io. )
  • or a single class using
  • (import java.awt.Button)
  • 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
10
  • 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)))

11
  • (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
  • Jessgt (defclass simple ExampleBean)
  • Jessgt (ppdeftemplate simple)

12
  • Jessgt (bind ?sb (new ExampleBean))
  • Jessgt (definstance simple ?sb static)
    dynamic
  • Jessgt (facts)
  • f-0 (MAINsimple
  • (class ltExternal-addressjava.lang.Classgt)
  • (name "Bob")
  • (OBJECT ltExternal-AddressExampleBeangt))
  • For a total of 1 facts
  • A fact representing the Bean appears in the
    knowledge base
  • Examples

Facts
Console
13
  • Rules
  • Rules can take actions based on the
    contents of facts
  • It is similar to if then, but
  • Jessgt (watch all)
  • Jessgt (defrule do-change-baby
  • "If baby is wet, change baby's diaper.
  • (baby-is-wet) gt (change-baby))
  • Jessgt (deffunction change-baby ()
  • (printout t "Baby is now dry" crlf))
  • Jessgt (assert (baby-is-wet))
  • Jessgt (run)
  • FIRE 1 MAINdo-change-baby f-1 Baby is now dry
  • lt Focus MAIN 1

14
  • Basic Patterns
  • Jessgt (defrule example
  • (different ?d1 ?d2?d1)
  • (same ?s ?s)
  • (more-than-one-hundred ?m(gt ?m 100))
  • (red-or-blue redblue)
  • (one-more ?X ( ?X 1))
  • gt
  • (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)

15
  • Pattern bindings
  • A pattern-binding variable used to retract
    /modify the fact
  • Jessgt (defrule example-5 ?fact lt- (a "retract
    me") gt (retract ?fact))
  • 1. Jessgt (assert (a "retract me"))
  • gt f-1 (MAINa "retract me")
  • gt Activation MAINexample-5 f-1
  • ltFact-1gt
  • If we try assert the fact again, Jess prompt
    FALSE
  • 2. Jessgt (assert (a "retract me"))
  • FALSE
  • 3. Jessgt (run)
  • FIRE 1 MAINexample-5 f-1
  • lt f-1 (MAINa "retract me")
  • lt Focus MAIN
  • 1

16
  • 4. Jessgt (run)
  • 0
  • 5. Jessgt (assert (a "retract me"))
  • gt f-2 (MAINa "retract me")
  • gt Activation MAINexample-5 f-2
  • ltFact-2gt
  • 6. Jessgt (facts)
  • f-0 (MAINinitial-fact)
  • f-2 (MAINa "retract me")
  • For a total of 2 facts.
  • Where is the f-1?
  • pattern-binding variable ?fact lt---- fact ID
  • lt----a reference to a jess.Fact object

17
  • Salience
  • Salience is a kind of rule priority, each rule
    has one
  • Salience high
  • be fired
  • if same salience
  • then conflict resolution strategy
  • Salience low
  • Salience values can be integers, global
    variables, or function calls
  • Jessgt (defrule example-6
  • (declare (salience -100)) ... gt .)
  • Or call (set-salience-evaluation ..)

Activated Rule1 Activated Rule2 Activated
Rule3 . . Activated Rule n
18
  • Conditional Element (CE.)
  • 'and' used to construct complex logical
    conditions with or and not
  • 'or'
  • Jessgt (defrule or-example-1 (or (a) (b) (c))
    gt)
  • MAINor-example-1 111t . (1)
  • MAINor-example-11 111t . (2)
  • MAINor-example-12 111t . (3)
  • TRUE
  • Jessgt (assert (a) (b) (c))
  • Jessgt (printout t (run) crlf)
  • FIRE 1 MAINor-example-1 f-3
  • FIRE 2 MAINor-example-1 f-2
  • FIRE 3 MAINor-example-1 f-1
  • 3
  • (1),(2) and (3) are 3 separate
    sub-rules

19
  • 'not
  • Jessgt (defrule forall-example (not (and (a ?x)
    (not (b ?x)))) gt)
  • a gt 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 () gt)
  • the first in an and group (or (..) (not(..))
    ..gt)
  • 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?

20
  • Rearranging not CE together with and and or
  • Based on DeMorgan's rules for logical
    operations
  • 1. (not (and (x) (y))) gt (or (not (x)) (not
    (y)))
  • ? (x ?y) gt (?x) ? (?y)
  • 2. (not (or (x) (y))) gt (and (not (x)) (not
    (y)))
  • ? (x ? y) gt (?x) ?(?y)
  • Two constraints of Jess rule LHS
  • 1. or CE must be at the top level
  • 2. not CE must apply DeMorgan's rules
  • Conjunctive Normal Form
  • atom exclude ? , conjunct with ?

21
  • 'test'
  • Jessgt (deftemplate person (slot age))
  • Jessgt (defrule example-8
  • (person (age ?x))
  • (test (gt ?x 60))
  • gt
  • (printout t ?x " is over 60!" crlf))
  • Jessgt (assert (person (age 65)))
  • gt f-8 (MAINperson (age 65))
  • gt Activation MAINexample-8 f-8,
  • ltFact-8gt
  • Jessgt (run)
  • FIRE 1 MAINexample-8 f-8,
  • 65 is over 60!

22
  • test CE is evaluated
  • as long as evaluated the preceding pattern
  • so
  • Jessgt (defrule rule_1 (foo ?X)
  • (test (gt ?X 3)) gt) same as
  • Jessgt (defrule rule_2 (foo ?X(gt ?X 3)) gt)
  • 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?

23
  • 'logical'
  • logical CE specify logical dependencies among
    facts
  • Jessgt (defrule rule-1 (logical (faucet-open))
  • gt (assert (water-flowing)))
  • Jessgt (assert (faucet-open))
  • Jessgt (run)
  • Jessgt (facts)
  • f-0 (MAINfaucet-open)
  • f-1 (MAINwater-flowing)
  • For a total of 2 facts.
  • Jessgt (watch facts)
  • Jessgt (retract (fact-id 0))
  • lt f-0 (MAINfaucet-open)
  • lt f-1 (MAINwater-flowing)
  • TRUE
  • logical CE must be the first patterns in the
    rule

24
  • 'unique'
  • Jessgt (deftemplate tax-form (slot
    social-security-number))
  • Jessgt (deftemplate person (slot
    social-security-number) (slot
    name))
  • Jessgt (defrule unique-demo
  • (tax-form (social-security-number ?num))
  • (unique (person
    (social-security-number ?num) (name ?name)))
  • gt
  • (printout t "Auditing " ?name "..." crlf))
  • unique CE hint to Jess that only one can have a
    given SSN so Jess dont look further
  • in the same patten,
  • unique dont combine with either test or not
    CEs
  • unique is quite similar to Prologs ! (cut)

25
  • 'exists'
  • Jessgt (defrule exists-demo (exists (honest ?))
  • gt
  • (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

26
  • Forward and backward chaining
  • 1) supports both forward and backward chaining
  • 2) To use backward chaining in Jess, use like
  • Jessgt (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
  • Jessgt (assert (human Socrates))
  • Jessgt (defrule mortal (human ?X)
  • gt
  • (assert (mortal ?X)))
  • Jessgt (watch facts)
  • Jessgt (run)
  • gt f-1 (MAINmortal Socrates) 1

27
  • Defqueries
  • defquery create a special kind of rule with no
    RHS
  • Jessgt (defquery search (declare (variables ?X))
    (foo ?X ?Y))
  • Jessgt (deffacts data (foo blue red) (bar blue
    green)
  • (foo blue pink) (foo red blue)
  • (foo blue blue) (foo orange yellow)
  • (bar blue purple))
  • Jessgt (reset)
  • Jessgt (bind ?it (run-query search blue))
  • Jessgt (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

28
  • Defmodules
  • Modules divide rules and templates into distinct
    groups
  • By default, current module is "MAIN
  • Jessgt (defmodule WORK)
  • Jessgt (deftemplate WORKjob (slot salary))
  • TRUE
  • Jessgt (list-deftemplates WORK)
  • WORKjob
  • For a total of 1 deftemplates.
  • Jessgt (get-current-module)
  • WORK
  • The MAIN is global namespace for templates.

29
  • Focus only rules in the focus module will fire
  • focus module independent from current module
  • Jessgt (defmodule DRIVING)
  • Jessgt (defrule get-in-car
  • gt
  • (printout t "Ready to go!" crlf))
  • Jessgt (reset)
  • Jessgt (run)
  • 0
  • Why not? Which is current module?
  • Jessgt (focus DRIVING)
  • MAIN
  • Jessgt (run)
  • Ready to go!
  • 1

30
  • 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 return from the call like a
    subroutine

31
Jess vs. Java
  • use Jess library in Java
  • import jess.
  • public class ExSquare
  • public static void main(String unused)
  • try
  • Rete r new Rete()
  • r.executeCommand("(deffunction square (?n)
    (return ( ?n ?n)))")
  • Value v r.executeCommand("(square 3)")
    // Prints '9' System.out.println(v.i
    ntValue(r.getGlobalContext()))
  • catch (JessException ex) System.err.println(
    ex)
  • C\gt java ExSquare
  • 9

32
  • 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(), assertFact(Fact),
  • retract(Fact), retract(int), and halt().
  • 4. Executing other Jess commands
  • Rete class's executeCommand(String cmd) a
    parseable String
  • returns the jess.Value object interpreted in
    the global context

33
  • 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

34
  • 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 clearStorage()
  • On jess side
  • (store ltnamegt ltvaluegt)
  • (fetch ltnamegt) (clear-storage)

35
  • 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\gt java ExFetch
  • f-0 (MAINdimension (class ltExternal-Addressjav
    a.lang.Classgt) (height 10.0) (size
    ltExternal-Addressjava.awt.Dimensiongt) (width
    10.0) (OBJECT ltExternal-Addressjava.awt.Dimension
    gt))
  • For a total of 1 facts.

36
  • 7. Adding new functions to the Jess
  • by implements interface Rete.addUserfunction()
  • import jess.
  • public class ExMyUpcase implements
    Userfunction
  • public String getName() return "my-upcase"
  • public Value call(ValueVector vv, Context
    context) throwsJessException
  • return new Value(vv.get(1).stringValue(context)
    .toUpperCase(), RU.STRING)
  • C\gt java ExMyUpcase
  • Jessgt (load-function ExMyUpcase)
  • Jessgt (my-upcase foo)
  • "FOO"

37
Jess vs. Jade
  • Jade Multi-agent platform
  • Jess Jade Multi-intelligent agent platform
  • Jade call Jess Java call Jess
  • Jess template Jade ontology
  • public class BasicJessBehaviour extends
    CyclicBehaviour
  • .
  • public class JessSend implements Userfunction
  • ..
  • jess.executeCommand(ACLJessTemplate())
  • jess.executeCommand("(deftemplate MyAgent (slot
    name))")
  • jess.addUserfunction(new JessSend(myAgent,this))
  • jess.executeCommand("(deffacts MyAgent \"All
    facts about this agent\"
    (MyAgent (name " myAgent.getName() ")))")
  • .

38
Jess VS. Protege
  • Difficult manage large/complex ontologies
  • Ontology editors should be programmable
  • 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/
  • JessTab Combining two popular systems
  • JessTab is a tab plug-in for running Jess inside
    Protégé

39
  • Jess console window in Protégé

40
  • Mirroring Jess definitions in Protégé knowledge
    bases

41
  • Editing Jess definitions in Protégé

42
Agent frameworks
  • Jess Jade Protégé JadeJessProtégé

Jess
Protégé
JADE
43
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 Protégé
  • Download from http//protege.stanford.edu/
Write a Comment
User Comments (0)
About PowerShow.com