Presentation is loading. Please wait.

Presentation is loading. Please wait.

March 12, 2003UMontreal ift68021 JESS:Java Expert System Shell Course IFT6802 Student ZHENG ZHEN.

Similar presentations


Presentation on theme: "March 12, 2003UMontreal ift68021 JESS:Java Expert System Shell Course IFT6802 Student ZHENG ZHEN."— Presentation transcript:

1

2 March 12, 2003UMontreal ift68021 JESS:Java Expert System Shell Course IFT6802 Student ZHENG ZHEN

3 March 12, 2003UMontreal ift68022 Overview What is Jess? tool for building intelligent software Jess is a tool for building a type of intelligent software called Expert Systems. An Expert System : a set of rules a set of rules can be repeatedly applied to a collection of facts Developed by Sandia Laboratories

4 March 12, 2003UMontreal ift68023 facts facts in Jess look like: (person "Bob Smith" Male 35) or (person (name "Bob Smith") (gender Male) (age 34)) slots rules rules in Jess look like: A rule A rule has two parts: LHS pattern => RHS action (defrule example-3 (not-b-and-c ?n1&~b ?n2&~c) (different ?d1 ?d2&~?d1) => (printout t "Found what I wanted!" crlf))

5 March 12, 2003UMontreal ift68024 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  …

6 March 12, 2003UMontreal ift68025 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

7 March 12, 2003UMontreal ift68026 Introduction  Basics: Atom Atom letters, numbers and $*=+/<>_?#. case sensitive Numbers, Strings, Comments (;) Lists Lists fundamental unit of syntax in Jess ( a b c), variables variables (?) + atoms : ?x multivariable 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 Global variable : ?*x* or ?*all-values*

8 March 12, 2003UMontreal ift68027 Function: 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 Examples: FunctionVariables

9 March 12, 2003UMontreal ift68028  Java reflection 1) Jess can create and manipulate Java objects directly (bind ?ht (new java.util.Hashtable)) Jess> (call ?ht put "key1" “Apple") Jess> (?ht get "key1") “Apple" 2) Jess access member variables of Java objects Jess> (bind ?pt (new java.awt.Point)) Jess> (set-member ?pt x 37) 37 Jess> (set-member ?pt y 42) 42 Jess> (get-member ?pt x) 37

10 March 12, 2003UMontreal ift68029  access static members by using the name of the class Jess> (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/wrappersRU.INTEGER StringRU.STRINGdouble, float /wrappersRU.FLOAT An arraymultifieldChar/CharacterRU.ATOM long / LongRU.LONGanything elseRU.EXTERNAL_ADDRESS

11 March 12, 2003UMontreal ift680210  Facts Facts Facts is one of the most important parts of Jess ordered facts (1) ordered facts Factsunordered facts Facts (2) unordered facts definstance 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)))

12 March 12, 2003UMontreal ift680211 (3): template similarity Java Beans slots similarity properties Jess provides: defclassJava 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 Jess> (defclass simple ExampleBean) Jess> (ppdeftemplate simple)

13 March 12, 2003UMontreal ift680212 Jess> (bind ?sb (new ExampleBean)) Jess> (definstance simple ?sb static) dynamic Jess> (facts) f-0 (MAIN::simple (class ) (name "Bob") (OBJECT )) For a total of 1 facts A fact representing the Bean appears in the knowledge base Examples: FactsConsole

14 March 12, 2003UMontreal ift680213  Rules 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

15 March 12, 2003UMontreal ift680214  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)

16 March 12, 2003UMontreal ift680215  Pattern bindings A pattern-binding variable used to retract /modify the fact Jess> (defrule example-5 ?fact (retract ?fact)) 1.Jess> (assert (a "retract me")) ==> f-1 (MAIN::a "retract me") ==> Activation: MAIN::example-5 : f-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

17 March 12, 2003UMontreal ift680216 4. Jess> (run) 0 5. Jess> (assert (a "retract me")) ==> f-2 (MAIN::a "retract me") ==> Activation: MAIN::example-5 : f-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? pattern-binding variable ?fact <---- fact ID <----a reference to a jess.Fact objectjess.Fact

18 March 12, 2003UMontreal ift680217  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 Jess> (defrule example-6 (declare (salience -100))... => ….) Or call(set-salience-evaluation..) Activated Rule1 Activated Rule2 Activated Rule3. Activated Rule n

19 March 12, 2003UMontreal ift680218  Conditional Element (CE.)  'and' used to construct complex logical conditions with or and notornot  '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

20 March 12, 2003UMontreal ift680219  '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?

21 March 12, 2003UMontreal ift680220 Rearranging not CE together with and and or Based on DeMorgan'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 DeMorgan's rules Conjunctive Normal Form atom exclude ‘  ’, conjunct with ‘  ‘

22 March 12, 2003UMontreal ift680221  '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, Jess> (run) FIRE 1 MAIN::example-8 f-8, 65 is over 60!

23 March 12, 2003UMontreal ift680222 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?

24 March 12, 2003UMontreal ift680223  '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. 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

25 March 12, 2003UMontreal ift680224  '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)

26 March 12, 2003UMontreal ift680225  '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

27 March 12, 2003UMontreal ift680226  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

28 March 12, 2003UMontreal ift680227  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

29 March 12, 2003UMontreal ift680228  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.

30 March 12, 2003UMontreal ift680229

31 March 12, 2003UMontreal ift680230  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

32 March 12, 2003UMontreal ift680231 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.intValue(r.getGlobalContext())); } catch (JessException ex) { System.err.println(ex); } } C:\> java ExSquare 9

33 March 12, 2003UMontreal ift680232  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

34 March 12, 2003UMontreal ift680233 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

35 March 12, 2003UMontreal ift680234 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 ) (fetch ) (clear-storage)

36 March 12, 2003UMontreal ift680235 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 ) (height 10.0) (size ) (width 10.0) (OBJECT )) For a total of 1 facts.

37 March 12, 2003UMontreal ift680236 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:\> java ExMyUpcase Jess> (load-function ExMyUpcase) Jess> (my-upcase foo) "FOO"

38 March 12, 2003UMontreal ift680237 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() + ")))"); ….

39 March 12, 2003UMontreal ift680238 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/ http://protege.stanford.edu/ JessTab – Combining two popular systems JessTab is a tab plug-in for running Jess inside Protégé

40 March 12, 2003UMontreal ift680239  Jess console window in Protégé

41 March 12, 2003UMontreal ift680240  Mirroring Jess definitions in Protégé knowledge bases

42 March 12, 2003UMontreal ift680241  Editing Jess definitions in Protégé

43 March 12, 2003UMontreal ift680242 Agent frameworks  Jess + Jade + Protégé = JadeJessProtégé Your system Your tab JessTab FuzzyJessPrologTab JadeJessProtege FloraTab More Protégé plug-ins More Jess extension s JADE JessProtégé

44 March 12, 2003UMontreal ift680243 References  http://www.iro.umontreal.ca/~vaucher/ift6802/Guide.html 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/http://www.ida.liu.se/~her/JessTab/  Obtain Protégé – Download from http://protege.stanford.edu/


Download ppt "March 12, 2003UMontreal ift68021 JESS:Java Expert System Shell Course IFT6802 Student ZHENG ZHEN."

Similar presentations


Ads by Google