home *** CD-ROM | disk | FTP | other *** search
- CLIPS User's Guide
-
- by
-
- Joseph C. Giarratano, Ph.d
-
- Artificial Intelligence Section
- NASA--Johnson Space Center
-
-
-
- Chapter 1 Just The Facts
-
-
-
- In this chapter, you'll learn the basic concepts of an expert system. You'll
- also see how to insert and remove facts in CLIPS.
-
-
- Introduction
-
-
- CLIPS is a type of computer language designed for writing applications in
- Expert Systems. The basic elements of CLIPS are:
-
- 1. fact-list: global memory for data
- 2. production memory: contains all the rules or productions.
- 3. inference engine: controls overall production execution
-
- A program written in CLIPS consists of rules and facts. The inference
- engine decides which rules should be executed. We'll discuss the parts
- of CLIPS in more detail in later chapters.
- Right now, let's take a look now at the facts of CLIPS. Facts are very
- important because execution in CLIPS cannot proceed without facts.
- This is one example in which CLIPS is different from procedural languages
- such as Pascal, Ada, BASIC, FORTRAN and C. In procedural languages,
- execution can proceed without facts. That is, the statements are sufficient
- in those languages to cause exection. However, in CLIPS, facts
- are required to cause the execution of rules and so play a very important role.
-
-
- The Beginning And The End
-
-
- To begin CLIPS, just enter the appropriate run command for your system. You
- should see the CLIPS prompt appear as follows.
-
- CLIPS>
-
- At this point, you can start entering commands to CLIPS.
- The normal mode of leaving CLIPS is with the exit command. Just type
-
- (exit)
-
- in response to the CLIPS prompt and then press the Return key.
-
-
- Making A List
-
-
- As with other programming languages, CLIPS recognizes certain keywords. For
- example, if you want to put data in the fact-list, you can use the
- assert command.
- As an example of assert, enter the following right after the CLIPS prompt,
- as shown.
-
- CLIPS>(assert (duck))
-
- After every line, always press the Return key to send the line to CLIPS. The
- above command will put the fact "duck" in fact-memory.
- Notice that the assert command and the (duck) fact are surrounded by
- parentheses. CLIPS, like many other expert system languages, has a LISP-like
- syntax which requires that all statements be surrounded by parentheses. Even
- though CLIPS is not written in LISP, the style of LISP has influenced
- the development of CLIPS. This is also why the exit command requires
- parentheses.
-
-
- And Checking It Twice
-
-
- Suppose you want to see what's in the fact-list. Just use the facts command.
- Enter (facts) in response to the CLIPS prompt and CLIPS will respond
- with a list of the facts in fact-memory. Be sure to put parentheses around
- the command or CLIPS will not accept it. The results of the (facts)
- command in this example should be
-
- f-1 (duck)
-
- The term "f-1" is the fact identifier put in by CLIPS. Every fact that is
- inserted into the fact-list is assigned a unique fact identifier
- starting with the letter f and followed by an integer called the fact-index.
- What happens if you try to put a second duck into the fact-list? Let's try
- it and see. Enter a new (duck) and then issue a (facts) command.
- You'll see just the original "f-1 (duck)". This shows that CLIPS will not
- accept a duplicate entry of a fact.
- Of course, you can put other facts in that are different. For example,
- enter a (quack) fact and then issue a (facts) command. You'll see
-
- f-1 (duck)
- f-2 (quack)
-
- As you can see, the (quack) fact is in the fact-list. An important point to
- realize is that identifiers in the fact-list may not be strictly
- sequential. You'll see in a later section that just as you can add facts to
- the fact-list, there is also a way to remove facts. As facts are
- removed from the fact-list, the deleted fact identifiers will be missing in
- the fact-list. So the fact-identifiers may not be stricly sequential
- as your program executes.
-
-
- Sensitive Atoms
-
-
- A fact such as (duck) or (quack) is said to consist of a single atom. In
- LISP-like languages, an atom is the smallest unit of meaning and cannot
- be split up into smaller pieces. In languages such as Pascal, Ada, C, BASIC
- and others, a fact such as (duck) could be split up into individual
- characters or groups of characters. However, (duck) cannot be split up any
- further in CLIPS because it is an atom.
- A symbolic atom is a type of atom starting with an alphabetic character and
- optionally followed by letters, numbers, and dashes. For example,
- all the words in this sentence are atoms. Another type of atom is the literal
- atom. A literal atom must begin and end with double quotes.
- The quotes are part of the atom. There can be zero or more characters
- between the double quotes. The third type of atom is the numeric atom,
- which represents a floating point number. All numbers in CLIPS are treated as
- floating point. A string can be either a symbolic or literal
- atom.
- A fact consists of one or more atoms, enclosed in matching left and right
- parentheses. Multiple atoms must be separated by one or more spaces.
- For example, enter the following command.
- (assert (The duck said "Quack."))
-
- and then issue a (facts) command. You'll see the group of atoms (The duck said
- "Quack.") asserted as a single fact since only one identifier
- is assigned.
- What happens if you use multiple spaces with atoms? Assert
-
- ( duck quack quack )
-
- and then check the fact-list. You'll see
-
- (duck quack quack)
-
- because CLIPS has eliminated unnecessary spaces around atoms.
- Also, notice that CLIPS preserved the upper-case and lower-case letters in
- the atoms of the fact. That is, the T of The and the Q of Quack
- are left as upper-case. CLIPS is said to be case-sensitive because it
- distinguishes between upper- and lower-case letters. As an example, assert
- the facts (duck) and (Duck) and then issue a (facts) command. You'll see that
- CLIPS allows you to assert (duck) and (Duck) because CLIPS is
- case-sensitive.
-
-
- Getting Spaced Out
-
-
- Since spaces are used to separate multiple atoms, it follows that spaces cannot
- be simply included in facts. For example, assert the facts (duck),
- (duck ), ( duck) and ( duck ). You will only see (duck) since CLIPS
- ignores spaces as part of the atom and considers all these facts as
- equivalent to (duck).
- If you want to include spaces in a fact, you must use double quotes. For
- example, assert
-
- ("duck")
- ("duck ")
- (" duck")
- (" duck ")
-
- and check the fact-list with a (facts) command. You'll see that all of these
- facts have been put in the fact-list.
- What if you want to include the double quotes themselves in a fact? At
- first, you might think you could quote the quotes. Let's try it and
- see what happens. Assert the fact
-
- (""duck"")
-
- and check the fact-list. You'll see a fact
-
- ("" duck "")
-
- While this fact resembles the assertion, it is actually quite different.
- Although the double quotes did appear in the fact, they are not
- around the duck fact anymore. The clue to this is the space before and after
- the duck fact that was put in by CLIPS. Notice that the asserted
- fact did not have spaces around duck. The spaces were put around duck because
- CLIPS thinks you are asserting the three atoms
-
- ""
- duck
- ""
-
- and so CLIPS puts spaces around duck to separate the three atoms.
- The correct way to put double quotes in a fact is with the backslash
- operator, \. For example, assert the fact
-
- ("\"duck\"")
-
- and check the fact-list. Now you'll see the correct fact
-
- (""duck"")
-
- with no spaces added by CLIPS.
-
-
- Retract That Fact
-
-
- Now that you know how to put facts into the fact-list, it's time to learn how
- to remove facts. Removing facts from the fact-list is called retraction
- and is done with the retract command. To retract a fact, you must specify the
- fact index of the fact as the argument of retract. For example,
- if your fact-list is
-
- f-1 (duck)
- f-3 (quack)
- f-4 (The duck said "Quack.")
-
- To remove the (quack), enter the command
-
- (retract 3)
-
- and then check the fact-list with a (facts) command. You'll see that (quack)
- has been retracted. To retract (duck), enter
-
- (retract 1)
-
- and to retract (The duck said "Quack."), enter
-
- (retract 4)
-
- Notice that to retract a fact you must specify the fact index and not the
- position of the fact in the fact-list. A potential error you can make
- is trying to retract the first fact in the fact-list with a (retract 1)
- command. This will only work correctly if the first fact has a fact
- index of 1.
- What happens if you try to retract a fact that isn't there? Enter a (retract
- 4) command again and CLIPS will respond with an error message
-
- fact 4 does not exist
-
- The moral is that you can't take back what you haven't given.
-
-
- Clearing Up The Facts
-
-
- If you want to retract specific facts, the (retract) command is very
- convenient. However, there will be times that you want to retract all the
- facts in the fact-list. Rather than your having to type in the fact index of
- each command, you can use the clear command. Just enter
-
- (clear)
-
- and all the facts in the fact-list will be removed.
- The (clear) restores the original state of CLIPS just as if you first
- started up CLIPS. To see this, assert (duck) and then check the fact-list.
- Notice that (duck) has a fact-identifier of f-1 because the (clear) command
- reset the fact identifiers. The (clear) command actually does
- more than just remove facts. For example, it also removes all the rules, as
- you'll see later. As you learn more about CLIPS, you'll understand
- more of what the (clear) command does.
- .PA
- Problem
-
-
- Are the following valid or invalid facts? Check to see if you're right by
- trying to assert them. If valid, identify the type of atoms each
- fact contains. Note that on some of the examples, CLIPS will hang up until
- you provide the necessary characters such as closing quotation marks
- or parentheses. If you can't figure out the correct characters, you'll have
- to break off execution of CLIPS with a Control C command and then
- restart CLIPS.
-
- (t)
- (this)
- (this is a test)
- (but what is th!!!)
- (**!!)
- (and so he ate 19 hamburgers)
- (1 2 3 4.5 -.0012)
- (A1)
- (start-fact)
- (the---end)
- (^&)
- (duck")
- ("")
- (John loves Mary)
- (" ")
- (1000 Main Street)
- (1000 Main St.)
- ("duck)
- ()
- (100 -25.5)
- (1e10 10000)
- (())
- (
- )(
- (duck (quack))
-
-
-