Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 01/12/2011Knowledge-Based Systems, Paula Matuszek More Intro to CLIPS Paula Matuszek CSC 9010, Spring, 2011.

Similar presentations


Presentation on theme: "1 01/12/2011Knowledge-Based Systems, Paula Matuszek More Intro to CLIPS Paula Matuszek CSC 9010, Spring, 2011."— Presentation transcript:

1 1 01/12/2011Knowledge-Based Systems, Paula Matuszek More Intro to CLIPS Paula Matuszek CSC 9010, Spring, 2011

2 2 Knowledge-Based Systems, Paula Matuszek 01/12/2011 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

3 3 Knowledge-Based Systems, Paula Matuszek 01/12/2011 Frames  The underlying representation of a fact in CLIPS is a frame with slots: Relation name  Slot name Slot value  Example: (class (number 8520) (day Monday (prerequisite 8310) )

4 4 Knowledge-Based Systems, Paula Matuszek 01/12/2011 (deftemplate)  The frame structures is defined using the (deftemplate) construct.  ( deftemplate [ ]  * )  is:  (slot )  (field )  (multislot ) (allows more than one value)

5 5 Knowledge-Based Systems, Paula Matuszek 01/12/2011 (deftemplate) Example  (deftemplate class “an example template”  (slot number)  (multislot prerequisite)  (multislot day)  (slot time)  ) 

6 6 Knowledge-Based Systems, Paula Matuszek 01/12/2011 Matching Fact for Deftemplate  (class “an example fact”  (number CSC8520 number)  (prerequisite CSC8301)  (day Monday Wednesday)  (time “2:00”)  ) 

7 7 Knowledge-Based Systems, Paula Matuszek 01/12/2011 Another Example  CLIPS> (deftemplate class “electives”  (slot number))  CLIPS> (assert (class (number csc8520))  (class (number csc8301)))   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

8 8 Knowledge-Based Systems, Paula Matuszek 01/12/2011 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) )

9 9 Knowledge-Based Systems, Paula Matuszek 01/12/2011 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 [ ] * )  (reset)  These correspond to our static information.  Must have a matching (deftemplate) declared first in the file or buffer.

10 10 Knowledge-Based Systems, Paula Matuszek 01/12/2011 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))

11 11 Knowledge-Based Systems, Paula Matuszek 01/12/2011 (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.

12 12 Knowledge-Based Systems, Paula Matuszek 01/12/2011 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)

13 13 Knowledge-Based Systems, Paula Matuszek 01/12/2011 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) )

14 14 Knowledge-Based Systems, Paula Matuszek 01/12/2011 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).

15 15 Knowledge-Based Systems, Paula Matuszek 01/12/2011 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.

16 16 Knowledge-Based Systems, Paula Matuszek 01/12/2011 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)

17 17 Knowledge-Based Systems, Paula Matuszek 01/12/2011 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 )

18 18 Knowledge-Based Systems, Paula Matuszek 01/12/2011 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)) )

19 19 Knowledge-Based Systems, Paula Matuszek 01/12/2011 Retracting Facts Using Wildcards (2)  You can retract several facts: (retract ?fact1 ?fact2 ?fact3)  Or you can retract all of them at once: (retract *)

20 20 Knowledge-Based Systems, Paula Matuszek 01/12/2011 Standard I/O  To print to STDOUT: (printout t …)  For the new line use: crlf  To read from STDIN into a field use: (read)

21 21 Knowledge-Based Systems, Paula Matuszek 01/12/2011 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))

22 22 Knowledge-Based Systems, Paula Matuszek 01/12/2011 Standard I/O Examples (2)  A slightly more advanced example: (defrule continue-check ?phase (retract ?phase) (printout t “Do you want to continue?” crlf) (bind ?answer (read)) (if(or (eq ?answer yes ) (eq ?answer y )) then (assert (phase continue)) else (halt) ) )

23 23 Knowledge-Based Systems, Paula Matuszek 01/12/2011 File I/O  File I/O (load ) (save )  NOTE: use \\ in paths if you trying to do it on Windows; or / will always work.

24 24 Knowledge-Based Systems, Paula Matuszek 01/12/2011 To Display Constructs  To display constructs:  (list-defrules)  (list-deftemplates)  (list-deffacts)  To display the text of definitions of the constructs:  (ppdefrule )  (ppdeftemplate )  (ppdeffeacts )

25 25 Knowledge-Based Systems, Paula Matuszek 01/12/2011 To Delete Constructs  To ”undefine“ a given construct:  (undefrule )  (undeftemplate )  (undeffacts )

26 26 Knowledge-Based Systems, Paula Matuszek 01/12/2011 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

27 27 Knowledge-Based Systems, Paula Matuszek 01/12/2011 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))

28 28 Knowledge-Based Systems, Paula Matuszek 01/12/2011 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)

29 29 Knowledge-Based Systems, Paula Matuszek 01/12/2011 Math Example  With:  (defrule addition  (numbers ?x ?y) =>  (assert (sumis (+ ?x ?y))))  CLIPS> (assert(numbers 1 2))  ==> f-0 (numbers 1 2)   CLIPS> Loading Selection...  Defining defrule: addition +j  ==> Activation 0 addition: f-0  CLIPS> (run)  FIRE 1 addition: f-0  ==> f-1 (sumis 3)

30 30 Knowledge-Based Systems, Paula Matuszek 01/12/2011 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))

31 31 Knowledge-Based Systems, Paula Matuszek 01/12/2011 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))

32 32 Knowledge-Based Systems, Paula Matuszek 01/12/2011 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.

33 33 Knowledge-Based Systems, Paula Matuszek 01/12/2011 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))

34 34 Knowledge-Based Systems, Paula Matuszek 01/12/2011 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 )  Example:  (test (> ?size 1))

35 35 Knowledge-Based Systems, Paula Matuszek 01/12/2011 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 )  = equal, != not equal, >= greater than or equal,  > greater than, <= less than or equal, < less than These are used for numeric values. ( )  These are used to test the type of a field: numberp, stringp, wordp, integerp, evenp, oddp

36 36 Knowledge-Based Systems, Paula Matuszek 01/12/2011 Debugging  “Watch them!” (watch rules) (watch facts)  Make save output to the file: To start logging: (dribble-on “output.log”) To stop: (dribble-off)

37 37 Knowledge-Based Systems, Paula Matuszek 01/12/2011 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)

38 38 Knowledge-Based Systems, Paula Matuszek 01/12/2011 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.

39 39 Knowledge-Based Systems, Paula Matuszek 01/12/2011 Conflict Resolution Strategies n Recency Rules which use more recent data are preferred. CLIPS time-tags WM elements n 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 n Refractoriness A rule should not be allowed to fire more than once for the same data. Prevents loops Used in CLIPS (need (refresh) )

40 40 Knowledge-Based Systems, Paula Matuszek 01/12/2011 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 )

41 41 Knowledge-Based Systems, Paula Matuszek 01/12/2011 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/ClipsT utorial/tableOfContents.htm

42 42 Knowledge-Based Systems, Paula Matuszek 01/12/2011 Infinite Loop or How To Shoot Oneself in the Foot  You can get into an infinite loop if you are not careful enough. (defrule simple-loop ?old-fact (printout t “Looping!” crlf) (retract ?oldfact) (assert (loop-fact)))  Use Control-C (or another interrupt command) to get break out of the loop.


Download ppt "1 01/12/2011Knowledge-Based Systems, Paula Matuszek More Intro to CLIPS Paula Matuszek CSC 9010, Spring, 2011."

Similar presentations


Ads by Google