1 01/12/2011Knowledge-Based Systems, Paula Matuszek More Intro to CLIPS Paula Matuszek CSC 9010, Spring, 2011.

Slides:



Advertisements
Similar presentations
Expert systems CLIPS Seyed Hashem Davarpanah
Advertisements

10/4/2003COMP 474/674 Fall 2003 Michelle Khalife1 Conflict Resolution in CLIPS COMP 474/674 FALL 2003 Michelle Khalifé.
Introduction to CLIPS COMP 474/674 FALL 2003 Michelle Khalifé.
The CLIPS Programming Tool History of CLIPS –Influenced by OPS5 and ART –Implemented in C for efficiency and portability –Developed by NASA, distributed.
Expert System Shells - Examples
Conflict Resolution  what to do if there is more than 1 matching rule in each inference cycle? match WM with LHS of rules select one rule (conflict resolution)
Samad Paydar Ferdowsi University of Mashhad.  C Language Integrated Production System (CLIPS)  A tool for building expert systems  An expert system.
1 01/12/2011Knowledge-Based Systems, Paula Matuszek Intro to CLIPS Paula Matuszek CSC 9010, Spring, 2011.
November 2, 2004AI: CLIPS Language Tutorial1 Artificial Intelligence CLIPS Language Tutorial Michael Scherger Department of Computer Science Kent State.
Chapter 8 Pattern Matching
© C. Kemke CLIPS 1 1 COMP 4200: Expert Systems Dr. Christel Kemke Department of Computer Science University of Manitoba.
Introduction to CLIPS (Lecture Note #17)
Chapter 7: Introduction to CLIPS
CLIPS C Language Integrated Production System Note: Some slides and/or pictures are adapted from Lecture slides / Books of Dr Zafar Alvi.
Artificial Intelligence Lecture No. 18 Dr. Asad Ali Safi ​ Assistant Professor, Department of Computer Science, COMSATS Institute of Information Technology.
Lecture 2 Introduction to C Programming
Introduction to C Programming
Introduction to CLIPS (Chapter 7) Fact List (contains data) Knowledge Base (contains rules) Inference Engine (controls execution)
Expert Systems 3 1 Rewriting Rules, CLIPS Content of this lecture: 1.Interpretation of rules 2.Example: CLIPS Sorting 3.Non-determinism 4.Control regimes:
© C. Kemke CLIPS 1 1 COMP 4200: Expert Systems Dr. Christel Kemke Department of Computer Science University of Manitoba.
© C. Kemke Control 1 COMP 4200: Expert Systems Dr. Christel Kemke Department of Computer Science University of Manitoba.
Chapter 8: Advanced Pattern Matching Expert Systems: Principles and Programming, Fourth Edition.
Chapter 9: Modular Design, Execution Control, and Rule Efficiency Expert Systems: Principles and Programming, Fourth Edition.
Introduction to Jess.
CS 561, Session 25 1 Introduction to CLIPS Overview of CLIPS Facts Rules Rule firing Control techniques Example.
Fundamentals of Python: From First Programs Through Data Structures
Shell Scripting Awk (part1) Awk Programming Language standard unix language that is geared for text processing and creating formatted reports but it.
Advanced Web 2012 Lecture 4 Sean Costain PHP Sean Costain 2012 What is PHP? PHP is a widely-used general-purpose scripting language that is especially.
Chapter 9: Modular Design, Execution Control, and Rule Efficiency Expert Systems: Principles and Programming, Fourth Edition.
Expert System Topic 2.
Chapter 7: Introduction to CLIPS Expert Systems: Principles and Programming, Fourth Edition.
Artificial Intelligence Lecture No. 17 Dr. Asad Ali Safi ​ Assistant Professor, Department of Computer Science, COMSATS Institute of Information Technology.
Chapter 10: Procedural Programming Expert Systems: Principles and Programming, Fourth Edition.
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
Chapter 7: Introduction to CLIPS Presented By: Farnaz Ronaghi.
IMSS013 CLIPS. 2 Background CLIPS is an expert system tool developed by the Software Technology Branch (STB), NASA/Lyndon B. Johnson Space Center. First.
CMP-MX21: Lecture 4 Selections Steve Hordley. Overview 1. The if-else selection in JAVA 2. More useful JAVA operators 4. Other selection constructs in.
Expert Systems Chapter 7 Introduction to CLIPS Entering and Exiting CLIPS A> CLIPS  CLIPS (V6.5 09/01/97) CLIPS> exit exit CLIPS> (+ 3 4)  7 CLIPS>
Advanced Pattern Matching. Field constraints Used to restrict the values of a field on LHS of a rule Used to restrict the values of a field on LHS of.
JavaScript, Fourth Edition
Artificial Intelligence Lecture No. 26 Dr. Asad Ali Safi ​ Assistant Professor, Department of Computer Science, COMSATS Institute of Information Technology.
Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals.
Artificial Intelligence Lecture No. 24 Dr. Asad Ali Safi ​ Assistant Professor, Department of Computer Science, COMSATS Institute of Information Technology.
Introduction to CLIPS. Expert Systems: Principles and Programming, Fourth Edition2 What is CLIPS? CLIPS is a multiparadigm programming language that provides.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Introduction to Perl. What is Perl Perl is an interpreted language. This means you run it through an interpreter, not a compiler. Similar to shell script.
XP New Perspectives on XML, 2 nd Edition Tutorial 7 1 TUTORIAL 7 CREATING A COMPUTATIONAL STYLESHEET.
Artificial Intelligence Lecture No. 19 Dr. Asad Ali Safi ​ Assistant Professor, Department of Computer Science, COMSATS Institute of Information Technology.
Artificial Intelligence Lecture No. 23 Dr. Asad Ali Safi ​ Assistant Professor, Department of Computer Science, COMSATS Institute of Information Technology.
Programming & Debugging. Key Programming Issues Modularity Modifiability Ease of Use Fail-safe programming Style Debugging.
Mostly adopted from Jason Morris notes (Morris Technical Solutions)
Artificial Intelligence Lecture No. 22 Dr. Asad Ali Safi ​ Assistant Professor, Department of Computer Science, COMSATS Institute of Information Technology.
XML Schema – XSLT Week 8 Web site:
The CLIPS Expert System Shell Dr Nicholas Gibbins
Quiz 3 Topics Functions – using and writing. Lists: –operators used with lists. –keywords used with lists. –BIF’s used with lists. –list methods. Loops.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
Linux Administration Working with the BASH Shell.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Summary for final exam Agent System..
Introduction to CLIPS 2 Session 13 Course: T0273 – EXPERT SYSTEMS Year: 2014.
Jörg Kewisch, June 10, 2013, LILUG Meeting CLIPS C Language Integrated Production System Developed at the Software Development Branch, NASA Lyndon B. Johnson.
Intelligent Systems JESS constructs.
Chapter 7: Introduction to CLIPS
MATLAB: Structures and File I/O
Chapter 8: Advanced Pattern Matching
بسم الله الرحمن الرحیم آموزش نرم افزار CLIPS
T. Jumana Abu Shmais – AOU - Riyadh
Chapter 11: Classes, Instances, and Message-Handlers
CPE/CSC 481: Knowledge-Based Systems
Presentation transcript:

1 01/12/2011Knowledge-Based Systems, Paula Matuszek More Intro to CLIPS Paula Matuszek CSC 9010, Spring, 2011

2 Knowledge-Based Systems, Paula Matuszek 01/12/2011 CLIPS Facts  Facts are what CLIPS believes to be true.  The simplest form of a fact is a single string. (snowing) (“January 11”)  An ordered fact is a list of one or more strings: (snowing “January 11”)  Our lab exercise last week showed that we really want some additional organization in our facts

3 Knowledge-Based Systems, Paula Matuszek 01/12/2011 Frames  The underlying representation of a fact in CLIPS is a frame with slots: Relation name  Slot name Slot value  Example: (class (number 8520) (day Monday (prerequisite 8310) )

4 Knowledge-Based Systems, Paula Matuszek 01/12/2011 (deftemplate)  The frame structures is defined using the (deftemplate) construct.  ( deftemplate [ ]  * )  is:  (slot )  (field )  (multislot ) (allows more than one value)

5 Knowledge-Based Systems, Paula Matuszek 01/12/2011 (deftemplate) Example  (deftemplate class “an example template”  (slot number)  (multislot prerequisite)  (multislot day)  (slot time)  ) 

6 Knowledge-Based Systems, Paula Matuszek 01/12/2011 Matching Fact for Deftemplate  (class “an example fact”  (number CSC8520 number)  (prerequisite CSC8301)  (day Monday Wednesday)  (time “2:00”)  ) 

7 Knowledge-Based Systems, Paula Matuszek 01/12/2011 Another Example  CLIPS> (deftemplate class “electives”  (slot number))  CLIPS> (assert (class (number csc8520))  (class (number csc8301)))   CLIPS> (facts)  f-0 (class (number csc8520))  f-1 (class (number csc8301))  For a total of 2 facts  CLIPS> (retract 1)  CLIPS> (facts)  f-0 (class (number csc8520))  For a total of 1 fact

8 Knowledge-Based Systems, Paula Matuszek 01/12/2011 Comments in CLIPS (a brief aside)  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 construct’s name and enclosed in quotations. (defrule my-rule “my comment” (initial-fact) => (printout t “Hello” crlf) )

9 Knowledge-Based Systems, Paula Matuszek 01/12/2011 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 [ ] * )  (reset)  These correspond to our static information.  Must have a matching (deftemplate) declared first in the file or buffer.

10 Knowledge-Based Systems, Paula Matuszek 01/12/2011 Deffacts Example  If we have the deftemplate  (deftemplate class “an example template”  (slot number)  (multislot prerequisite)  And the deffacts (deffacts classes (class (number CSC8520) (prereq 8301)) (class (number CSC8301))  The result of (reset) is:  (class (number CSC8520) (prereq 8301)) (class (number CSC8301))

11 Knowledge-Based Systems, Paula Matuszek 01/12/2011 (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.  This makes it easy to set up all the static information at the beginning of a run.

12 Knowledge-Based Systems, Paula Matuszek 01/12/2011 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)

13 Knowledge-Based Systems, Paula Matuszek 01/12/2011 Variables Examples  (defrule grandfather (is-a-grandfather ?name) => (assert (is-a-man ?name)) )  (defrule grandfather (is-a-grandfather ?name) => (assert (is-a-father ?name)) (assert (is-a-man ?name)) (printout t ?name “ is a grandfather” crlf) )

14 Knowledge-Based Systems, Paula Matuszek 01/12/2011 Giving a Value to a Variable  Variables in CLIPS are bound or instantiated.  They get a value when they are part of a fact matched on the LHS; their scope is within that rule.  You normally do not directly assign a value to a variable, and once it has a value you normally do not change it within that rule. If you must do so, there is a bind command, but if you’re using it often you’re probably thinking procedurally, a case of “You can write a C program in any language”.  defglobal can be used to create a global variable with (reset) or (load).

15 Knowledge-Based Systems, Paula Matuszek 01/12/2011 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 ‘ <- ’: ?num <- (class (number ?cmp))  This appears in the LHS of the rule, and can be referred to in either LHS and RHS.

16 Knowledge-Based Systems, Paula Matuszek 01/12/2011 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)

17 Knowledge-Based Systems, Paula Matuszek 01/12/2011 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 )

18 Knowledge-Based Systems, Paula Matuszek 01/12/2011 Retracting Facts Using Wildcards (defrule change-grandfather-fact ?old-fact <- (is-a-grandfather ?name) => (retract ?old-fact) (assert (has-a-grandchild ?name) (is-a-man ?name)) )

19 Knowledge-Based Systems, Paula Matuszek 01/12/2011 Retracting Facts Using Wildcards (2)  You can retract several facts: (retract ?fact1 ?fact2 ?fact3)  Or you can retract all of them at once: (retract *)

20 Knowledge-Based Systems, Paula Matuszek 01/12/2011 Standard I/O  To print to STDOUT: (printout t …)  For the new line use: crlf  To read from STDIN into a field use: (read)

21 Knowledge-Based Systems, Paula Matuszek 01/12/2011 Standard I/O Examples (1)  Keyboard input example: (defrule to-start “Rule to start & enter a name” (phase choose-name) => (printout t “Enter your name” crlf) (assert (your-name =(read)))) ; ’=’ is optional  Another example using a variable: (defrule to-start => (printout t “Enter something: ”) (bind ?something (read)) (printout t “You have entered ” ?something crlf))

22 Knowledge-Based Systems, Paula Matuszek 01/12/2011 Standard I/O Examples (2)  A slightly more advanced example: (defrule continue-check ?phase (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) ) )

23 Knowledge-Based Systems, Paula Matuszek 01/12/2011 File I/O  File I/O (load ) (save )  NOTE: use \\ in paths if you trying to do it on Windows; or / will always work.

24 Knowledge-Based Systems, Paula Matuszek 01/12/2011 To Display Constructs  To display constructs:  (list-defrules)  (list-deftemplates)  (list-deffacts)  To display the text of definitions of the constructs:  (ppdefrule )  (ppdeftemplate )  (ppdeffeacts )

25 Knowledge-Based Systems, Paula Matuszek 01/12/2011 To Delete Constructs  To ”undefine“ a given construct:  (undefrule )  (undeftemplate )  (undeffacts )

26 Knowledge-Based Systems, Paula Matuszek 01/12/2011 Field Constraints  These apply to the value of a field  NOT ~ (number ~ comp672)  OR | (number comp672 | comp674)  AND & (number ?course_n & comp674 | comp675)  Variable ?course_n will be bound to both (number ?course_n & ~ comp674 & ~ comp672)  Variable ?course_n will be bound to none of the two

27 Knowledge-Based Systems, Paula Matuszek 01/12/2011 Field Constraints Examples  NOT  (defrule person-without-brown-hair (person ?name ? ~ brown) => (printout t ?name “ does not have brown hair” crlf))  OR  (defrule black-or-brown-hair (person ?name ? brown | black) => (printout t ?name “ has dark hair” crlf))  AND  (defrule black-or-brown-hair (person ?name ? ?colour & brown | black) => (printout t ?name “ has” ?colour “ hair” crlf))

28 Knowledge-Based Systems, Paula Matuszek 01/12/2011 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)

29 Knowledge-Based Systems, Paula Matuszek 01/12/2011 Math Example  With:  (defrule addition  (numbers ?x ?y) =>  (assert (sumis (+ ?x ?y))))  CLIPS> (assert(numbers 1 2))  ==> f-0 (numbers 1 2)   CLIPS> Loading Selection...  Defining defrule: addition +j  ==> Activation 0 addition: f-0  CLIPS> (run)  FIRE 1 addition: f-0  ==> f-1 (sumis 3)

30 Knowledge-Based Systems, Paula Matuszek 01/12/2011 Pattern Logical OR (1)  These apply to the truth of an entire pattern  (defrule shut-off-electricity-1 (emergency flood) => (printout t “Shut off the electricity” crlf))  (defrule shut-off-electricity-2 (fire-class C) =>  (printout t “Shut off the electricity” crlf))  (defrule shut-off-electricity-3 (sprinkler-systems active) => (printout t “Shut off the electricity” crlf))

31 Knowledge-Based Systems, Paula Matuszek 01/12/2011 Pattern Logical OR (2)  The OR pattern will match if any of its component patterns is true.  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) ) => (printout t “Shut off the electricity” crlf))

32 Knowledge-Based Systems, Paula Matuszek 01/12/2011 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 activate the rule.

33 Knowledge-Based Systems, Paula Matuszek 01/12/2011 Pattern Logical NOT  The logical NOT can only be used to negate a single pattern: (defrule no-emergency (report-status) (not (emergency ?) ) => (printout t “No emergency being handled” crlf))

34 Knowledge-Based Systems, Paula Matuszek 01/12/2011 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 )  Example:  (test (> ?size 1))

35 Knowledge-Based Systems, Paula Matuszek 01/12/2011 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 )  = equal, != not equal, >= greater than or equal,  > greater than, <= less than or equal, < less than These are used for numeric values. ( )  These are used to test the type of a field: numberp, stringp, wordp, integerp, evenp, oddp

36 Knowledge-Based Systems, Paula Matuszek 01/12/2011 Debugging  “Watch them!” (watch rules) (watch facts)  Make save output to the file: To start logging: (dribble-on “output.log”) To stop: (dribble-off)

37 Knowledge-Based Systems, Paula Matuszek 01/12/2011 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 => use (refresh)

38 Knowledge-Based Systems, Paula Matuszek 01/12/2011 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.

39 Knowledge-Based Systems, Paula Matuszek 01/12/2011 Conflict Resolution Strategies n Recency Rules which use more recent data are preferred. CLIPS time-tags WM elements n 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 n Refractoriness A rule should not be allowed to fire more than once for the same data. Prevents loops Used in CLIPS (need (refresh) )

40 Knowledge-Based Systems, Paula Matuszek 01/12/2011 Conflict Resolution in CLIPS  Salience first.  Other strategies to sort rules with equal salience.  CLIPS uses refraction, recency & specificity: 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 )

41 Knowledge-Based Systems, Paula Matuszek 01/12/2011 Where to find more  This has been a BRIEF overview  User’s Guide is a readable intro, worth skimming. Lots of examples and discussion of style.  Basic Programming Guide Vol 1 is an excellent reference manual  The remaining documentation is mostly more detailed than we need.  There’s a decent set of introductory tutorials at utorial/tableOfContents.htm

42 Knowledge-Based Systems, Paula Matuszek 01/12/2011 Infinite Loop or How To Shoot Oneself in the Foot  You can get into an infinite loop if you are not careful enough. (defrule simple-loop ?old-fact (printout t “Looping!” crlf) (retract ?oldfact) (assert (loop-fact)))  Use Control-C (or another interrupt command) to get break out of the loop.