Intro to Jess The Java Expert System Shell

  • Slides: 61
Download presentation
Intro to Jess The Java Expert System Shell By Jason Morris Technical Solutions

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

Quotable Quotes Each problem that I solved became a rule which served afterwards to

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

Quotable Quotes As a rule we disbelieve all the facts and theories for which

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

Quotable Quotes Hell, there are no rules here -we're trying to accomplish something. -

Quotable Quotes Hell, there are no rules here -we're trying to accomplish something. - Thomas A. Edison

Quotable Quotes Get your facts first, and then you can distort them as much

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

Quotable Quotes If the facts don't fit theory, change the facts. - Albert Einstein

Quotable Quotes If the facts don't fit theory, change the facts. - Albert Einstein

Quotable Quotes There are two rules for success: 1) Never tell everything you know.

Quotable Quotes There are two rules for success: 1) Never tell everything you know. - Roger H. Lincoln

Quotable Quotes Facts do not cease to exist just because they are ignored. -

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

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

Agenda • What are expert systems? • What are rule-based expert systems? • Introduction to Jess • The Jess Language 5 minute break

Agenda • Scripting & The Jess API • Demo 1 : Design Pattern Expert

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

Expert Systems… • Are a branch of artificial intelligence. • Simulate human reasoning in

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.

Types Of Expert Systems • Neural Networks • Blackboard Systems • Belief (Bayesian) Networks

Types Of Expert Systems • Neural Networks • Blackboard Systems • Belief (Bayesian) Networks • Case-Based Reasoning • Rule-Based Systems

Rule-Based Expert Systems • Originated from AI research in the 70 s and 80

Rule-Based Expert Systems • Originated from AI research in the 70 s and 80 s. • Problem data stored as facts. • “Reason” using IF…THEN…ELSE rules. • Can “reason” deductively (forwardchaining) or inductively (backwardchaining).

When to Use Rule-Based Systems • Problem Domain = narrow, wellunderstood domain theory •

When to Use Rule-Based Systems • Problem Domain = narrow, wellunderstood domain theory • Knowledge Representation = facts and rules • Output = recommendation • Explanation = rule firing trace • Learning Ability = generally no (but…)

Inference Process 1. Rules and facts compared using pattern matcher. 2. Matched rules activated

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.

The Java Expert System Shell • Developed at Sandia National Laboratories in late 1990

The Java Expert System Shell • Developed at Sandia National Laboratories in late 1990 s. • 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.

Rule-Based Expert System Architecture • Rule Base (knowledge base) • Working Memory (fact base)

Rule-Based Expert System Architecture • Rule Base (knowledge base) • Working Memory (fact base) • Inference Engine (rule engine)

Inference (Rule) Engines • Pattern Matcher – decides what rules to fire and when.

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.

Inference Process • Match the facts against the rules. • Choose which rules to

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

How Does Jess Work? • Jess matches facts in the fact base to rules

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.

Jess Architecture Diagram WORKING MEMORY INFERENCE ENGINE PATTERN MATCHER RULE BASE AGENDA EXECUTION ENGINE

Jess Architecture Diagram WORKING MEMORY INFERENCE ENGINE PATTERN MATCHER RULE BASE AGENDA EXECUTION ENGINE

Procedural Programming • Traditional programming (BASIC, C, FORTRAN, Pascal, etc. ). • Largely based

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.

Declarative Programming • New programming paradigm - rules. • Programmer does not really control

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.

Wait a minute! What? I…I can’t control my code? ? Well…yes and no…but don’t

Wait a minute! What? I…I can’t control my code? ? Well…yes and no…but don’t worry, Calvin!

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

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…

Intuition and Rules • Dump the puzzle pieces on a card table in no

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?

Intuitive Inferencing (corner_found (piece_is_corner) => (assert corner-found) (save_piece)) (edge_found (piece_is_edge) => (assert edge-found) (save_piece))

Intuitive Inferencing (corner_found (piece_is_corner) => (assert corner-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.

What’s Going On… • Your brain recalls rules or heuristics to solve the problem.

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.

The Jess Language • Architecturally inspired by CLIPS • LISP-like syntax. • Basic data

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 Java. Beans. • Easy to learn and use.

Obligatory Tradition Your very first Jess program! (printout t “Hello PJUG-ers!” crlf)

Obligatory Tradition Your very first Jess program! (printout t “Hello PJUG-ers!” crlf)

Lists in Jess Here are some valid lists in Jess: • (a b c)

Lists in Jess Here are some valid lists in Jess: • (a b c) • (1 2 3) • (+ 2 3) • (“Hello • (foo ? x ; list of tokens ; list of integers ; an expression world!”) ; a string ? y) ; a function call

Jess Variables • Named containers that hold a single value. • Untyped. Begin with

Jess Variables • Named containers that hold a single value. • Untyped. Begin with a ? mark. • Can change types during lifetime. • Assigned using bind function.

Jess Variables and Lists Everything is a list in Jess! EXAMPLE: Adding two numbers

Jess Variables and Lists Everything is a list in Jess! EXAMPLE: Adding two numbers (bind ? x 2) ; assign x = 2 (bind ? y 3) ; assign y = 3 (bind ? result (+ ? x ? y)) ; find sum

Control Flow Common Jess-specific • foreach • if/then/else • while • apply • build

Control Flow Common Jess-specific • foreach • if/then/else • while • apply • build • eval • progn

Jess Functions Even functions are lists. (deffunction get-input() “Get user input from console. ”

Jess Functions Even functions are lists. (deffunction get-input() “Get user input from console. ” (bind ? s (read)) (return ? s))

Jess Function Example 1. (deffunction area-sphere (? radius) 2. “Calculate the area of a

Jess Function Example 1. (deffunction area-sphere (? radius) 2. “Calculate the area of a sphere” 3. (bind ? area (* (* (pi) 2)(* ? radius))) 4. (return ? area))

Jess Function Example 1. How do we use this in Jess? 1. (printout t

Jess Function Example 1. How do we use this in Jess? 1. (printout t "The surface area of a radius = 2 meter sphere is " + 2. (area-sphere 2) + " m^2")

Working With Facts • Facts have a head and one or more slots. •

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.

Jess Fact Types • Ordered – head only. • Ordered – single slot. •

Jess Fact Types • Ordered – head only. • Ordered – single slot. • Unordered – multiple slot, like a database record. • Shadow – slots correspond to properties of a Java. Bean.

Deftemplate Used to define the structure of a fact. (deftemplate pattern “A design pattern.

Deftemplate Used to define the structure of a fact. (deftemplate pattern “A design pattern. ” (slot name) (slot type (default “creation”)) (slot intent) (slot solution))

Asserting Facts store the initial conditions. ; ; Asserting a new “pattern” fact. (printout

Asserting Facts store the initial conditions. ; ; Asserting a new “pattern” fact. (printout t “Enter pattern name: ” crlf) (bind ? x get. Input) (assert pattern (name ? x))

All Kinds of Facts ; ; An ordered fact with no slots – a

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))

Shadow Facts Shadow facts are unordered facts whose slots correspond to the properties of

Shadow Facts Shadow facts are unordered facts whose slots correspond to the properties of a Java. Bean. • defclass – creates a deftemplate from a bean. • definstance – adds bean to working memory.

Jess Rules… • … are the knowledge-base of the system. • … fire only

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.

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

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.

Simple Rule Example Checking working memory state. ; ; A not very useful error

Simple Rule Example Checking working memory state. ; ; A not very useful error handler (defrule report-error (error-is-present) => (printout t “There is an error” crlf))

A More Complex Rule Using pattern bindings in rules. ; ; A more useful

A More Complex Rule Using pattern bindings in rules. ; ; A more useful error handler (defrule report-err ? err <- (is-error (msg ? msg)) => (printout t "Error was: " ? msg crlf) (retract ? err))

More Pattern and Control Tools matching control and structure • Literal / variable •

More Pattern and Control Tools matching control and structure • Literal / variable • constraints • • Logical • conditional tests • • Predicate functions Salience Modules Defquery Backwardchaining

Break Time! Let’s take a quick 5 minute pause…

Break Time! Let’s take a quick 5 minute pause…

Scripting Java from Jess • You can interactively access all Java APIs from Jess.

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.

Scripting Java with Jess (import javax. swing. *) (import java. awt. event. *) (set-reset-globals

Scripting Java with Jess (import javax. swing. *) (import java. awt. event. *) (set-reset-globals FALSE) (defglobal ? *frame* = (new JFrame "Hello PJUG")) (defglobal ? *button* = (new JButton "Click my PJUG")) (? *frame* set. Size 500 300) ((? *frame* get. Content. Pane) add ? *button*) (? *frame* set. Visible TRUE) swing. Demo. bat

Demo 1: PAT (Pattern Analysis Tool) • PAT is a simple decision-tree for choosing

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 Go. F Java designpattern to fit the available facts. examples/pattern. clp

The Jess API Organized into 3 packages, 64 classes (not hard to learn) •

The Jess API Organized into 3 packages, 64 classes (not hard to learn) • jess - inference engine “guts”. • jess. awt – GUI wrappers. • jess. factory - Allows extensions that “get into the guts of Jess”.

The Rete (ree-tee) Object • The reasoning engine and the central class in the

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.

Using the Jess API… try { Rete engine = new Rete(); engine. execute. Command(

Using the Jess API… try { Rete engine = new Rete(); engine. execute. Command( “printout t “Hello PJUG”); engine. run(); } catch (Jess. Exception je {} …is simple…all you really do need to do is call one or more Rete methods …

Demo 2: Catalog Servlet import jess. *; javax. servlet. http. *; java. io. *;

Demo 2: Catalog Servlet import jess. *; javax. servlet. http. *; java. io. *; Here is an example of a Jess public abstract class Base. Servlet extends Http. Servlet { application running as a public void do. Post(Http. Servlet. Request request, Http. Servlet. Response response) throws IOException, Java servlet… Servlet. Exception { do. Get(request, response); } }. . .

Pattern References • Shalloway, A. , Design Patterns Explained, Addison-Wesley, 2002 • Gamma, E.

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

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

Expert Systems References • Friedman-Hill, E. J. , Jess In Action, Manning Press, 2003 • Jackson, P. , Introduction to Expert Systems – 3 rd Ed. , Addison. Wesley, 1999 • Giarratano, J. , Expert Systems: Principals and Programming, PSWKent, 1989

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

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. s html • See Dr. Friedman-Hill’s Jess in Action at: http: //www. manning. com/friedman-hill/

For Further Study • CLIPS Expert System Shell http: //www. ghg. net/clips/CLIPS. html •

For Further Study • CLIPS Expert System Shell http: //www. ghg. net/clips/CLIPS. html • Fuzzy. J Website http: //www. iit. nrc. ca/ IR_public/fuzzy/ fuzzy. JToolkit 2. html

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

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