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

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

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

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

101/12/2011 Knowledge-Based Systems, Paula Matuszek

More Intro to CLIPSPaula MatuszekCSC 9010, Spring, 2011

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

2Knowledge-Based Systems, Paula Matuszek01/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

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

3Knowledge-Based Systems, Paula Matuszek01/12/2011

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)

)

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

4Knowledge-Based Systems, Paula Matuszek01/12/2011

(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)

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

5Knowledge-Based Systems, Paula Matuszek01/12/2011

(deftemplate) Example(deftemplate class “an example template”

(slot number)

(multislot prerequisite)

(multislot day)

(slot time)

)

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

6Knowledge-Based Systems, Paula Matuszek01/12/2011

Matching Fact for Deftemplate(class “an example fact”

(number CSC8520 number)

(prerequisite CSC8301)

(day Monday Wednesday)

(time “2:00”)

)

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

7Knowledge-Based Systems, Paula Matuszek01/12/2011

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

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

8Knowledge-Based Systems, Paula Matuszek01/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)

)

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

9Knowledge-Based Systems, Paula Matuszek01/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 <deffacts-name> [<comment>] <facts>*)(reset)

These correspond to our static information. Must have a matching (deftemplate) declared first in

the file or buffer.

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

10

Knowledge-Based Systems, Paula Matuszek01/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))

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

11

Knowledge-Based Systems, Paula Matuszek01/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.

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

12

Knowledge-Based Systems, Paula Matuszek01/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)

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

13

Knowledge-Based Systems, Paula Matuszek01/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)

)

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

14

Knowledge-Based Systems, Paula Matuszek01/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).

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

15

Knowledge-Based Systems, Paula Matuszek01/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.

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

16

Knowledge-Based Systems, Paula Matuszek01/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)

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

17

Knowledge-Based Systems, Paula Matuszek01/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)

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

18

Knowledge-Based Systems, Paula Matuszek01/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))

)

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

19

Knowledge-Based Systems, Paula Matuszek01/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 *)

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

20

Knowledge-Based Systems, Paula Matuszek01/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)

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

21

Knowledge-Based Systems, Paula Matuszek01/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))

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

22

Knowledge-Based Systems, Paula Matuszek01/12/2011

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 ?answer yes) (eq ?answer y)) then (assert (phase continue)) else (halt))

)

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

23

Knowledge-Based Systems, Paula Matuszek01/12/2011

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.

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

24

Knowledge-Based Systems, Paula Matuszek01/12/2011

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>)

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

25

Knowledge-Based Systems, Paula Matuszek01/12/2011

To Delete Constructs To ”undefine“ a given construct:

(undefrule <defrule-name>)

(undeftemplate <deftemplate-name>)

(undeffacts <deffacts-name>)

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

26

Knowledge-Based Systems, Paula Matuszek01/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

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

27

Knowledge-Based Systems, Paula Matuszek01/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))

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

28

Knowledge-Based Systems, Paula Matuszek01/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)

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

29

Knowledge-Based Systems, Paula Matuszek01/12/2011

Math ExampleWith:

(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)

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

30

Knowledge-Based Systems, Paula Matuszek01/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))

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

31

Knowledge-Based Systems, Paula Matuszek01/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))

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

32

Knowledge-Based Systems, Paula Matuszek01/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.

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

33

Knowledge-Based Systems, Paula Matuszek01/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))

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

34

Knowledge-Based Systems, Paula Matuszek01/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 <predicate-function>) Example:

(test (> ?size 1))

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

35

Knowledge-Based Systems, Paula Matuszek01/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 <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 test the type of a field: numberp, stringp, wordp, integerp, evenp, oddp

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

36

Knowledge-Based Systems, Paula Matuszek01/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)

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

37

Knowledge-Based Systems, Paula Matuszek01/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)

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

38

Knowledge-Based Systems, Paula Matuszek01/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.

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

39

Knowledge-Based Systems, Paula Matuszek01/12/2011

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) )

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

40

Knowledge-Based Systems, Paula Matuszek01/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 <strategy>)

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

41

Knowledge-Based Systems, Paula Matuszek01/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/ClipsTutorial/tableOfContents.htm

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

42

Knowledge-Based Systems, Paula Matuszek01/12/2011

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.