Second CLIPS Presentation

advertisement
More Intro to CLIPS
Paula Matuszek
CSC 9010, Spring, 2011
01/12/2011
Knowledge-Based Systems, Paula Matuszek
1
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
2
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
3
(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>)
01/12/2011
Knowledge-Based Systems, Paula Matuszek
(allows more than one value)
4
(deftemplate) Example
 (deftemplate class “an example template”




(slot number)
(multislot prerequisite)
(multislot day)
(slot time)
 )

01/12/2011
Knowledge-Based Systems, Paula Matuszek
5
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
6
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
7
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
8
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
9
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
10
(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
11
Variables
 Variable name is made of ? and one or more
characters:
 Example:

(course (number ?cmp))
 Variables are used for



01/12/2011
Pattern matching
I/O
As pointers to facts (fact indices)
Knowledge-Based Systems, Paula Matuszek
12
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
13
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
14
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
15
Wildcards (1)
 To specify a general pattern we can use:
 Single field variables:
wildcard
wildcard
?
$?
 Multifield variables:
 (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:

01/12/2011
(list c), (list c d), (list a c d b)
Knowledge-Based Systems, Paula Matuszek
16
Wildcards (2)
 The fact
 (do carwash on Sunday)
 will match any of the following
 (do
? ? Sunday)

(do
(do
(do
(do
? on ?)
? on ?when)
$? )
$? Sunday)

(do
?chore



01/12/2011
$?when)
Knowledge-Based Systems, Paula Matuszek
17
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
18
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
19
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
20
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
21
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))
)
01/12/2011
Knowledge-Based Systems, Paula Matuszek
22
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
23
To Display Constructs
 To display constructs:



(list-defrules)
(list-deftemplates)
(list-deffacts)
 To display the text of definitions of the constructs:



01/12/2011
(ppdefrule <defrule-name>)
(ppdeftemplate <deftemplate-name>)
(ppdeffeacts <deffacts-name>)
Knowledge-Based Systems, Paula Matuszek
24
To Delete Constructs
 To ”undefine“ a given construct:



01/12/2011
(undefrule <defrule-name>)
(undeftemplate <deftemplate-name>)
(undeffacts <deffacts-name>)
Knowledge-Based Systems, Paula Matuszek
25
Field Constraints
 These apply to the value of a field
 NOT ~ (number ~comp672)
|
 OR
(number comp672|comp674)
 AND &


01/12/2011
(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
Knowledge-Based Systems, Paula Matuszek
26
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
27
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
28
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
29
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
30
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
31
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
32
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
33
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:

01/12/2011
(test (> ?size 1))
Knowledge-Based Systems, Paula Matuszek
34
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
01/12/2011
Knowledge-Based Systems, Paula Matuszek
35
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
36
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
37
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
38
Conflict Resolution Strategies
Recency
n


Rules which use more recent data are preferred.
CLIPS time-tags WM elements
Specificity
n
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

n



01/12/2011
A rule should not be allowed to fire more than once for the same data.
Prevents loops
Used in CLIPS (need (refresh) )
Knowledge-Based Systems, Paula Matuszek
39
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
40
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
01/12/2011
Knowledge-Based Systems, Paula Matuszek
41
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 <- (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
42
Download