7 Introduction to CLIPS

Notation

    (example)               ;symbol
    (example [1])           ;optional
    (example <INTEGER>)     ;replacement
    <INTEGER>*              ;zero or more
    <INTEGER>+              ;one or more
    all | none | some       ;or

Fields

float

    1.5
    9.3e+7

integer

    5
    -37

symbol

    starts with a printable ASCII character except ? and $?
    ends with a delimiter,

    delimiters:
        non-printable(space, tab, carriage return, or line feed),
        " ; & | ~ <

    Examples:
        Space-Station
        high_end
        !?#$^*
        678

string

    "!?#$^*"
    "John S. Liu"
    "\"Note\""      ;""Note"", '\' as escape character
    "\\Note\\"      ;"\Note\"

external address

instance name

instance address

Entering and Exiting Clips

    C>CLIPS

    (exit)

Facts

    ( <relation-name> <field>+ )

    (person (name "John S. Liu")
            (age 23)
            (eye-color brown)
            (hair-color black))

The Deftemplate Construct

    ( deftemplate <relation-name> [<optional-comment>]
        <slot-definition>* )
    <slot-definition>
        (slot <slot-name>) | (multislot <slot-name>)

    (deftemplate person
        (slot name)
        (slot age)
        (slot eye-color)
        (slot hair-color))

Multified Slots

    (deftemplate person
        (multislot name)
        (slot age)
        (slot eye-color)
        (slot hair-color))

    (person (name John S. Liu)      ;multislot
            (age 23)
            (eye-color brown)
            (hair-color black))

Ordered Facts

facts without a deftemplate
    (person John S. Liu     ;name John
            23              ;age
            brown           ;eye-color
            black)          ;hair-color

Adding and Removing Facts

    (assert <fact>+)

    (assert (person (name John S. Liu)
                    (age 23)
                    (eye-color brown)
                    (hair-color black)))

    (retract <fact-index>+)

    (facts)
    (retract 0)

Modifying and Duplicating Facts

    (modify <fact-index> <slot-modifier>+)

    <slot-modifier>
            (<slot-name> <slot-value>)

    (modify 0 (age 24) )
    (facts)

    (duplicate 2 (name "Jack P. Chen") )
    (facts)

The Watch Command

    (watch <watch-item>)

    <watch-item> : facts, rules, activations, statistics, compilations, focus, all

    (watch facts)
    (modify 3 (age 25) )
    (facts)

The Deffacts Construct

    (deffacts <deffacts name> [<optional comment>]
              <facts>*)
                ; initially define a set of facts

    (reset)

        reset : 1. remove all activated rules from agenda
                2. remove all facts from the fact-list
                3. assert the facts from existing deffacts

The Components of A Rule

    (defrule <rule-name> [<optional comment>]
        <patterns>*           ;LHS
        =>
        <actions>*            ;RHS
        )

    (defrule fire-emergency "An example rule"       ;Rule header
        (emergency (type fire))                     ;Patterns
        =>
        (assert (response                           ;Actions
                    (action activate-sprinkler-system)))
        )

The Agenda and Execution

To list the list of rules on the agenda, use
    (agenda)

Rule and Refraction
    (run)
    (facts)
    (run)
    (facts)

To make the rule to fire again, use
    (refresh <rule-name>)

    (agenda)
    (refresh fire-emergency)
    (agenda)

Watching Activations, Rules, and Statistics
    (clear)   ; remove all facts, rules, and deffacts.

    (reset)
    (watch activations)
    (assert (emergency (type fire)))
    (agenda)
    (retract 1)
    (agenda)

    (reset)
    (watch rules)
    (assert (emergency (type fire)))
    (run)
    (agenda)

    (unwatch all)
    (watch statistics)
    (assert (emergency (type fire)))
    (run)

Commands for Manipulating Constructs

Displaying the List of Members of a Specified Construct
    (list-defrules)

    (list-deftemplates)

    (list-deffacts)

Displaying the Text Representation of a Specified Construct Member
    (ppdefrule <defrule-name>)

    (ppdeftemplate <deftemplate-name>)

    (ppdeffacts <deffacts-name>)

Deleting a Specified Construct Member
    (undefrule <defrule-name>)

    (undeftemplate <deftemplate-name>)

    (undeffacts <deffacts-name>)

The Printout Command

    (printout <logical-name> <print-items>*)

    (defrule fire-emergency
        (emergency (type fire))
        =>
        (printout t "action activate-sprinkler-system"
                    crlf))

Using Multiple Rules

Example:

  1. emergency.clp (Demo)
  2. A Simple Expert System: theater
    1. pseudo code
    2. theater.clp (Demo)

The set-break Command

    (set-break <rule-name>)

    (show-breaks)

    (remove-break <rule-name>)

Example

Loading and Saving Construct

Loading Constructs from a File
    (load "xmp.clp")

    (load "c:\\clips\\xmp.clp")       ;use \\ for backslash

Watching Compilations
    (watch compilations)
    (load "c:\\clips\\xmp.clp")

Saving Constructs to a File
    (save "xmp.clp")

Commenting Construct

    ; (emergency < type >)


Go to Clips Summary

Previous Clips Overview   Up TOC   Next Pattern Matching