Expert System Shell Dr Nicholas Gibbins nmgecssotonacuk The CLIPS Expert System Shell A framework for building expert systems Dont need to build basic infrastructure every time you build an expert system ID: 620013
Download Presentation The PPT/PDF document "The CLIPS" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Slide1
The CLIPS Expert System Shell
Dr Nicholas Gibbins
nmg@ecs.soton.ac.ukSlide2
The CLIPS Expert System Shell
A framework for building expert systems
Don’t need to build basic infrastructure every time you build an expert system
Commonly used and well-understood
First developed at NASA JSC in mid-1980s
Provides the following facilities:
Representation of facts and rules
Search mechanism for identifying and firing matching rulesSlide3
Facts in CLIPS
Represented in CLIPS by s-expressions:
(likes fred sally)
likes
,
fred
and
sally
are symbols
Two types of CLIPS fact
Ordered (as above)
Unordered, specified using
deftemplate
(we’ll come to this later)Slide4
Starting and stopping CLIPS
Run CLIPS from the UNIX command line:
CLIPS>
To quit CLIPS, type
(exit)Slide5
Asserting facts
Add a new fact to the knowledge base with
assert
:
CLIPS> (assert (likes fred mary))
<Fact-0>
CLIPS>
Check for asserted facts with
facts
:
CLIPS> (facts)
f-0 (likes fred mary)
For a total of 1 fact.
CLIPS>
The
0
in
f-0
and
Fact-0
is the fact-index of the new factSlide6
Retracting facts
Remove a fact from the knowledge base with retract:
CLIPS> (assert (likes fred mary))
<Fact-0>
CLIPS> (facts)
f-0 (likes fred mary)
For a total of 1 fact.
CLIPS> (retract 0)
CLIPS> (facts)
CLIPS>
Note that you need to know the fact-index to retract a fact
Remove all facts from the knowledge base using
(clear)Slide7
Watching the knowledge base
Observe the activity in the knowledge base using
watch
:
CLIPS> (watch facts)
CLIPS> (assert (likes fred mary))
==> f-0 (likes fred mary)
<Fact-0>
CLIPS> (retract 0)
<== f-0 (likes fred mary)
CLIPS>
==>
indicates that a fact is being added
<==
indicates that a fact is being removed
Turn off watch using
(unwatch facts)Slide8
Creating rules
A simple rule:
(defrule mary-loved
(likes fred mary)
=>
(assert (loved mary)))
The name of the rule
The rule condition
The rule actionSlide9
Rules in general
(defrule <rule name> “optional comment”
<rule condition 1>
<rule condition 2
...
=>
<rule action 1>
<rule action 2>
...)
Everything before
=>
is known as the
left-hand side
(LHS), everything after the
right-hand side
(RHS)
The rule is fired if all of the conditions in the LHS match
When the rule fires, all of the actions are carried outSlide10
The Agenda
The list of rules which are waiting to be fired (the conflict set) is known as the agenda
This can be examined using
(agenda)
:
CLIPS> (agenda)
0 mary-loved: f-0
For a total of 1 activation.
CLIPS>Slide11
Running your rules
Start executing your rules with run:
CLIPS> (run)
==> f-1 (loved mary)
CLIPS>
Execution continues until there are no rules left to be fired (the agenda is empty)
Can control number of rule firings with
(run <number>)
Slide12
Variables
We can have variables in the conditions of our rules:
(defrule loved
(likes ?sub ?obj)
=>
(assert (loved ?obj)))Slide13
Variables
CLIPS> (assert (likes fred mary))
==> f-0 (likes fred mary)
<Fact-0>
CLIPS> (assert (likes fred sue))
==> f-1 (likes fred sue)
<Fact-1>
CLIPS> (agenda)
0 loved: f-1
0 mary-loved: f-0
0 loved: f-0
For a total of 3 activations.
CLIPS> (run)
==> f-2 (loved sue)
==> f-3 (loved mary)
CLIPS> Slide14
Loading from files
If your rules are defined in a file called
myrules.clp
At the shell command line:
$ clips -f myrules.clp
At the CLIPS command line:
CLIPS> (load “myrules.clp”)Slide15
Retracting facts
(defrule remove-liked
?fact <- (likes ?sub ?obj)
=>
(retract ?fact))
<-
binds the address of a matching fact to a variableSlide16
Anonymous variables
If we’re not interested in the value of a variable, we can match using a single-field wildcard variable:
?
(defrule remove-liked
?fact <- (likes ? ?)
=>
(retract ?fact))Slide17
Multifield variables
Consider a knowledge base with variable-length facts of the form
(route town-1 town-2 town-3 … town-n)
If we wish to match all routes that start at A and end at B, we can match all the intermediate towns using a multifield wildcard:
$?
Multifield variables match a list of objects (…)
(defrule find-route
(route town-a $?towns town-b)
=>
(printout t “Route from A to B via “ ?towns crlf))Slide18
Multifield variables
We have a fact:
(route a b c d e f g h)
In how many ways can this be matched by this pattern?
(route $?start ?item $?end)Slide19
Multifield variables
$?start = (), ?item = a, $?end = (b c d e f g h)
$?start = (a), ?item = b, $?end = (c d e f g h)
$?start = (a b), ?item = c, $?end = (d e f g h)
$?start = (a b c), ?item = d, $?end = (e f g h)
$?start = (a b c d), ?item = e, $?end = (f g h)
$?start = (a b c d e), ?item = f, $?end = (g h)
$?start = (a b c d e f), ?item = g, $?end = (h)
$?start = (a b c d e f g), ?item = h, $?end = ()Slide20
Field Constraints
We can further constraint the values taken by variables
~symbol
matches anything but
symbol
symbol1|symbol2
matches either
symbol1
or
symbol2
symbol1&symbol2
matches both symbols
?var&constraint matches anything that satisfies the constraint, and assigns it to ?varSlide21
Test Conditions
LHS of rule can contain other sorts of constraints on variable values (see BPG p. 47)
For example:
(defrule my-rule
(age fred ?var1)
(age sally ?var2)
(test (> ?var1 ?var2))
=>
…
)Slide22
Functions
CLIPS has a number of built-in functions which can be invoked like their Scheme look-a-likes:
(+ 1 2)
Complete list of built-in functions starting at p.149 of the CLIPS reference manual (Basic Programming Guide) Slide23
Defining functions
We can define a custom function using deffunction:
(deffunction square
(?x)
(* ?x ?x))Slide24
Defining functions
(deffunction function-name
(?arg1 ?arg2 ... $?args)
(action1
action2
...
actionn))
Value returned by function is that of last action
Final argument may be a multifield variableSlide25
Binding variables
We can bind the result of a function to a variable:
(bind ?sum (+ ?x ?y))Slide26
Example: Route Finding
E
C
B
D
A
5
7
4
2
8
3Slide27
Example: Route Finding
Represent routes as facts of the form:
(route
start
… end distance
)
For example:
(route town-a town-b town-d town-e 16)
How can we generate all of the shortest routes from every town to every other town?Slide28
Writing output
We can write to stdout using printout:
(defrule print-likes
(likes ?x ?y)
=>
(printout t ?x “ likes “ ?y crlf))
prints:
“john likes sally”Slide29
Reading input
(defrule read-colour
(initial-fact)
=>
(printout t “Name a colour” crlf)
(assert (colour (read))))
(read)
reads a single symbol, integer or string
(readline)
reads until the next carriage returnSlide30
Templated facts
The ordered lists of terms we’ve seen so far are only one kind of fact
We can also create facts containing unordered (but named) terms:
(car (colour red) (doors 4) (maker vw))
is the same as:
(car (doors 4) (maker vw) (colour red))Slide31
Templated facts
We introduce a new type of templated fact as follows:
(deftemplate car
(slot registration
(type SYMBOL))
(slot color
(type SYMBOL)
(allowed-symbols white red green blue))
(slot doors
(type INTEGER)
(default 4))
(slot maker
(type SYMBOL)))Slide32
Templated facts
Templated facts are asserted like any other fact:
(assert (car (registration g885tnj)
(doors 4)
(maker vw)
(colour green)))Slide33
Example: Classification
We wish to build an expert system to classify the following farmyard animals:
Dog
Cows
Chickens
Ducks
PigsSlide34
Example: Classification
Possible means for classification:
Fur/feathers/skin
Number of legs
Swims/doesn’t swim
Eats meat/grass/grainSlide35
Example: Classification
(deftemplate observation
(slot name
(type SYMBOL))
(slot value))
(deftemplate classification
(slot name
(type SYMBOL)))