intro to jess

Upload: nhoc-buong-binh

Post on 14-Apr-2018

221 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/30/2019 Intro to Jess

    1/61

    Intro to JessThe Java Expert System

    Shell

    By Jason Morris

    Morris Technical Solutions

  • 7/30/2019 Intro to Jess

    2/61

    Quotable Quotes

    Each problem that I solved

    became a rule which servedafterwards to solve otherproblems.- Rene Descartes

  • 7/30/2019 Intro to Jess

    3/61

    Quotable Quotes

    As a rule we disbelieve allthe facts and theories forwhich we have no use.

    - William James

  • 7/30/2019 Intro to Jess

    4/61

    Quotable Quotes

    Hell, there are no rules here --

    we're trying to accomplish

    something.- Thomas A. Edison

  • 7/30/2019 Intro to Jess

    5/61

    Quotable Quotes

    Get your facts first, and then

    you can distort them as muchas you please.- Mark Twain

  • 7/30/2019 Intro to Jess

    6/61

    Quotable Quotes

    If the facts don't fit the

    theory, change the facts.- Albert Einstein

  • 7/30/2019 Intro to Jess

    7/61

    Quotable Quotes

    There are two rules

    for success:

    1) Never tell

    everything you know.- Roger H. Lincoln

  • 7/30/2019 Intro to Jess

    8/61

    Quotable Quotes

    Facts do not cease to

    exist just because theyare ignored.

    -Aldous Huxley

  • 7/30/2019 Intro to Jess

    9/61

    Agenda

    What are expert systems?

    What are rule-based expertsystems?

    Introduction to Jess

    The Jess Language

    5 minute break

  • 7/30/2019 Intro to Jess

    10/61

    Agenda

    Scripting & The Jess API

    Demo 1 : Design Pattern Expert Demo 2 : Catalog Servlet

    References and Further Study

    Q & A

  • 7/30/2019 Intro to Jess

    11/61

    Expert Systems

    Are a branch ofartificialintelligence.

    Simulate human reasoning in somedomain.

    Reason by heuristic or

    approximate methods. Explain and justify solutions inuser-friendly terms.

  • 7/30/2019 Intro to Jess

    12/61

    Types Of Expert Systems

    Neural Networks

    Blackboard Systems

    Belief (Bayesian) Networks

    Case-Based Reasoning Rule-Based Systems

  • 7/30/2019 Intro to Jess

    13/61

    Rule-Based Expert Systems

    Originated from AI research in the70s and 80s.

    Problem data stored as facts. Reason using IFTHENELSE

    rules.

    Can reason deductively (forward-chaining) or inductively(backward-chaining).

  • 7/30/2019 Intro to Jess

    14/61

    When to Use Rule-Based

    Systems Problem Domain = narrow, well-

    understood domain theory

    Knowledge Representation = factsand rules

    Output = recommendation

    Explanation = rule firing trace Learning Ability= generally no (but)

  • 7/30/2019 Intro to Jess

    15/61

    Inference Process

    1. Rules and facts compared usingpattern matcher.

    2. Matched rules activated into aconflict 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.

  • 7/30/2019 Intro to Jess

    16/61

    The Java Expert System Shell

    Developed at Sandia NationalLaboratories in late 1990s.

    Created by Dr. Ernest J. Friedman-Hill.

    Inspired by the AI production rule

    language CLIPS. Fully developed Java API forcreating rule-based expertsystems.

  • 7/30/2019 Intro to Jess

    17/61

    Rule-Based Expert System

    Architecture

    Rule Base (knowledge base)

    Working Memory (fact base)

    Inference Engine (rule engine)

  • 7/30/2019 Intro to Jess

    18/61

    Inference (Rule) Engines

    Pattern Matcher decides whatrules to fire and when.

    Agenda schedules the order inwhich activated rules will fire.

    Execution Engine responsible

    for firing rules and executing othercode.

  • 7/30/2019 Intro to Jess

    19/61

    Inference Process

    Match the facts against the rules. Choose which rules to fire.

    Execute the actions associated

    with the rules.

  • 7/30/2019 Intro to Jess

    20/61

    How Does Jess Work?

    Jess matches facts in the fact base torules in the rule base.

    The rules contain function calls thatmanipulate the fact base and/or otherJava code.

    Jess uses the Rete (ree-tee) algorithm

    to match patterns. Rete network = an interconnectedcollection of nodes = working memory.

  • 7/30/2019 Intro to Jess

    21/61

    Jess Architecture Diagram

    WORKING

    MEMORY

    RULEBASE

    EXECUTION

    ENGINE

    INFERENCE

    ENGINE

    PATTERN

    MATCHER

    AGENDA

  • 7/30/2019 Intro to Jess

    22/61

    Procedural Programming

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

    Largely based on functions.

    Programmer controls logic.

    Sequential and deterministic.

    Object-oriented programming isprocedural within object methods.

  • 7/30/2019 Intro to Jess

    23/61

    Declarative Programming

    New programming paradigm -rules.

    Programmer does not reallycontrol code logic.

    Rule engine finds most efficientpath of code execution.

    Replaces hard to maintain nestedIFTHENELSE coding.

  • 7/30/2019 Intro to Jess

    24/61

    Wait a minute!

    Wellyes andnobut dont worry,Calvin!

    What? II

    cant control

    my code??

  • 7/30/2019 Intro to Jess

    25/61

    Thought Experiment

    Imagine writing a procedural/OOPalgorithm to solve ajigsaw

    puzzle. 500+ pieces, different shapes and

    colors.

    Polymorphism runs amok!

    Yet we manage to solve thepuzzle

  • 7/30/2019 Intro to Jess

    26/61

    Intuition and Rules

    Dump the puzzle pieces on a cardtable in no particular order.

    Your brain instinctivelybegins toapply rules to solve the puzzle!

    What might this look like in code?

  • 7/30/2019 Intro to Jess

    27/61

    Intuitive Inferencing

    (corner_found(piece_is_corner)

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

    (edge_found

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

    Your brainknows what todo with a cornerpiece

    and an edgepiece.

  • 7/30/2019 Intro to Jess

    28/61

    Whats Going On

    Your brain recalls rules orheuristics to solve the problem.

    Your brain pattern-matches,prioritizes, and applies rulesaccording to the facts in memory.

    A particular solution algorithmemergesas rules fire on facts.

  • 7/30/2019 Intro to Jess

    29/61

    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.

  • 7/30/2019 Intro to Jess

    30/61

    Obligatory Tradition

    (printoutt Hello PJUG-ers! crlf)

    Your very first Jess

    program!

  • 7/30/2019 Intro to Jess

    31/61

    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:

  • 7/30/2019 Intro to Jess

    32/61

    Jess Variables

    Named containers that hold asingle value.

    Untyped. Begin with a ? mark.

    Can change types during lifetime.

    Assigned using bind function.

  • 7/30/2019 Intro to Jess

    33/61

    Jess Variables and Lists

    EXAMPLE: Adding two numbers

    (bind?x 2) ; assign x = 2

    (bind?y 3) ; assign y = 3(bind?result (+ ?x ?y)) ; findsum

    Everything is a list in Jess!

  • 7/30/2019 Intro to Jess

    34/61

    Control Flow

    foreach

    if/then/else

    while

    apply

    build

    eval

    progn

    Common Jess-specific

  • 7/30/2019 Intro to Jess

    35/61

    Jess Functions

    (deffunction get-input()Get user input from

    console.

    (bind?s (read))(return ?s))

    Even functions are lists.

  • 7/30/2019 Intro to Jess

    36/61

    Jess Function Example

    (deffunction area-sphere(?radius)

    Calculate the area of a

    sphere

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

    (return ?area))

  • 7/30/2019 Intro to Jess

    37/61

    Jess Function Example

    (printout t "The surfacearea of a radius = 2 meter

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

    How do we use this in Jess?

  • 7/30/2019 Intro to Jess

    38/61

    Working With Facts

    Facts have a head and one ormore slots.

    Slots hold data (can be typed). Multislots can hold lists.

    You can modify slot values at

    runtime. Facts are constructed from

    templates.

  • 7/30/2019 Intro to Jess

    39/61

    Jess Fact Types

    Ordered head only.

    Ordered single slot. Unordered multiple slot, like a

    database record.

    Shadow slots correspond toproperties of a JavaBean.

  • 7/30/2019 Intro to Jess

    40/61

    Deftemplate

    Used to define the structure of a fact.

    (deftemplatepattern A design pattern.(slot name)(slot type (defaultcreation))(slot intent)(slot solution))

  • 7/30/2019 Intro to Jess

    41/61

    Asserting Facts

    ;; Asserting a new patternfact.

    (printoutt Enter pattern

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

    Facts store the initial conditions.

  • 7/30/2019 Intro to Jess

    42/61

    All Kinds of Facts

    ;; An ordered fact with no

    slots a placeholder thatindicates state.

    (assert(answer-is-valid))

    ;; A ordered fact of one slot

    (assert(weightfactor 0.75))

  • 7/30/2019 Intro to Jess

    43/61

    Shadow Facts

    defclasscreates a deftemplate

    from a bean. definstanceadds bean to

    working memory.

    Shadow facts are unordered factswhose slots correspond to the

    properties of a JavaBean.

  • 7/30/2019 Intro to Jess

    44/61

    Jess Rules

    are the knowledge-base of thesystem.

    fire only once on a given set offacts.

    use pattern constraints tomatch facts.

    are much faster than IF-THENstatements.

  • 7/30/2019 Intro to Jess

    45/61

    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.

  • 7/30/2019 Intro to Jess

    46/61

    Simple Rule Example

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

    (error-is-present)=>

    (printoutt There is an errorcrlf))

    Checking working memory state.

  • 7/30/2019 Intro to Jess

    47/61

    A More Complex Rule

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

    Using pattern bindings in rules.

  • 7/30/2019 Intro to Jess

    48/61

    More Pattern and Control

    Tools

    Literal /variableconstraints

    Logical

    conditionaltests

    Predicatefunctions

    Salience Modules

    Defquery

    Backward-

    chaining

    matching control and structure

  • 7/30/2019 Intro to Jess

    49/61

    Break Time!

    Lets take a quick 5minute pause

  • 7/30/2019 Intro to Jess

    50/61

    Scripting Java from Jess

    You can interactively access allJava APIs from Jess.

    This makes exploring Javasomewhat easier and immediate.

    No code, compile, debug cycle.

  • 7/30/2019 Intro to Jess

    51/61

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

    (?*frame* setSize 500 300)((?*frame* getContentPane) add ?*button*)

    (?*frame* setVisible TRUE)

    swingDemo.bat

  • 7/30/2019 Intro to Jess

    52/61

    Demo 1: PAT

    (Pattern Analysis Tool) PAT is a simple decision-tree for

    choosing a Java design-pattern.

    Uses an initial interviewtoestablish problem space.

    Recommends a GoF Java design-

    pattern to fit the available facts.

    examples/pattern.clp

  • 7/30/2019 Intro to Jess

    53/61

    The Jess API

    jess - inference engine guts.

    jess.awt GUI wrappers.

    jess.factory - Allows extensionsthat get into the guts of Jess.

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

  • 7/30/2019 Intro to Jess

    54/61

    The Rete (ree-tee) Object

    The reasoning engine and thecentral class in the Jess library.

    Executes the built Rete network,and coordinates many otheractivities.

    Rete is essentially a facade for theJess API.

  • 7/30/2019 Intro to Jess

    55/61

    Using the Jess API

    issimpleall

    you really doneed to do iscall one or

    more Retemethods

    try {

    Rete engine = new

    Rete();engine.executeCommand(

    printout t Hello PJUG);

    engine.run();}

    catch (JessException je {}

  • 7/30/2019 Intro to Jess

    56/61

    Demo 2: Catalog Servlet

    import jess.*;

    import javax.servlet.*;

    import javax.servlet.http.*;

    import java.io.*;

    publicabstractclass BaseServletextends HttpServlet {

    publicvoiddoPost(HttpServletRequestrequest, HttpServletResponseresponse) throws IOException,ServletException {

    doGet(request, response);

    }

    }...

    Here is an

    example of aJessapplicationrunning as aJava servlet

  • 7/30/2019 Intro to Jess

    57/61

    Pattern References

    Shalloway, A., Design PatternsExplained, Addison-Wesley, 2002

    Gamma, E. et. al., DesignPatterns: Elements of ReusableObject-Orient Software, Addison-

    Wesley, 1995

  • 7/30/2019 Intro to Jess

    58/61

    Expert Systems References

    Friedman-Hill, E. J.,Jess InAction, Manning Press, 2003

    Jackson, P., Introduction toExpert Systems 3rdEd., Addison-Wesley, 1999

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

  • 7/30/2019 Intro to Jess

    59/61

    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-Hills Jess in

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

    http://herzberg.ca.sandia.gov/jess/index.shtmlhttp://herzberg.ca.sandia.gov/jess/mailing_list.shtmlhttp://herzberg.ca.sandia.gov/jess/mailing_list.shtmlhttp://www.manning.com/friedman-hill/http://www.manning.com/friedman-hill/http://www.manning.com/friedman-hill/http://www.manning.com/friedman-hill/http://herzberg.ca.sandia.gov/jess/mailing_list.shtmlhttp://herzberg.ca.sandia.gov/jess/mailing_list.shtmlhttp://herzberg.ca.sandia.gov/jess/index.shtml
  • 7/30/2019 Intro to Jess

    60/61

    For Further Study

    CLIPS Expert System Shell

    http://www.ghg.net/clips/CLIPS.html

    FuzzyJ Websitehttp://www.iit.nrc.ca/

    IR_public/fuzzy/

    fuzzyJToolkit2.html

    http://www.ghg.net/clips/CLIPS.htmlhttp://www.iit.nrc.ca/IR_public/fuzzy/fuzzyJToolkit2.htmlhttp://www.iit.nrc.ca/IR_public/fuzzy/fuzzyJToolkit2.htmlhttp://www.iit.nrc.ca/IR_public/fuzzy/fuzzyJToolkit2.htmlhttp://www.iit.nrc.ca/IR_public/fuzzy/fuzzyJToolkit2.htmlhttp://www.iit.nrc.ca/IR_public/fuzzy/fuzzyJToolkit2.htmlhttp://www.iit.nrc.ca/IR_public/fuzzy/fuzzyJToolkit2.htmlhttp://www.iit.nrc.ca/IR_public/fuzzy/fuzzyJToolkit2.htmlhttp://www.iit.nrc.ca/IR_public/fuzzy/fuzzyJToolkit2.htmlhttp://www.ghg.net/clips/CLIPS.html
  • 7/30/2019 Intro to Jess

    61/61

    Q & A

    Thanks for yourattention, and Ihope that you

    try Jess!