Introduction to CLIPS

来源:百度文库 编辑:神马文学网 时间:2024/04/18 18:20:37
CS320: Introduction to Artificial Intelligence
Introduction to CLIPS
CLIPS is an Expert System development environment, developed at NASA and used in a large number of applications. CLIPS is freely available; you can download versions for Suns, Macs and PCs from the AI repository (see the 320 webpage for a link). CLIPS is written in C but has a syntax based on that of Lisp, which is one of the reasons for choosing to use it in this course.
For such a freely available system, CLIPS is surprisingly powerful, incorporating features such as its own OO language COOL (CLIPS Object Oriented Language) and foreign function interfacing. However, in this course we‘ll be using only a very small subset of features of the system. This tutorial introduces you to the basics of CLIPS, hopefully providing enough information for you to complete the assignment. More details on CLIPS is available from links on the 320 homepage. Also, don‘t hesitate to mail cavedon@goanna or post to the 320 newsgroup with CLIPS-related queries, and (hopefully!) your questions will be answered. You can also get (basic) on-line help from within CLIPS with the command (help): you need to grab the file /public/320/CLIPS/clips.hlp for this to work.
This tutorial requires you to work through it, providing some simple exercises along the way.
Starting and Exiting CLIPSFacts, the Fact-KB and Working MemoryFactsAdding and Deleting Facts; Resetting Working MemoryCreating a Fact KB
Defining Production RulesDefining Simple RulesRules Containing VariablesCreating a Rule KB; Loading FilesRetracting Facts in Rules
Running a Production SystemFurther Control in CLIPS: salience and haltingBuiltin Functions and Other Features
The CLIPS environment, including executables, can be found in the directory /public/320/CLIPS. To run CLIPS, execute the command clips in this directory; you may want to set an alias to the command /public/320/CLIPS/clips.
Once you have started CLIPS, you are in the CLIPS interpreter, which is similar to the Lisp interpreter. CLIPS evaluates expressions given to it, but it does not require quotes: symbols are just treated as symbols. Within the interpreter, you can assert new facts, define new rules, or run the Expert System you have defined. All these functions are described below.
To exit CLIPS, use the command ‘(exit)‘ (don‘t forget the parentheses!).
You should start CLIPS now.
FactsAdding and Deleting Facts; Resetting Working MemoryCreating a Fact KB
Facts in CLIPS are defined using a Lisp-like representation of first-order logic. For example, the atomic formula duck(Daffy), which might represent the information ``Daffy is a duck‘‘, is written as follows: (duck Daffy) Of course, a predicate can take multiple arguments. For example, likes(Bugs,Elmer), which might represent the information ``Bugs likes Elmer‘‘, is written (like Bugs Elmer)
Note that, unlike Lisp, CLIPS is case-sensitive so you need to take care that you are consistent in your use of upper and lower case: for example, the fact (duck Daffy) will not match the fact (duck daffy).
To add a fact to working memory, you use the command assert. This takes any number of facts as arguments and asserts each into the working memory of ``current facts‘‘. For example (assert (duck Daffy)) asserts the fact (duck Daffy) into working memory. Enter the above command now.
You can check the facts that are currently in working memory using the following command: (facts) Enter the above command now. You should see the previously entered fact listed, as well as a corresponding number for it (f-0). This number is used to refer to facts when we want to delete them.
You delete facts from working memory by using the command retract. To retract a fact, you must give the corresponding fact number. For example, the duck fact entered earlier is fact number 0 (f-0). To retract this fact, you use the following command: (retract 0) Enter this command now. If you use the facts command again, it should tell you that working memory is now empty.
Before running a new expert system problem, you will usually need to reset the working memory. This involves clearing the working memory and then loading facts from the Fact KB into it. To reset, use the command (reset) Enter this command now. You have not yet defined a Fact KB, but if you use the facts command, you‘ll see that there‘s a ``dummy‘‘ fact called (initial-fact) that has been added to working memory (this can be useful for triggering rules with no premises).
You can also clear working memory using the command (clear) Note that clearing leaves working memory completely empty: the Fact KB is not loaded after a clear!
To create a Fact KB, use the command deffacts. This command is similar to a Lisp defun (except that there is no argument list). deffacts requires a name and a list of facts. For example: (deffacts daffy-facts (duck Daffy) (black Daffy))So, daffy-facts is the name of a Fact KB about Daffy. You can type a deffacts directly into CLIPS. Do this now. If you use the facts command, you‘ll find that the above facts do not appear: that‘s because they‘ve been defined as part of the Fact KB but have not yet been added to working memory. Now try using the reset command: if you now use the facts command, you should find that the above facts have been added to working memory by the reset.
You can define Fact KBs at the CLIPS prompt, or you can create a file containing them. For example, if we put the above deffacts definition in a file called facts.clp, we can then load the file using the command (load "facts.clp") Again, the facts would not be added to the working memory until a reset.
There‘s nothing wrong with having multiple deffacts definitions in a single file: when the file is loaded, each Fact KB is created; when reset is executed, all facts are added to the working memory.
The central component of a production/expert system is the Rule KB.
Defining Simple RulesRules Containing VariablesCreating a Rule KB; Loading FilesRetracting Facts in Rules
In lectures, we saw that production rules are effectively logical rules of the following form: P1 & ... & Pn => action1 & ... & actionm where each action is either an assertion or a deletion.
In CLIPS, the implication symbol is written simply as ‘=>‘, i.e., a ‘=‘ followed by a ‘>‘. Since we are only allowed conjunctions, in both the premise and consequent, we don‘t need any other logical connectives.
To define a new rule, we use the command defrule. This is similar to deffacts: it requires a name for the rule, followed by a number of facts which form the premise, followed by the implication symbol, followed by the actions to be taken. For example, the information ``IF Daffy is talking and Daffy is angry THEN Daffy spits‘‘ can be represented by the following rule: (defrule daffy-spits (talking Daffy) (angry Daffy) => (assert (spits Daffy)))
The meaning of this rule is as follows: if every condition before the => is satisfied by working memory, then every action after the => is executed. In this case, it would result in a fact being added to working memory.
Notice that the consequent is an action: in this example it is an assert. You can‘t just have a fact listed in the consequent: all conditions in the consequent of a rule must be actions. There are other possible actions besides assert: we‘ll see examples later on.
Notice the indentation used above: each of the conditions is indented with respect to the defrule, while the implication symbol is not indented, allowing it to clearly stand out.
Most interesting rules contain variables. A variable in CLIPS is represented by putting a question mark in front of it: e.g., ?x ?duckare examples of variables.
A simple rule containing a variable is the following, which is meant to represent the information ``IF ?x is a duck THEN ?x quacks‘‘ (i.e., ``All ducks quack‘‘): ;; This rule defines ``All ducks quack‘‘ (defrule ducks-quack (duck ?x) ;; found a duck => (assert (quacks ?x))) ;; assert that it quacks
When the above rule is executed, it checks working memory for facts matching the conditions in the premise (before the arrow), which will probably involve unifying the variable ?x with some value (e.g., Daffy). When the actions are executed, ?x will have the value it matched, meaning that the asserted fact will most probably not contain a variable (e.g., (quacks Daffy) will be added to working memory).
Notice that comments are defined in the same way as for Lisp, using a ``;‘‘. You must, of course, add sensible documentation to any rules and facts you define (the above rule is probably over-documented).
We can add as many rules as we like in the CLIPS interpreter. However, it usually makes more sense to edit a file and then load it. For example, we may want to add the rule definition for ducks-quack to a file called rules.clp. We can then load that file using the command (load "rules.clp") Do this now: create a file, add the ducks-quack rule, and load it.
When we‘re in CLIPS, we can check the names of the rules which have been defined using the command (rules) This command just prints the name of the rules. Enter this command now; you should see that the rule ducks-quack is named.
To check the definition of a rule in CLIPS, we use the command ppdefrule (‘pp‘ stands for ``pretty print‘‘). For example, (ppdefrule ducks-quack) should print your rule in a nicely indented format. Do this now. Note that there is a MAIN symbol added to the rule name: CLIPS supports a form of package, allowing information hiding. By default, rules and facts are added to the MAIN package. We won‘t be worrying about packages.
If we want to retract a fact as the action of a rule, things become a little tricky since you have to pass the fact-number to the retract command. The way to do this is to set a variable to store the fact-number, and then passing this variable to retract. The built-in function <- is used to assign the fact-number to a variable.
For example, suppose we want to retract the information that something quacks if we know that that object is mute: IF x quacks AND x is mute THEN (retract the fact that x quacks) This can be represented by the following rule in CLIPS: (defrule retract-quacks ?q-fact <- (quacks ?x) (mute ?x) => (retract ?q-fact))This is a disciplined use of retract: it means we can‘t try to retract a fact that doesn‘t exist.
You may never need to use the above construct, but it is potentially very handy.
If you‘ve been following the instructions so far, then you‘ve created and loaded a Fact KB (containing some simple facts) and a Rule KB (containing a single rule). Make sure the rule is loaded by using the rules command.
Before running the system, you should reset it: use the facts command to check that the working memory contains (initial-fact), the facts of the Fact KB, and nothing else.
To run the system, use the following command: (run) This runs rules to completion using forward chaining and the Rete algorithm (you don‘t need to understand this algorithm).
After running, you should check working memory using the facts command. What new facts have been added? Are they what you expect?
Two useful facilities for controlling execution in CLIPS are salience and the halt command.
Salience allows the expert system designer to enforce a priority on CLIPS rules which is useful for resolving conflicts. The default conflict resolution mechanism in CLIPS is based on the order in which the rules are added to the rule database. For any rule, it can be given a salience, which is a numeric value indicating the rule‘s priority: the higher the salience value, the higher the priority. When there is a conflict (i.e. two rules can fire), then the one with the higher salience is selected.
Salience is defined as in the following example:
(defrule daffy-spits (declare (salience 20)) (talking Daffy) (angry Daffy) => (assert (spits Daffy)))
If the salience of a rule is not explicitly specified, then by default it is 0. Negative salience is allowed.
In general, the CLIPS forward-chaining algorithm keeps looping until no further rules fire--it does not stop when an answer is found. The (halt) command, when executed, simply halts the execution of the forward chaining. This is useful if you want to stop CLIPS after it has found the goal being queried:
(defrule answer-found ( ... ) ;; condition testing for answer being found => (halt))
Such a rule as the above should probably be given a high priority.
CLIPS has many builtin functions and commands that are useful for writing complex rules for Expert Systems. These include many of the functions which we saw in Lisp, including operations on numbers and boolean connectives.
If you want to use a boolean test in the LHS of a rule, using one of the built-in boolean functions, then you need to use the test command: (defrule demonstrate-test ?x <- ... ?y <- ... (test (eq ?x ?y)) => ...)Some of the built-in tests include eq and neq--equality and inequality tests for any types--and the usual numeric-type comparisons (=, <>, <=, <, >, >=).
You can also use the standard boolean connectives and, or and not in the LHS of a rule.
Feel free to mail cavedon@goanna, or post to the 320 newsgroup, to ask about specific functions. There will also be some additions to this section later on. Don‘t forget the on-line (help) command: you‘ll need to copy the file /public/320/CLIPS/clips.hlp to your home directory to get this to work.
Copyright   © 1996 Royal Melbourne Institution of Technology
Author :Lawrence Cavedon
Last Modified : Mon May 5 08:25:12 EST 1997_xyz