1、10/9/2018,Expert Systems, Paula Matuszek,1,Intro to CLIPS Paula Matuszek CSC 8750, Fall, 2004,Based on presentation from COMP474/6741 Expert Systems Update: January 20, 2002 Concordia University A. Andreevskaia and S. Mokhiv www.cs.concordia.ca/comp474/tutorial/clips-intro.ppt,Expert Systems, Paula
2、Matuszek,2,10/9/2018,CLIPS History,CLIPS = C Language Integrated Production System Developed at NASAs Johnson Space Center in the mid-1980-s C was used as implementation language (because of restricted availability of LISP compilers and problems of integration LISP-code with non-LISP applications) I
3、nitial version: a production rule interpreter. Now: COOL = CLIPS Object-Oriented Language Also JESS: Java Expert Systems Shell,Expert Systems, Paula Matuszek,3,10/9/2018,Where does it fit?,Classical Rule-Based Expert Systems CLIPS as a Rule-Based Language CLIPS Components Forward Chaining (CLIPS) vs
4、. Backward Chaining (e.g. MYCIN and derivatives),Expert Systems, Paula Matuszek,4,10/9/2018,Why do we study CLIPS?,Advantages of CLIPS include: A top-level interpreter Production rule interpreter Runs on many platforms (portability) like different flavors of UNIX, Linux, Windows, MacOS A public-doma
5、in well-documented software Fast Easy to embed in other applications Easy to get started,Expert Systems, Paula Matuszek,5,10/9/2018,Components of a Rule-Based Expert System,Knowledge Base (RULES),Inference Engine,Agenda,Working Memory (FACTS),UI,Explanation Facility,Knowledge Acquisition Facility,Ex
6、pert Systems, Paula Matuszek,6,10/9/2018,Versions of CLIPS,CLIPS is written in C = CLIPS is portable = there are different versions that run on different platforms: mac, unix, windows. From CD you can get version for Windows Latest version 6.20 can be downloaded from the CLIPS web site.,Expert Syste
7、ms, Paula Matuszek,7,10/9/2018,Starting CLIPS,1. To start text-based CLIPS interpreter under UNIX you type clips and CLIPS prompt will appear. You can try simple CLIPS commands. Remember - like LISP CLIPS requires all statements to be in ( ); otherwise, it will read them as simple string and just ec
8、ho them. To exit CLIPS interpreter type (exit).,Expert Systems, Paula Matuszek,8,10/9/2018,Summary of Basic CLIPS Commands,(exit) to exit from CLIPS (clear) to clear the environment from facts, rules, and other active definitions (reset) to set the fact base to its initial state (clears existing fac
9、ts; sets (initial-fact), and all (deffacts) constructs in the program). Perform (reset) before each program run! (run) executes a program currently loaded into the CLIPS interpreter against currently defined rule- and fact-bases.,Expert Systems, Paula Matuszek,9,10/9/2018,More Basic CLIPS Commands,(
10、load “filename.clp”) to load a CLIPS program into the interpreter from the file named filename.clp . This also does syntax check and makes constructs in the file defined. In some cases you may omit quotes in the name. (facts) to display a list of currently active facts in the fact base. (rules) to d
11、isplay a set of rules currently in the rule base.,Expert Systems, Paula Matuszek,10,10/9/2018,Comments in CLIPS,Program comments begin with a semicolon “;”. Everything after on the same line until the end of line is ignored. ; This is an inline comment example Construct comments are used as a part o
12、f the CLIPS constructs (e.g. deftemplate, defrule, etc) and follows the constructs name and enclosed in quotations. (defrule my-rule “my comment” (initial-fact) = (printout t “Hello” crlf) ),Expert Systems, Paula Matuszek,11,10/9/2018,Few Simple Clips Commands,To assert a fact:(assert (first-fact as
13、serted) To define a rule:(defrule first_rule (first-fact asserted)=(assert (second-fact asserted),Expert Systems, Paula Matuszek,12,10/9/2018,“Hello World” in CLIPS,(defrule start(initial-fact) =(printout t “Hello, world!” crlf),Expert Systems, Paula Matuszek,13,10/9/2018,To Make It Run,Type the cod
14、e in a file, save it (e.g. hello-world.clp) Start CLIPS (type clips or xclips) Do: File - Load (in XCLIPS) or type (load hello-world.clp) When the file is loaded CLIPS will display:(load hello-world.clp)defining defrule start +jTRUE,Expert Systems, Paula Matuszek,14,10/9/2018,To Make It Run,Type (re
15、set) Type (run)Tip: You can also use the menu To exit CLIPS use the menu, Q or (exit),Expert Systems, Paula Matuszek,15,10/9/2018,Fields,There are seven data types (types of tokens) called fields in CLIPS. float: +- * .* e|E+|-* integer: +|- * symbol: * string: “* “ (e.g. “John”, “848-3000”) externa
16、l address instance name instance address,Expert Systems, Paula Matuszek,16,10/9/2018,Word,a word CANNOT start with these: | ,Expert Systems, Paula Matuszek,17,10/9/2018,Valid Expressions,Examples of valid words fire emergency-fire activate_sprinkler_systemshut-down-electrical-junction-387!?#$* CLIPS
17、 is case-sensitive So fire FIRE Fire are all different,Expert Systems, Paula Matuszek,18,10/9/2018,Valid Expressions,Examples of valid strings “Activate the sprinkler system.” “Shut down electrical junction 387.” “!?#$” “-;( ) +-” Spaces act as delimiters to separate fields These are different strin
18、gs “fire” “fire “ “ fire” “ fire “ but would be the same with no quotes Valid numbers 1 1.5 .7 +3 -1 65 3.5e10,Expert Systems, Paula Matuszek,19,10/9/2018,Facts,Fact is a chunk of information consisting of relation name, (optional) slots and slot values. Example: (person (name “John”) In CLIPS facts
19、 are frame structures often defined using the (deftemplate) construct.,Expert Systems, Paula Matuszek,20,10/9/2018,(deftemplate),(deftemplate * )is:(slot ) (field ) (multislot ),Expert Systems, Paula Matuszek,21,10/9/2018,Valid Facts,Examples of valid facts(single-field)(two fields)(speed 38 mph)(co
20、st 78 dollars 23 cents)(name “John Doe”),Expert Systems, Paula Matuszek,22,10/9/2018,(deftemplate) Example,(deftemplate person “an example template”(multislot name)(slot age)(slot eye-color)(slot hair-color) CLIPS defining deftemplate: person TRUE,Expert Systems, Paula Matuszek,23,10/9/2018,Facts,De
21、ftemplate can also be implicit = ordered facts: (numbers 1 2 3) To add facts: (assert ) (you can add more than one fact with the same (assert) command) To remove facts: (retract *) To list facts: (facts) will give facts identifiers like f-0 (not sequential),Expert Systems, Paula Matuszek,24,10/9/201
22、8,Facts Example,CLIPS (deftemplate course “electives”(slot number) CLIPS (assert (course (number comp674)(course (number comp672)CLIPS (facts) f-0 (course (number comp674) f-1 (course (number comp672) For a total of 2 facts CLIPS (retract 1) CLIPS (facts) f-0 (course (number comp674) For a total of
23、1 fact,Expert Systems, Paula Matuszek,25,10/9/2018,Modifying Facts,To modify a fact: (modify *)is ( ) Example: CLIPS (modify 0 (number comp675)CLIPS (facts) f-2 (course (number comp675) for a total of 1 fact,Expert Systems, Paula Matuszek,26,10/9/2018,Duplicating Facts,To create a duplicate of a fac
24、t: Example (continued) CLIPS (duplicate 2 (number comp775)CLIPS (facts) f-2 (course (number comp675) f-3 (course (number comp775) For a total of 2 facts Note: (duplicate) modifies a fact without deleting (retracting) the original, whereas (modify) does.,Expert Systems, Paula Matuszek,27,10/9/2018,As
25、serting a Group of Facts,To define groups of facts that represent the original (initial) knowledge: use (deffacts). Facts from (deffacts) are asserted using (reset) (or on (load):(deffacts *)(reset),Expert Systems, Paula Matuszek,28,10/9/2018,Retracting Facts,Facts can be removed or retracted using
26、(retract ) (retract 2) Retract can be used for more than one fact (retract 1 2),Expert Systems, Paula Matuszek,29,10/9/2018,(deftemplate) as a Record,Templates can be extended to hold more information like records: (deftemplate (slot (type ) (default ) Example: (deftemplate student “a student record
27、” (slot name (type STRING) (slot age (type NUMBER) (default 18),Expert Systems, Paula Matuszek,30,10/9/2018,(deftemplate) Example,After the template declaration and adding: (deffacts student-Ids (student (name Tarzan) (student (name Jane) (age 19) The result is:(student (name Tarzan) (age 18) (stude
28、nt (name Jane) (age 19),Expert Systems, Paula Matuszek,31,10/9/2018,(deftemplate): Summary,Look at the templates as to user-defined types of facts. In a template you can have several slots (or fields), on which you can operate separately or all at the same time. Think of it as a sort of object. This
29、 allows you to group multiple pieces of information of a given fact in one structure, which is described by the defftemplate construct, and the facts are instances of it.,Expert Systems, Paula Matuszek,32,10/9/2018,Rules (1),LHS = RHS Syntax: (defrule ; salience* ;LHS, premises, patterns, ;condition
30、s, antecedent =*) ;RHS, actions, consequent,Expert Systems, Paula Matuszek,33,10/9/2018,Rules (2),Example: (defrule class-A-fire-emergency (emergency fire) = (printout t “FIRE!” crlf) Rules can have more than one pattern/premise: (defrule class-B-fire-emergency (emergency fire) (fire-class B) = (pri
31、ntout t “Use carbon dioxide extinguisher” crlf),Expert Systems, Paula Matuszek,34,10/9/2018,Agenda,If the pattern(s) in the LHS of the rule match asserted facts, the rule is activated and put on the agenda. Rules are ordered on the agenda according to their salience (read: priority). When the agenda
32、 is empty the program stops. Refraction: each rule is fired only once for a specific set of facts = use (refresh),Expert Systems, Paula Matuszek,35,10/9/2018,Salience,Normally the agenda acts like a stack. The most recent activation placed on the agenda is the first rule to fire. Salience allows mor
33、e important rules to stay at the top of the agenda regardless of when they were added. If you do not explicitly say, CLIPS will assume the rule has a salience of 0.,Expert Systems, Paula Matuszek,36,10/9/2018,Conflict Resolution Strategies,Recency Rules which use more recent data are preferred. CLIP
34、S time-tags WM elements Specificity Rules with more conditions are preferred to more general rules that are easier to satisfy. Good if dealing with general rules with specific rules for exceptions Refractoriness A rule should not be allowed to fire more than once for the same data. Prevents loops Us
35、ed in CLIPS (need (refresh) ),Expert Systems, Paula Matuszek,37,10/9/2018,Conflict Resolution in CLIPS,First, CLIPS uses salience to sort the rules. Then it uses the other strategies to sort rules with equal salience. CLIPS uses refraction, recency & specificity in the form of following 7 strategies
36、: The depth strategy The breadth strategy The simplicity strategy The complexity strategy The LEX strategy The MEA strategy It is possible also to set strategy to random Syntax: (set-strategy ),Expert Systems, Paula Matuszek,38,10/9/2018,Variables,Variable name is made of ? and one or more character
37、s: Example:(course (number ?cmp) Variables are used for Pattern matching I/O As pointers to facts (fact indices),Expert Systems, Paula Matuszek,39,10/9/2018,Variables Examples,(defrule grandfather (is-a-grandfather ?name) = (assert (is-a-man ?name) ) (defrule grandfather (is-a-grandfather ?name) = (
38、assert (is-a-father ?name) (assert (is-a-man ?name) (printout t ?name “ is a grandfather” crlf) ),Expert Systems, Paula Matuszek,40,10/9/2018,Fact Address,To remove a fact from the fact-list use (retract) Before a fact can be retracted it must be specified to CLIPS by its index. Rules can be used to
39、 modify the fact base. To achieve it variables have to be bound to fact addresses using -: ?num - (course (number ?cmp) This appears in the LHS of the rule, and can be referred to in either LHS and RHS.,Expert Systems, Paula Matuszek,41,10/9/2018,Wildcards (1),To specify a general pattern we can use
40、: Single field variables: wildcard ? Multifield variables: wildcard $? (courses (numbers $?course_nums) (printout t “Your courses are” $?course_nums crlf) (list ? $? c ?) can match these: (list a c e), (list a d c b) but not these: (list c), (list c d), (list a c d b),Expert Systems, Paula Matuszek,
41、42,10/9/2018,Wildcards (2),The fact (do carwash on Sunday) will match any of the following (do ? ? Sunday) (do ? on ?) (do ? on ?when) (do $? ) (do $? Sunday) (do ?chore $?when),Expert Systems, Paula Matuszek,43,10/9/2018,Retracting Facts Using Wildcards,(defrule change-grandfather-fact?old-fact (re
42、tract ?old-fact)(assert (has-a-grandchild ?name)(is-a-man ?name) ),Expert Systems, Paula Matuszek,44,10/9/2018,Retracting Facts Using Wildcards (2),You can retract several facts: (retract ?fact1 ?fact2 ?fact3) Or you can retract all of them at once: (retract *),Expert Systems, Paula Matuszek,45,10/9
43、/2018,Standard I/O,To print to STDOUT: (printout t ) For the new line use: crlf To read from STDIN into a field use:(read),Expert Systems, Paula Matuszek,46,10/9/2018,Standard I/O Examples (1),Keyboard input example: (defrule to-start “Rule to start = is optional Another example using a variable: (d
44、efrule to-start =(printout t “Enter something: ”)(bind ?something (read)(printout t “You have entered ” ?something crlf),Expert Systems, Paula Matuszek,47,10/9/2018,Standard I/O Examples (2),A slightly more advanced example: (defrule continue-check ?phase (retract ?phase) (printout t “Do you want to
45、 continue?” crlf) (bind ?answer (read) (if (or (eq ?answer yes) (eq ?answer y) then (assert (phase continue) else (halt) ),Expert Systems, Paula Matuszek,48,10/9/2018,File I/O,File I/O (load ) (save ) NOTE: use in paths if you trying to do it on Windows; or / will always work.,Expert Systems, Paula
46、Matuszek,49,10/9/2018,To Display Constructs,To display constructs:(list-defrules)(list-deftemplates)(list-deffacts) To display the text of definitions of the constructs:(ppdefrule )(ppdeftemplate )(ppdeffeacts ),Expert Systems, Paula Matuszek,50,10/9/2018,To Delete Constructs,To ”undefine“ a given c
47、onstruct: (undefrule )(undeftemplate )(undeffacts ),Expert Systems, Paula Matuszek,51,10/9/2018,Field Constraints,NOT (number comp672) OR | (number comp672|comp674) AND & (number ?course_n & comp674|comp675) Variable ?course_n will be bound to both (number ?course_n & comp674 & comp672) Variable ?co
48、urse_n will be bound to none of the two,Expert Systems, Paula Matuszek,52,10/9/2018,Field Constraints Examples,NOT(defrule person-without-brown-hair (person ?name ? brown) = (printout t ?name “ does not have brown hair” crlf) OR(defrule black-or-brown-hair (person ?name ? brown|black) = (printout t
49、?name “ has dark hair” crlf) AND(defrule black-or-brown-hair (person ?name ? ?colour&brown|black) = (printout t ?name “ has” ?colour “ hair” crlf),Expert Systems, Paula Matuszek,53,10/9/2018,Math,CLIPS maths expressions are written in the prefix format, just like in LISP or Scheme: (+ 2 3) evaluates to 5 Operators are: + addition, - subtraction, * multiplication, / division, * exponentiation(+ 2 (* 3 4) evaluates to 14(* (+ 2 3) 4) evaluates to 20(evaluation is from the inside out),