home *** CD-ROM | disk | FTP | other *** search
/ Crawly Crypt Collection 2 / crawlyvol2.bin / program / compiler / clips / clip_doc / clips1.man < prev    next >
Encoding:
Text File  |  1993-09-01  |  10.6 KB  |  304 lines

  1.                        CLIPS User's Guide
  2.  
  3.                                by
  4.  
  5.                     Joseph C. Giarratano, Ph.d
  6.  
  7.                   Artificial Intelligence Section
  8.                     NASA--Johnson Space Center
  9.  
  10.  
  11.  
  12.                    Chapter 1   Just The Facts
  13.  
  14.  
  15.  
  16. In this chapter, you'll learn the basic concepts of an expert system.  You'll 
  17. also see how to insert and remove facts in CLIPS.
  18.  
  19.  
  20. Introduction
  21.  
  22.  
  23. CLIPS is a type of computer language designed for writing applications in 
  24. Expert Systems.  The basic elements of CLIPS are:
  25.  
  26.    1. fact-list: global memory for data
  27.    2. production memory: contains all the rules or productions.
  28.    3. inference engine: controls overall production execution
  29.  
  30.    A program written in CLIPS consists of rules and facts.  The inference 
  31. engine decides which rules should be executed.  We'll discuss the parts 
  32.  of CLIPS in more detail in later chapters.
  33.    Right now, let's take a look now at the facts of CLIPS.  Facts are very 
  34. important because execution in CLIPS cannot proceed without facts. 
  35.   This is one example in which CLIPS is different from procedural languages 
  36. such as Pascal, Ada, BASIC, FORTRAN and C.  In procedural languages, 
  37.  execution can proceed without facts.  That is, the statements are sufficient 
  38. in those languages to cause exection.  However, in CLIPS, facts 
  39.  are required to cause the execution of rules and so play a very important role.
  40.  
  41.  
  42. The Beginning And The End
  43.  
  44.  
  45. To begin CLIPS, just enter the appropriate run command for your system.  You 
  46. should see the CLIPS prompt appear as follows.
  47.  
  48. CLIPS>
  49.  
  50. At this point, you can start entering commands to CLIPS.
  51.    The normal mode of leaving CLIPS is with the exit command.  Just type
  52.  
  53. (exit)
  54.  
  55. in response to the CLIPS prompt and then press the Return key.
  56.  
  57.  
  58. Making A List
  59.  
  60.  
  61. As with other programming languages, CLIPS recognizes certain keywords.  For 
  62. example, if you want to put data in the fact-list, you can use the 
  63.  assert command.
  64.    As an example of assert, enter the following right after the CLIPS prompt, 
  65. as shown.
  66.  
  67. CLIPS>(assert (duck))
  68.  
  69. After every line, always press the Return key to send the line to CLIPS.  The 
  70. above command will put the fact "duck" in fact-memory.
  71.    Notice that the assert command and the (duck) fact are surrounded by 
  72. parentheses.  CLIPS, like many other expert system languages, has a LISP-like 
  73.  syntax which requires that all statements be surrounded by parentheses.  Even 
  74. though CLIPS is not written in LISP, the style of LISP has influenced 
  75.  the development of CLIPS.  This is also why the exit command requires 
  76. parentheses.
  77.  
  78.  
  79. And Checking It Twice
  80.  
  81.  
  82. Suppose you want to see what's in the fact-list.  Just use the facts command.  
  83. Enter (facts) in response to the CLIPS prompt and CLIPS will respond 
  84.  with a list of the facts in fact-memory.  Be sure to put parentheses around 
  85. the command or CLIPS will not accept it.  The results of the (facts) 
  86.  command in this example should be
  87.  
  88. f-1    (duck)
  89.  
  90.    The term "f-1" is the fact identifier put in by CLIPS.  Every fact that is 
  91. inserted into the fact-list is assigned a unique fact identifier 
  92.  starting with the letter f and followed by an integer called the fact-index.
  93.    What happens if you try to put a second duck into the fact-list?   Let's try 
  94. it and see.  Enter a new (duck) and then issue a (facts) command. 
  95.   You'll see just the original "f-1    (duck)".  This shows that CLIPS will not 
  96. accept a duplicate entry of a fact.
  97.    Of course, you can put other facts in that are different.  For example, 
  98. enter a (quack) fact and then issue a (facts) command.  You'll see
  99.  
  100. f-1    (duck)
  101. f-2    (quack)
  102.  
  103.    As you can see, the (quack) fact is in the fact-list.  An important point to 
  104. realize is that identifiers in the fact-list may not be strictly 
  105.  sequential.  You'll see in a later section that just as you can add facts to 
  106. the fact-list, there is also a way to remove facts.  As facts are 
  107.  removed from the fact-list, the deleted fact identifiers will be missing in 
  108. the fact-list.  So the fact-identifiers may not be stricly sequential 
  109.  as your program executes.
  110.  
  111.  
  112. Sensitive Atoms
  113.  
  114.  
  115. A fact such as (duck) or (quack) is said to consist of a single atom.  In 
  116. LISP-like languages, an atom is the smallest unit of meaning and cannot 
  117.  be split up into smaller pieces.  In languages such as Pascal, Ada, C, BASIC 
  118. and others, a fact such as (duck) could be split up into individual 
  119.  characters or groups of characters.  However, (duck) cannot be split up any 
  120. further in CLIPS because it is an atom.
  121.    A symbolic atom is a type of atom starting with an alphabetic character and 
  122. optionally followed by letters, numbers, and dashes.  For example, 
  123.  all the words in this sentence are atoms.  Another type of atom is the literal 
  124. atom.  A literal atom must begin and end with double quotes. 
  125.   The quotes are part of the atom.  There can be zero or more characters 
  126. between the double quotes.  The third type of atom is the numeric atom, 
  127.  which represents a floating point number.  All numbers in CLIPS are treated as 
  128. floating point.  A string can be either a symbolic or literal 
  129.  atom.
  130.    A fact consists of one or more atoms, enclosed in matching left and right 
  131. parentheses.  Multiple atoms must be separated by one or more spaces. 
  132.   For example, enter the following command.
  133. (assert (The duck said "Quack."))
  134.  
  135. and then issue a (facts) command.  You'll see the group of atoms (The duck said 
  136. "Quack.") asserted as a single fact since only one identifier 
  137.  is assigned.
  138.    What happens if you use multiple spaces with atoms?  Assert
  139.  
  140. (   duck   quack   quack   )
  141.  
  142. and then check the fact-list.  You'll see
  143.  
  144. (duck quack quack) 
  145.  
  146. because CLIPS has eliminated unnecessary spaces around atoms.
  147.    Also, notice that CLIPS preserved the upper-case and lower-case letters in 
  148. the atoms of the fact.  That is, the T of The and the Q of Quack 
  149.  are left as upper-case.  CLIPS is said to be case-sensitive because it 
  150. distinguishes between upper- and lower-case letters. As an example, assert 
  151.  the facts (duck) and (Duck) and then issue a (facts) command.  You'll see that 
  152. CLIPS allows you to assert (duck) and (Duck) because CLIPS is 
  153.  case-sensitive.
  154.  
  155.  
  156. Getting Spaced Out
  157.  
  158.  
  159. Since spaces are used to separate multiple atoms, it follows that spaces cannot 
  160. be simply included in facts.  For example, assert the facts (duck), 
  161.  (duck ), ( duck) and (   duck   ).  You will only see (duck) since CLIPS 
  162. ignores spaces as part of the atom and considers all these facts as 
  163.  equivalent to (duck).
  164.    If you want to include spaces in a fact, you must use double quotes.  For 
  165. example, assert
  166.  
  167. ("duck")
  168. ("duck ")
  169. (" duck")
  170. (" duck ")
  171.  
  172. and check the fact-list with a (facts) command.  You'll see that all of these 
  173. facts have been put in the fact-list.
  174.    What if you want to include the double quotes themselves in a fact? At 
  175. first, you might think you could quote the quotes.  Let's try it and 
  176.  see what happens.  Assert the fact
  177.  
  178. (""duck"")
  179.  
  180. and check the fact-list.  You'll see a fact
  181.  
  182. ("" duck "")
  183.  
  184.    While this fact resembles the assertion, it is actually quite different.  
  185. Although the double quotes did appear in the fact, they are not 
  186.  around the duck fact anymore.  The clue to this is the space before and after 
  187. the duck fact that was put in by CLIPS.  Notice that the asserted 
  188.  fact did not have spaces around duck.  The spaces were put around duck because 
  189. CLIPS thinks you are asserting the three atoms  
  190.  
  191. ""
  192. duck
  193. ""
  194.  
  195. and so CLIPS puts spaces around duck to separate the three atoms.
  196.    The correct way to put double quotes in a fact is with the backslash 
  197. operator, \.  For example, assert the fact
  198.  
  199. ("\"duck\"")
  200.  
  201. and check the fact-list.  Now you'll see the correct fact
  202.  
  203. (""duck"")
  204.  
  205. with no spaces added by CLIPS.
  206.  
  207.  
  208. Retract That Fact
  209.  
  210.  
  211. Now that you know how to put facts into the fact-list, it's time to learn how 
  212. to remove facts.  Removing facts from the fact-list is called retraction 
  213.  and is done with the retract command.  To retract a fact, you must specify the 
  214. fact index of the fact as the argument of retract.  For example, 
  215.  if your fact-list is
  216.  
  217. f-1    (duck)
  218. f-3    (quack)
  219. f-4    (The duck said "Quack.")
  220.  
  221.    To remove the (quack), enter the command
  222.  
  223. (retract 3)
  224.  
  225. and then check the fact-list with a (facts) command.  You'll see that (quack) 
  226. has been retracted.  To retract (duck), enter
  227.  
  228. (retract 1)
  229.  
  230. and to retract (The duck said "Quack."), enter
  231.  
  232. (retract 4)
  233.  
  234. Notice that to retract a fact you must specify the fact index and not the 
  235. position of the fact in the fact-list.  A potential error you can make 
  236.  is trying to retract the first fact in the fact-list with a (retract 1) 
  237. command.  This will only work correctly if the first fact has a fact 
  238.  index of 1.
  239.    What happens if you try to retract a fact that isn't there? Enter a (retract 
  240. 4) command again and CLIPS will respond with an error message
  241.  
  242. fact 4 does not exist
  243.  
  244. The moral is that you can't take back what you haven't given.
  245.  
  246.  
  247. Clearing Up The Facts
  248.  
  249.  
  250. If you want to retract specific facts, the (retract) command is very 
  251. convenient.  However, there will be times that you want to retract all the 
  252.  facts in the fact-list.  Rather than your having to type in the fact index of 
  253. each command, you can use the clear command.  Just enter
  254.  
  255. (clear)
  256.  
  257. and all the facts in the fact-list will be removed.
  258.    The (clear) restores the original state of CLIPS just as if you first 
  259. started up CLIPS.  To see this, assert (duck) and then check the fact-list. 
  260.   Notice that (duck) has a fact-identifier of f-1 because the (clear) command 
  261. reset the fact identifiers.     The (clear) command actually does 
  262.  more than just remove facts.  For example, it also removes all the rules, as 
  263. you'll see later.  As you learn more about CLIPS, you'll understand 
  264.  more of what the (clear) command does.
  265. .PA
  266.                              Problem
  267.  
  268.  
  269.    Are the following valid or invalid facts?  Check to see if you're right by 
  270. trying to assert them.  If valid, identify the type of atoms each 
  271.  fact contains.  Note that on some of the examples, CLIPS will hang up until 
  272. you provide the necessary characters such as closing quotation marks 
  273.  or parentheses.  If you can't figure out the correct characters, you'll have 
  274. to break off execution of CLIPS with a Control C command and then 
  275.  restart CLIPS.
  276.  
  277. (t)
  278. (this)
  279. (this is a test)
  280. (but what is th!!!)
  281. (**!!)
  282. (and so he ate 19 hamburgers)
  283. (1 2 3 4.5 -.0012)
  284. (A1)
  285. (start-fact)
  286. (the---end)
  287. (^&)
  288. (duck")
  289. ("")
  290. (John loves Mary)
  291. (" ")
  292. (1000 Main Street)
  293. (1000 Main St.)
  294. ("duck)
  295. ()
  296. (100 -25.5)
  297. (1e10 10000)
  298. (())
  299. (
  300. )(
  301. (duck (quack))
  302.  
  303.  
  304.