CPECSC 481 KnowledgeBased Systems Dr Franz J Kurfess
CPE/CSC 481: Knowledge-Based Systems Dr. Franz J. Kurfess Computer Science Department Cal Poly © 2002 -05 Franz J. Kurfess CLIPS 1
Course Overview u Introduction u CLIPS u Overview Concepts, Notation, Usage u Knowledge u and Inference Predicate Logic, Inference Methods, Resolution u Reasoning u Representation Semantic Nets, Frames, Logic u Reasoning u u Pattern with Uncertainty Probability, Bayesian Decision Making © 2002 -05 Franz J. Kurfess u Variables, Functions, Expressions, Constraints u Expert u Matching System Design ES Life Cycle u Expert System Implementation u Salience, Rete Algorithm u Expert System Examples u Conclusions and Outlook CLIPS 2
Overview CLIPS u Motivation u Variables u Objectives Matching u CLIPS u u u History Main ideas Terminology u Facts u u Background and Rules Formats Using Facts and Rules © 2002 -05 Franz J. Kurfess u u Variables Pattern Matching u Execution u u and Pattern of Programs Invoking and leaving CLIPS Watching u Important Concepts and Terms u Chapter Summary CLIPS 3
Logistics u u Introductions Course Materials u u textbooks (see below) lecture notes u u u handouts Web page u u u Power. Point Slides will be available on my Web page http: //www. csc. calpoly. edu/~fkurfess Term Project Lab and Homework Assignments Exams Grading © 2002 -05 Franz J. Kurfess CLIPS 4
Bridge-In © 2002 -05 Franz J. Kurfess CLIPS 5
Pre-Test © 2002 -05 Franz J. Kurfess CLIPS 6
Motivation u CLIPS is a decent example of an expert system shell u rule-based, forward-chaining system u it illustrates many of the concepts and methods used in other ES shells u it allows the representation of knowledge, and its use for solving suitable problems © 2002 -05 Franz J. Kurfess CLIPS 7
Objectives u be familiar with the important concepts and methods used in rule-based ES shells u facts, rules, pattern matching, agenda, working memory, forward chaining u understand u u the fundamental workings of an ES shell knowledge representation reasoning u apply rule-based techniques to simple examples u evaluate the suitability of rule-based systems for specific tasks dealing with knowledge © 2002 -05 Franz J. Kurfess CLIPS 8
Introduction u CLIPS u stands for C Language Implementation Production System u forward-chaining u starting from the facts, a solution is developed u pattern-matching u Rete matching algorithm: find ``fitting'' rules and facts u knowledge-based u empty system shell tool, to be filled with knowledge u multi-paradigm u rule-based, © 2002 -05 Franz J. Kurfess programming language object-oriented (Cool) and procedural CLIPS 10
The CLIPS Programming Tool u history of CLIPS u influenced by OPS 5 and ART u implemented in C for efficiency and portability u developed by NASA, distributed & supported by COSMIC u runs on PC, Mac, UNIX, VAX VMS u CLIPS provides mechanisms for expert systems ua top-level interpreter u production rule interpreter u object oriented programming language u LISP-like procedural language © 2002 -05 Franz J. Kurfess [Jackson 1999] CLIPS 11
Components of CLIPS u rule-based language u can create a fact list u can create a rule set u an inference engine matches facts against rules u object-oriented language (COOL) u can define classes u can create different sets of instances u special forms allow you to interface rules and objects © 2002 -05 Franz J. Kurfess [Jackson 1999] CLIPS 12
Notation u symbols, characters, keywords u u u square brackets [. . . ] u u u replace with zero or more instances of the type <char>* plus + u u u replace contents by an instance of that type (example <char>) star * u u contents are optional: (example [test]) pointed brackets (less than / greater than signs) <. . . > u u entered exactly as shown: (example) replace with one or more instances of the type <char>+ (is equivalent to <char>* ) vertical bar | u u choice among a set of items: true | false © 2002 -05 Franz J. Kurfess CLIPS 13
Tokens and Fields u tokens u groups of characters with special meaning for CLIPS, u e. g. ( ) separated by delimiters v (space, tab, Carriage Return, . . . ) u fields u particularly important group of tokens u CLIPS primitive data types v float, integer, symbol, string, external address, instance name, instance address © 2002 -05 Franz J. Kurfess CLIPS 14
CLIPS Primitive Data Types u float: decimal point (1. 5) or exponential notation (3. 7 e 10) u integer: [sign] <digit>+ u symbol: <printable ASCII character>+ v e. g. this-is-a-symbol, wrzlbrmft, !? @*+ u string: v u address of external data structure returned by user-defined functions instance name (used with Cool) v u e. g. "This is a string" external address v u delimited by double quotes delimited by square brackets instance address (used with Cool) v return values from functions © 2002 -05 Franz J. Kurfess CLIPS 15
Invoke / Exit CLIPS u entering CLIPS double-click on icon, or type program name (CLIPS) system prompt appears: CLIPS> u exiting CLIPS at the system prompt CLIPS> type (exit) u Note: enclosing parentheses are important; they indicate a command to be executed, not just a symbol © 2002 -05 Franz J. Kurfess CLIPS 16
Facts u elementary information items (“chunks”) u relation name u u symbolic field used to access the information often serves as identifier for the fact u slots u (zero or more) symbolic fields with associated values u deftemplate u construct used to define the structure of a fact v names and number of slots u deffacts u used to define initial groups of facts © 2002 -05 Franz J. Kurfess CLIPS 17
Examples of Facts u ordered fact (person-name Franz J. Kurfess) u deftemplate fact (deftemplate (slot © 2002 -05 Franz J. Kurfess person "deftemplate example” name) age) eye-color) hair-color)) CLIPS 18
Defining Facts u Facts can be asserted CLIPS> (assert (today is sunday)) <Fact-0> u Facts can be listed CLIPS> (facts) f-0 (today is sunday) u Facts can be retracted CLIPS> (retract 0) CLIPS> (facts) © 2002 -05 Franz J. Kurfess [Jackson 1999] CLIPS 19
Instances u an instance of a fact is created by (assert (person (name "Franz J. Kurfess") (age 46) (eye-color brown) (hair-color brown)) ) © 2002 -05 Franz J. Kurfess CLIPS 20
Initial Facts (deffacts kurfesses "some members of the Kurfess family" (person (name "Franz J. Kurfess") (age 46) (eye-color brown) (hair-color brown)) (person (name "Hubert Kurfess") (age 44) (eye-color blue) (hair-color blond)) (person (name "Bernhard Kurfess") (age 41) (eye-color blue) (hair-color blond)) (person (name "Heinrich Kurfess") (age 38) (eye-color brown) (hair-color blond)) (person (name "Irmgard Kurfess") (age 37) (eye-color green) (hair-color blond)) ) © 2002 -05 Franz J. Kurfess CLIPS 21
Usage of Facts u adding facts u u deleting facts u u <fact-index>+) (modify <fact-index> (<slot-name> <slot-value>)+ ) v retracts the original fact and asserts a new, modified fact duplicating facts u u (retract modifying facts u u (assert <fact>+) (duplicate <fact-index> (<slot-name> <slot-value>)+ ) v adds a new, possibly modified fact inspection of facts u u (facts) v prints the list of facts (watch facts) v automatically displays changes to the fact list © 2002 -05 Franz J. Kurfess CLIPS 22
Rules u general format (defrule <rule name> ["comment"] <patterns>* ; left-hand side (LHS) ; or antecedent of the rule => <actions>*) ; right-hand side (RHS) ; or consequent of the rule © 2002 -05 Franz J. Kurfess CLIPS 23
Rule Components u rule header u defrule u rule keyword, name of the rule, optional comment string antecedent (LHS) u patterns u rule to be matched against facts arrow u separates u rule antecedent and consequent (RHS) u actions to be performed when the rule fires © 2002 -05 Franz J. Kurfess CLIPS 24
Examples of Rules u simple rule (defrule birthday-FJK (person (name "Franz J. Kurfess") (age 46) (eye-color brown) (hair-color brown)) (date-today April-13 -02) => (printout t "Happy birthday, Franz!") (modify 1 (age 47)) ) © 2002 -05 Franz J. Kurfess CLIPS 25
Properties of Simple Rules u very limited: u LHS must match facts exactly u facts must be accessed through their index number u changes must be stated explicitly u can be enhanced through the use of variables © 2002 -05 Franz J. Kurfess CLIPS 26
Variables, Operators, Functions u variables u symbolic u variable name beginning with a question mark "? " bindings variables in a rule pattern (LHS) are bound to the corresponding values in the fact, and then can be used on the RHS v all occurrences of a variable in a rule have the same value v the left-most occurrence in the LHS determines the value v bindings are valid only within one rule v u access to facts variables can be used to make access to facts more convenient: ? age <- (age harry 17) v © 2002 -05 Franz J. Kurfess CLIPS 27
Wildcards u question mark ? u matches u multi-field u matches any single field within a fact wildcard $? zero or more fields in a fact © 2002 -05 Franz J. Kurfess CLIPS 28
Field Constraints u not constraint ~ u the u field can take any value except the one specified or constraint | u specifies u alternative values, one of which must match and constraint & u the value of the field must match all specified values u mostly used to place constraints on the binding of a variable © 2002 -05 Franz J. Kurfess CLIPS 29
Mathematical Operators u basic operators (+, -, *, /) and many functions (trigonometric, logarithmic, exponential) are supported u prefix notation u no built-in precedence, only left-to-right and parentheses u test feature u evaluates an expression in the LHS instead of matching a pattern against a fact u pattern u u connectives multiple patterns in the LHS are implicitly AND-connected patterns can also be explicitly connected via AND, OR, NOT u user-defined u u functions external functions written in C or other languages can be integrated Jess is tightly integrated with Java © 2002 -05 Franz J. Kurfess CLIPS 30
Examples of Rules u more complex rule (defrule find-blue-eyes (person (name ? name) (eye-color blue)) => (printout t ? name " has blue eyes. " crlf)) © 2002 -05 Franz J. Kurfess CLIPS 31
Example Rule with Field Constraints (defrule silly-eye-hair-match (person (name ? name 1) (eye-color ? eyes 1&blue|green) (hair-color ? hair 1&~black)) (person (name ? name 2&~? name 1) (eye-color ? eyes 2&~eyes 1) (hair-color ? hair 2&red|hair 1)) => (printout t ? name 1 " has "? eyes 1 " eyes and " ? hair 1 " hair. " crlf) (printout t ? name 2 " has "? eyes 2 " eyes and " ? hair 2 " hair. " crlf)) © 2002 -05 Franz J. Kurfess CLIPS 32
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 (name ? name) (age ? age)) => (printout t name? “ is “ ? age) ) © 2002 -05 Franz J. Kurfess [Jackson 1999] CLIPS 33
An Example CLIPS Rule (defrule sunday “Things to do on Sunday” (salience 0) ; salience in the interval [-10000, (today is Sunday) (weather is sunny) => (assert (chore wash car)) (assert (chore chop wood)) ) © 2002 -05 Franz J. Kurfess [Jackson 1999] 10000] CLIPS 34
Getting the Rules Started u 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”) ) © 2002 -05 Franz J. Kurfess [Jackson 1999] CLIPS 35
Variables & Pattern Matching u Variables make rules more applicable (defrule pick-a-chore (today is ? day) (chore is ? job) => (assert (do ? job on ? day)) ) u if conditions are matched, then bindings are used © 2002 -05 Franz J. Kurfess [Jackson 1999] CLIPS 36
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) ) ua variable must be assigned to the item for retraction © 2002 -05 Franz J. Kurfess [Jackson 1999] CLIPS 37
Pattern Matching Details u one-to-one matching (do ? job on ? day) (do washing on monday) u use (do (do (do of wild cards ? ? monday) ? on ? ) ? ? ? day) $? monday) ? chore $? when) © 2002 -05 Franz J. Kurfess [Jackson 1999] CLIPS 38
Defining Functions in CLIPS u Uses a LISP or Scheme-like syntax (deffunction-name (arg. . . arg) action. . . action) (deffunction hypotenuse (? a ? b) (sqrt (+ (* ? a) (* ? b)))) (deffunction initialize () (clear) (assert (today is sunday))) © 2002 -05 Franz J. Kurfess [Jackson 1999] CLIPS 39
Defining Classes & Instances u defining the class CAR (defclass car (is-a user) (name) (made-by)) u defining an instance of CAR (make-instance corvette of car (made-by chevrolet)) © 2002 -05 Franz J. Kurfess [Jackson 1999] CLIPS 40
Concrete & Abstract Classes u some classes only exist for inheritance purposes Person Man Woman Jack Jill © 2002 -05 Franz J. Kurfess [Jackson 1999] CLIPS 41
Managing Instances u Commands to display instances CLIPS> (instances) [corvette] of car CLIPS> (send [corvette] print) [corvette] of car (made-by chevrolet) u Command to group instances (in a file) (definstances (corvette of car (made-by chevrolet)) (thunderbird of car (made-by ford))) © 2002 -05 Franz J. Kurfess [Jackson 1999] CLIPS 42
Clearing & Resetting Instances u deleting an instance CLIPS> (send [corvette] delete) u deleting all instances CLIPS> (unmake-instance *) u resetting creates an initial object CLIPS> (reset) CLIPS> (instances) [initial-object] of INITIAL-OBJECT © 2002 -05 Franz J. Kurfess [Jackson 1999] CLIPS 43
Manipulation of Constructs u show list of constructs (list-defrules), (list-deftemplates), (list-deffacts) v u show prints a list of the respective constructs text of constructs (ppdefrule <defrule-name>), (ppdeftemplate <deftemplate-name>), (ppdeffacts <deffacts-name>) v displays the text of the construct (``pretty print'') u deleting constructs (undefrule <defrule-name>), (undeftemplate <deftemplate-name>), (undeffacts <deffacts-name>) v deletes the construct (if it is not in use) u clearing the CLIPS environment (clear) v removes all constructs and adds the initial facts to the CLIPS environment © 2002 -05 Franz J. Kurfess CLIPS 44
Input / Output u print information (printout <logical-device> <print-items>*) v logical device frequently is the standard output device t (terminal) u terminal input (read [<logical-device>]), (readline [<logical-device>]) v read an atom or string from a logical device v the logical device can be a file which must be open u open / close file (open <file-name> <file-ID> [<mode>]), (close [<file-ID>]) v open /close file with <file-id> as internal name u load / save constructs from / to file (load <file-name>), (save <file-name>) v backslash is a special character and must be ``quoted'' (preceded by a backslash ) v e. g. (load "B: \clips\example. clp") © 2002 -05 Franz J. Kurfess CLIPS 45
Program Execution u agenda u if all patterns of a rule match with facts, it is put on the agenda u (agenda) displays all activated rules u salience u indicates priority of rules u refraction u rules v fire only once for a specific set of facts prevents infinite loops u (refresh v <rule-name>) reactivates rules © 2002 -05 Franz J. Kurfess CLIPS 46
Execution of a Program u (reset) prepares (re)start of a program: u all previous facts are deleted u initial facts are asserted u rules matching these facts are put on the agenda u (run [<limit>]) starts the execution u breakpoints u (set-break [<rule-name>]) stops the execution before the rule fires, v continue with (run) v u (remove-break [<rule-name>]) u (show-breaks) © 2002 -05 Franz J. Kurfess CLIPS 47
Watching u watching the execution <watch-item>) prints messages about activities concerning a <watch-item> u (watch v (facts, rules, activations, statistics, compilation, focus, all) u (unwatch v <watch-item>) turns the messages off © 2002 -05 Franz J. Kurfess CLIPS 48
Watching Facts, Rules and Activations u facts u assertions u of (add) and retractions (delete) facts u rules u message for each rule that is fired u activations u activated rules: matching antecedents u these rules are on the agenda © 2002 -05 Franz J. Kurfess CLIPS 49
More Watching. . . u statistics u information about the program execution u (number of rules fired, run time, . . . ) u compilation u shows (default) information for constructs loaded by (load) Defining deftemplate: . . . v Defining defrule: . . . +j=j v +j, =j indicates the internal structure of the compiled rules v » +j » =j v important join added join shared for the efficiency of the Rete pattern matching network u focus u used with modules u indicates which module is currently active © 2002 -05 Franz J. Kurfess CLIPS 50
User Interface u menu-based version u most relevant commands are available through windows and menus u command-line interface u all commands must be entered at the prompt u (don’t forget enclosing parentheses) © 2002 -05 Franz J. Kurfess CLIPS 51
Limitations of CLIPS u single level rule sets u in LOOPS, you could arrange rule sets in a hierarchy, embedding one rule set inside another, etc u loose coupling of rules and objects u rules can communicate with objects via message passing u rules cannot easily be embedded in objects, as in Centaur u CLIPS has no explicit agenda mechanism u the basic control flow is forward chaining u to implement other kinds of reasoning you have to manipulate tokens in working memory © 2002 -05 Franz J. Kurfess [Jackson 1999] CLIPS 52
Alternatives to CLIPS u JESS u see below u Eclipse u u u 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 d. Base new extension RETE++ can generate C++ header files u NEXPERT u u 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 © 2002 -05 Franz J. Kurfess [Jackson 1999] CLIPS 53
JESS u JESS stands for Java Expert System Shell u it uses the same syntax and a large majority of the features of CLIPS u tight integration with Java u can be invoked easily from Java programs u can utilize object-oriented aspects of Java u some incompatibilities with CLIPS u COOL replaced by Java classes u a few missing constructs v more and more added as new versions of JESS are released © 2002 -05 Franz J. Kurfess CLIPS 54
Post-Test © 2002 -05 Franz J. Kurfess CLIPS 55
CLIPS Summary u notation u u facts u u (printout. . . ), (read. . . ), (load. . . ) program execution u u advanced pattern matching input/output u u (defrule. . . ), agenda variables, operators, functions u u (deftemplate), (deffacts), assert / retract rules u u similar to Lisp, regular expressions (reset), (run), breakpoints user interface u command line or GUI © 2002 -05 Franz J. Kurfess CLIPS 57
Important Concepts and Terms u u u u agenda antecedent assert backward chaining consequent CLIPS expert system shell fact field forward chaining function inference mechanism instance If-Then rules JESS © 2002 -05 Franz J. Kurfess u u u knowledge base knowledge representation pattern matching refraction retract rule header salience template variable wild card CLIPS 58
Summary CLIPS © 2002 -05 Franz J. Kurfess CLIPS 59
© 2002 -05 Franz J. Kurfess CLIPS 60
- Slides: 58