Introduction to CLIPS Lecture Note 17 1 Outline





![Notation (example) q (example [1]) ; optional q – (example) – (example 1) q Notation (example) q (example [1]) ; optional q – (example) – (example 1) q](https://slidetodoc.com/presentation_image_h2/5b976813527e9292c00a5e92726a5ff0/image-6.jpg)



















![Deffacts Construct q Assert a set of facts – (deffacts <deffacts name> [<optional comments>] Deffacts Construct q Assert a set of facts – (deffacts <deffacts name> [<optional comments>]](https://slidetodoc.com/presentation_image_h2/5b976813527e9292c00a5e92726a5ff0/image-26.jpg)



















![I/O Functions q Open function – (open <file-name> <file-ID> [, file-access. ]) – E. I/O Functions q Open function – (open <file-name> <file-ID> [, file-access. ]) – E.](https://slidetodoc.com/presentation_image_h2/5b976813527e9292c00a5e92726a5ff0/image-46.jpg)




- Slides: 50

Introduction to CLIPS (Lecture Note #17) 인공지능 이복주 단국대학교 컴퓨터공학과 1

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

Introduction q Three types of programming paradigms in CLIPS – Rule-based – Object-oriented – Procedural q Will focus on rule-based – Three components: fact list, knowledge base (rules), inference engine 3

CLIPS Support rule-based, object-oriented, and procedural programming q Programming language style is similar to OPS 5, but more powerful q Support only forward chaining rules q – Not backward chaining q COOL: CLIPS Object-Oriented Language – Extension of Common Lisp Object System (CLOS) – Syntactically similar to LISP 4

CLIPS q CLIPS: C Language Integrated Production System – Designed at NASA Johnson Space Center, mid-80 s – 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/airepository/ai/areas/expert/systems/clips 5
![Notation example q example 1 optional q example example 1 q Notation (example) q (example [1]) ; optional q – (example) – (example 1) q](https://slidetodoc.com/presentation_image_h2/5b976813527e9292c00a5e92726a5ff0/image-6.jpg)
Notation (example) q (example [1]) ; optional q – (example) – (example 1) q (example <integer>) – – q <> means replacement (example 1) (example 5) (example – 20) <integer>* – – * means zero or more 1 12 123 6

Notation q <integer>+ – + means one or more – <integer>+ is same as <integer>* q All | none | some – | indicates a choice 7

Field q Tokens – Group of characters – Some tokens consist of one character: e. g. , ‘(‘, ‘)’ q Field: special tokens – Seven types of field – Float, integer, symbol, string, external address, instance name, instance address q Numerical fields: Float, integer – Float: 1. 5, 1. 0, 0. 7, 9 e+1, 3. 5 e 10 – Integer: 1, +3, -1, 65 q Symbol – Starts with printable ASCII character, followed by zero or more characters 8

Field q Delimiters – Any non-printable ASCII characters (spaces, tabs, CRs, LFs) – “ (double quote) – ( (opening parenthesis) – ) (closing parenthesis) – ; (semi colon) – & (ampersand) – | (vertical bar) – ~ (tilde) – < (less than) q ? , $: variable – Cannot be placed at the beginning of a symbol 9

Field q Examples of valid symbols – – – – q Space-Station February fire Activate_sprinkler_system notify-fire-department !? #$^* 345 B 346 -95 -6156 Case-sensitive – Case-Sensitive – CASE-SENSITIVE are different symbols 10

Field q 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 • “” 11

Field q “”single-token”” – “”single-token””: one string field q “\single-token\” – “single-token”: one string field q External addresses, instance names – Of little interest in rule-based q 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 12

Entering and Exiting CLIPS q CLIPS> – CLIPS prompt – Top level: Commands can be entered directly q (exit): exiting command – Vs. exit: a symbol Parentheses should be matched and balanced q ex<enter-key>it q – Two tokens: ex and it q A command sequence – – – CLIPS> exit CLIPS> (+ 3 4) 7 CLIPS> (exit) ; exit is a symbol ; a function ; exit command 13

Facts q Facts consists of – Relation name: a symbolic field – Zero or more slots: also symbolic fields – Associated values q Example – (person (name “John Q. Public”) – (age 23) – (eye-color blue) – (hair-color black)) q Order of slots are irrelevant – (person (hair-color black) – (name “John Q. Public”) – (age 23) – (eye-color blue) – is the same as above 14

Deftemplate Construct q Deftemplate construct – Groups of facts share the same relation name – The common information is described – Analogous to a record structure in Pascal and C q General format – (deftemplate <relation-name> [<optional-comment>] <slot-definition>*) q <slot-definition> – (slot <slot-name>) | (multislot <slot-name>) q Person fact – (deftemplate person “An example deftemplate” – (slot name) – (slot age) – (slot eye-color) – (slot hair-color)) 15

Multi-field Slots Place one or more fields into a given slot q Person deftemplate q – (deftemplate person “An example deftemplate” – (multislot name) – (slot age) – (slot eye-color) – (slot hair-color)) q Person fact – (person (name John Q. Public) – (age 23) – (eye-color blue) – (hair-color black)) 16

Ordered Facts q Ordered facts – Does not have a corresponding deftemplate – Slot name is not required q Example – (number-list 7 9 3 4 20) q Equivalent to – (deftemplate number-list (multislot values) – (number-list (values 7 9 3 4 20)) 17

Adding and Removing Facts are added and removed from the fact list q Assert command: add a fact q – (assert <fact>+) q 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> 18

Adding and Removing Facts q Facts command – (facts) – Displays the facts in the fact list q Example – – – – q 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 19

Adding and Removing Facts Duplicate facts are not allowed q Adding another facts q – – – – – 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> 20

Adding and Removing Facts q 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))) 21

Adding and Removing Facts q Viewing a portion of fact list – (facts [<start> [<end> [<maximum>]]]) – No more than <maximum> facts are displayed q Removing a fact – The deleted fact identifier will be missing – Called retraction – (retract <fact-index>+) • (retract 0) • (retract 1) q Removing non-existent fact – – q ; John Q. Public removed ; Jane Q. Public removed CLIPS> (retract 1) [PRNTUTIL 1] Unable to find fact f-1. CLIPS> Retract multiple facts – (retract 0 1) 22

Modifying and Duplicating Facts q Modify command – Slot values of deftemplate facts can be modified – (modify <fact-index> <slot-modifier>+) q <slot-modifier> – (<slot-name> <slot-value>) q Example: John Q. Public’s age is increased – – – – – CLIPS> (modify 0 (age 24)) <Fact-2> CLIPS> (facts) f-2 (person (name “John Q. Public”) (age 24) (eye-color blue) (hair-color black)) For a total of 1 fact. CLIPS> ; new index 23

Modifying and Duplicating Facts q Duplicate command – Same as modify except – does not retract the original fact q 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> 24

The Watch Command q – CLIPS> (watch facts) – CLIPS> (modify 3 (age 25)) – <== f-3 (person (name “Jack S. Public”) – (age 24) – (eye-color blue) – (hair-color black)) – ==> f-4 (person (name “Jack S. Public”) – (age 25) – (eye-color blue) – (hair-color black)) – <Fact-4> – CLIPS> Watch command – For debugging – (watch <watch-item>) q <Watch-item> – One of the symbols: facts, rules, activations, statistics, compilations, focus, all q 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. q Unwatch – (unwatch <watch-item) 25
![Deffacts Construct q Assert a set of facts deffacts deffacts name optional comments Deffacts Construct q Assert a set of facts – (deffacts <deffacts name> [<optional comments>]](https://slidetodoc.com/presentation_image_h2/5b976813527e9292c00a5e92726a5ff0/image-26.jpg)
Deffacts Construct q Assert a set of facts – (deffacts <deffacts name> [<optional comments>] <facts>*) q 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))) q Assert the deffatcs statement – (reset) 26

Deffacts Construct q 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> 27

The Components of a Rule q 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 q Define template for the emergency and response – (deftemplate emergency (slot type)) • type could be fire, flood, power outage – (deftemplate response (slot action)) q Define rule – (defrule fire-emergency “An example rule” – (emergency (type fire)) – => – (assert (response (action activate-sprinkler-system)))) q General format – (defrule <rule name> [<comment>] – <pattern>* ; LHS of the rule – => – <actions>*) ; RHS of the rule 28

The Agenda and Execution q Agenda – The collection of activated rules q Salience – The priority of a rule in integer q Run command – (run <limit>) • <limit>: maximum number of rules to be fired q Displaying the agenda – (agenda) q 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> 29

Rules and Refraction q Run command – – – CLIPS> (run) CLIPS> (facts) f-0 (initial-fact) f-1 (emergency (type fire)) f-2 (response (action activate-sprinklersystem)) – For a total of 3 facts. – CLIPS> q refraction – Rules are not fired more than once for a specific set of facts q (clear) – removes all constructs and all facts – Vs. (reset) asserts the deffacts statement 30

The Printout Command q Printout command – (printout <logical-name> <print-items>*) q Example – (defrule fire-emergency – (emergency (type fire)) – => – (printout t “Activate the sprinkler system” crlf)) – ; t: terminal, destination name q Using multiple rules – (defrule flood-emergency – (emergency (type flood)) – => – (printout t “Shut down electrical equipment” crlf)) 31

Using Multiple Patterns q 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)) 32

Loading and Saving Construct q Loading constructs from a file – (load <file-name>) – E. g. , (load “fire. clp”) q Watching compilation – – – q 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> 33

Saving Construct to a File q (save <file-name>) – E. g. , (save “fire. clp”) – Saves all the constructs q Saving partial constructs – Use an text editor 34

Variables q Variables – Question mark followed by symbol – e. g. , ? speed, ? sensor, ? value, ? noun, ? color q Bind – Assignment of a value to a variable 35

Variables q Example – – – – – CLIPS> (clear) CLIPS> (deftemplate person (slot name) (slot eyes) (slot hair)) CLIPS> (defrule find-blue-eyes) (person (name ? name) (eyes blue)) – => – (printout t ? name “ has blue 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> 36

Blocks World q q Blocks world initial configuration A D B E C F Goal: C on top of E 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)) 37

Blocks World q Floor and nothing are not blocks – – – q (block (block A) B) C) D) E) F) A D B E C 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 q Initial state – (deffatcs initial-state – (block A) – … – (on-top-of … – (goal (move C) (on-top-of E))) 38

Blocks World q Move-directly rule block 1 block 3 block 2 – (defrule move-directly – ? goal <- (goal (move ? block 1) – (on-top-of ? block 2)) – (block ? block 1) – (block ? block 2) – (on-top-of (upper nothing) (lower ? block 1)) – ? stack-1 <- (on-top-of (upper ? blocks 1) (lower ? block 3)) – ? stack-2 <- (on-top-of (upper nothing) (lower ? block 2)) – => – (retract ? goal ? stack-1 ? stack-2) – (assert (on-top-of (upper ? block 1) (lower ? block 2)) – (on-top-of (upper nothing) (lower ? block 3))) – (printout t ? block 1 “moved on top of “ ? block 2 “. ” crlf)) 39

Blocks World q Move-to-floor rule block 1 block 2 – (defrule move-to-floor – ? goal <- (goal (move ? block 1) – (on-top-of floor)) – (block ? block 1) – (on-top-of (upper nothing) (lower ? block 1)) – ? stack <- (on-top-of (upper ? block 1) (lower ? block 2)) – => – (retract ? goal ? stack) – (assert (on-top-of (upper ? block 1) (lower floor)) – (on-top-of (upper nothing) (lower ? block 2))) – (printout t ? block 1 “moved on top of floor. “ crlf)) 40

Blocks World q Clear-upper-block rule block 2 block 1 – (defrule clear-upper-block – (goal (move ? block 1)) ; move is a slot of goal – (block ? block 1) – (on-top-of (upper ? block 2) (lower ? block 1)) – (block ? block 2) – => – (assert (goal (move ? block 2) (on-top-of floor)))) q Clear-lower-block rule (actually clear the top of block) – (defrule clear-lower-block – (goal (on-top-of ? block 1)) ; on-top-of is a slot of goal – (block ? block 1) – (on-top-of (upper ? block 2) (lower ? block 1)) – (block ? block 2) – => – (assert (goal (move ? block 2) (on-top-of floor)))) 41

Blocks World q 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> A D B E C F 42

Functions and Expressions q 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 43

Functions and Expressions q Embed the expression within other expressions – – – – q 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> 44

I/O Functions q 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> 45
![IO Functions q Open function open filename fileID fileaccess E I/O Functions q Open function – (open <file-name> <file-ID> [, file-access. ]) – E.](https://slidetodoc.com/presentation_image_h2/5b976813527e9292c00a5e92726a5ff0/image-46.jpg)
I/O Functions q Open function – (open <file-name> <file-ID> [, file-access. ]) – E. g. , (open “input. dat” data “r”) • Can use “w”, “r+”, “a” also q Close function – (close [<file-ID>]) – E. g. , (close data) q Writing to a file – – – – CLIPS> TRUE CLIPS> (open “example. dat” example “w”) (printout example “green” crlf) (printout example 7 crlf) (close example) 46

I/O Functions q Reading from a file – – – CLIPS> TRUE CLIPS> green CLIPS> 7 CLIPS> EOF CLIPS> TRUE CLIPS> (open “example. dat” example “r”) (read example) (close example) 47

I/O Functions q 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> 48

I/O Functions q 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> 49

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