Introduction to CLIPS Expert System

32
CLIPS: Expert System Shell Motaz K. Saad College of IT – CS Dept. 1

description

Introduction to CLIPS Expert System

Transcript of Introduction to CLIPS Expert System

Page 1: Introduction to CLIPS Expert System

CLIPS: Expert System Shell

Motaz K. SaadCollege of IT – CS Dept.

1

Page 2: Introduction to CLIPS Expert System

What is expert system?

• AN EXPERT SYSTEM (ES) IS A COMPUTER PROGRAM DESIGNED TO SIMULATE THE PROBLEM-SOLVING BEHAVIOR OF AN EXPERT IN A NARROW DOMAIN OR DISCIPLINE.

2

Page 3: Introduction to CLIPS Expert System

What is expert system?

ARTIFICIALINTELLIGENCE

PROGRAMS

Exhibit intelligentbehavior by skillful

application of heuristics

make domain knowledgeexplicit and separate from

the rest of the system

KNOWLEDGE-BASEDSYSTEMS

Apply expert knowledgeto difficult, real world

problems

EXPERTSYSTEMS

3

Page 4: Introduction to CLIPS Expert System

The structure of an expert system

4

KNOWLEDGE BASE Domain Knowledge

FACTS

RULES

INTERPRETER

SCHEDULER

INFERENCE ENGINE General

problem-solving knowledge

Page 5: Introduction to CLIPS Expert System

Facts and Rules

• A fact is a piece of information such as (color green)

• Facts on their own are of only limited use.• The application of rules is necessary to

develop a program capable of some useful function.

5

Page 6: Introduction to CLIPS Expert System

Rules

• A rule is a formal way of specifying a recommendation, directive, or advice

• A rule is expressed as IF IF premisepremise THEN THEN conclusionconclusion

orIF IF conditioncondition THEN THEN actionaction

6

Page 7: Introduction to CLIPS Expert System

Drawing inferences from rules

• Forward chaining– In forward chaining, the reasoning proceeds from

the IF part of the rule to the THEN part.

• Backward chaining– In backward chaining, the reasoning proceeds

from the THEN part to the IF part.

7

Page 8: Introduction to CLIPS Expert System

Expert system building tool

• Programming language– An expert system can be implemented using a general

purpose programming language. However, the programming languages LISP and PROLOG are typically used in expert systems implementation, in particular Artificial Intelligence applications. This is due to their capabilities in handling symbolic data efficiently.

• Shells– A shell consists mainly of an inference engine and an

editor to assist developers in building their knowledge base.

8

Page 9: Introduction to CLIPS Expert System

Shells vs. Programming languages

9

Features Shells Prog. Lang.

Ease & speed of development

Higher Less

KB Structure &reasoning

Restricted by the tool

May be developed As needed

KB maintenance Easier Difficult

Page 10: Introduction to CLIPS Expert System

Shells vs. Programming languages

10

Features Shells Prog. Lang.

Interfaces Not alwaysfriendly oravailable

Have to bedeveloped

Efficiency/Performance

Slower Faster

Explanation Restricted bythe tool

May be developedas needed

Page 11: Introduction to CLIPS Expert System

CLIPS: Expert System Shell

• Rule-based expert systems are often known as production systems (CLIPS actually stands for C Language Integrated Production System).

11

Page 12: Introduction to CLIPS Expert System

The CLIPS Programming Tool

• History of CLIPS– Influenced by OPS5 and ART– Implemented in C for efficiency and portability– Developed by NASA, distributed & supported by COSMIC– Runs on PC, Mac, also under UNIX and VAX VMS

• CLIPS provides mechanisms for expert systems– A top-level interpreter– Production rule interpreter– Object oriented programming language– LISP-like procedural language

Page 13: Introduction to CLIPS Expert System

Components of CLIPS

• Rule-Based Language– Can create a fact list– Can create a rule set– An inference engine matches facts against rules

• Object-Oriented Language– Can define classes– Can create different sets of instances– Special forms allow you to interface rules and

objects

Page 14: Introduction to CLIPS Expert System

Defining Facts

• Facts can be assertedCLIPS> (assert (today is sunday))<Fact-0>

• Facts can be listedCLIPS> (facts)f-0 (today is sunday)

• Facts can be retractedCLIPS> (retract 0)CLIPS> (facts)

Page 15: Introduction to CLIPS Expert System

Managing Facts

• Clearing all factsCLIPS> (clear)CLIPS> (facts)

• Grouping facts - typically in a file (“today.clp”)(deffacts today ; can be cleared with

(undeffacts today)(today is sunday)(weather is warm)

)

• After loading facts, assert with (reset)

Page 16: Introduction to CLIPS Expert System

Defining Rules

• Rules have the following structure(defrule rule-name optional-comment

optional-declarationcondition...condition=>action...action

)

Page 17: Introduction to CLIPS Expert System

An Example CLIPS Rule

(defrule sunday “Things to do on Sunday”(salience 0) ; salience in the interval [-10000,

10000](today is Sunday)(weather is sunny)=>(assert (chore wash car))(assert (chore chop wood))

)• So, if fact list contains conditions, add

assertions

Page 18: Introduction to CLIPS Expert System

Getting the Rules Started

• The reset command creates a special factCLIPS> (load “today.clp”)CLIPS> (facts)CLIPS> (reset)CLIPS> (facts)f-0 (initial-fact) ...(defrule start

(initial-fact)=>(printout t “hello”)

)

Page 19: Introduction to CLIPS Expert System

Tracing & Recording Things

• Watch command can watch facts (and rules)CLIPS> (watch facts)CLIPS> (reset)==> f-0 (initial-fact)CLIPS> (retract 0)<== f-0 (initial-fact)

• Contents of dialog window can be sent to fileCLIPS> (dribble-on “dribble.clp”) ; any file name will do...CLIPS> (dribble-off “dribble.clp”)

Page 20: Introduction to CLIPS Expert System

Variables & Pattern Matching

• Variables make rules more applicable (defrule pick-a-chore

(today is ?day)(chore is ?job)=>(assert (do ?job on ?day))

)

• If conditions are matched, then bindings are used

Page 21: Introduction to CLIPS Expert System

Retracting Facts from a Rule

(defrule do-a-chore(today is ?day) ; ?day must have a consistent binding?chore <- (do ?job on ?day)=>(printout t ?job “ done”)(retract ?chore)

)• We must assign a variable to item for

retraction

Page 22: Introduction to CLIPS Expert System

Pattern Matching Details

• One-to-one matching(do ?job on ?day)(do washing on monday)

• Use of wild cards(do ? ? monday)(do ? on ?)(do ? ? ?day)(do $?)(do $? monday)(do ?chore $?when)

Page 23: Introduction to CLIPS Expert System

Using Templates(deftemplate student “a student record”

(slot name (type STRING))(slot age (type NUMBER) (default 18)))

CLIPS> (assert (student (name fred)))

(defrule print-a-student(student (name ?name) (age ?age))=>(printout t name? “ is “ ?age)

)

Page 24: Introduction to CLIPS Expert System

Defining Functions in CLIPS

• Uses a LISP or Scheme-like syntax

(deffunction function-name (arg ... arg)action ... action)

(deffunction hypotenuse (?a ?b)(sqrt (+ (* ?a ?a) (* ?b ?b))))

(deffunction initialize ()(clear)(assert (today is sunday)))

Page 25: Introduction to CLIPS Expert System

Defining Classes & Instances

• Defining the class CAR

(defclass car(is-a user)(name)(made-by))

• Defining an instance of CAR

(make-instance corvette of car(made-by chevrolet))

Page 26: Introduction to CLIPS Expert System

Concrete & Abstract Classes

• Some classes only exist for inheritance purposes

Person

Man Woman

Jack Jill

Page 27: Introduction to CLIPS Expert System

Managing Instances

• Commands to display instancesCLIPS> (instances)[corvette] of carCLIPS> (send [corvette] print)[corvette] of car(made-by chevrolet)

• Command to group instances (in a file)(definstances

(corvette of car (made-by chevrolet))(thunderbird of car (made-by ford)))

Page 28: Introduction to CLIPS Expert System

Clearing & Resetting Instances

• Deleting an instanceCLIPS> (send [corvette] delete)

• Deleting all instancesCLIPS> (unmake-instance *)

• Resetting creates an initial objectCLIPS> (reset)CLIPS> (instances)[initial-object] of INITIAL-OBJECT

Page 29: Introduction to CLIPS Expert System

Message Passing

• The SEND function(send [instance] message arg ... arg)

• Converting from symbols to namesCLIPS> (symbol-to-instance-name corvette)[corvette]

This is useful when SENDing from inside a rule

Page 30: Introduction to CLIPS Expert System

Limitations of CLIPS

• Single level rule sets– in LOOPS, you could arrange rule sets in a hierarchy,

embedding one rule set inside another

• Loose coupling of rules and objects– rules can communicate with objects via message passing– rules cannot easily be embedded in objects, as in Centaur

• CLIPS has no explicit agenda mechanism– the basic control flow is forward chaining– to implement other kinds of reasoning you have to

manipulate tokens in working memory

Page 31: Introduction to CLIPS Expert System

Alternatives to CLIPS

• Eclipse– has same syntax as CLIPS (both are based on ART)– supports goal-driven (i.e., backwards) reasoning– has a truth maintenance facility for checking consistency– can be integrated with C++ and dBase– new extension RETE++ can generate C++ header files

• NEXPERT OBJECT– another rule- and object-based system– has facilities for designing graphical interfaces– has a ‘script language’ for designing user front-end– written in C, runs on many platforms, highly portable

Page 32: Introduction to CLIPS Expert System

The End!

32

http://motaz.saad.googlepages.com