Kingdom of Saudi Arabia
Ministry of Higher Education المملكة العربية السعودية
Taibah University وزارة التعليم العالي
College of Computer Science and جامعة طيبة
Engineering كلية علوم و هندسة الحاسبات
MSCS822 – Expert Systems and Knowledge Engineering
Applications
Lectures of Week 9:
Expert Systems Programming and Shells – Part 2
Mathematical Operators in CLIPS
• basic operators (+,-,*,/) and many functions (trigonometric,
logarithmic, exponential) are supported
• prefix notation
• no built-in precedence, only left-to-right and parentheses
• test feature
– evaluates an expression in the LHS instead of matching a pattern
against a fact
• pattern connectives
– multiple patterns in the LHS are implicitly AND-connected
– patterns can also be explicitly connected via AND, OR, NOT
• user-defined functions
– external functions written in C or other languages can be
integrated
– Jess is tightly integrated with Java
Variables in CLIPS
• Variables
– symbolic name beginning with a question mark "?"
– variable bindings
» variables in a rule pattern (LHS) are bound to the
corresponding values in the fact, and then can be used
on the RHS
» all occurrences of a variable in a rule have the same
value
» the left-most occurrence in the LHS determines the valu
» bindings are valid only within one rule
– Access to facts
» variables can be used to make access to facts more
convenient:
?age <- (age ahmed 17)
Wildcards
• question mark ?
– matches any single field within a fact
• multi-field wildcard $?
– matches zero or more fields in a fact
Example of Rule with Variable &
Pattern Matching
(defrule find-brown-eyes
(person (name ?name)
(eye-color brown))
=>
(printout t ?name " has brown
eyes." crlf))
Variables & Pattern Matching
(defrule pick-a-chore
(today is ?day)
(chore is ?job)
=>
(assert (do ?job on ?day))
)
If conditions are matched, then bindings are used.
For example conditions are: (today is Monday) &
(chore is washing)
Variables Using Templates
(deftemplate student " a student record "
(slot name (type SYMBOL))
(slot age (type NUMBER) (default 18)))
CLIPS> (assert (student (name ahmed)))
(defrule print-a-student
(student (name ?name) (age ?age))
=>
(printout t ?name " is " ?age crlf ))
Retracting Facts from a Rule
(defrule do-a-chore
(today is ?day)
?chore <- (do ?job on ?day)
=>
(printout t ?job " done " crlf)
(retract ?chore) )
• Here a variable must be assigned to the fact for
retraction
Pattern Matching Details
• one-to-one matching
(do ?job on ?day)
(do washing on monday)
• use of wild cards
(do ? ? monday)
(do ? on ?)
(do ? ? ?day)
(do $?)
(do $? monday)
(do ?chore $?when)
Field Constraints
• not constraint ~
– the field can take any value except the one
specified
• or constraint |
– specifies alternative values, one of which
must match
• and constraint &
– the value of the field must match all
specified values
– mostly used to place constraints on the
binding of a variable
Example Rule with Field
Constraints
(defrule eye-hair-match
(person (name ?name1)
(eye-color ?eyes1&black|dark-brown)
(hair-color ?hair1&~black))
(person (name ?name2&~?name1)
(eye-color ?eyes2&~?eyes1)
(hair-color ?hair2&dark-brown|?hair1))
=>
(printout t ?name1 " has "?eyes1 " eyes and "
?hair1 " hair." crlf)
(printout t ?name2 " has "?eyes2 " eyes and
" ?hair2 " hair." crlf))
Example Rule with Field
Constraints - Facts
(deftemplate person "deftemplate example"
(slot name)
(slot age)
(slot eye-color)
(slot hair-color))
(deffacts Siddiqi "some members of the Siddiqi family"
(person (name "Nasim Siddiqi") (age 49)
(eye-color black) (hair-color dark-brown))
(person (name "Usman Siddiqi") (age 44)
(eye-color black) (hair-color black))
(person (name "Fatima Siddiqi") (age 35)
(eye-color dark-brown) (hair-color black))
(person (name "Bublu Siddiqi") (age 99)
(eye-color green) (hair-color dark-brown)))
Defining Functions in CLIPS
• Uses a LISP or Scheme-like syntax
(deffunction function-name (arg ... arg)
action ... action)
(deffunction hypotenuse (?a ?b)
(sqrt (+ (* ?a ?a) (* ?b ?b))))
(deffunction initialize ()
(clear)
(assert (today is sunday)))
• print information Input / Output
(printout <logical-device> <print-items>*)
» logical device frequently is the standard output device t
(terminal)
• terminal input
(read [<logical-device>]), (readline [<logical-
device>])
» read an atom or string from a logical device
» the logical device can be a file which must be open
• open / close file
(open <file-name> <file-ID> [<mode>]), (close [<file-
ID>])
» open /close file with <file-id> as internal name
• load / save constructs from / to file
(load <file-name>), (save <file-name>)
» backslash \ is a special character and must be ``quoted''
(preceded by a backslash \)
• e.g. (load "B:\\clips\\example.clp")
Input / Output
• Use of read – Getting data from user
• Example 1: (Refer Tutorial 2)
(defrule what-is-child
(animal ?name)
(not (child-of ?name ?))
=>
(printout t "What do you call the child of a " ?name "?")
(assert (child-of ?name (read))))
Note: Please see the facts for this rule on next slide.
Input / Output
• Example of use of read – Getting data from user
Facts:
(deffacts startup
(animal dog) (animal duck) (animal turtle)
(warm-blooded dog) (warm-blooded cat)
(warm-blooded duck)
(lays-eggs duck) (lays-eggs turtle)
(child-of dog puppy) (child-of cat kitten)
(child-of turtle hatchling)
)
Input / Output
• Use of read – Getting data from user
• Example 2: (Refer Tutorial 2)
(defrule are-lights-working
(not (lights-working ?))
=>
(printout t "Are the car's lights working (yes or no)?")
(assert (lights-working (read))))
Defining Classes & Instances
(defclass CAR (is-a USER) ; user-defined class
(role concrete) ; instances can be made
(slot id)
(multislot owner)
(slot color)
(slot value) )
(defclass LUXURY (is-a CAR)
(slot color (allowed-values grey red green))
(slot value (range 0.0 100000.0)) )
(definstances Cars
(c1 of CAR (value 5000.0))
(c2 of LUXURY (color red) (value 15000.)))
[Jackson 1999]
Concrete & Abstract Classes
• some classes only exist for inheritance
purposes
Person
Man Woman
Jack Jill
[Jackson 1999]
Managing Instances
• (make-instance c3 of CAR (color blue))
• Commands to display instances
CLIPS> (instances)
……………
CLIPS> (send [c3] print)
[c3] of car
(color blue)
[Jackson 1999]
Clearing & Resetting Instances
• deleting an instance
CLIPS> (send [c3] delete)
• deleting all instances
CLIPS> (unmake-instance *)
• resetting creates an initial object
CLIPS> (reset)
CLIPS> (instances)
[initial-object] of INITIAL-
OBJECT
[Jackson 1999]
Identify the problem with Car
Program Execution
• Agenda/watch
– if all patterns of a rule match with facts, it is put on the
agenda
– (agenda) displays all activated rules
• salience
– indicates priority of rules
• reactivation
– (refresh <rule-name>)
» reactivates rules
User Interface
• menu-based version
– most relevant commands are available
through windows and menus
• command-line interface
– all commands must be entered at the
prompt
– (don’t forget enclosing parentheses)
Alternatives to CLIPS
• JESS
– see next
• Eclipse
– enhanced, commercial variant of CLIPS
– has same syntax as CLIPS (both are based on ART)
– supports goal-driven (i.e., backwards) reasoning, where as CLIPS
supports forward chaining
– can be integrated with C++ and dBase
• NEXPERT OBJECT
– another rule- and object-based system
– has facilities for designing graphical interfaces
– has a ‘script language’ for designing user front-end
– written in C, runs on many platforms, highly portable
JESS
• JESS stands for Java Expert System Shell
• it uses the same syntax and a large majority of the
features of CLIPS
• tight integration with Java
– can be invoked easily from Java programs
– can utilize object-oriented aspects of Java
References
1. Dr. Franz J. Kurfess, CSC 481 - Knowledge-based Systems,
http://users.csc.calpoly.edu/~fkurfess/Courses/CSC-481/, 2005.
2. Giarratano JC, Riley GD. Expert Systems: Principles and Programming.
2n' edition. PWS Kent, 1992.
3. Peter Jackson, Introduction to Expert Systems, 3rd Edition, Addison-
Wesley, 1999.
4. Wan Hussain Wan Ishak, Expert Systems Course,
http://staf.uum.edu.my/hussain, UUM Malaysia, 2009.