420 likes | 537 Views
More Intro to CLIPS Paula Matuszek CSC 9010, Spring, 2011. 01/12/2011. Knowledge-Based Systems, Paula Matuszek. CLIPS Facts. Facts are what CLIPS believes to be true. The simplest form of a fact is a single string. (snowing) (“January 11”) An ordered fact is a list of one or more strings:
E N D
More Intro to CLIPSPaula MatuszekCSC 9010, Spring, 2011 01/12/2011 Knowledge-Based Systems, Paula Matuszek
CLIPS Facts • Facts are what CLIPS believes to be true. • The simplest form of a fact is a single string. • (snowing) • (“January 11”) • An ordered fact is a list of one or more strings: • (snowing “January 11”) • Our lab exercise last week showed that we really want some additional organization in our facts 01/12/2011 Knowledge-Based Systems, Paula Matuszek
Frames • The underlying representation of a fact in CLIPS is a frame with slots: • Relation name • Slot name Slot value • Slot name Slot value • Example: • (class • (number 8520) • (day Monday • (prerequisite 8310) • ) 01/12/2011 Knowledge-Based Systems, Paula Matuszek
(deftemplate) • The frame structures is defined using the (deftemplate) construct. • (deftemplate <relation_name> [<comment>] • <slot-definition>* ) • <slot-definition>is: • (slot <slot-name>) • (field <slot-name>) • (multislot <slot-name>) (allows more than one value) 01/12/2011 Knowledge-Based Systems, Paula Matuszek
(deftemplate) Example • (deftemplate class “an example template” • (slot number) • (multislot prerequisite) • (multislot day) • (slot time) • ) 01/12/2011 Knowledge-Based Systems, Paula Matuszek
Matching Fact for Deftemplate • (class “an example fact” • (number CSC8520 number) • (prerequisite CSC8301) • (day Monday Wednesday) • (time “2:00”) • ) 01/12/2011 Knowledge-Based Systems, Paula Matuszek
Another Example • CLIPS> (deftemplate class “electives” • (slot number)) • CLIPS> (assert (class (number csc8520)) • (class (number csc8301))) • <Fact-1> • CLIPS> (facts) • f-0 (class (number csc8520)) • f-1 (class (number csc8301)) • For a total of 2 facts • CLIPS> (retract 1) • CLIPS> (facts) • f-0 (class (number csc8520)) • For a total of 1 fact 01/12/2011 Knowledge-Based Systems, Paula Matuszek
Comments in CLIPS (a brief aside) • 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)) 01/12/2011 Knowledge-Based 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) • These correspond to our static information. • Must have a matching (deftemplate) declared first in the file or buffer. 01/12/2011 Knowledge-Based Systems, Paula Matuszek
Deffacts Example • If we have the deftemplate • (deftemplate class “an example template” • (slot number) • (multislot prerequisite) • And the deffacts • (deffacts classes(class (number CSC8520) (prereq 8301))(class (number CSC8301)) • The result of (reset) is: • (class (number CSC8520) (prereq 8301))(class (number CSC8301)) 01/12/2011 Knowledge-Based 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. • This makes it easy to set up all the static information at the beginning of a run. 01/12/2011 Knowledge-Based 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) 01/12/2011 Knowledge-Based 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)) 01/12/2011 Knowledge-Based Systems, Paula Matuszek
Giving a Value to a Variable • Variables in CLIPS are bound or instantiated. • They get a value when they are part of a fact matched on the LHS; their scope is within that rule. • You normally do not directly assign a value to a variable, and once it has a value you normally do not change it within that rule. • If you must do so, there is a bind command, but if you’re using it often you’re probably thinking procedurally, a case of “You can write a C program in any language”. • defglobal can be used to create a global variable with (reset) or (load). 01/12/2011 Knowledge-Based 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 <- (class (number ?cmp)) • This appears in the LHS of the rule, and can be referred to in either LHS and RHS. 01/12/2011 Knowledge-Based 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) 01/12/2011 Knowledge-Based 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) 01/12/2011 Knowledge-Based 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)) • ) 01/12/2011 Knowledge-Based 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 *) 01/12/2011 Knowledge-Based 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) 01/12/2011 Knowledge-Based 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)) 01/12/2011 Knowledge-Based 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))) 01/12/2011 Knowledge-Based 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. 01/12/2011 Knowledge-Based 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>) 01/12/2011 Knowledge-Based Systems, Paula Matuszek
To Delete Constructs • To ”undefine“ a given construct: • (undefrule <defrule-name>) • (undeftemplate <deftemplate-name>) • (undeffacts <deffacts-name>) 01/12/2011 Knowledge-Based Systems, Paula Matuszek
Field Constraints • These apply to the value of a field • 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 ?course_n will be bound to none of the two 01/12/2011 Knowledge-Based Systems, Paula Matuszek
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 ?name “ has dark hair” crlf)) • AND • (defrule black-or-brown-hair(person ?name ? ?colour&brown|black)=>(printout t ?name “ has” ?colour “ hair” crlf)) 01/12/2011 Knowledge-Based Systems, Paula Matuszek
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) 01/12/2011 Knowledge-Based Systems, Paula Matuszek
Math Example • With: • (defrule addition • (numbers ?x ?y) => • (assert (sumis (+ ?x ?y)))) • CLIPS> (assert(numbers 1 2)) • ==> f-0 (numbers 1 2) • <Fact-0> • CLIPS> Loading Selection... • Defining defrule: addition +j • ==> Activation 0 addition: f-0 • CLIPS> (run) • FIRE 1 addition: f-0 • ==> f-1 (sumis 3) 01/12/2011 Knowledge-Based Systems, Paula Matuszek
Pattern Logical OR (1) • These apply to the truth of an entire pattern • (defrule shut-off-electricity-1(emergency flood)=>(printout t “Shut off the electricity” crlf)) • (defrule shut-off-electricity-2(fire-class C) => • (printout t “Shut off the electricity” crlf)) • (defrule shut-off-electricity-3(sprinkler-systems active) => (printout t “Shut off the electricity” crlf)) 01/12/2011 Knowledge-Based Systems, Paula Matuszek
Pattern Logical OR (2) • The OR pattern will match if any of its component patterns is true. • The three previous rules can be replaced by one rule if OR is used • (defrule shut-off-electricity • (or(emergency flood) • (fire-class C) • (sprinkler-systems active))=>(printout t “Shut off the electricity” crlf)) 01/12/2011 Knowledge-Based Systems, Paula Matuszek
Pattern Logical AND • The default situation • It requires that all the patterns of the LHS of the rule to be matched to facts in order to activate the rule. 01/12/2011 Knowledge-Based Systems, Paula Matuszek
Pattern Logical NOT • The logical NOT can only be used to negate a single pattern:(defrule no-emergency(report-status)(not (emergency ?))=>(printout t “No emergency being handled” crlf)) 01/12/2011 Knowledge-Based Systems, Paula Matuszek
Test Control Pattern • Control pattern can be used in the LHS to test a condition, which happens not to be fact in the fact base, but rather something else. • General syntax: • (test <predicate-function>) • Example: • (test (> ?size 1)) 01/12/2011 Knowledge-Based Systems, Paula Matuszek
Predicate Functions • The predicate functions are used to return a value of either true or false - and, not, or • eq equal, neq not equal • (eq <any-value> <any-value>) • = equal, != not equal, >= greater than or equal, • > greater than, <= less than or equal, < less than These are used for numeric values. • (<= <numeric-value><numeric-value>) • These are used to testthe type of a field: numberp, stringp, wordp, integerp, evenp, oddp 01/12/2011 Knowledge-Based Systems, Paula Matuszek
Debugging • “Watch them!” • (watch rules) • (watch facts) • Make save output to the file: • To start logging: (dribble-on “output.log”) • To stop: (dribble-off) 01/12/2011 Knowledge-Based 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) 01/12/2011 Knowledge-Based 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. 01/12/2011 Knowledge-Based 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) ) 01/12/2011 Knowledge-Based Systems, Paula Matuszek
Conflict Resolution in CLIPS • Salience first. • Other strategies to sort rules with equal salience. • CLIPS uses refraction, recency & specificity: • 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>) 01/12/2011 Knowledge-Based Systems, Paula Matuszek
Where to find more • This has been a BRIEF overview • User’s Guide is a readable intro, worth skimming. Lots of examples and discussion of style. • Basic Programming Guide Vol 1 is an excellent reference manual • The remaining documentation is mostly more detailed than we need. • There’s a decent set of introductory tutorials at http://iweb.tntech.edu/bhuguenard/ds6530/ClipsTutorial/tableOfContents.htm 01/12/2011 Knowledge-Based Systems, Paula Matuszek
Infinite Loop orHow To Shoot Oneself in the Foot • You can get into an infinite loop if you are not careful enough. • (defrule simple-loop?old-fact <- (loop-fact) =>(printout t “Looping!” crlf)(retract ?oldfact)(assert (loop-fact))) • Use Control-C (or another interrupt command) to get break out of the loop. 01/12/2011 Knowledge-Based Systems, Paula Matuszek