650 likes | 1.08k Views
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 . CLIPS History.
E N D
Intro to CLIPSPaula MatuszekCSC 8750, Fall, 2004 Based on presentation from COMP474/6741 – Expert Systems Update: January 20, 2002Concordia University A. Andreevskaia and S. Mokhiv www.cs.concordia.ca/~comp474/tutorial/clips-intro.ppt Expert Systems, Paula Matuszek
CLIPS History • CLIPS = CLanguage Integrated Production System • Developed at NASA’s 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) • Initial version: a production rule interpreter. • Now: COOL = CLIPS Object-Oriented Language • Also JESS: Java Expert Systems Shell Expert Systems, Paula Matuszek
Where does it fit? • Classical Rule-Based Expert Systems • CLIPS as a Rule-Based Language • CLIPS’ Components • Forward Chaining (CLIPS) vs. Backward Chaining (e.g. MYCIN and derivatives) Expert Systems, Paula Matuszek
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-domain well-documented software • Fast • Easy to embed in other applications • Easy to get started Expert Systems, Paula Matuszek
Components of a Rule-Based Expert System Knowledge Base (RULES) Inference Engine Working Memory (FACTS) Agenda Explanation Facility Knowledge Acquisition Facility UI Expert Systems, Paula Matuszek
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 Systems, Paula Matuszek
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 echo them. To exit CLIPS interpreter type (exit). Expert Systems, Paula Matuszek
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 facts; 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
More Basic CLIPS Commands • (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 display a set of rules currently in the rule base. Expert Systems, Paula Matuszek
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 of the CLIPS constructs (e.g. deftemplate, defrule, etc) and follows the construct’s name and enclosed in quotations.(defrule my-rule “my comment” (initial-fact) => (printout t “Hello” crlf)) Expert Systems, Paula Matuszek
Few Simple Clips Commands • To assert a fact: (assert (first-fact asserted)) • To define a rule: (defrule first_rule (first-fact asserted) => (assert (second-fact asserted))) Expert Systems, Paula Matuszek
“Hello World” in CLIPS (defrule start (initial-fact) => (printout t “Hello, world!” crlf)) Expert Systems, Paula Matuszek
To Make It Run • Type the code 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 +j TRUE Expert Systems, Paula Matuszek
To Make It Run • Type (reset) • Type (run) Tip: You can also use the menu • To exit CLIPS use the menu, ^Q or (exit) Expert Systems, Paula Matuszek
Fields • There are seven data types (types of tokens) called fields in CLIPS. • float: [+\-] <digit>* [.<digit>*] [e|E[+|-]<digit>*] • integer: [+|-] <digits> * • symbol: [<] <char>* • string: “<char>* “ (e.g. “John”, “848-3000”) • external address • instance name • instance address Expert Systems, Paula Matuszek
Word • a word CANNOT start with these:< | & $ ? + - ( ) ; • a word CANNOT contain any of these:< | & ( ) ; Expert Systems, Paula Matuszek
Valid Expressions • Examples of valid words • fire • emergency-fire • activate_sprinkler_system • shut-down-electrical-junction-387 • !?#$^* • CLIPS is case-sensitive • So fireFIREFire are all different Expert Systems, Paula Matuszek
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 strings “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
Facts • Fact is a chunk of information consisting of relation name, (optional) slots and slot values. Example:(person (name “John”)) • In CLIPS facts are frame structures often defined using the (deftemplate) construct. Expert Systems, Paula Matuszek
(deftemplate) (deftemplate <relation_name> [<comment>] <slot-definition>* ) <slot-definition> is: • (slot <slot-name>) • (field <slot-name>) • (multislot <slot-name>) Expert Systems, Paula Matuszek
Valid Facts • Examples of valid facts (single-field) (two fields) (speed 38 mph) (cost 78 dollars 23 cents) (name “John Doe”) Expert Systems, Paula Matuszek
(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
Facts • Deftemplate can also be implicit = ordered facts: (numbers 1 2 3) • To add facts: (assert <fact>) (you can add more than one fact with the same (assert) command) • To remove facts: (retract <fact-index>*) • To list facts: (facts) will give facts’ identifiers like f-0 (not sequential) Expert Systems, Paula Matuszek
Facts Example CLIPS> (deftemplate course “electives” (slot number)) CLIPS> (assert (course (number comp674)) (course (number comp672))) <Fact-1> 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 1 fact Expert Systems, Paula Matuszek
Modifying Facts • To modify a fact: (modify <fact-index> <slot-modifier>*) <slot-modifier> is (<slot-name> <slot-value>) Example: CLIPS> (modify 0(number comp675)) <fact-2> CLIPS> (facts) f-2 (course (number comp675)) for a total of 1 fact Expert Systems, Paula Matuszek
Duplicating Facts • To create a duplicate of a fact: Example (continued) CLIPS> (duplicate 2 (number comp775)) <fact-3> 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
Asserting 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 <deffacts-name> [<comment>] <facts>*) (reset) Expert Systems, Paula Matuszek
Retracting Facts • Facts can be removed or retracted using (retract <fact-index>) (retract 2) • Retract can be used for more than one fact (retract 1 2) Expert Systems, Paula Matuszek
(deftemplate) as a Record • Templates can be extended to hold more information like records:(deftemplate <deftemplate-name> [<optional comment>](slot <slot-name> (type <data-type>) [(default <value>)]) • Example: (deftemplate student “a student record”(slot name (type STRING))(slot age (type NUMBER) (default 18))) Expert Systems, Paula Matuszek
(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))(student (name Jane) (age 19)) Expert Systems, Paula Matuszek
(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 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
Rules (1) • LHS => RHS • Syntax: (defrule <rule-name> [<comment>] [<declaration>] ; salience <patterns>* ;LHS, premises, patterns, ;conditions, antecedent => <actions>*) ;RHS, actions, consequent Expert Systems, Paula Matuszek
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)=> (printout t “Use carbon dioxide extinguisher” crlf)) Expert Systems, Paula Matuszek
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 is empty the program stops. • Refraction: each rule is fired only once for a specific set of facts => use (refresh) Expert Systems, Paula Matuszek
Salience • Normally the agenda acts like a stack. • The most recent activation placed on the agenda is the first rule to fire. • Salience allows more 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
Conflict Resolution Strategies • Recency • Rules which use more recent data are preferred. • CLIPS 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 • Used in CLIPS (need (refresh) ) Expert Systems, Paula Matuszek
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: • 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 <strategy>) Expert Systems, Paula Matuszek
Variables • Variable name is made of ? and one or more characters: • Example: (course (number ?cmp)) • Variables are used for • Pattern matching • I/O • As pointers to facts (fact indices) Expert Systems, Paula Matuszek
Variables Examples • (defrule grandfather (is-a-grandfather ?name)=> (assert (is-a-man ?name))) • (defrule grandfather (is-a-grandfather ?name) => (assert (is-a-father ?name)) (assert (is-a-man ?name)) (printout t ?name “ is a grandfather” crlf)) Expert Systems, Paula Matuszek
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 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
Wildcards (1) To specify a general pattern we can use: • 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
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
Retracting Facts Using Wildcards (defrule change-grandfather-fact ?old-fact <- (is-a-grandfather ?name) => (retract ?old-fact) (assert (has-a-grandchild ?name) (is-a-man ?name)) ) Expert Systems, Paula Matuszek
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
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
Standard I/O Examples (1) • Keyboard input example: (defrule to-start “Rule to start & enter a name” (phase choose-name) => (printout t “Enter your name” crlf) (assert (your-name =(read)))) ; ’=’ is optional • Another example using a variable:(defrule to-start => (printout t “Enter something: ”) (bind ?something (read)) (printout t “You have entered ” ?something crlf)) Expert Systems, Paula Matuszek
Standard I/O Examples (2) • A slightly more advanced example:(defrule continue-check ?phase <- (phase check-continue)=> (retract ?phase) (printout t “Do you want to continue?” crlf) (bind ?answer (read))(if(or(eq ?answeryes)(eq ?answer y))then (assert (phase continue))else (halt))) Expert Systems, Paula Matuszek
File I/O • File I/O • (load <filename>) • (save <filename>) • NOTE: use \\ in paths if you trying to do it on Windows; or / will always work. Expert Systems, Paula Matuszek
To Display Constructs • To display constructs: (list-defrules) (list-deftemplates) (list-deffacts) • To display the text of definitions of the constructs: (ppdefrule <defrule-name>) (ppdeftemplate <deftemplate-name>) (ppdeffeacts <deffacts-name>) Expert Systems, Paula Matuszek
To Delete Constructs • To ”undefine“ a given construct: (undefrule <defrule-name>) (undeftemplate <deftemplate-name>) (undeffacts <deffacts-name>) Expert Systems, Paula Matuszek