50
1 Introduction to CLIPS Introduction to CLIPS (Lecture Note #17) (Lecture Note #17) 인인인인 인인인 인인인인인 인인인인인인

Introduction to CLIPS (Lecture Note #17)

Embed Size (px)

DESCRIPTION

Introduction to CLIPS (Lecture Note #17). 인공지능 이복주 단국대학교 컴퓨터공학과. Outline. Expert Systems: Principles and Programming, Joseph Giarratano & Gary Riley, PWS Publishing Company, 1998 Chapter 7 Introduction to CLIPS Chapter 8 Pattern Matching Introduction to Expert Systems, Peter Jackson - PowerPoint PPT Presentation

Citation preview

Page 1: Introduction to CLIPS (Lecture Note #17)

1

Introduction to CLIPSIntroduction to CLIPS(Lecture Note #17)(Lecture Note #17)

인공지능

이복주단국대학교 컴퓨터공학과

Page 2: Introduction to CLIPS (Lecture Note #17)

2

OutlineOutline

Expert Systems: Principles and Programming, Joseph Giarratano & Gary Riley, PWS Publishing Company, 1998– Chapter 7 Introduction to CLIPS– Chapter 8 Pattern Matching

Introduction to Expert Systems, Peter Jackson– Appendix CLIPS Programming

Page 3: Introduction to CLIPS (Lecture Note #17)

3

IntroductionIntroduction

Three types of programming paradigms in CLIPS– Rule-based– Object-oriented– Procedural

Will focus on rule-based– Three components: fact list, knowledge base (rules),

inference engine

Page 4: Introduction to CLIPS (Lecture Note #17)

4

CLIPSCLIPS

Support rule-based, object-oriented, and procedural programming

Programming language style is similar to OPS5, but more powerful

Support only forward chaining rules– Not backward chaining

COOL: CLIPS Object-Oriented Language– Extension of Common Lisp Object System (CLOS)– Syntactically similar to LISP

Page 5: Introduction to CLIPS (Lecture Note #17)

5

CLIPSCLIPS

CLIPS: C Language Integrated Production System– Designed at NASA Johnson Space Center, mid-80s– High portability, low cost, easy integration with

external system– Original CLIPS supported only rule-based (note:

Production System)– CLIPS version 5.0 added procedural and OO– Due to its portability, installed various computers

from PCs to CRAY– Runs on UNIX, DOS, Windows, and Macintosh– Maintained as public domain software

• E.g., http://www.cosmic.uga.edu

– Downloadable by anonymous ftp• e.g.,

http://www-cgi.cs.cmu.edu/afs/cs/project/ai-repository/ai/areas/expert/systems/clips

Page 6: Introduction to CLIPS (Lecture Note #17)

6

NotationNotation

(example) (example [1]) ; optional

– (example)– (example 1)

(example <integer>)– <> means replacement– (example 1)– (example 5)– (example –20)

<integer>*– * means zero or more– 1– 1 2– 1 2 3

Page 7: Introduction to CLIPS (Lecture Note #17)

7

NotationNotation

<integer>+ – + means one or more– <integer>+ is same as <integer> <integer>*

All | none | some– | indicates a choice

Page 8: Introduction to CLIPS (Lecture Note #17)

8

FieldField

Tokens– Group of characters– Some tokens consist of one character: e.g., ‘(‘, ‘)’

Field: special tokens– Seven types of field– Float, integer, symbol, string, external address,

instance name, instance address Numerical fields: Float, integer

– Float: 1.5, 1.0, 0.7, 9e+1, 3.5e10– Integer: 1, +3, -1, 65

Symbol– Starts with printable ASCII character, followed by

zero or more characters

Page 9: Introduction to CLIPS (Lecture Note #17)

9

FieldField

Delimiters– Any non-printable ASCII characters (spaces, tabs, CRs, LFs)– “ (double quote)– ( (opening parenthesis)– ) (closing parenthesis)– ; (semi colon)– & (ampersand)– | (vertical bar)– ~ (tilde)– < (less than)

?, $: variable– Cannot be placed at the beginning of a symbol

Page 10: Introduction to CLIPS (Lecture Note #17)

10

FieldField

Examples of valid symbols– Space-Station– February– fire– Activate_sprinkler_system– notify-fire-department– !?#$^*– 345B– 346-95-6156

Case-sensitive– Case-sensitive– Case-Sensitive– CASE-SENSITIVE are different symbols

Page 11: Introduction to CLIPS (Lecture Note #17)

11

FieldField

String– Begin and end with double quotation– Examples of valid strings

• “Activate the sprinkler system.”• “!?#$^”• “John Q. Public”

– Spaces in a string are preserved• “spaces”• “spaces “• “ spaces”• “ spaces “ are different strings

– “”three-tokens””• “”• three-tokens• “”

Page 12: Introduction to CLIPS (Lecture Note #17)

12

FieldField

“\”single-token\””– “”single-token””: one string field

“\\single-token\\”– “\single-token\”: one string field

External addresses, instance addresses, instance names– Of little interest in rule-based

Multi-field value– Zero or more fields– When calling a function– (): zero length multi-field– (this that): multi-field containing the symbols this

and that

Page 13: Introduction to CLIPS (Lecture Note #17)

13

Entering and Exiting CLIPSEntering and Exiting CLIPS

CLIPS> – CLIPS prompt– Top level: Commands can be entered directly

(exit): exiting command– Vs. exit: a symbol

Parentheses should be matched and balanced ex<enter-key>it

– Two tokens: ex and it A command sequence

– CLIPS> exit ; exit is a symbol– exit– CLIPS> (+ 3 4) ; a function– 7– CLIPS> (exit) ; exit command

Page 14: Introduction to CLIPS (Lecture Note #17)

14

FactsFacts

Facts consists of– Relation name: a symbolic field– Zero or more slots: also symbolic fields– Associated values

Example– (person (name “John Q. Public”)– (age 23)– (eye-color blue)– (hair-color black))

Order of slots are irrelevant– (person (hair-color black)– (name “John Q. Public”)– (age 23)– (eye-color blue)– is the same as above

Page 15: Introduction to CLIPS (Lecture Note #17)

15

Deftemplate ConstructDeftemplate Construct

Deftemplate construct– Groups of facts share the same relation name– The common information is described– Analogous to a record structure in Pascal and C

General format– (deftemplate <relation-name> [<optional-comment>] <slot-definitio

n>*) <slot-definition>

– (slot <slot-name>) | (multislot <slot-name>) Person fact

– (deftemplate person “An example deftemplate”– (slot name)– (slot age)– (slot eye-color)– (slot hair-color))

Page 16: Introduction to CLIPS (Lecture Note #17)

16

Multi-field SlotsMulti-field Slots

Place one or more fields into a given slot Person deftemplate

– (deftemplate person “An example deftemplate”– (multislot name)– (slot age)– (slot eye-color)– (slot hair-color))

Person fact– (person (name John Q. Public)– (age 23)– (eye-color blue)– (hair-color black))

Page 17: Introduction to CLIPS (Lecture Note #17)

17

Ordered FactsOrdered Facts

Ordered facts– Does not have a corresponding deftemplate– Slot name is not required

Example– (number-list 7 9 3 4 20)

Equivalent to – (deftemplate number-list (multislot values)– (number-list (values 7 9 3 4 20))

Page 18: Introduction to CLIPS (Lecture Note #17)

18

Adding and Removing FactsAdding and Removing Facts

Facts are added and removed from the fact list Assert command: add a fact

– (assert <fact>+) Example

– CLIPS> – (deftemplate person– (slot name)– (slot age)– (slot eye-color)– (slot hair-color))– CLIPS>– (assert (person (name “John Q. Public”)– (age 23)– (eye-color blue)– (hair-color black)))– <Fact-0> ; assert returns a value– CLIPS>

Page 19: Introduction to CLIPS (Lecture Note #17)

19

Adding and Removing FactsAdding and Removing Facts

Facts command– (facts)– Displays the facts in the fact list

Example– CLIPS> (facts)– f-0 (person (name “John Q. Public”)– (age 23)– (eye-color blue)– (hair-color black))– For a total of 1 fact.– CLIPS>

f-0: fact identifier– 0: fact index

Page 20: Introduction to CLIPS (Lecture Note #17)

20

Adding and Removing FactsAdding and Removing Facts

Duplicate facts are not allowed Adding another facts

– CLIPS>– (assert (person (name “Jane Q. Public”)– (age 36)– (eye-color green)– (hair-color red)))– <Fact-1>– CLIPS> (facts)– f-0 (person (name “John Q. Public”)– (age 23)– (eye-color blue)– (hair-color black))– f-1 (person (name “Jane Q. Public”)– (age 34)– (eye-color green)– (hair-color red))– For a total of 2 facts.– CLIPS>

Page 21: Introduction to CLIPS (Lecture Note #17)

21

Adding and Removing FactsAdding and Removing Facts

Adding more than one facts– (assert (person (name “Jane Q. Public”)– (age 36)– (eye-color green)– (hair-color red))– (person (name “Jane Q. Public”)– (age 34)– (eye-color green)– (hair-color red)))

Page 22: Introduction to CLIPS (Lecture Note #17)

22

Adding and Removing FactsAdding and Removing Facts

Viewing a portion of fact list– (facts [<start> [<end> [<maximum>]]])– No more than <maximum> facts are displayed

Removing a fact– The deleted fact identifier will be missing– Called retraction– (retract <fact-index>+)

• (retract 0) ; John Q. Public removed• (retract 1) ; Jane Q. Public removed

Removing non-existent fact– CLIPS> (retract 1)– CLIPS> (retract 1)– [PRNTUTIL1] Unable to find fact f-1.– CLIPS>

Retract multiple facts– (retract 0 1)

Page 23: Introduction to CLIPS (Lecture Note #17)

23

Modifying and Duplicating FactsModifying and Duplicating Facts

Modify command– Slot values of deftemplate facts can be modified– (modify <fact-index> <slot-modifier>+)

<slot-modifier>– (<slot-name> <slot-value>)

Example: John Q. Public’s age is increased– CLIPS> (modify 0 (age 24)) – <Fact-2> – CLIPS> (facts)– f-2 (person (name “John Q. Public”) ; new index– (age 24)– (eye-color blue)– (hair-color black))– For a total of 1 fact.– CLIPS>

Page 24: Introduction to CLIPS (Lecture Note #17)

24

Modifying and Duplicating FactsModifying and Duplicating Facts

Duplicate command– Same as modify except– does not retract the original fact

Example– CLIPS> (duplicate 2 (name “Jack S. Public”))– <Fact-3>– CLIPS> (facts)– f-2 (person (name “John Q. Public”)– (age 24)– (eye-color blue)– (hair-color black))– f-3 (person (name “Jack S. Public”)– (age 24)– (eye-color blue)– (hair-color black))– For a total of 2 facts.– CLIPS>

Page 25: Introduction to CLIPS (Lecture Note #17)

25

The Watch CommandThe Watch Command

Watch command– For debugging– (watch <watch-item>)

<Watch-item>– One of the symbols:

facts, rules, activations, statistics, compilations, focus, all

Example– CLIPS> (facts 3 3)– f-3 (person (name “Jack

S. Public”)– (age 24)– (eye-color blue)– (hair-color

black))– For a total of 1 fact.

– CLIPS> (watch facts)– CLIPS> (modify 3 (age 25))– <== f-3 (person (name “Jack S. Pu

blic”)– (age 24)– (eye-color blue)– (hair-color black))– ==> f-4 (person (name “Jack S. Pu

blic”)– (age 25)– (eye-color blue)– (hair-color black))– <Fact-4>– CLIPS>

Unwatch– (unwatch <watch-item)

Page 26: Introduction to CLIPS (Lecture Note #17)

26

Deffacts ConstructDeffacts Construct

Assert a set of facts– (deffacts <deffacts name> [<optional comments>] <facts>*)

example– (deffacts people “Some people we know”– (person (name “John Q. Public”) (age 24)– (eye-color blue) (hair-color black))– (person (name “Jack S. Public”) (age 24)– (eye-color blue) (hair-color black))– (person (name “Jane Q. Public”) (age 36)– (eye-color green) (hair-color red)))

Assert the deffatcs statement– (reset)

Page 27: Introduction to CLIPS (Lecture Note #17)

27

Deffacts ConstructDeffacts Construct

Example– CLIPS> (unwatch facts)– CLIPS> (reset)– CLIPS> (facts)– f-0 (initial-fact) ; new fact by reset command– f-1 (person (name “John Q. Public”) (age 24)– (eye-color blue) (hair-color black))– f-2 (person (name “Jack S. Public”) (age 24)– (eye-color blue) (hair-color black))– f-3 (person (name “Jane Q. Public”) (age 36)– (eye-color green) (hair-color red))– For a total of 4 facts.– CLIPS>

Page 28: Introduction to CLIPS (Lecture Note #17)

28

The Components of a RuleThe Components of a Rule

A rule example– In an industrial plant monitoring expert system– IF the emergency is a fire – THEN the response is to activate the sprinkler system

Define template for the emergency and response– (deftemplate emergency (slot type))

• type could be fire, flood, power outage

– (deftemplate response (slot action)) Define rule

– (defrule fire-emergency “An example rule”– (emergency (type fire))– =>– (assert (response (action activate-sprinkler-system))))

General format– (defrule <rule name> [<comment>]– <pattern>* ; LHS of the rule– =>– <actions>*) ; RHS of the rule

Page 29: Introduction to CLIPS (Lecture Note #17)

29

The Agenda and ExecutionThe Agenda and Execution

Agenda– The collection of activated rules

Salience– The priority of a rule in integer

Run command– (run <limit>)

• <limit>: maximum number of rules to be fired

Displaying the agenda– (agenda)

Example– CLIPS> (reset)– CLIPS> (assert (emergency (type fire)))– <Fact-1>– CLIPS> (agenda)– 0 fire-emergency: f-1 ; salience, rule, fact– For a total of 1 activation.– CLIPS>

Page 30: Introduction to CLIPS (Lecture Note #17)

30

Rules and RefractionRules and Refraction

Run command– CLIPS> (run)– CLIPS> (facts)– f-0 (initial-fact)– f-1 (emergency (type fire))– f-2 (response (action activate-sprinkler-system))– For a total of 3 facts.– CLIPS>

refraction– Rules are not fired more than once for a specific set of facts

(clear)– removes all constructs and all facts– Vs. (reset) asserts the deffacts statement

Page 31: Introduction to CLIPS (Lecture Note #17)

31

The Printout CommandThe Printout Command

Printout command– (printout <logical-name> <print-items>*)

Example– (defrule fire-emergency– (emergency (type fire))– =>– (printout t “Activate the sprinkler system” crlf))– ; t: terminal, destination name

Using multiple rules– (defrule flood-emergency– (emergency (type flood))– =>– (printout t “Shut down electrical equipment” crlf))

Page 32: Introduction to CLIPS (Lecture Note #17)

32

Using Multiple PatternsUsing Multiple Patterns Rules with multiple patterns

– (deftemplate extinguisher-system– (slot type)– (slot status))– (defrule class-A-fire-emergency– (emergency (type class-A-fire))– (extinguisher-system (type water-sprinkler)– (status off))– =>– (printout t “Activate water sprinkler” crlf))– (defrule class-B-fire-emergency– (emergency (type class-B-fire))– (extinguisher-system (type carbon-dioxide)– (status off))– =>– (printout t “Use carbon dioxide extinguisher” crlf))

Page 33: Introduction to CLIPS (Lecture Note #17)

33

Loading and Saving ConstructLoading and Saving Construct

Loading constructs from a file– (load <file-name>)– E.g., (load “fire.clp”)

Watching compilation– CLIPS> (load “fire.clp”)– Defining deftemplate: emergency– Defining deftemplate: response– Defining defrule: fire-emergency– TRUE– CLIPS>

No watch– CLIPS> (clear)– CLIPS> (unwatch compilations)– CLIPS> (load “fire.clp”)– %%* ; % for defconstruct * for rule– TRUE– CLIPS>

Page 34: Introduction to CLIPS (Lecture Note #17)

34

Saving Construct to a FileSaving Construct to a File

(save <file-name>)– E.g., (save “fire.clp”)– Saves all the constructs

Saving partial constructs– Use an text editor

Page 35: Introduction to CLIPS (Lecture Note #17)

35

VariablesVariables

Variables– Question mark followed by symbol– e.g., ?speed, ?sensor, ?value, ?noun, ?color

Bind– Assignment of a value to a variable

Page 36: Introduction to CLIPS (Lecture Note #17)

36

VariablesVariables

Example– CLIPS> (clear)– CLIPS>– (deftemplate person– (slot name) – (slot eyes)– (slot hair))– CLIPS>– (defrule find-blue-eyes)– (person (name ?name) (e

yes blue))– =>– (printout t ?name “ has bl

ue eyes.” crlf))

– CLIPS> – (deffacts people– (person (name Jane)– (eyes blue) (hair red))– (person (name Joe)– (eyes green) (hair brown))– (person (name Jack)– (eyes blue) (hair black))– (person (name Jeff)– (eyes green) (hair brown)))– CLIPS> (reset) – ; Assert the deffatcs– CLIPS> (run)– Jack has blue eyes.– Jane has blue eyes.– CLIPS>

Page 37: Introduction to CLIPS (Lecture Note #17)

37

Blocks WorldBlocks World

Blocks world initial configuration

Facts about which blocks are on top of others– (deftemplate on-top-of– (slot upper)– (slot lower))– (on-top-of (upper A) (lower B))– (on-top-of (upper B) (lower C))– (on-top-of (upper D) (lower E))– (on-top-of (upper E) (lower F))– (on-top-of (upper nothing) (lower A))– (on-top-of (upper C) (lower floor))– (on-top-of (upper nothing) (lower D))– (on-top-of (upper F) (lower floor))

C

B

A

F

E

DGoal: C on top of E

Page 38: Introduction to CLIPS (Lecture Note #17)

38

Blocks WorldBlocks World

Floor and nothing are not blocks– (block A)– (block B)– (block C)– (block D)– (block E)– (block F)

Describe the goal– (deftemplate goal (slot move) (slot on-top-of))– (goal (move C) (on-top-of E))– on-top-of was also a template name in the prev page

Initial state– (deffatcs initial-state– (block A)– …– (on-top-of …– …– (goal (move C) (on-top-of E)))

C

B

A

F

E

D

Page 39: Introduction to CLIPS (Lecture Note #17)

39

Blocks WorldBlocks World

Move-directly rule– (defrule move-directly– ?goal <- (goal (move ?block1) – (on-top-of ?block2))– (block ?block1)– (block ?block2)– (on-top-of (upper nothing) (lower ?block1))– ?stack-1 <- (on-top-of (upper ?blocks1) (lower ?block3))– ?stack-2 <- (on-top-of (upper nothing) (lower ?block2))– =>– (retract ?goal ?stack-1 ?stack-2)– (assert (on-top-of (upper ?block1) (lower ?block2))– (on-top-of (upper nothing) (lower ?block3)))– (printout t ?block1 “moved on top of “ ?block2 “.” crlf))

block3

block1

block2

Page 40: Introduction to CLIPS (Lecture Note #17)

40

Blocks WorldBlocks World

Move-to-floor rule– (defrule move-to-floor– ?goal <- (goal (move ?block1) – (on-top-of floor))– (block ?block1)– (on-top-of (upper nothing) (lower ?block1))– ?stack <- (on-top-of (upper ?block1) (lower ?block2))– =>– (retract ?goal ?stack)– (assert (on-top-of (upper ?block1) (lower floor))– (on-top-of (upper nothing) (lower ?block2)))– (printout t ?block1 “moved on top of floor.“ crlf))

block2

block1

Page 41: Introduction to CLIPS (Lecture Note #17)

41

Blocks WorldBlocks World

Clear-upper-block rule– (defrule clear-upper-block– (goal (move ?block1)) ; move is a slot of goal– (block ?block1)– (on-top-of (upper ?block2) (lower ?block1))– (block ?block2)– =>– (assert (goal (move ?block2) (on-top-of floor))))

Clear-lower-block rule (actually clear the top of block)– (defrule clear-lower-block– (goal (on-top-of ?block1)) ; on-top-of is a slot of goal– (block ?block1)– (on-top-of (upper ?block2) (lower ?block1))– (block ?block2)– =>– (assert (goal (move ?block2) (on-top-of floor))))

block1

block2

Page 42: Introduction to CLIPS (Lecture Note #17)

42

Blocks WorldBlocks World

Run– CLIPS> (reset)– CLIPS> (run)– A moved on top of floor.– B moved on top of floor.– D moved on top of floor.– C moved on top of E.– CLIPS>

C

B

A

F

E

D

Page 43: Introduction to CLIPS (Lecture Note #17)

43

Functions and ExpressionsFunctions and Expressions

Elementary math functions– CLIPS> (+ 2 2)– 4– CLIPS> (/ 2 3)– 0.666667– CLIPS> (+ 2 3.0)– 5.0– CLIPS> (+ 2 3 4)– 9– CLIPS> (- 2 3 4)– -5– CLIPS> (+ 2 (* 3 4))– 14

Page 44: Introduction to CLIPS (Lecture Note #17)

44

Functions and ExpressionsFunctions and Expressions

Embed the expression within other expressions– CLIPS> (clear)– CLIPS> (assert (answer (+ 2 3)))– <Fact-0>– CLIPS (facts)– f-0 (answer 4)– For a total of 1 fact.– CLIPS>

Function names are also symbols– CLIPS> (clear)– CLIPS> (assert (expression 2 + 3 * 4))– <Fact-0>– CLIPS> (facts)– f-0 (expression 2 + 3 * 4)– For a total of 1 fact.– CLIPS>

Page 45: Introduction to CLIPS (Lecture Note #17)

45

I/O FunctionsI/O Functions

The read function– CLIPS> (clear)– CLIPS>– (defrule get-first-name– => ; no pattern– (printout t “What is your first name? “)– (bind ?response (read))– (assert (user’s-name ?response)))– CLIPS> (reset)– CLIPS> (run)– What is your first name? Gary– CLIPS> (facts)– F-0 (initial-fact)– F-1 (user’s-name Gary)– For a total of 2 facts.– CLIPS>

Page 46: Introduction to CLIPS (Lecture Note #17)

46

I/O FunctionsI/O Functions

Open function– (open <file-name> <file-ID> [,file-access.])– E.g., (open “input.dat” data “r”)

• Can use “w”, “r+”, “a” also

Close function– (close [<file-ID>])– E.g., (close data)

Writing to a file– CLIPS> (open “example.dat” example “w”)– TRUE– CLIPS> (printout example “green” crlf)– CLIPS> (printout example 7 crlf)– CLIPS> (close example)– TRUE– CLIPS>

Page 47: Introduction to CLIPS (Lecture Note #17)

47

I/O FunctionsI/O Functions

Reading from a file– CLIPS> (open “example.dat” example “r”)– TRUE– CLIPS> (read example)– green– CLIPS> (read example)– 7– CLIPS> (read example)– EOF– CLIPS> (close example)– TRUE– CLIPS>

Page 48: Introduction to CLIPS (Lecture Note #17)

48

I/O FunctionsI/O Functions

The readline function– CLIPS> (clear)– CLIPS>– Defrule get-name– =>– (printout t “What is your name? “)– (bind ?response (readline))– (assert (user’s-name ?response)))– CLIPS> (reset)– CLIPS> (run)– What is your name? Gary Riley– CLIPS> (facts)– F-0 (initial-fact)– F-1 (user’s-name “Gary Riley”)– For a total of 2 facts.– CLIPS>

Page 49: Introduction to CLIPS (Lecture Note #17)

49

I/O FunctionsI/O Functions

The readline function with multi-field– CLIPS> (clear)– CLIPS>– Defrule get-name– =>– (printout t “What is your name? “)– (bind ?response (explode$ (readline)))– (assert (user’s-name ?response)))– CLIPS> (reset)– CLIPS> (run)– What is your name? Gary Riley– CLIPS> (facts)– F-0 (initial-fact)– F-1 (user’s-name Gary Riley)– For a total of 2 facts.– CLIPS>

Page 50: Introduction to CLIPS (Lecture Note #17)

50

SummarySummary

Expert Systems: Principles and Programming, Joseph Giarratano & Gary Riley, PWS Publishing Company, 1998– Chapter 7 Introduction to CLIPS– Chapter 8 Pattern Matching

Introduction to Expert Systems, Peter Jackson– Appendix CLIPS Programming