Intro to CLIPS Paula Matuszek CSC 8750, Fall, 2004 - PowerPoint PPT Presentation

1 / 62
About This Presentation
Title:

Intro to CLIPS Paula Matuszek CSC 8750, Fall, 2004

Description:

Intro to CLIPS Paula Matuszek CSC 8750, Fall, 2004 Based on presentation from COMP474/6741 Expert Systems Update: January 20, 2002 Concordia University – PowerPoint PPT presentation

Number of Views:47
Avg rating:3.0/5.0
Slides: 63
Provided by: cscVilla
Category:
Tags: clips | csc | fall | intro | matuszek | paula

less

Transcript and Presenter's Notes

Title: Intro to CLIPS Paula Matuszek CSC 8750, Fall, 2004


1
Intro to CLIPSPaula MatuszekCSC 8750, Fall,
2004
Based on presentation from COMP474/6741 Expert
Systems Update January 20, 2002Concordia
University A. Andreevskaia and S.
Mokhiv www.cs.concordia.ca/comp474/tutorial/clips
-intro.ppt
2
CLIPS History
  • CLIPS C Language Integrated Production System
  • Developed at NASAs Johnson Space Center in the
    mid-1980-s
  • C was used as implementation language
  • (because of restricted availability of LISP
    compilers and problems of integration LISP-code
    with non-LISP applications)
  • Initial version a production rule interpreter.
  • Now COOL CLIPS Object-Oriented Language
  • Also JESS Java Expert Systems Shell

3
Where does it fit?
  • Classical Rule-Based Expert Systems
  • CLIPS as a Rule-Based Language
  • CLIPS Components
  • Forward Chaining (CLIPS) vs. Backward Chaining
    (e.g. MYCIN and derivatives)

4
Why do we study CLIPS?
  • Advantages of CLIPS include
  • A top-level interpreter
  • Production rule interpreter
  • Runs on many platforms (portability) like
    different flavors of UNIX, Linux, Windows, MacOS
  • A public-domain well-documented software
  • Fast
  • Easy to embed in other applications
  • Easy to get started

5
Components of a Rule-Based Expert System
Knowledge Base (RULES)
Inference Engine
Working Memory (FACTS)
Agenda
Explanation Facility
Knowledge Acquisition Facility
UI
6
Versions of CLIPS
  • CLIPS is written in C gt CLIPS is portable gt
    there are different versions that run on
    different platforms mac, unix, windows.
  • From CD you can get version for Windows
  • Latest version 6.20 can be downloaded from the
    CLIPS web site.

7
Starting CLIPS
  • 1. To start text-based CLIPS interpreter under
    UNIX you type
  • clips
  • and
  • CLIPSgt
  • prompt will appear. You can try simple CLIPS
    commands. Remember - like LISP CLIPS requires all
    statements to be in ( ) otherwise, it will read
    them as simple string and just echo them. To exit
    CLIPS interpreter type (exit).

8
Summary of Basic CLIPS Commands
  • (exit) to exit from CLIPS
  • (clear) to clear the environment from
    facts, rules, and other active definitions
  • (reset) to set the fact base to its initial
    state (clears existing facts sets
    (initial-fact), and all (deffacts) constructs
    in the program). Perform (reset) before
    each program run!
  • (run) executes a program currently loaded into
    the CLIPS interpreter against currently defined
    rule- and fact-bases.

9
More Basic CLIPS Commands
  • (load filename.clp) to load a CLIPS program
    into the interpreter from the file named
    filename.clp . This also does syntax check and
    makes constructs in the file defined. In some
    cases you may omit quotes in the name.
  • (facts) to display a list of currently active
    facts in the fact base.
  • (rules) to display a set of rules currently in
    the rule base.

10
Comments in CLIPS
  • Program comments begin with a semicolon .
    Everything after on the same line until the end
    of line is ignored. This is an inline comment
    example
  • Construct comments are used as a part of the
    CLIPS constructs (e.g. deftemplate, defrule, etc)
    and follows the constructs name and enclosed in
    quotations.(defrule my-rule my
    comment (initial-fact) gt (printout t Hello
    crlf))

11
Few Simple Clips Commands
  • To assert a fact
  • (assert (first-fact asserted))
  • To define a rule
  • (defrule first_rule
  • (first-fact asserted)
  • gt
  • (assert (second-fact asserted)))

12
Hello World in CLIPS
  • (defrule start
  • (initial-fact)
  • gt
  • (printout t Hello, world! crlf))

13
To Make It Run
  • Type the code in a file, save it (e.g.
    hello-world.clp)
  • Start CLIPS (type clips or xclips)
  • Do File -gt Load (in XCLIPS) or type
  • (load hello-world.clp)
  • When the file is loaded CLIPS will display
  • (load hello-world.clp)
  • defining defrule start j
  • TRUE

14
To Make It Run
  • Type (reset)
  • Type (run)
  • Tip You can also use the menu
  • To exit CLIPS use the menu, Q or (exit)

15
Fields
  • There are seven data types (types of tokens)
    called fields in CLIPS.
  • float \- ltdigitgt .ltdigitgt
    eE-ltdigitgt
  • integer - ltdigitsgt
  • symbol lt ltchargt
  • string ltchargt (e.g. John, 848-3000)
  • external address
  • instance name
  • instance address

16
Word
  • a word CANNOT start with theselt ? - (
    )
  • a word CANNOT contain any of theselt ( )

17
Valid Expressions
  • Examples of valid words
  • fire
  • emergency-fire
  • activate_sprinkler_system
  • shut-down-electrical-junction-387
  • !?
  • CLIPS is case-sensitive
  • So fire FIRE Fire are all different

18
Valid Expressions
  • Examples of valid strings
  • Activate the sprinkler system.
  • Shut down electrical junction 387.
  • !?
  • lt-( ) -
  • Spaces act as delimiters to separate fields
  • These are different strings fire fire
    fire fire but would be the same with no
    quotes
  • Valid numbers
  • 1 1.5 .7 3 -1 65 3.5e10

19
Facts
  • Fact is a chunk of information consisting of
    relation name, (optional) slots and slot values.
    Example(person (name John))
  • In CLIPS facts are frame structures often defined
    using the (deftemplate) construct.

20
(deftemplate)
  • (deftemplate ltrelation_namegt ltcommentgt
  • ltslot-definitiongt
  • )
  • ltslot-definitiongt is
  • (slot ltslot-namegt)
  • (field ltslot-namegt)
  • (multislot ltslot-namegt)

21
Valid Facts
  • Examples of valid facts
  • (single-field)
  • (two fields)
  • (speed 38 mph)
  • (cost 78 dollars 23 cents)
  • (name John Doe)

22
(deftemplate) Example
  • (deftemplate person an example template
  • (multislot name)
  • (slot age)
  • (slot eye-color)
  • (slot hair-color))
  • CLIPSgt defining deftemplate person
  • TRUE

23
Facts
  • Deftemplate can also be implicit ordered
    facts (numbers 1 2 3)
  • To add facts (assert ltfactgt) (you can add more
    than one fact with the same (assert) command)
  • To remove facts (retract ltfact-indexgt)
  • To list facts (facts) will give facts
    identifiers like f-0 (not sequential)

24
Facts Example
  • CLIPSgt (deftemplate course electives
  • (slot number))
  • CLIPSgt (assert (course (number comp674))
  • (course (number comp672)))
  • ltFact-1gt
  • CLIPSgt (facts)
  • f-0 (course (number comp674))
  • f-1 (course (number comp672))
  • For a total of 2 facts
  • CLIPSgt (retract 1)
  • CLIPSgt (facts)
  • f-0 (course (number comp674))
  • For a total of 1 fact

25
Modifying Facts
  • To modify a fact
  • (modify ltfact-indexgt ltslot-modifiergt)
  • ltslot-modifiergt is (ltslot-namegt ltslot-valuegt)
  • Example
  • CLIPSgt (modify 0 (number comp675))
  • ltfact-2gt
  • CLIPSgt (facts)
  • f-2 (course (number comp675))
  • for a total of 1 fact

26
Duplicating Facts
  • To create a duplicate of a fact
  • Example (continued)
  • CLIPSgt (duplicate 2 (number comp775))
  • ltfact-3gt
  • CLIPSgt (facts)
  • f-2 (course (number comp675))
  • f-3 (course (number comp775))
  • For a total of 2 facts
  • Note (duplicate) modifies a fact without
    deleting (retracting) the original, whereas
    (modify) does.

27
Asserting a Group of Facts
  • To define groups of facts that represent the
    original (initial) knowledge use (deffacts).
    Facts from (deffacts) are asserted using (reset)
    (or on (load))
  • (deffacts ltdeffacts-namegt ltcommentgt ltfactsgt)
  • (reset)

28
Retracting Facts
  • Facts can be removed or retracted using
  • (retract ltfact-indexgt)
  • (retract 2)
  • Retract can be used for more than one fact
  • (retract 1 2)

29
(deftemplate) as a Record
  • Templates can be extended to hold more
    information like records(deftemplate
    ltdeftemplate-namegt ltoptional commentgt(slot
    ltslot-namegt (type ltdata-typegt) (default
    ltvaluegt))
  • Example
  • (deftemplate student a student record(slot
    name (type STRING))(slot age (type NUMBER)
    (default 18)))

30
(deftemplate) Example
  • After the template declaration and adding
  • (deffacts student-Ids(student (name
    Tarzan))(student (name Jane) (age 19)))
  • The result is
  • (student (name Tarzan) (age 18))(student (name
    Jane) (age 19))

31
(deftemplate) Summary
  • Look at the templates as to user-defined types of
    facts. In a template you can have several slots
    (or fields), on which you can operate separately
    or all at the same time. Think of it as a sort of
    object.
  • This allows you to group multiple pieces of
    information of a given fact in one structure,
    which is described by the defftemplate construct,
    and the facts are instances of it.

32
Rules (1)
  • LHS gt RHS
  • Syntax
  • (defrule ltrule-namegt ltcommentgt
    ltdeclarationgt salience
  • ltpatternsgt LHS, premises, patterns,
    conditions, antecedent
  • gt
  • ltactionsgt) RHS, actions, consequent

33
Rules (2)
  • Example
  • (defrule class-A-fire-emergency (emergency
    fire)gt (printout t FIRE!!! crlf))
  • Rules can have more than one pattern/premise(de
    frule class-B-fire-emergency (emergency
    fire) (fire-class B)gt (printout t Use carbon
    dioxide extinguisher crlf))

34
Agenda
  • If the pattern(s) in the LHS of the rule match
    asserted facts, the rule is activated and put on
    the agenda.
  • Rules are ordered on the agenda according to
    their salience (read priority).
  • When the agenda is empty the program stops.
  • Refraction each rule is fired only once for a
    specific set of facts gt use (refresh)

35
Salience
  • Normally the agenda acts like a stack.
  • The most recent activation placed on the agenda
    is the first rule to fire.
  • Salience allows more important rules to stay at
    the top of the agenda regardless of when they
    were added.
  • If you do not explicitly say, CLIPS will assume
    the rule has a salience of 0.

36
Conflict Resolution Strategies
  • Recency
  • Rules which use more recent data are preferred.
  • CLIPS time-tags WM elements
  • Specificity
  • Rules with more conditions are preferred to more
    general rules that are easier to satisfy.
  • Good if dealing with general rules with specific
    rules for exceptions
  • Refractoriness
  • A rule should not be allowed to fire more than
    once for the same data.
  • Prevents loops
  • Used in CLIPS (need (refresh) )

37
Conflict Resolution in CLIPS
  • First, CLIPS uses salience to sort the rules.
    Then it uses the other strategies to sort rules
    with equal salience.
  • CLIPS uses refraction, recency specificity in
    the form of following 7 strategies
  • The depth strategy
  • The breadth strategy
  • The simplicity strategy
  • The complexity strategy
  • The LEX strategy
  • The MEA strategy
  • It is possible also to set strategy to random
  • Syntax (set-strategy ltstrategygt)

38
Variables
  • Variable name is made of ? and one or more
    characters
  • Example
  • (course (number ?cmp))
  • Variables are used for
  • Pattern matching
  • I/O
  • As pointers to facts (fact indices)

39
Variables Examples
  • (defrule grandfather (is-a-grandfather
    ?name)gt (assert (is-a-man ?name)))
  • (defrule grandfather (is-a-grandfather ?name)
    gt (assert (is-a-father ?name)) (assert
    (is-a-man ?name)) (printout t ?name is a
    grandfather crlf))

40
Fact Address
  • To remove a fact from the fact-list use (retract)
  • Before a fact can be retracted it must be
    specified to CLIPS by its index.
  • Rules can be used to modify the fact base. To
    achieve it variables have to be bound to fact
    addresses using lt-?num lt- (course (number
    ?cmp))
  • This appears in the LHS of the rule, and can be
    referred to in either LHS and RHS.

41
Wildcards (1)
  • To specify a general pattern we can use
  • Single field variables wildcard ?
  • Multifield variables wildcard ?
  • (courses (numbers ?course_nums))
  • (printout t Your courses are ?course_nums
    crlf))
  • (list ? ? c ?)
  • can match these
  • (list a c e), (list a d c b)
  • but not these
  • (list c), (list c d), (list a c d b)

42
Wildcards (2)
  • The fact
  • (do carwash on Sunday)
  • will match any of the following
  • (do ? ? Sunday)
  • (do ? on ?)
  • (do ? on ?when)
  • (do ? )
  • (do ? Sunday)
  • (do ?chore ?when)

43
Retracting Facts Using Wildcards
  • (defrule change-grandfather-fact
  • ?old-fact lt- (is-a-grandfather ?name)
  • gt
  • (retract ?old-fact)
  • (assert (has-a-grandchild ?name)
  • (is-a-man ?name))
  • )

44
Retracting Facts Using Wildcards (2)
  • You can retract several facts(retract ?fact1
    ?fact2 ?fact3)
  • Or you can retract all of them at once(retract
    )

45
Standard I/O
  • To print to STDOUT (printout t )
  • For the new line use crlf
  • To read from STDIN into a field use(read)


46
Standard I/O Examples (1)
  • Keyboard input example
  • (defrule to-start Rule to start enter a name
  • (phase choose-name)
  • gt
  • (printout t Enter your name crlf)
  • (assert (your-name (read)))) is optional
  • Another example using a variable(defrule
    to-start
  • gt
  • (printout t Enter something )
  • (bind ?something (read))
  • (printout t You have entered ?something crlf))

47
Standard I/O Examples (2)
  • A slightly more advanced example(defrule
    continue-check
    ?phase lt- (phase
    check-continue)gt (retract ?phase) (printout t
    Do you want to continue? crlf) (bind ?answer
    (read)) (if (or (eq ?answer yes) (eq ?answer
    y)) then (assert (phase continue))
    else (halt)))

48
File I/O
  • File I/O
  • (load ltfilenamegt)
  • (save ltfilenamegt)
  • NOTE use \\ in paths if you trying to do it on
    Windows or / will always work.

49
To Display Constructs
  • To display constructs
  • (list-defrules)
  • (list-deftemplates)
  • (list-deffacts)
  • To display the text of definitions of the
    constructs
  • (ppdefrule ltdefrule-namegt)
  • (ppdeftemplate ltdeftemplate-namegt)
  • (ppdeffeacts ltdeffacts-namegt)

50
To Delete Constructs
  • To undefine a given construct
  • (undefrule ltdefrule-namegt)
  • (undeftemplate ltdeftemplate-namegt)
  • (undeffacts ltdeffacts-namegt)

51
Field Constraints
  • NOT (number comp672)
  • OR (number comp672comp674)
  • AND
  • (number ?course_n comp674comp675)
  • Variable ?course_n will be bound to both
  • (number ?course_n comp674 comp672)
  • Variable ?course_n will be bound to none of the
    two

52
Field Constraints Examples
  • NOT
  • (defrule person-without-brown-hair (person
    ?name ? brown)gt (printout t ?name does not
    have brown hair crlf))
  • OR
  • (defrule black-or-brown-hair (person ?name ?
    brownblack)gt (printout t ?name has dark
    hair crlf))
  • AND
  • (defrule black-or-brown-hair (person ?name ?
    ?colourbrownblack)gt (printout t ?name has
    ?colour hair crlf))

53
Math
  • CLIPS maths expressions are written in the prefix
    format, just like in LISP or Scheme
  • ( 2 3) evaluates to 5
  • Operators are addition, - subtraction,
    multiplication, / division,
    exponentiation
  • ( 2 ( 3 4)) evaluates to 14
  • ( ( 2 3) 4) evaluates to 20
  • (evaluation is from the inside out)

54
Pattern Logical OR (1)
  • (defrule shut-off-electricity-1 (emergency
    flood)gt (printout t Shut off the electricity
    crlf))
  • (defrule shut-off-electricity-2 (fire-class C)

    gt
  • (printout t Shut off the electricity crlf))
  • (defrule shut-off-electricity-3 (sprinkler-syste
    ms active) gt

    (printout t Shut off the
    electricity crlf))

55
Pattern Logical OR (2)
  • The three previous rules can be replaced by one
    rule if OR is used
  • (defrule shut-off-electricity
  • (or (emergency flood)
  • (fire-class C)
  • (sprinkler-systems active))gt (printout t
    Shut off the electricity crlf))

56
Pattern Logical AND
  • The default situation
  • It requires that all the patterns of the LHS of
    the rule to be matched to facts in order to
    trigger the rule.

57
Pattern Logical NOT
  • The logical NOT can only be used to negate a
    single pattern(defrule no-emergency (report-st
    atus) (not (emergency ?))gt (printout t No
    emergency being handled crlf))

58
Conditional Elements
  • AND is implicit
  • NOT and OR should be used explicitly
  • EXISTS conditional element
  • FORALL conditional element

59
Test Control Pattern
  • Control pattern can be used in the LHS to test a
    condition, which happens not to be fact in the
    fact base, but rather something else.
  • General syntax
  • (test ltpredicate-functiongt)
  • Example
  • (test (gt ?size 1))

60
Predicate Functions
  • The predicate functions are used to return a
    value of either true or false - and, not, or
  • eq equal, neq not equal
  • (eq ltany-valuegt ltany-valuegt)
  • equal, ! not equal, gt greater than or equal,
  • gt greater than, lt less than or equal, lt less
    than These are used for numeric values.
  • (lt ltnumeric-valuegtltnumeric-valuegt)
  • These are used to test the type of a field
    numberp, stringp, wordp, integerp, evenp, oddp

61
Debugging
  • Watch them!
  • (watch rules)
  • (watch facts)
  • Make save output to the file
  • To start logging (dribble-on output.log)
  • To stop (dribble-off)

62
Infinite Loop orHow To Shoot Oneself in the Foot
  • You can get into an infinite loop if you are not
    careful enough.
  • (defrule simple-loop ?old-fact lt- (loop-fact)
    gt (printout
    t Looping! crlf) (retract ?oldfact) (assert
    (loop-fact)))
  • Use Control-C (or another interrupt command) to
    get break out of the loop.
Write a Comment
User Comments (0)
About PowerShow.com