Вы находитесь на странице: 1из 13

Course Overview

1. Introduction 6. Pattern Matching


2. CLIPS Overview Variables, Functions,

COMP 4200:
Expressions, Constraints
Concepts, Notation, Usage
3. Knowledge Representation 7. Expert System Design
Expert Systems Semantic Nets, Frames,
Logic
XPS Life Cycle
8. Expert System
4. Reasoning and Inference Implementation
Dr. Christel Kemke Predicate Logic, Inference Salience, Rete Algorithm
Methods, Resolution 9. Expert System Examples
Department of Computer Science 5. Reasoning with 10. Conclusions and Outlook
University of Manitoba Uncertainty
Probability, Bayesian
Decision Making

© C. Kemke CLIPS 1 © C. Kemke CLIPS 2

Overview CLIPS Motivation

Motivation Variables and Pattern CLIPS is a decent example of an expert system


Objectives Matching shell
Variables
CLIPS Background rule-based, forward-chaining system
Pattern Matching
History
Execution of Programs
it illustrates many of the concepts and methods used
Main ideas
Terminology Invoking and leaving CLIPS in other XPS shells
Facts and Rules Watching it allows the representation of knowledge, and its
Formats Important Concepts and use for solving suitable problems
Using Facts and Rules Terms
Chapter Summary

© C. Kemke CLIPS 3 © C. Kemke CLIPS 4

1
Objectives Introduction
be familiar with the important concepts and methods used in CLIPS stands for
rule-based XPS shells C Language Implementation Production System
facts, rules, pattern matching, agenda, working memory, forward
chaining forward-chaining
understand the fundamental workings of an XPS shell starting from the facts, a solution is developed
knowledge representation pattern-matching
reasoning
Rete matching algorithm: find ``fitting'' rules and facts
apply rule-based techniques to simple examples
evaluate the suitability of rule-based systems for specific
knowledge-based system shell
tasks dealing with knowledge empty tool, to be filled with knowledge
multi-paradigm programming language
rule-based, object-oriented (Cool) and procedural

© C. Kemke CLIPS 5 © C. Kemke CLIPS 6

The CLIPS Programming Tool Components of CLIPS


history of CLIPS rule-based language
influenced by OPS5 and ART can create a fact list
implemented in C for efficiency and portability can create a rule set
developed by NASA, distributed & supported by COSMIC an inference engine matches facts against rules
runs on PC, Mac, UNIX, VAX VMS
object-oriented language (COOL)
can define classes
CLIPS provides mechanisms for expert systems
a top-level interpreter can create different sets of instances
production rule interpreter special forms allow you to interface rules and objects
object oriented programming language
LISP-like procedural language

© C. Kemke [Jackson 1999] CLIPS 7 © C. Kemke [Jackson 1999] CLIPS 8

2
Notation Tokens and Fields
symbols, characters, keywords
entered exactly as shown: tokens
(example)
square brackets [...] groups of characters with special meaning for CLIPS,
contents are optional: e.g. ( ) \ separated by delimiters
(example [test])
pointed brackets (less than / greater than signs) < ... > (space, tab, Carriage Return, ...)
replace contents by an instance of that type
(example <char>)
fields
star * particularly important group of tokens
replace with zero or more instances of the type
<char>* CLIPS primitive data types
plus + float, integer, symbol, string,
replace with one or more instances of the type external address, instance name, instance address
<char>+ (is equivalent to <char> <char>* )
vertical bar |
choice among a set of items:
true | false
© C. Kemke CLIPS 9 © C. Kemke CLIPS 10

CLIPS Primitive Data Types Invoke / Exit CLIPS


float: decimal point (1.5) or exponential notation (3.7e10)
entering CLIPS
integer: [sign] <digit>+
double-click on icon, or type program name (CLIPS)
symbol: <printable ASCII character>+
e.g. this-is-a-symbol, wrzlbrmft, !?@*+ system prompt appears:
string: delimited by double quotes CLIPS>
e.g. "This is a string" exiting CLIPS
external address at the system prompt
address of external data structure returned by user-defined CLIPS>
functions
type (exit)
instance name (used with Cool)
delimited by square brackets
Note: enclosing parentheses are important; they indicate
a command to be executed, not just a symbol
instance address (used with Cool)
return values from functions
© C. Kemke CLIPS 11 © C. Kemke CLIPS 12

3
Facts Examples of Facts
elementary information items (“chunks”) ordered fact
relation name (person-name Franz J. Kurfess)
symbolic field used to access the information
deftemplate fact
often serves as identifier for the fact
(deftemplate person "deftemplate example”
slots (zero or more)
symbolic fields with associated values (slot name)
deftemplate construct (slot age)
used to define the structure of a fact (slot eye-color)
names and number of slots (slot hair-color))
deffacts
used to define initial groups of facts

© C. Kemke CLIPS 13 © C. Kemke CLIPS 14

Defining Facts Instances


Facts can be asserted an instance of a fact is created by
CLIPS> (assert (today is sunday)) (assert (person (name "Franz J. Kurfess")
<Fact-0> (age 46)
(eye-color brown)
Facts can be listed (hair-color brown))
CLIPS> (facts) )
f-0 (today is sunday)

Facts can be retracted


CLIPS> (retract 0)
CLIPS> (facts)

© C. Kemke [Jackson 1999] CLIPS 15 © C. Kemke CLIPS 16

4
Initial Facts Usage of Facts
(deffacts kurfesses "some members of the
adding facts
Kurfess family" (assert <fact>+)
(person (name "Franz J. Kurfess") (age 46) deleting facts
(eye-color brown) (hair-color brown)) (retract <fact-index>+)
modifying facts
(person (name "Hubert Kurfess") (age 44) (modify <fact-index> (<slot-name> <slot-value>)+ )
(eye-color blue) (hair-color blond)) retracts the original fact and asserts a new, modified fact

(person (name "Bernhard Kurfess") (age 41) duplicating facts


(duplicate <fact-index> (<slot-name> <slot-value>)+ )
(eye-color blue) (hair-color blond)) adds a new, possibly modified fact
(person (name "Heinrich Kurfess") (age 38) inspection of facts
(eye-color brown) (hair-color blond)) (facts)
prints the list of facts
(person (name "Irmgard Kurfess") (age 37) (watch facts)
(eye-color green) (hair-color blond)) automatically displays changes to the fact list

)
© C. Kemke CLIPS 17 © C. Kemke CLIPS 18

Rules Rule Components

general format rule header


(defrule <rule name> ["comment"] defrule keyword, name of the rule, optional comment
<patterns>* ; left-hand side (LHS) string
; or antecedent of the rule rule antecedent (LHS)
=> patterns to be matched against facts
<actions>*) ; right-hand side (RHS) rule arrow
; or consequent of the rule separates antecedent and consequent
rule consequent (RHS)
actions to be performed when the rule fires

© C. Kemke CLIPS 19 © C. Kemke CLIPS 20

5
Examples of Rules Properties of Simple Rules
simple rule very limited:
(defrule birthday-FJK LHS must match facts exactly
(person (name "Franz J. Kurfess") facts must be accessed through their index number
(age 46)
changes must be stated explicitly
(eye-color brown)
(hair-color brown)) can be enhanced through the use of variables
(date-today April-13-02)
=>
(printout t "Happy birthday, Franz!")
(modify 1 (age 47))
)

© C. Kemke CLIPS 21 © C. Kemke CLIPS 22

Variables, Operators, Functions Wildcards


variables question mark ?
symbolic name beginning with a question mark "?"
matches any single field within a fact
variable bindings
variables in a rule pattern (LHS) are bound to the corresponding
multi-field wildcard $?
values in the fact, and then can be used on the RHS matches zero or more fields in a fact
all occurrences of a variable in a rule have the same value
the left-most occurrence in the LHS determines the value
bindings are valid only within one rule
access to facts
variables can be used to make access to facts more convenient:
?age <- (age harry 17)

© C. Kemke CLIPS 23 © C. Kemke CLIPS 24

6
Field Constraints Mathematical Operators
basic operators (+,-,*,/) and many functions
not constraint ~ (trigonometric, logarithmic, exponential) are supported
the field can take any value except the one specified prefix notation
or constraint | no built-in precedence, only left-to-right and parentheses
specifies alternative values, one of which must match test feature
evaluates an expression in the LHS instead of matching a pattern
and constraint & against a fact
the value of the field must match all specified values pattern connectives
multiple patterns in the LHS are implicitly AND-connected
mostly used to place constraints on the binding of a
patterns can also be explicitly connected via AND, OR, NOT
variable
user-defined functions
external functions written in C or other languages can be integrated
Jess is tightly integrated with Java

© C. Kemke CLIPS 25 © C. Kemke CLIPS 26

Examples of Rules Example Rule with Field Constraints


(defrule silly-eye-hair-match
more complex rule (person (name ?name1)
(defrule find-blue-eyes (eye-color ?eyes1&blue|green)
(person (name ?name) (hair-color ?hair1&~black))
(person (name ?name2&~?name1)
(eye-color blue))
(eye-color ?eyes2&~?eyes1)
=> (hair-color ?hair2&red|?hair1))
(printout t ?name " has blue eyes." =>
crlf)) (printout t ?name1 " has "?eyes1 " eyes and
" ?hair1 " hair." crlf)
(printout t ?name2 " has "?eyes2 " eyes and
" ?hair2 " hair." crlf))

© C. Kemke CLIPS 27 © C. Kemke CLIPS 28

7
Using Templates An Example CLIPS Rule
(deftemplate student “a student record” (defrule sunday “Things to do on Sunday”
(slot name (type STRING)) (salience 0) ; salience in the interval [-10000, 10000]
(slot age (type NUMBER) (default 18))) (today is Sunday)
(weather is sunny)
CLIPS> (assert (student (name fred)))
=>
(assert (chore wash car))
(defrule print-a-student
(assert (chore chop wood))
(student (name ?name) (age ?age))
=> )
(printout t ?name “ is “ ?age)
)

© C. Kemke [Jackson 1999] CLIPS 29 © C. Kemke [Jackson 1999] CLIPS 30

Getting the Rules Started Variables & Pattern Matching


The reset command creates a special fact Variables make rules more applicable
CLIPS> (load “today.clp”)
CLIPS> (facts) (defrule pick-a-chore
CLIPS> (reset)
(today is ?day)
CLIPS> (facts)
(chore is ?job)
f-0 (initial-fact) ...
=>
(defrule start (assert (do ?job on ?day))
(initial-fact) )
=>
(printout t “hello”) if conditions are matched, then bindings are used
)

© C. Kemke [Jackson 1999] CLIPS 31 © C. Kemke [Jackson 1999] CLIPS 32

8
Retracting Facts from a Rule Pattern Matching Details
(defrule do-a-chore one-to-one matching
(today is ?day) ; ?day must have a (do ?job on ?day)
consistent binding (do washing on monday)
?chore <- (do ?job on ?day)
=>
(printout t ?job “ done”) use of wild cards
(retract ?chore) (do ? ? monday)
) (do ? on ?)
(do ? ? ?day)
(do $?)
a variable must be assigned to the item for retraction (do $? monday)
(do ?chore $?when)

© C. Kemke [Jackson 1999] CLIPS 33 © C. Kemke [Jackson 1999] CLIPS 34

Defining Functions in CLIPS Defining Classes & Instances


Uses a LISP or Scheme-like syntax defining the class CAR
(defclass car
(deffunction function-name (arg ... arg) (is-a user)
action ... action) (name)
(made-by))
(deffunction hypotenuse (?a ?b)
(sqrt (+ (* ?a ?a) (* ?b ?b))))
defining an instance of CAR
(deffunction initialize () (make-instance corvette of car
(clear) (made-by chevrolet))
(assert (today is sunday)))

© C. Kemke [Jackson 1999] CLIPS 35 © C. Kemke [Jackson 1999] CLIPS 36

9
Concrete & Abstract Classes Managing Instances

some classes only exist for inheritance purposes Commands to display instances
CLIPS> (instances)
[corvette] of car
Person CLIPS> (send [corvette] print)
[corvette] of car
(made-by chevrolet)
Man Woman
Command to group instances (in a file)
(definstances
(corvette of car (made-by chevrolet))
Jack Jill (thunderbird of car (made-by ford)))

© C. Kemke [Jackson 1999] CLIPS 37 © C. Kemke [Jackson 1999] CLIPS 38

Clearing & Resetting Instances Manipulation of Constructs

deleting an instance show list of constructs


(list-defrules), (list-deftemplates), (list-
CLIPS> (send [corvette] delete) deffacts)
prints a list of the respective constructs
show text of constructs
deleting all instances (ppdefrule <defrule-name>), (ppdeftemplate
CLIPS> (unmake-instance *) <deftemplate-name>), (ppdeffacts <deffacts-name>)
displays the text of the construct (``pretty print'')
deleting constructs
resetting creates an initial object (undefrule <defrule-name>), (undeftemplate
CLIPS> (reset) <deftemplate-name>), (undeffacts <deffacts-name>)
deletes the construct (if it is not in use)
CLIPS> (instances)
clearing the CLIPS environment
[initial-object] of INITIAL-OBJECT (clear)
removes all constructs and adds the initial facts to the CLIPS
© C. Kemke [Jackson 1999] CLIPS 39 © C. Kemke environment CLIPS 40

10
Input / Output Program Execution
print information agenda
(printout <logical-device> <print-items>*) if all patterns of a rule match with facts, it is put on the
logical device frequently is the standard output device t (terminal) agenda
terminal input (agenda) displays all activated rules
(read [<logical-device>]), (readline [<logical-device>])
read an atom or string from a logical device salience
the logical device can be a file which must be open
indicates priority of rules
open / close file
(open <file-name> <file-ID> [<mode>]), (close [<file-ID>]) refraction
open /close file with <file-id> as internal name rules fire only once for a specific set of facts
load / save constructs from / to file prevents infinite loops
(load <file-name>), (save <file-name>)
backslash \ is a special character and must be ``quoted'' (preceded by a
(refresh <rule-name>)
backslash \) reactivates rules
☯ e.g. (load "B:\\clips\\example.clp")
© C. Kemke CLIPS 41 © C. Kemke CLIPS 42

Execution of a Program Watching


(reset) prepares (re)start of a program:
watching the execution
all previous facts are deleted
(watch <watch-item>) prints messages about
initial facts are asserted activities concerning a <watch-item>
rules matching these facts are put on the agenda (facts, rules, activations, statistics,
compilation, focus, all)
(run [<limit>]) starts the execution
(unwatch <watch-item>)
breakpoints turns the messages off
(set-break [<rule-name>])
stops the execution before the rule fires,
continue with (run)
(remove-break [<rule-name>])
(show-breaks)
© C. Kemke CLIPS 43 © C. Kemke CLIPS 44

11
Watching Facts, Rules and Activations More Watching ...
statistics
facts information about the program execution
assertions (add) and retractions (delete) (number of rules fired, run time, ... )
of facts compilation (default)
shows information for constructs loaded by (load)
rules Defining deftemplate: ...
message for each rule that is fired Defining defrule: ... +j=j
☯ +j, =j indicates the internal structure of the compiled rules
activations » +j join added
activated rules: matching antecedents » =j join shared

these rules are on the agenda ☯ important for the efficiency of the Rete pattern matching network
focus
used with modules
indicates which module is currently active
© C. Kemke CLIPS 45 © C. Kemke CLIPS 46

User Interface Limitations of CLIPS

menu-based version single level rule sets


most relevant commands are available through windows in LOOPS, you could arrange rule sets in a hierarchy,
and menus embedding one rule set inside another, etc
command-line interface loose coupling of rules and objects
all commands must be entered at the prompt rules can communicate with objects via message passing
(don’t forget enclosing parentheses) rules cannot easily be embedded in objects, as in
Centaur
CLIPS has no explicit agenda mechanism
the basic control flow is forward chaining
to implement other kinds of reasoning you have to
manipulate tokens in working memory
© C. Kemke CLIPS 47 © C. Kemke [Jackson 1999] CLIPS 48

12
Alternatives to CLIPS JESS
JESS
see below
JESS stands for Java Expert System Shell
Eclipse
enhanced, commercial variant of CLIPS it uses the same syntax and a large majority of the
has same syntax as CLIPS (both are based on ART) features of CLIPS
supports goal-driven (i.e., backwards) reasoning
has a truth maintenance facility for checking consistency tight integration with Java
can be integrated with C++ and dBase can be invoked easily from Java programs
new extension RETE++ can generate C++ header files
can utilize object-oriented aspects of Java
not related to the (newer) IBM Eclipse environment
NEXPERT OBJECT some incompatibilities with CLIPS
another rule- and object-based system COOL replaced by Java classes
has facilities for designing graphical interfaces a few missing constructs
has a ‘script language’ for designing user front-end
more and more added as new versions of JESS are released
written in C, runs on many platforms, highly portable
© C. Kemke [Jackson 1999] CLIPS 49 © C. Kemke CLIPS 50

CLIPS Summary Important Concepts and Terms


notation agenda knowledge base
similar to Lisp, regular expressions antecedent knowledge representation
facts assert
pattern matching
(deftemplate), (deffacts), assert / retract backward chaining
consequent refraction
rules
(defrule ...), agenda CLIPS retract
expert system shell rule
variables, operators, functions
fact
advanced pattern matching rule header
field
input/output salience
forward chaining
(printout ...), (read ...), (load ...) template
function
program execution inference variable
(reset), (run), breakpoints
inference mechanism wild card
user interface instance
command line or GUI If-Then rules
JESS
© C. Kemke CLIPS 51 © C. Kemke CLIPS 52

13

Вам также может понравиться