intro to jess - discoold.disco.unimib.it/simone/ldp_com/jess-mac-2005.pdf · over data: here just...
Post on 17-Mar-2020
0 Views
Preview:
TRANSCRIPT
1
Intro to Jess The Java Expert System
Shell
Ing. Federico Cabitzafederico.cabitza-at-disco.unimib.itMAClab – http://www.mac.disco.unimib.it
Get your facts first, and then you can distort them as much as you please.- Mark Twain
To break the ice…
We’ll speak of facts and rules for a couple of hours, so… keep always in mind these two wise hints:
There are two rules for success:1) Never tell everything you know.- Roger H. Lincoln
2
Agenda
1) What are expert systems (ES)?2) What are rule-based ES?3) Introduction to Jess4) The Jess Language5) Jess API & Jess Scripting6) References
…more or less 4h and 70 slides…Prepare yourself! ;-)
Expert System
An ES is a program that is able to “emulate” human reasoning and decision taking in some way and in some domain.
A very general definition:
3
Expert Systems
They are quite common, embedded in everyday softwares and their use ranges from easy tasks to very difficult ones.
Some use domains:
Spam filteringFault diagnosisMedical diagnosisFactory Process ControlPrediction/classification…
Expert System
“a program that uses knowledge and inference procedures to solve problems that are difficult enough to require significant human expertise for their solution.”
Feigenbaum 1982
A historical citation:
4
An Expert System has…
some inferencing(reasoning on factual knowledge)
some interface to world (user and enviroment)
some expertise(expert knowledge in a particular area)
KNOWLEDGE BASE
INFERENTIAL ENGINE
API’s, Shell, Etc….
A very general architecture:
Expert System: inside
5
Expert System: outside
expertise
solutions
users
expertsprogrammers
KNOWLEDGE BASE
INFERENTIAL ENGINE
API’s, Shell, Etc….
KNOWLEDGE BASE
INFERENTIAL ENGINE
API’s, Shell, Etc….
models,algorithms
facts
World representation
A conceptual guide
In the following we’ll treat the three keywords above outlined
Expertise: we’ll speak of Knowledge and Rules
Inferencing: we’ll speak of reasoning and the way Rule
Based ES’s do it
Interface: we’ll speak of a Java Interface to RBES’s: JESS !
6
Knowledge
What is it? I mean, in this context…Three, ever present, concepts:
Mind: in Expert Systems jargon they are used quite idiosyncratically between the classical (if any) AI acceptation and the classical ICT one.Misunderstanding is no surprise…
DataInformation
Knowledge
Knowledge
In the Expert System domain:
A proceduralmodel of the world
“Factual” knowledge
Data
Information
Knowledge
inference
Facts about the world
Recommendations, solutions, etc.
7
Knowledge
Data
Information
Knowledge
processing
infering
A DIKW (??) view of the same concept:
Usual Programs
Expert Systems
The DIKW Pyramid
Zeleny, M. "Management Support Systems: Towards Integrated Knowledge Management," Human Systems Management, 7(1987)1, pp. 59-70.
Ackoff, R.L. "From Data to Wisdom", Journal of Applied Systems Analysis, Volume 16, 1989 p 3-9.
In 1987 Milan Zeleny and then Russell Ackoff posited the hierarchy of Data, Information, Knowledge and… Wisdom
8
By the way…
In your opinion,Information is a stuff or a process??
Knowledge examples
Your Bank Balance is:cash: 1000,00 €trading: 100,00 €
This is dataa multivalue, typed data
9
Knowledge examples
Over the last month your trading made you earn 1000,00 €. Your cash has decreased by 90%
This is information.The result of some processing over data: here just an algebric sum and a proportion
Knowledge examples
Your cash is low, you should sell some titles andput some cash on your Account balance
This is what an ES could propose according to the actual situation and to some simple knowledge of bank accounts (being in the red is a real pain!! ;-)
10
Knowledge examples
Hey, pal! Trading is risky and you won’t be so lucky forever!Be careful!!
This is wisdom.We don’t treat it in this course… ;-)
Types Of Expert Systems
- Neural Networks
- Blackboard Systems
- Belief (Bayesian) Networks
- Case-Based Reasoning
- Rule-Based Systems
11
- represent knowledge as a bunch of rules that tell you what you should do or what you could conclude in different situations represented as a bunch of facts
- originated from AI research in the 70s and 80s.
MYCIN 1974 written in LISP by Shortliffe
Rule-Based Expert Systems
Rules in a nutshell
Rules are computational units that are computed only when they are applicable. A rule is composed of an IF-portion (LHS) and a THEN-portion (RHS).
some patternssome actions
to execute
IF THEN
12
Rules in a nutshell
IF THEN
A RBES compares facts of the world with patterns of rules
..whenever the patterns of the rule match with some facts of the environment, then the rule is activated and its actions executed.
In emulating human reasoning RBES can reason (“infer”) in two ways:
- deductively (forward-chaining)
- abductively (backward-chaining).
Rule-Based Expert Systems
Both the “directions” are valid: it depends on the situation and on the required outcome which is the most suitable.
13
Deduction:
Induction:
The act or process of deriving logical conclusions from premises known or assumed to be true
The process of deriving general principles from particular facts
Rule-Based Expert Systems
Abduction: The process of deriving causes from the effects
Rule-Based Expert Systems
Deduction Forward Chaining aka “production systems” :
Known facts “New” factsrules
OPS5 (1977) in LISP
1) start from initial facts 2) keep using the rules to draw new conclusions
(or take certain actions)
14
Abduction Backward Chaining
PROLOG (1972) in ALGOL
1) start from some hypothesis (or goal) to prove2) keep looking for rules that would allow you to conclude that hypothesis
(also by setting new subgoals to prove as you go).
Right rules New factsKnown facts
Rule-Based Expert Systems
When to Use Rule-Based Systems
Problem Domainnarrowwell-understood
Knowledge Representation facts rules
Output recommendations to humans, support to decisions
Learning not essential
often!
15
RBES Architecture
Rule Base (knowledge base)
Working Memory (fact base)
Rule Engine (inference engine)
INFERENCEENGINEWORKING
MEMORY
RULE BASE
EXECUTIONENGINE
PATTERNMATCHER
CONFLICT RESOLVER
RBES Architecture
16
Inference (Rule) Engine
Pattern Matcherdecides what rules to fire and when
(conflict set).Conflict Resolver
schedules the order in which activated rules will fire (agenda).
Execution Engineresponsible for firing rules and executing other code.
Inference Process
1) Match the facts against the rules.
2) Select which rules to fire.
3) Execute the actions associated to rules.
17
1) Initial facts put in a working memory (WM)
2) Rules and facts compared using pattern matching.
3) Matched rules activated put into a conflict set.
4) Conflict set resolved into agenda (ordered c.s.)(process called conflict resolution).
5) Rule engine fires on agenda (->new facts in WM).
Engine cycles until all rules in agenda are fired.
Inference Process details
Halt
Break Time
18
JESSsome definitions
The Java Expert System Shell is a toolfor building Expert Systems in Java.It can be viewed also as…
…a programmer's library
…an interpreter
…a scripting language
…an interface (API’s)
well… a shell!!
Developed entirely in Sun's Java languageat Sandia National Laboratories (Livermore, California, USA)in late 1990s by Dr. Ernest J. Friedman-Hill.
JESScredits
http://herzberg.ca.sandia.gov/jess/
19
• Jess matches facts in the fact base to rules in the rule base.
• The rules contain function calls that manipulate the fact base and/or other Java code.
• Jess uses the Rete (ree-tee) algorithmto match patterns.
• Rete network = an interconnected collection of nodes = working memory.
JESSin short
The Jess Language
20
Syntax similar to LISP(Lots of Irrititating, Superflous Parentheses)
Basic data structure is the list.
Inspired to CLIPS (C Language Integrated Production System)
all structured data are lists:
facts, rules, also function calls are lists.
The Jess Language
Numbers: INTEGER, LONG, FLOAT
Data Types
Atoms: ATOM
foobarFoobarnumber#3 3#number
Nil TRUE FALSE
0.357 42 6.0E4
21
Data Types
Strings: STRING
“0.357” “foobar” “ \”between commas\” ”
( foobar) (a foobar 4) (deftamplate foo (slot bar))
Lists: LIST
head
Comments: ;*
Obligatory Tradition
Your very first Jess program!
(printout t “Hello World!” crlf)
22
Functions in Jess
Function call: a list whose head is the name of an existing function list
Prefix notations
Nesting: the outer function evaluates the inner one(s)
(+ 2 3) 5
(* (+ 2 3) (+ (* 2 3) 2))
5 8 640
(printout t “Hello World!” crlf)
• Atoms that begin with a ? mark.
• Untyped: types can change during lifetime (like in dynamically typed languages)
• Can be local or global (aka defglobals)
• Variables (?x) are declared by first assigning(using bind function), defglobals (?*x*) areassigned in declaration
Variables in Jess
23
(bind ?x 2)
(defglobal ?*y* = 3)
(bind ?result (+ ?x ?y))
Variables in Jess
assignes value 2 to variable x
assignes value 3 to global variable y
assignes to variable resultthe result of the expression(3 + 2)
An example
Variables in Jess
Differences between variables and defglobals??
Easy:Global variables (defined through the defglobalconstruct, hence defglobals) are variables thatare not wiped out by issuing the resetcommand.
24
Functions in Jess
You can create a function by creating a particular list whose head is the deffunction atom
(deffunction max (?a ?b)(if (> ?a ?b) then
(return ?a)else
(return ?b))
)
Functions in Jess
In a function you can create any variableand use it with input parameters. Example:
(deffunction area-circle (?radius)(bind ?area (* (pi) (* ?radius
?radius)))(return ?area))
25
Functions in Jess
Putting things togetherafter having created a new function, touse it is quite trivial. Example:
(printout t "The area of a 2 meter radius circle is " + (area-circle 2) + " m^2")
Common Functions
(reset): resets the Working Memory *(clear): clears Jess (more deeply than reset)(facts): shows facts currently in WM(rules): shows rules currently in WM(batch <filename>): sends the Shell some file content(run): starts the engine(apply) calls a function on a given set of arguments.(build) parses and executes a function from a string.(eval) ”(open <filename> id w): opens a file in write mode(read t): reads from Standard Input(read id): reads the file content(printout t “…” crlf): prints to StdOutput(printout id “…” crlf): prints to file(exit): exits the Shell
26
Let’s talk facts
Jess (and any other RBES) representsthe world and simple knowledge of it bymeans of facts.
Facts are lists with a specific structure:
( atom [slot]*)headlike a label, it expresses the category of the fact
slotsData containers (simple or lists…)
Jess Facts Types
- Ordered, not structured
Where just position counts.
- Unordered, structured
Where types and default values are allowed.
- Shadow
Where Jess and Java meet.
27
Jess Facts Examples
(person (surname “Smith”)(first-names Robert Jerome) (age 35)
)
Slots: like fields of a form, they hold (un)typed data.(first-names is a multislot: see below)
(error-occurred)
(shopping-list eggs milk bread)
(person “Robert J. Smith" Male 35)
Just a head, no slot: this fact indicates a state in the world
Jess Facts assertion
Facts “exist” if they are asserted.Asserting a fact means to put it in the “fact base”.Likewise, retracting a fact means to remove it from the Working Memory.
Jess> (assert (shopping-list eggs bread))<Fact-1>
Jess> (facts)f-0 (MAIN::initial-fact)f-1 (MAIN::shopping-list eggs bread)For a total of 2 facts.
Jess> (retract (fact-id 1))
28
Deftemplate
To work with unordered facts they must be defined first by calling the deftemplateconstruct.
(deftemplate <deftemplate-name>(slot | multislot <slot-name>)* )
A multifield slot (multislot) can have any number (including zero) of values.
Deftemplate Ex. 1/2
Jess> (deftemplate automobile"A specific car."(slot make)(slot model)(slot year (type INTEGER))(slot color (default white))
)
Jess> (bind ?id (assert (automobile (make Volvo) (model 460)(year 1997))))<Fact-0>
Jess> (facts)f-0
(MAIN::automobile (make Volvo) (model 460)(year 1997) (color white))
29
Deftemplate Ex. 2/2
Jess> (facts)f-0
(MAIN::automobile (make Volvo) (model 460)(year 1997) (color black))
Jess> (modify ?id (color black))
Modify will… just modify a value in a slot!
Shadow Facts
- Defclasscreates a deftemplate that represents a
category of Java bean.
- Definstanceadds a representation of one specific bean to
working memory.
Shadow facts are unordered facts whose slots correspond to the properties of a JavaBean.
Sort of class import
Sort of object instance
30
What are patterns?How actions
are expressed?
IF THEN
Jess Rules…
Now we can look inside these boxes…
This is the rule syntax:
(defrule <rule-name> {;optional comment}<condition-element-1><condition-element-2>…..<condition-element-k>
=><action-1>…..<action-m>
)
Jess Rules Syntax
31
What is a Condition Element? (CE)
<condition-element> =<pattern> or <NOT-CE> or <AND-CE>
or <OR-CE> or <test-CE>
Condition Elements
Yes, this is a recursive definition……at last CE’s are bunches of patterns and some of them can be evaluated in logical expressions.A CE can be either True or False.
Eg.: an OR-CE is true if any CE is True, False otherwise.
When is a CE true?
CE and Patterns
It depends on whether the patterns that constitute it find a match in the WM.
Patterns are partial descriptions of data.In Jess they can be expressed by facts that include some variables looking for a… value!
32
(Exp (Name ?X) (Arg1 5) (Op ?))
(Exp (Name E21) (Arg1 5) (Op *) (Arg2 X))
… will match with this fact:
1) This pattern…
3 Patterns Examples
field constraintliteral constraint
wildcard constraint
2) ? is a monofield wildcard; $? is a multifield one.Ex.: $?items can match any number of values
Patterns Examples
(defrule cart-containing-milk(shopping-cart (contents $?anything_before milk $?anything_after))
=>(printout t “The cart contains milk.” crlf))
3) You can use Connective Constraints (&, |, ~, :)
?x&:(> 5 ?x) match with any x that is less than 5
(person (city ~Milano& ~Roma)) match with anyone who lives neither in Rome nor in Milan
33
Patterns and rules…
If all the LHS CE’s of a rule are True thenthe rule instance is entered into the conflict set and becomes an Activation.Then attention moves to the RHS, where actions wait to be fired. Actions are expressed quite easily…they are function calls!!
;; This is a trivial error handler
(defrule report-error
(error-is-present)
=>
(printout t “There is an error” crlf))
Let’s see again the JESS rule syntax in a simple example:
definition
L H S
then R H S
34
What a Rule can do…
A rule can change the Fact base
Adding…(assert ()) to assert a fact(deffacts ()) to assert more unstructured facts
Removing…(retract (fact-id ?fact-id)) to delete a fact
Modifying…(modify ?f (slotname slotvalue))
to modify a fact
( defrule fratelli_mamma(madre ?mamma ?figlio1)(madre ?mamma ?figlio2)(test (neq ?figlio1 ?figlio2))=>(assert (fratelli ?figlio1 ?figlio2))
)
IF a mother has got two children…
… I mean, TWO children
THEN
These two guys are brothers!!
Jess Rule Example
Not a big deal, uh?! ;-)
35
A More Complex Rule
;; A better error handler
(defrule report-err?err <- (is-error (msg ?msg))=>(printout t "Error was: " ?msg crlf)(retract ?err))
A more useful rule…
Inference Process details
1) Initial facts put in a working memory (WM)
2) Rules and facts compared using pattern matching.
3) Matched rules activated put into a conflict set.
4) Conflict set resolved into agenda(process called conflict resolution).
5) Rule engine fires on agenda (new facts in WM).
Engine cycles until all rules in agenda are fired.
Halt
36
Conflict Resolution
The conflict set is the set of all the rules that are eligible to be fired (executed).
Conflict resolution is the process of putting the rules in firing order.
Different strategies can be applied according to the domain (and how this is formalized into rules).
Usually strategies are accomplished according to some rationale (trade-off).
Conflict Resolution
- Rule-based rationale- Recency (activation order) - Specificity (number of conditions, or lenght of conditions)- Anti-refraction (not the same rule under the same conditions) - Anti-repetition (not the same effect twice)- …
1. - Object-based rationale- Classification and ranking of object referenced in LHSs
2. - Effect-based rationale- Assessment of effects beforehand
3. - Metarules
37
Conflict Resolution
The Jess built-in strategies are depth and breadth and can be called with the (set-strategy (depth | breadth)) command.
- Depth (default):The most recently activated rules fire first. Last in, first out.
Caveat: troubles arise if every fired rule activates another rule, since old activations can never get a chance to fire.
Conflict Resolution
- Breadth: The most recently activated rules fire last (i.e., firing order is activation order): First in, first out.
Not enough? You can write your own strategy in Java by implementing the jess.Strategyinterface (‘compare’ method):
(set-strategy your-alluring-strategy)
You lazy? You can use salience…
38
Salience
- Each rule has a property called salience.It’s sort of rule priority.
- Activated rules of the highest salience will fire first, followed by rules of lower salience.
- Rules can include a salience declaration
(defrule last-rule(declare (salience -100))(command exit-when-idle)=>(printout t "exiting..." crlf))
Java ← Jess
- Jess comes with other 200 built-in functions and you can also define any function you want. This makes Jess a good scripting language.
- More importantly from JESS you can access all Java APIs (link to databases, GUI forms, etc.), create and manipulate Java objects.
(foreach <variable> <multifield-expression> <action>*)
(if <expression> then <action>* [else <action>*])
(try <expression>* [catch <expression>*])
(while <expression> [do] <action>*)
39
Jess> (bind ?ht (new java.util.Hashtable))<External-Address:java.util.Hashtable>
Jess> (call ?ht put "key1" "element1")Jess> (call ?ht put "key2" "element2")Jess> (call ?ht get "key1")
"element1"
Examples
Name of an existing class
Methods of the class
Examples
public class java_class{public int fibonacci(int n){
if(n < 3) return 1;else return fibonacci(n-1) +
fibonacci(n-2);}
}
Jess> (defglobal ?*external-class-obj* = 0)Jess> (bind ?*external-class-obj* (new java_class))) Jess> (printout t "The 6th fibonacci number is: “
(?*external-class-obj* fibonacci 6) crlf)
This class must be compiled
Methods of the class
40
Java → Jess
From Java you can call JESS (Jess API).
import jess.*;
public class ExSquare { public static void main(String[] str) { try {
Rete r = new Rete(); r.executeCommand(“(deffunction square (?n)
(return (* ?n ?n)))"); Value v = r.executeCommand("(square 3)"); System.out.println(v.intValue(r.getGlobalContext()));
} catch (JessException ex) { System.err.println(ex);
} }
}
try {
Rete engine = new Rete();
engine.executeCommand(
(batch foobar.clp));
engine.executeCommand(“(run)”);
}
catch (JessException je) {
System.err.println(je);
}
Java → Jess
41
Two good methods to exchange data between Java and Jess and viceversa.
Java → Jess
- Using Fetch / Store commands
- Using Shadow facts
Shadow Facts
- Defclasscreates a deftemplate that represents a
category of Java bean.- Definstance
adds a representation of one specific bean to working memory.
Shadow facts are unordered facts whose slots correspond to the properties of a JavaBean.
42
Java → Jess
Shadow factsPublic class JavaBean{public String getPropertyOne() …public void setPropertyOne(String)…public int String getPropertyTwo() …public void setPropertyTwo(int)…
}
(deftemplate Javabean(slot propertyOne)(slot propertyTtwo)
)
Java → Jess
public class DimmerSwitch { private int brightness = 0; public int getBrightness() { return brightness; } public void setBrightness(int b) {
brightness = b; adjustTriac(b);
} private void adjustTriac(int brightness) {
// Code not shown }
}
43
Java → Jess
Jess> (defclass dimmer DimmerSwitch)
This generate a deftemplate that represent the class
Jess> (bind ?ds (new DimmerSwitch))
This creates an instance (object) of DimmerSwitch (it’s a Java Object)
Jess> (definstance dimmer ?ds)
This adds the object into the working memory
Jess> (call ?ds setBrightness 10)
This is a call to a method of Dimmerswitch classthat changes the attribute of the object referenced by ?ds.
Java → Jess
If the definstance is static only the object changes, not the fact (unless you run a reset command). In fact the reset updates the shadow fact to match the JavaBean, like (update ?ds).
In the “dynamic” fact Jess puts a PropertyChangeListener but to have a sf change when a bean does, you need also to define a PropertyChangeSupport in your java class.
Jess> (definstance dimmer ?ds [static])
44
Java → Jess
A propertyChangeSupport
private PropertyChangeSupport pcs = new PropertyChangeSupport(this);public voidaddPropertyChangeListener(PropertyChangeListener p) {
pcs.addPropertyChangeListener(p);}public void
removePropertyChangeListener(PropertyChangeListener p) {pcs.removePropertyChangeListener(p);
}
1) Add these code in the class definition
2) Change the Set Method of the JavaBean
In the setBrightness method of the example: pcs.firePropertyChange(“brightness",new Integer(old),(new Integer(b)));
Java → Jess
Jess> (facts)f-0 (MAIN: :dimmer
(brightness 0)(class <External-Address…>)(OBJECT <External-Address…>)
)
(defrule example(dimmer (OBJECT ?o))(test (eq (call ?o getBrightness) 10))
=>(assert (Brightness 10))
)
How to use a shadow fact in a rule…
45
Java → Jess
You CANNOT use assert on shadow facts (sf).Use definstance instead.Neither you can assert a sf with deffactsconstruct.
You CAN use both retract and modify on sf.If you modify a sf, Jess automatically updates the corresponding JavaBean property.
(call ?sf setProperty x) == (modify (Property x))
Java → Jess
At lab we’ll see inside the black box called foobar.clp!!
engine.executeCommand((batch foobar.clp));
See you there!!
46
Some labs tricks1) Independent engines: Note that each individual jess.Rete object represents an independent reasoning engine. A single program can then include several independent engines. Jess can be used in a multithreaded environment.
2) Mind that try-catch in the rete thread is blocking
3) Conceive any fact in jess as a javabean with some properties. At the end of the execution go and read the properties to check what has been modified by inferential engine.
4) Initialize the javabeans, make in the jess code the proper links (“shadows”) to these beans and then let the program reason for you by reading and writing on these javabeans (as well as on other java objects)
5) Mind that scripts MUST be well-written (clear, defclass, definstances, deftemplate, deffacts, defrule, reset…)
Expert Systems References
- Friedman-Hill, E. J., Jess In Action, Manning Press, 2003 (strongly recommended)
- Jackson, P., Introduction to Expert Systems –3rd Ed., Addison-Wesley, 1999
- Giarratano, J., Expert Systems: Principals and Programming, PSW-Kent, 1989
47
Jess Links
- Download Jess at: http://herzberg.ca.sandia.gov/jess/index.shtml
- The official Jess Manual by E.J. Friedman-Hillhttp://herzberg.ca.sandia.gov/jess/docs/manual.pdf
- A Jess presentation by Jason Morrishttp://www.pjug.org/introtojess.ppt
- A Jess tutorial by Maarten Menken http://www.cs.vu.nl/~ksprac/export/jess-tutorial.pdf
Q & A
Thanks for your attention!
Last modified April 2005An updated version of these slides will be
regularly uploaded at http://www.entechne.comPlease, report any mistake to author. Thank you.
top related