Title: COMP 4200: Expert Systems
1COMP 4200 Expert Systems
- Dr. Christel Kemke
- Department of Computer Science
- University of Manitoba
2CLIPS Introduction 2
- Review and Introduction 2
- CLIPS Programming System
- Basic CLIPS Constructs and Syntax
- Fields and Templates
- Complex Condition Patterns
- Input and Output
- Salience
3CLIPS Programming Systems
- CLIPS Interpreter
- enter commands, direct modification of fact base
... - CLIPS File-Menu
- load, save and edit CLIPS program files
- CLIPS Edit-Menu
- Balance (ctrl B), Comment
- CLIPS Execution-Menu
- set execution parameters (e.g. reset, run, clear,
watch, constraint checking, halt) - CLIPS Browse-Menu
- manage and info about constructs
- CLIPS Window-Menu
- current status info, e.g. Facts, Activations,
4Working with Facts
- Changing fact base
- Adding facts (assert ltfactsgt)
- Deleting facts (retract ltfact-identifiergt)
- Modifying facts (modify ltfact-identifiergt
- (deftemplate facts) (ltslot-namegt ltslot-valuegt))
- Duplicating facts (duplicate ltfact-identifiergt
- (deftemplate facts) (ltslot-namegt
ltslot-valuegt)) - Monitoring program execution
- Print all facts (facts)
- Displays changes (watch facts), also for rules
... - Dribble record trace into file
5CLIPS Basic Constructs
- Basic Language Elements
- Fields (basic Data Types slot fillers)
- Facts (used in condition patterns)
- Rules (condition-action rules)
- Templates (like records define facts)
- Classes (like objects define facts)
- Messagehandlers (like methods defined for
classes used in condition patterns and
actions)
6Fields - Examples
- Fields (data types)
- float 4.00, 2.0e2, 2e-2
- integer 4, 2, 22
- symbol Alpha24, !?_at_
- string Johnny B. Good
- instance name titanic, PPK
- Variables
- ?var, ?x, ?day variables for single field value
- ?names variable for multi-field value
7Template and Fact with Single Field
- Single value in a slot
- (person (name "Johnny B. Good" ) (age 42))
- Defined based on
- (deftemplate person
- (slot name)
- (slot age))
- A single field slot stores one single field
value, e.g. the string "Johnny B. Good" or the
integer / number 42 in the example above.
8Multislots and Multifields
- More than one value in a slot
- (person (name Johnny B. Good) (age 42))
- Defined based on
- (deftemplate person
- (multislot name)
- (slot age))
- A multi-slot allows several field values to be
stored in one slot, e.g. the 3 field values
Johnny, B. and Good in the example above.
9Slot Restrictions
- Defined Template with Slot-Restrictions
(Constraint-Attributes) -
-
(deftemplate person (slot name (type
STRING)) (slot age (type INTEGER)) (slot gender
(allowed-symbols male female)) )
10Slot Constraint-Attributes
- ltconstraint-attributegt lttype-attributegt
- ltallowed-constant-attri
butegt - ltrange-attributegt
- ltcardinality-attributegt
- ltdefault-attributegt
- lttype-attributegt (type
lttype-specificationgt ) - lttype-specificationgt ltallowed-typegt
?VARIABLE - ltallowed-typegt SYMBOL STRING
LEXEME - INTEGER FLOAT
NUMBER - INSTANCE-NAME
INSTANCE-ADDRESS - INSTANCE
FACT-ADDRESS - EXTERNAL-ADDRESS
11Slot Constraint-Attributes
- Type Allowed Values (Enumeration, Collection)
- ltallowed-constant-attributegt
- (allowedsymbols ltsymbol
-listgt) - (allowedstrings
ltstring-listgt) - (allowed-lexemes
ltlexeme-listgt) - (allowedintegers ltinteg
er-listgt) - (allowedfloats ltfloat-l
istgt) - (allowednumbers ltnumber
-listgt) - (allowed-instance-names
ltinstance-listgt) - (allowedvalues ltvalue-l
istgt)
12Slot Constraint-Attributes
- Range of Values (min and max value)
- ltrange-attributegt (range
ltrange-specificationgt ltrange-specification
gt) - ltrange-specificationgt ltnumbergt ?VARIABLE
- Number of Fillers (min and max)
- ltcardinality-attributegt (cardinality
- ltcardinality-specificationgt
-
ltcardinality-specificationgt) - ltcardinality-specificationgt ltintegergt
?VARIABLE
13Variables and Wildcards
- single-field variable binds single value
- ?ltsymbolgt e.g. ?age, ?x, ?address
- ?fact-variable
- multi-field variable accepts multiple values
?ltsymbolgt can bind multi-field value - single-field wildcard matches any single-field
value - ?
- multi-field wildcard matches any multi-field
value - ?
-
14All Kinds of Variables
- (defrule birthday A persons birthday
- ?f1 lt- (person (name ?name) (age ?age))
- ?f2 lt- (has-birthday ?name)
- gt
- (printout t Happy Birthday, ?name)
- (retract ?f1)
- (retract ?f2)
- (assert (person (name ?name) (age ( ?age 1)))))
Q With which patterns does the Condition match?
A e.g. (person (name Johnny B. Good) (age 42))
15All Kinds of Variables 2
- (defrule birthday A persons birthday
- ?f1 lt- (person (name ?name ?) (age ?age))
- ?f2 lt- (has-birthday ?name)
- gt
- (printout t Happy Birthday, ?name)
- (retract ?f2)
- (modify ?f1 (age ( ?age 1))))
Q1 What is ? matching with? Q2 What is the
rule doing?
16Complex Condition Elements
- logical connectives and, or, not to combine
patterns - forall and exists consider all matches/ only
one match in further evaluation - logical connects condition element and asserted
fact in action (Truth Maintenance) - use test-condition
- (test ltpredicate-expressiongt)
- field-constraints , , and attached to slot
of (deftemplate) condition pattern
17Field Constraints (Connected Pattern Constraints)
- not not this value
- (name Simpson)
- (age 40)
- or could be one of these values
- (name SimpsonOswald)
- (age 304050)
- and attaches constraint to variable
- (name ?nameSimpson)
- (name ?nameHarvey)
- expr. adds expression as constraint
- (age ?age(gt ?age 20))
- (name ?name(eq (sub-string 1 1 ?name) "S")
checks whether the age is above 20
checks whether the sub-string from 1 to 1 (first
letter) of the name is "S"
18Field Constraint
- (defrule you-wish something with people and age
- (person (name ?name) (age ?age(gt ?age 20))
- gt
- (printout t ?name is over twenty. crlf)
- (printout t ?name is ?age years old. crlf))
Q1 Which patterns match this Condition? Q2
What is the rule doing?
19Condition Patterns with Logical Connectives
- Complex Conditions with logical connectives
- (or (pattern1) (pattern2))
- Rule becomes active if one of the patterns
matches. - example (or (birthday) (anniversary))
- matches fact base with facts (birthday) or
(anniversary) - Equivalent for
- and (is default)
- not
- exists to be fulfilled for one matching fact
- forall to be fulfilled for all facts which match
based on first fact and variable binding
20Complex Condition Elements - or
- (defrule report-emergency
- (or (emergency (emergency-type fire) (location
?building)) - (emergency (emergency-type bomb) (location
?building)) - )
- gt
- (printout t evacuate ?building)
- )
reports a building if there is a fire or bomb
emergency in this building
21Complex Condition Elements exists
- (defrule emergency-report
- (exists
- (or (emergency (emergency-type fire))
- (emergency (emergency-type bomb)))
- )
- gt
- (printout t There is an emergency. crlf )
- )
prints one emergency-message if there is a fire
or bomb emergency. (no further matching, firing,
or printout)
22Complex Condition Elements forall
- (defrule evacuated-all-buildings
- (forall (emergency (emergency-type fire
bomb) - (location ?building) )
- (evacuated (building ?building)))
- gt
- (printout t All buildings with emergency are
evacuated crlf))
prints evacuated-message if for all buildings,
which have a fire or bomb emergency, the building
is evacuated.
23Exercise
(defrule special-age 18, 21, 100 (or (person
(name ?name) (age 18)) (person (name ?name)
(age 21)) (person (name ?name) (age
100))) gt (printout t ?name has a special
age.))
Task Modify the condition pattern so that you
use a variable for age, and field constraints for
the values, and printout the name and age of any
matching person.
24- (deftemplate person
- (slot age (type INTEGER))
- (multislot name (type STRING))
- (slot gender (allowed-values f m)) )
- (deffacts people
- (person (age 20) (name "Mike" "M." "Moore")
(gender m)) - (person (age 40) (name "James" "J." "James" )
(gender m)) - (person (age 42) (name "Susan" "D." "More")
(gender f)) - (person (age 28) (name "John" "J." "Jones")
(gender m)) ) - (defrule what-am-I-doing
- (or (person (name ?first ?middle ?last(eq
?last "Moore"))) - (person (name ?first ?middle ?last(eq
?last "More"))) ) - gt
- (printout t "Found " ?first " " ?last crlf) )
Q1 What is the rule doing?
Q2 Can you simplify the Condition Element? Do it!
25bind-function
- bind-function explicitly binds value to
variable - (bind ?age (read))
- stores value of single field which is read into
single-field variable ?age - (bind ?name (readline))
- stores line which is read as STRING into
single-field STRING-variable ?address - (bind ?address (explode (readline)))
- explode splits line which is read as STRING into
multifield-value which is stored in
multislot-variable ?address
26Open, Close File
- Open file for read/write
- (open ltfile-namegt ltlogical-namegt r)
- ltfile-namegt is physical file-name (path)
- ltlogical-namegt is name used in program
- r indicates read-access (w, r)
- example (open example.dat my-file r)
- (read my-file)
- Close file
- (close ltlogical-namegt)
27Input read, readline
- read input of single field
- readline input of complete (line as string)
- general
- (read ltlogical namegt)
- ltlogical namegt refers to file-name in program
- (read) keyboard is default
- read with bind-function to bind input to
variable - (bind ?input (read))
- (bind ?input (readline))
28Input read, readline
- (read / readline ltlogical namegt)
- default is keyboard/terminal
- file has to be opened using
- (open ltfile-namegt ltlogical-namegt r)
- ltfile-namegt is physical file-name (can include
path) - ltlogical-namegt is name used in read command
- r indicates read-access
- example (open example.dat example r)
- (read example)
- use with bind-function to bind input to variable
29Output - printout
- (printout ltlogical-namegt ... )
- t terminal is standard
- otherwise ltlogical-namegt refers to a file-name
- file has to be opened using
- (open ltfile-namegt ltlogical-namegt w )
- ltfile-namegt is physical file-name (can include
path) - ltlogical-namegt is name used in printout command
- w indicates write-access
- example (open example.dat my-output w )
- (printout my-output ?name crlf)
30Rules - Runtime Effects
- refraction
- each rule fires only once on the same data
- agenda
- rules are activated in sequence and put on
agenda agenda is like a stack the last rule
fires first - salience
- sequence of rule firings - in case of a conflicts
is determined by their set salience factor the
higher the salience, the higher the rule priority
31Rules - Salience
Rules defined with salience factor
(defrule say-hello (declare (salience
10)) (person (name ?name)) gt (printout t
Hello, ?name)) (defrule say-happy-birthday (de
clare (salience 100)) (person (name ?name))
gt (printout t Happy Birthday, ?name))
The higher the salience, the higher the rule
priority in conflict situations.