CLIPS

Document Sample
CLIPS Powered By Docstoc
					                   CLIPS
                  Michael Scherger
           Department of Computer Science
                Kent State University

November 2, 2004    AI: CLIPS Language Tutorial   1
                   Introduction
• CLIPS is a tool for building expert systems
     – Originally developed by the Software
       Technology Branch (STB) at NASA Johnson
       Space Center
     – First release in 1986


• Web location
     – http://www.ghg.net/clips/CLIPS.html

November 2, 2004    AI: CLIPS Language Tutorial   2
                   Introduction
• CLIPS was designed to facilitate the
  development of software to model human
  knowledge
     – Facts
     – Rules
     – Deffunctions and generic functions
     – Object oriented programming


November 2, 2004    AI: CLIPS Language Tutorial   3
            Starting / Exiting CLIPS
• To start CLIPS (Windows)…just double
  click the CLIPSWin.exe icon

• To exit CLIPS type (exit) at the CLIPS>
  prompt.




November 2, 2004   AI: CLIPS Language Tutorial   4
                                Facts
• Fact Assertion
     – (assert (play Ivan tennis))
     – (assert (duck))
     – (assert (chair red))


• As facts are entered into the KB, they are
  assigned a Fact Index
     – (retract 1)
          • Removes fact 1 from the KB
     – (clear)
          • Removes all facts from the fact base and KB
November 2, 2004           AI: CLIPS Language Tutorial    5
                              Facts
• Fact Assertion
     – (facts)
          • Dump the “fact base”
          • Fact identifier – “time tag”
               – f-0
               – f-1
     – Special fact
          • (initial-fact)
          • Is always F0 and is used to match the first/start
            rules
November 2, 2004         AI: CLIPS Language Tutorial            6
                               Facts
• deffacts is a way of initializing the fact
  base (group of facts)
• Example:
     (deffacts tennis-players “people who play tennis”
                 (athelete Ivan very-good)
                 (play Ivan tennis)
                 (athelete Martina very-good)
                 (play Martina tennis))

• Will cause the fact base to be initialized with the facts +
  (initial-fact)

November 2, 2004          AI: CLIPS Language Tutorial           7
                                 Facts
• When (reset) is entered, the result is…
     f-0   (initial-fact)
     f-1   (athelete Ivan very-good)
     f-2   (play Ivan tennis)
     f-3   (athelete Martina very-good)
     f-4   (play Martina tennis)




November 2, 2004            AI: CLIPS Language Tutorial   8
                            Rules
• Syntax
     (defrule r-name            “comment”
           pattern-1
           …
           pattern-n
          =>
           action-1
           …
           action-m)

November 2, 2004       AI: CLIPS Language Tutorial   9
                        Rules
•   r-name is the rule name
•   comment must be surrounded by quotes
•   pattern-i is the antecedent pattern
•   action-j is the consequent pattern




November 2, 2004   AI: CLIPS Language Tutorial   10
                             Rules
• The agenda is the list of rules that have been
  matched and are waiting execution

• (agenda) will print out the rules

• The agenda is prioritized by salience value
     – Salience is specified by the programmer and is from -
       10000 to 10000
     – Default is 0 if (declare (salience 25)) is not in rule e.g.
     – Rules are selected for firing by salience
     – Two rules of same salience use LIFO to fire

November 2, 2004        AI: CLIPS Language Tutorial             11
                        Rules
• (pprule r-name) will pretty print out the
  rule

• (excise r-name) will remove a rule from
  the system




November 2, 2004   AI: CLIPS Language Tutorial   12
                     Variables
• Variables start with a ?
     – E.g. ?age
     – Bindings are valid within a rule only




November 2, 2004     AI: CLIPS Language Tutorial   13
                   Fact Base Updates
• (retract fact-id)
     – requires “fact” to be the index number which
       is sometime difficult to determine
     – Therefore use
          • variable with <- notation which binds the fact
            index number to the variable




November 2, 2004         AI: CLIPS Language Tutorial         14
                   Fact Base Updates
• Example                                   • What facts are retracted?
(defrule become-adult                       • What facts are kept?
   ?child <- (child harry)
   (birthday harry August-15)
                                            • What facts are
   ?age <- (age harry 17)                     generated?
   (date today August-15)
=>
                                            • Changing harry to
   (assert (adult harry))                     ?person and August-15 to
   (retract ?child)                           ?date will generalize this
   (retract ?age)
                                              rule
   (assert (age harry 18))
   (printout t “harry is now an adult”
   crlf))

November 2, 2004             AI: CLIPS Language Tutorial              15
                   Firing Rules




November 2, 2004    AI: CLIPS Language Tutorial   16
                   Firing Rules




November 2, 2004    AI: CLIPS Language Tutorial   17
            Firing Rules (Matching)




November 2, 2004   AI: CLIPS Language Tutorial   18
            Firing Rules (Matching)




November 2, 2004   AI: CLIPS Language Tutorial   19
                   Wildcard Matching
•?
     – matches one


• $?
     – matches any number


• $?name
     – match and bind
November 2, 2004       AI: CLIPS Language Tutorial   20
   Variables, Variables, Variables
• Variables start with a ?



• Examples
     ?x                     ?sensor              ?color
     ?location              ?room                ?size



November 2, 2004   AI: CLIPS Language Tutorial            21
   Variables, Variables, Variables




November 2, 2004   AI: CLIPS Language Tutorial   22
                   Wildcard Matching
• Example
• (name ? ?Kennedy)
     – will match
          • (name John Fitzgerald Kennedy)
• (name ? $? SMITH)
     – will match
          • (name John SMITH)
          • (name Suzie Jane SMITH)
          • (name John James Jones SMITH)
     – but would not match
          • (name SMITH)
          • (name John Jones SMITH Rogers)

• $?name is the same as the previous but the matches are bound to
  $?name


November 2, 2004              AI: CLIPS Language Tutorial           23
                   Wildcard Matching




November 2, 2004       AI: CLIPS Language Tutorial   24
                   Field Constraints
• Negation           ~
     (defrule apply-heat
          (temperature water ~boil)
          =>
          (adjust heat maximum); a function call
          (printout t “Turn the heat to the maximum setting”
            crlf))




November 2, 2004         AI: CLIPS Language Tutorial           25
                       Field Constraints
• OR               |
     (defrule apply-heat
          (temperature water cold|cool|warm)
          =>
          (adjust heat maximum); a function call
          (printout t “Turn the heat to a medium setting” crlf))




November 2, 2004          AI: CLIPS Language Tutorial         26
                       Field Constraints
• AND              &
          • (temperature water ?temp&hot|boil)
     – will match either of the following facts
          • (temperature water hot)
          • (temperature water boil)




November 2, 2004          AI: CLIPS Language Tutorial   27
           Mathematical Operators
• Uses prefix notation as in Lisp
     (+ 3 4)
     (+ (* 3 4) (* 5 6))
• Use = as assignment for fact assertion on
  left hand side
          • (assert (answer = ( * 3 4 ) ) )
     – put
          • (answer 12)
     – in the fact list

November 2, 2004          AI: CLIPS Language Tutorial   28
                   Systematic Manner




November 2, 2004       AI: CLIPS Language Tutorial   29
                   Templates




November 2, 2004   AI: CLIPS Language Tutorial   30
                         CLIPS             (Review)


• History of CLIPS
     –   Influenced by OPS5 and ART
     –   Implemented in C for efficiency and portability
     –   Developed by NASA, distributed & supported by COSMIC
     –   Runs on PC, Mac, also under UNIX and VAX VMS
     CLIPS provides mechanisms for expert systems
     – A top-level interpreter
     – Production rule interpreter
     – Object oriented programming language
     – LISP-like procedural language


November 2, 2004          AI: CLIPS Language Tutorial           31
              Components of CLIPS
• Rule-Based Language
     – Can create a fact list
     – Can create a rule set
     – An inference engine matches facts against rules


• Object-Oriented Language
     – Can define classes
     – Can create different sets of instances
     – Special forms allow you to interface rules and objects



November 2, 2004          AI: CLIPS Language Tutorial           32
                     Defining Facts
• Facts can be asserted
     CLIPS> (assert (today is sunday))
     <Fact-0>


• Facts can be listed
     CLIPS> (facts)
     f-0 (today is sunday)


• Facts can be retracted
     CLIPS> (retract 0)
     CLIPS> (facts)
November 2, 2004             AI: CLIPS Language Tutorial   33
                   Managing Facts
• Clearing all facts
     CLIPS> (clear)
     CLIPS> (facts)


• Grouping facts - typically in a file                   (“today.clp”)
     (deffacts today     ; can be cleared with (undeffacts today)
         (today is sunday)
         (weather is warm)
     )
          After loading facts, assert with (reset)

November 2, 2004          AI: CLIPS Language Tutorial                    34
                      Defining Rules
• Rules have the following structure
(defrule rule-name optional-comment
          optional-declaration
          condition
          ...
          condition
          =>
          action
          ...
          action
     )

November 2, 2004            AI: CLIPS Language Tutorial   35
            An Example CLIPS Rule
(defrule sunday “Things to do on Sunday”
     (salience 0) ; salience in the interval [-10000, 10000]
     (today is Sunday)
     (weather is sunny)
     =>
     (assert (chore wash car))
     (assert (chore chop wood))
)

• So, if fact list contains conditions, add assertions



November 2, 2004           AI: CLIPS Language Tutorial         36
          Getting the Rules Started
• The reset command creates a special fact
     CLIPS> (load “today.clp”)
     CLIPS> (facts)
     CLIPS> (reset)
     CLIPS> (facts)
     f-0 (initial-fact) ...

     (defrule start
         (initial-fact)
         =>
         (printout t “hello”)
     )
November 2, 2004            AI: CLIPS Language Tutorial   37
      Tracing & Recording Things
• Watch command can watch facts (and
  rules)
     CLIPS> (watch facts)
     CLIPS> (reset)
     ==> f-0 (initial-fact)
     CLIPS> (retract 0)
     <== f-0 (initial-fact)

• Contents of dialog window can be sent to file
   CLIPS> (dribble-on “dribble.clp”)  ; any file name will do
   ...
   CLIPS> (dribble-off “dribble.clp”)
November 2, 2004              AI: CLIPS Language Tutorial       38
    Variables & Pattern Matching
• Variables make rules more applicable
 (defrule pick-a-chore
    (today is ?day)
    (chore is ?job)
    =>
    (assert (do ?job on ?day))
)

• If conditions are matched, then bindings are used




November 2, 2004         AI: CLIPS Language Tutorial   39
     Retracting Facts from a Rule
(defrule do-a-chore
    (today is ?day)      ; ?day must have a consistent binding
    ?chore <- (do ?job on ?day)
    =>
    (printout t ?job “ done”)
    (retract ?chore)
)

• We must assign a variable to item for retraction




November 2, 2004         AI: CLIPS Language Tutorial             40
           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)


November 2, 2004          AI: CLIPS Language Tutorial   41
                   Using Templates
(deftemplate student “a student record”
    (slot name (type STRING))
    (slot age (type NUMBER) (default 18)))

     CLIPS> (assert (student (name fred)))

(defrule print-a-student
    (student (name ?name) (age ?age))
    =>
    (printout t name? “ is “ ?age)
)


November 2, 2004          AI: CLIPS Language Tutorial   42
      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)))


November 2, 2004          AI: CLIPS Language Tutorial   43
     Defining Classes & Instances
• Defining the class CAR

(defclass car
    (is-a user)
    (name)
    (made-by))

• Defining an instance of CAR

(make-instance corvette of car
   (made-by chevrolet))

November 2, 2004         AI: CLIPS Language Tutorial   44
      Concrete & Abstract Classes
• Some classes only exist for inheritance purposes




                              Person




                   Man                             Woman




                   Jack                                 Jill
November 2, 2004          AI: CLIPS Language Tutorial          45
                   Managing Instances
• Commands to display instances
     CLIPS> (instances)
     [corvette] of car
     CLIPS> (send [corvette] print)
     [corvette] of car
     (made-by chevrolet)


• Command to group instances (in a file)
     (definstances
         (corvette of car (made-by chevrolet))
         (thunderbird of car (made-by ford)))


November 2, 2004          AI: CLIPS Language Tutorial   46
  Clearing & Resetting Instances
• Deleting an instance
   CLIPS> (send [corvette] delete)

• Deleting all instances
   CLIPS> (unmake-instance *)

• Resetting creates an initial object
   CLIPS> (reset)
   CLIPS> (instances)
   [initial-object] of INITIAL-OBJECT




November 2, 2004         AI: CLIPS Language Tutorial   47
                   Message Passing
• The SEND function

     (send [instance] message arg ... arg)

• Converting from symbols to names

     CLIPS> (symbol-to-instance-name corvette)
     [corvette]

This is useful when SENDing from inside a rule


November 2, 2004          AI: CLIPS Language Tutorial   48
                   Limitations of CLIPS
• Single level rule sets
     – in LOOPS, you could arrange rule sets in a hierarchy,
       embedding one rule set inside another, etc
     Loose coupling of rules and objects
     – rules can communicate with objects via message passing
     – 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

November 2, 2004          AI: CLIPS Language Tutorial                 49
               Alternatives to CLIPS
• Eclipse
     –   has same syntax as CLIPS (both are based on ART)
     –   supports goal-driven (i.e., backwards) reasoning
     –   has a truth maintenance facility for checking consistency
     –   can be integrated with C++ and dBase
     –   new extension RETE++ can generate C++ header files

• 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
November 2, 2004            AI: CLIPS Language Tutorial              50

				
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
views:44
posted:11/23/2011
language:English
pages:50