Intro to Jess The Java Expert System Shell By Jason Morris Morris Technical Solutions.

61
Intro to Jess The Java Expert System Shell By Jason Morris Morris Technical Solutions

Transcript of Intro to Jess The Java Expert System Shell By Jason Morris Morris Technical Solutions.

Page 1: Intro to Jess The Java Expert System Shell By Jason Morris Morris Technical Solutions.

Intro to Jess The Java Expert System

Shell

By Jason MorrisMorris Technical Solutions

Page 2: Intro to Jess The Java Expert System Shell By Jason Morris Morris Technical Solutions.

Quotable Quotes

Each problem that I solved became a rule which served afterwards to solve other problems. - Rene Descartes

Page 3: Intro to Jess The Java Expert System Shell By Jason Morris Morris Technical Solutions.

Quotable Quotes

As a rule we disbelieve all the facts and theories for which we have no use. - William James

Page 4: Intro to Jess The Java Expert System Shell By Jason Morris Morris Technical Solutions.

Quotable Quotes

Hell, there are no rules here -- we're trying to accomplish

something. - Thomas A. Edison

Page 5: Intro to Jess The Java Expert System Shell By Jason Morris Morris Technical Solutions.

Quotable Quotes

Get your facts first, and then you can distort them as much as you please. - Mark Twain

Page 6: Intro to Jess The Java Expert System Shell By Jason Morris Morris Technical Solutions.

Quotable Quotes

If the facts don't fit the

theory, change the facts. - Albert Einstein

Page 7: Intro to Jess The Java Expert System Shell By Jason Morris Morris Technical Solutions.

Quotable Quotes

There are two rules for success:

1) Never tell everything you know. - Roger H. Lincoln

Page 8: Intro to Jess The Java Expert System Shell By Jason Morris Morris Technical Solutions.

Quotable Quotes

Facts do not cease to exist just because they are ignored. - Aldous Huxley

Page 9: Intro to Jess The Java Expert System Shell By Jason Morris Morris Technical Solutions.

Agenda

• What are expert systems?• What are rule-based expert

systems?• Introduction to Jess• The Jess Language

5 minute break

Page 10: Intro to Jess The Java Expert System Shell By Jason Morris Morris Technical Solutions.

Agenda

• Scripting & The Jess API• Demo 1 : Design Pattern Expert• Demo 2 : Catalog Servlet• References and Further Study• Q & A

Page 11: Intro to Jess The Java Expert System Shell By Jason Morris Morris Technical Solutions.

Expert Systems…

• Are a branch of artificial intelligence.

• Simulate human reasoning in some domain.

• “Reason” by heuristic or approximate methods.

• Explain and justify solutions in user-friendly terms.

Page 12: Intro to Jess The Java Expert System Shell By Jason Morris Morris Technical Solutions.

Types Of Expert Systems

• Neural Networks • Blackboard Systems• Belief (Bayesian) Networks

• Case-Based Reasoning• Rule-Based Systems

Page 13: Intro to Jess The Java Expert System Shell By Jason Morris Morris Technical Solutions.

Rule-Based Expert Systems

• Originated from AI research in the 70s and 80s.

• Problem data stored as facts.• “Reason” using IF…THEN…ELSE

rules.• Can “reason” deductively (forward-

chaining) or inductively (backward-chaining).

Page 14: Intro to Jess The Java Expert System Shell By Jason Morris Morris Technical Solutions.

When to Use Rule-Based Systems

• Problem Domain = narrow, well-understood domain theory

• Knowledge Representation = facts and rules

• Output = recommendation• Explanation = rule firing trace• Learning Ability = generally no

(but…)

Page 15: Intro to Jess The Java Expert System Shell By Jason Morris Morris Technical Solutions.

Inference Process

1. Rules and facts compared using pattern matcher.

2. Matched rules activated into a conflict set.

3. Conflict set resolved into agenda (process called conflict resolution).

4. Rule engine fires on agenda.5. Engine cycles until all rules are

satisfied.

Page 16: Intro to Jess The Java Expert System Shell By Jason Morris Morris Technical Solutions.

The Java Expert System Shell

• Developed at Sandia National Laboratories in late 1990s.

• Created by Dr. Ernest J. Friedman-Hill.

• Inspired by the AI production rule language CLIPS.

• Fully developed Java API for creating rule-based expert systems.

Page 17: Intro to Jess The Java Expert System Shell By Jason Morris Morris Technical Solutions.

Rule-Based Expert System Architecture

• Rule Base (knowledge base)• Working Memory (fact base)• Inference Engine (rule engine)

Page 18: Intro to Jess The Java Expert System Shell By Jason Morris Morris Technical Solutions.

Inference (Rule) Engines

• Pattern Matcher – decides what rules to fire and when.

• Agenda – schedules the order in which activated rules will fire.

• Execution Engine – responsible for firing rules and executing other code.

Page 19: Intro to Jess The Java Expert System Shell By Jason Morris Morris Technical Solutions.

Inference Process

• Match the facts against the rules.• Choose which rules to fire.• Execute the actions associated

with the rules.

Page 20: Intro to Jess The Java Expert System Shell By Jason Morris Morris Technical Solutions.

How Does Jess Work?

• 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) algorithm to match patterns.

• Rete network = an interconnected collection of nodes = working memory.

Page 21: Intro to Jess The Java Expert System Shell By Jason Morris Morris Technical Solutions.

Jess Architecture Diagram

WORKINGMEMORY

RULE BASE

EXECUTIONENGINE

INFERENCEENGINE

PATTERNMATCHER

AGENDA

Page 22: Intro to Jess The Java Expert System Shell By Jason Morris Morris Technical Solutions.

Procedural Programming

• Traditional programming (BASIC, C, FORTRAN, Pascal, etc.).

• Largely based on functions.• Programmer controls logic.• Sequential and deterministic.• Object-oriented programming is

procedural within object methods.

Page 23: Intro to Jess The Java Expert System Shell By Jason Morris Morris Technical Solutions.

Declarative Programming

• New programming paradigm - rules.

• Programmer does not really control code logic.

• Rule engine finds most efficient “path” of code execution.

• Replaces hard to maintain nested IF…THEN…ELSE coding.

Page 24: Intro to Jess The Java Expert System Shell By Jason Morris Morris Technical Solutions.

Wait a minute!

Well…yes and no…but don’t worry, Calvin!

What? I…I can’t control my code??

Page 25: Intro to Jess The Java Expert System Shell By Jason Morris Morris Technical Solutions.

Thought Experiment…

• Imagine writing a procedural/OOP algorithm to solve a jigsaw puzzle.

• 500+ pieces, different shapes and colors.

• Polymorphism runs amok!

Yet we manage to solve the puzzle…

Page 26: Intro to Jess The Java Expert System Shell By Jason Morris Morris Technical Solutions.

Intuition and Rules

• Dump the puzzle pieces on a card table in no particular order.

• Your brain instinctively begins to apply rules to solve the puzzle!

• What might this look like in code?

Page 27: Intro to Jess The Java Expert System Shell By Jason Morris Morris Technical Solutions.

Intuitive Inferencing

(corner_found(piece_is_corner)=>(assert corner-found)(save_piece))

(corner_found(piece_is_corner)=>(assert corner-found)(save_piece))

(edge_found(piece_is_edge)=>(assert edge-found)(save_piece))

(edge_found(piece_is_edge)=>(assert edge-found)(save_piece))

Your brain “knows” what to do with a corner piece …

… and an edge piece.

Page 28: Intro to Jess The Java Expert System Shell By Jason Morris Morris Technical Solutions.

What’s Going On…

• Your brain recalls rules or heuristics to solve the problem.

• Your brain pattern-matches, prioritizes, and applies rules according to the facts in memory.

• A particular solution algorithm emerges as rules “fire” on facts.

Page 29: Intro to Jess The Java Expert System Shell By Jason Morris Morris Technical Solutions.

The Jess Language

• Architecturally inspired by CLIPS• LISP-like syntax.• Basic data structure is the list.• Can be used to script Java API.• Can be used to access JavaBeans.• Easy to learn and use.

Page 30: Intro to Jess The Java Expert System Shell By Jason Morris Morris Technical Solutions.

Obligatory Tradition

(printout t “Hello PJUG-ers!” crlf)(printout t “Hello PJUG-ers!” crlf)

Your very first Jess program!

Page 31: Intro to Jess The Java Expert System Shell By Jason Morris Morris Technical Solutions.

Lists in Jess

• (a b c) ; list of tokens• (1 2 3) ; list of integers• (+ 2 3) ; an expression• (“Hello world!”) ; a string• (foo ?x ?y) ; a function call

Here are some valid lists in Jess:

Page 32: Intro to Jess The Java Expert System Shell By Jason Morris Morris Technical Solutions.

Jess Variables

• Named containers that hold a single value.

• Untyped. Begin with a ? mark.• Can change types during lifetime.• Assigned using bind function.

Page 33: Intro to Jess The Java Expert System Shell By Jason Morris Morris Technical Solutions.

Jess Variables and Lists

EXAMPLE: Adding two numbers

(bind ?x 2) ; assign x = 2(bind ?y 3) ; assign y = 3(bind ?result (+ ?x ?y)) ; find sum

Everything is a list in Jess!

Page 34: Intro to Jess The Java Expert System Shell By Jason Morris Morris Technical Solutions.

Control Flow

• foreach• if/then/else• while

• apply• build• eval• progn

Common Jess-specific

Page 35: Intro to Jess The Java Expert System Shell By Jason Morris Morris Technical Solutions.

Jess Functions

(deffunction get-input()“Get user input from console.”(bind ?s (read))(return ?s))

Even functions are lists.

Page 36: Intro to Jess The Java Expert System Shell By Jason Morris Morris Technical Solutions.

Jess Function Example

(deffunction area-sphere (?radius)

“Calculate the area of a sphere”

(bind ?area (* (* (pi) 2)(* ?radius ?radius)))

(return ?area))

Page 37: Intro to Jess The Java Expert System Shell By Jason Morris Morris Technical Solutions.

Jess Function Example

(printout t "The surface area of a radius = 2 meter sphere is " +

(area-sphere 2) + " m^2")

How do we use this in Jess?

Page 38: Intro to Jess The Java Expert System Shell By Jason Morris Morris Technical Solutions.

Working With Facts

• Facts have a head and one or more slots.

• Slots hold data (can be typed).• Multislots can hold lists. • You can modify slot values at

runtime.• Facts are constructed from

templates.

Page 39: Intro to Jess The Java Expert System Shell By Jason Morris Morris Technical Solutions.

Jess Fact Types

• Ordered – head only.• Ordered – single slot.• Unordered – multiple slot, like a

database record.• Shadow – slots correspond to

properties of a JavaBean.

Page 40: Intro to Jess The Java Expert System Shell By Jason Morris Morris Technical Solutions.

Deftemplate

Used to define the structure of a fact.

(deftemplate pattern “A design pattern.”(slot name)(slot type (default “creation”))(slot intent)(slot solution))

Page 41: Intro to Jess The Java Expert System Shell By Jason Morris Morris Technical Solutions.

Asserting Facts

;; Asserting a new “pattern” fact.

(printout t “Enter pattern name:” crlf)

(bind ?x getInput)(assert pattern (name ?x))

Facts store the initial conditions.

Page 42: Intro to Jess The Java Expert System Shell By Jason Morris Morris Technical Solutions.

All Kinds of Facts

;; An ordered fact with no slots – a placeholder that indicates state.

(assert(answer-is-valid))

;; A ordered fact of one slot

(assert(weightfactor 0.75))

Page 43: Intro to Jess The Java Expert System Shell By Jason Morris Morris Technical Solutions.

Shadow Facts

• defclass – creates a deftemplate from a bean.

• definstance – adds bean to working memory.

Shadow facts are unordered facts whose slots correspond to the properties of a JavaBean.

Page 44: Intro to Jess The Java Expert System Shell By Jason Morris Morris Technical Solutions.

Jess Rules…

• … are the knowledge-base of the system.

• … fire only once on a given set of facts.

• … use pattern constraints to match facts.

• … are much faster than IF-THEN statements.

Page 45: Intro to Jess The Java Expert System Shell By Jason Morris Morris Technical Solutions.

Rule Syntax

• Rules have a “left-hand” side (LHS) and a “right-hand” side (RHS).

• The LHS contains facts fitting certain patterns.

• The RHS contains function calls.

Page 46: Intro to Jess The Java Expert System Shell By Jason Morris Morris Technical Solutions.

Simple Rule Example

;; A not very useful error handler(defrule report-error

(error-is-present)=>(printout t “There is an error” crlf))

Checking working memory state.

Page 47: Intro to Jess The Java Expert System Shell By Jason Morris Morris Technical Solutions.

A More Complex Rule

;; A more useful error handler(defrule report-err?err <- (is-error (msg ?msg))=>(printout t "Error was: " ?msg crlf)(retract ?err))

Using pattern bindings in rules.

Page 48: Intro to Jess The Java Expert System Shell By Jason Morris Morris Technical Solutions.

More Pattern and Control Tools

• Literal / variable constraints

• Logical conditional tests

• Predicate functions

• Salience• Modules• Defquery• Backward-

chaining

matching control and structure

Page 49: Intro to Jess The Java Expert System Shell By Jason Morris Morris Technical Solutions.

Break Time!

Let’s take a quick 5 minute pause…

Page 50: Intro to Jess The Java Expert System Shell By Jason Morris Morris Technical Solutions.

Scripting Java from Jess

• You can interactively access all Java APIs from Jess.

• This makes exploring Java somewhat easier and immediate.

• No code, compile, debug cycle.

Page 51: Intro to Jess The Java Expert System Shell By Jason Morris Morris Technical Solutions.

Scripting Java with Jess

(import javax.swing.*)

(import java.awt.*)

(import java.awt.event.*)

(set-reset-globals FALSE)

(defglobal ?*frame* = (new JFrame "Hello PJUG"))

(defglobal ?*button* = (new JButton "Click my PJUG"))

(?*frame* setSize 500 300)

((?*frame* getContentPane) add ?*button*)

(?*frame* setVisible TRUE)

swingDemo.bat

Page 52: Intro to Jess The Java Expert System Shell By Jason Morris Morris Technical Solutions.

Demo 1: PAT (Pattern Analysis Tool)

• PAT is a simple decision-tree for choosing a Java design-pattern.

• Uses an initial interview to establish problem space.

• Recommends a GoF Java design-pattern to fit the available facts.

examples/pattern.clpexamples/pattern.clp

Page 53: Intro to Jess The Java Expert System Shell By Jason Morris Morris Technical Solutions.

The Jess API

• jess - inference engine “guts”.• jess.awt – GUI wrappers.• jess.factory - Allows extensions

that “get into the guts of Jess”.

Organized into 3 packages, 64 classes (not hard to learn)

Page 54: Intro to Jess The Java Expert System Shell By Jason Morris Morris Technical Solutions.

The Rete (ree-tee) Object

• The reasoning engine and the central class in the Jess library.

• Executes the built Rete network, and coordinates many other activities.

• Rete is essentially a facade for the Jess API.

Page 55: Intro to Jess The Java Expert System Shell By Jason Morris Morris Technical Solutions.

Using the Jess API…

…is simple…all you really do need to do is call one or more Rete methods …

try {

Rete engine = new Rete();

engine.executeCommand(

“printout t “Hello PJUG”);

engine.run();

}

catch (JessException je {}

Page 56: Intro to Jess The Java Expert System Shell By Jason Morris Morris Technical Solutions.

Demo 2: Catalog Servlet

import jess.*;import javax.servlet.*;import javax.servlet.http.*;import java.io.*;

public abstract class BaseServlet extends HttpServlet {

public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { doGet(request, response); }}...

Here is an example of a Jess application running as a Java servlet…

Here is an example of a Jess application running as a Java servlet…

Page 57: Intro to Jess The Java Expert System Shell By Jason Morris Morris Technical Solutions.

Pattern References

• Shalloway, A., Design Patterns Explained, Addison-Wesley, 2002

• Gamma, E. et. al., Design Patterns: Elements of Reusable Object-Orient Software, Addison-Wesley, 1995

Page 58: Intro to Jess The Java Expert System Shell By Jason Morris Morris Technical Solutions.

Expert Systems References

• Friedman-Hill, E. J., Jess In Action, Manning Press, 2003

• Jackson, P., Introduction to Expert Systems – 3rd Ed., Addison-Wesley, 1999

• Giarratano, J., Expert Systems: Principals and Programming, PSW-Kent, 1989

Page 59: Intro to Jess The Java Expert System Shell By Jason Morris Morris Technical Solutions.

Links

• Download Jess at: http://herzberg.ca.sandia.gov/jess/index.shtml

• Join the Jess user community at:http://herzberg.ca.sandia.gov/jess/mailing_list.shtml

• See Dr. Friedman-Hill’s Jess in Action at:

http://www.manning.com/friedman-hill/

Page 60: Intro to Jess The Java Expert System Shell By Jason Morris Morris Technical Solutions.

For Further Study

• CLIPS Expert System Shellhttp://www.ghg.net/clips/CLIPS.html• FuzzyJ Websitehttp://www.iit.nrc.ca/IR_public/fuzzy/fuzzyJToolkit2.html

Page 61: Intro to Jess The Java Expert System Shell By Jason Morris Morris Technical Solutions.

Q & A

Thanks for your attention, and I hope that you try Jess!