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

  1.            Chapter 2   Following The Rules
  2.  
  3.  
  4.  
  5.  
  6. In the previous chapter you learned about facts.  Now you'll see how the rules 
  7. of an expert system utilize facts in making a program execute.
  8.  
  9.  
  10. Making Good Rules
  11.  
  12.  
  13. In order to accomplish useful work, an expert system must have rules as well as 
  14. facts.  Now that you've seen how facts are asserted and retracted, 
  15.  it's time to see how rules work.
  16.    A rule is analogous to an IF THEN statement in a language such as Ada, 
  17. Pascal, FORTRAN, BASIC, or C.  An IF THEN rule can be expressed in 
  18.  a mixture of natural language and computer language as follows.
  19.  
  20. IF certain conditions are true
  21. THEN execute the following actions
  22.  
  23. Another term for the above statement is pseudocode, which literally means false 
  24. code.  More specific pseuodocode statements are a useful aid 
  25.  to the programmer in designing and documenting rules.
  26.    In computer languages such as Ada, Pascal, FORTRAN, BASIC, and C, there are 
  27. IF THEN keywords which are recognized by the language.  However, 
  28.  since CLIPS has a LISP-like syntax, there are no IF THEN keywords in CLIPS.  
  29. However, the translation of rules from natural language to CLIPS 
  30.  is not very difficult if you keep the IF THEN analogy in mind.  As your 
  31. experience with CLIPS grows, you'll soon find that writing rules in 
  32.  CLIPS becomes easy.
  33.    You can either type rules directly into CLIPS or load rules in from a file 
  34. of rules created by a word processor.  You can use any type of 
  35.  word processor so long as it does not introduce formatting characters into 
  36. your source code.  Formatting characters will not be accepted by 
  37.  CLIPS, so be sure your word processor is in non-document mode when you type in 
  38. the source code of your program.
  39.    Initially, let's see how to type rules directly into CLIPS.  The pseudocode 
  40. for this rule is
  41.  
  42. IF there is a duck THEN make a quack
  43.  
  44. The following is the rule expressed in CLIPS syntax.  The only additional 
  45. feature is the term in double quotes, which is an optional description 
  46.  of the rule.  To enter the rule, just type it in after the CLIPS prompt and 
  47. then press the Return key.
  48.  
  49. (defrule duck-quack "The First Rule" (duck) => (assert (quack)))
  50.  
  51. If you type in the rule correctly as shown, you should see the CLIPS prompt 
  52. reappear.  Otherwise, you'll see an error message.  If you get an 
  53.  error message, most likely you misspelled a keyword or left out a parenthesis. 
  54.  Remember, the number of left and right parentheses must always 
  55.  match in a statement.
  56.    Shown following is the same rule with numbers below the rule keyed to an 
  57. explanation of the parts of the rule.
  58.  
  59. (defrule duck-quack "The First Rule" (duck) => (assert (quack)))
  60.  |<  2 >|<   3    >|<      4       >|<  5 >|<6>|<      7     >|
  61. |<                            1                               >|
  62.     
  63.    A rule consists of the following parts:
  64.    1.  The entire rule must be surrounded by parentheses.
  65.    2.  The rule must start with a defrule keyword.
  66.    3.  Following defrule must be the name of the rule.  The name can be any 
  67. valid symbolic atom.  If you type in a rule name that is the same 
  68.  as an existing rule, the new rule replaces the old rule.  In this rule, the 
  69. rule name is duck-quack.
  70.    4.  Next is an optional comment within double quotes.  The comment is 
  71. normally used to describe the purpose of the rule or any other information 
  72.  the programmer desires.
  73.    5.  After the optional comment are one or more conditional elements or 
  74. patterns.  Each conditional element consists of one or more fields. 
  75.   In the duck-quack rule, the conditional element is (duck) and the one field 
  76. is duck.  An example of a conditional element with two fields is 
  77.  (duck quack).
  78.    CLIPS attempts to match conditional elements of rules against facts in the 
  79. fact-list.  If all the conditional elements of a rule match facts, 
  80.  the rule is activated and put on the agenda.  The agenda is a collection of 
  81. activated rules.  There may be zero  or more rules in the agenda.
  82.    6.  The symbol => that follows the conditional elements in a rule is called 
  83. an arrow.  The arrow is a symbol representing the THEN part of 
  84.  an IF THEN rule.
  85.    7.  The last part of a rule is the list of actions that will be executed 
  86. when the rule fires.  The term fires means that CLIPS has selected 
  87.  a certain rule for execution from the agenda.  While there may be multiple 
  88. rules in the agenda, CLIPS fires the rule with the highest priority. 
  89.   In our example, the one action is to assert the fact (duck).
  90.    The part of the rule on the left of the arrow is called the left-hand side 
  91. (LHS) and the part on the right of the arrow is called the right-hand 
  92.  side (RHS).
  93.  
  94.  
  95. No Quack 
  96.  
  97.  
  98. A rule fires when it has the highest priority of all rules in the agenda.  It 
  99. follows that if there is only one rule in the agenda, that rule 
  100.  will fire.  Since we have the duck-quack rule and its conditional element is 
  101. satisfied by the fact (duck), then the duck-quack rule should fire.
  102.    To make a program run, just enter the run command.  Type (run) and press the 
  103. Return key in response to the CLIPS prompt.
  104.    Nothing happens.
  105.    In case you're wondering, no, CLIPS is not all messed up.  The reason CLIPS 
  106. does not fire the rule has to do with the way CLIPS is designed. 
  107.   The design of CLIPS is such that rules only see facts that have been entered 
  108. after the rules.  Remember that you asserted (duck) first when 
  109.  you learned about (clear) in the previous section.  Then you typed in the 
  110. duck-quack rule.  Since (duck) was entered before the duck-quack rule, 
  111.  the rule doesn't see the fact and is not put on the agenda for firing.  
  112. Because duck-quack is not on the agenda, it can't be fired.
  113.    You can check what's in the agenda with the agenda command.  Type (agenda) 
  114. and press Return in response to the CLIPS prompt.  In this case, 
  115.  nothing will be printed since there's nothing in the agenda and CLIPS will 
  116. just respond with a prompt.
  117.  
  118.  
  119. A Quack At Last
  120.  
  121.  
  122. OK, it's time now to get serious and fire this crazy rule.  In order to fire 
  123. it, we've got to assert a (duck) after the rule has been entered. 
  124.   At first thought, this seems trivial since you just need to assert (duck).  
  125. However, if you try this and (run) again, the rule still won't 
  126.  fire.  This may be somewhat frustrating.  However, before you do something 
  127. drastic to ease your frustration, like getting married or kicking 
  128.  your pet duck, just think a minute.
  129.    Since there is already a (duck) in the fact-list, typing in a new (duck) 
  130. doesn't do anything.  CLIPS will not accept the new (duck) because 
  131.  there is already a (duck).  So, what's the solution?
  132.    The solution is to retract the first (duck) and then assert the new duck.  
  133. Once you've done this, issue an (agenda) command again.  You'll 
  134.  see
  135.  
  136. 0   duck-quack  f-1.
  137.  
  138. The 0 indicates this is the priority of the rule in the agenda.  You'll learn 
  139. about priority in the next chapter.  Next comes the name of the 
  140.  rule followed by the fact identifiers that match the rule.  In this case, 
  141. there is only one fact identifier, f-1.
  142.    Now type the (run) command and you'll see the rule fire at last.  If you 
  143. check the fact-list, you'll see that there is a quack at last.
  144.  
  145.  
  146. Nevermore, Nevermore
  147.  
  148.  
  149. An interesting question may occur to you at this time.  What if you (run) again 
  150. ? There is a rule and there is a fact which satisfies the rule 
  151.  and so the rule should fire again.  Try a (run) command and you'll see that 
  152. nothing happens.  If you check the agenda, you'll see that the rule 
  153.  didn't fire because there was nothing on the agenda.
  154.    The rule didn't fire again because of the way that CLIPS is designed.  CLIPS 
  155. was programmed with a characteristic of a nerve cell called a 
  156.  neuron.  After a neuron transmits a nerve impulse (fires), no amount of 
  157. stimulation will make it fire for some time.  This phenomenon is called 
  158.  refraction and is very important in expert systems.
  159.    Without refraction, expert systems would be always caught in trivial loops.  
  160. That is, as soon as a rule fired, it would keep on firing on 
  161.  that same fact over and over again.  In the real world, the stimulus that 
  162. caused the firing would eventually disappear.  For example, the duck 
  163.  would eventually swim away or fly south for the winter.  However, in the 
  164. computer world, once a fact is entered in the fact-list, it stays there 
  165.  until explicitly removed or the power is turned off.
  166.    So how do you get the rule to fire again ? Just retract the fact and assert 
  167. it again.  It's as if the old duck flew away and a new one came 
  168.  to trigger the rule again.  Basically, CLIPS remembers the fact identifiers 
  169. that triggered a rule into firing and will not activate that rule 
  170.  again with the same fact identifiers.  Of course, a rule may be put on the 
  171. agenda with the same fact identifiers many times.  However, being 
  172.  put on the agenda is not the same as being fired.  Only rules that have been 
  173. fired experience refraction, not rules on the agenda.
  174.  
  175.  
  176. Show Me The Rules
  177.  
  178.  
  179. Very often you'd like to see the rules while you're in CLIPS.  There's a 
  180. command that shows the rules called pprule, which stands for pretty 
  181.  print rule.  To see a rule, specify the rule name as argument.  For example, 
  182. enter the following in response to a CLIPS prompt and press Return.
  183.  
  184. (pprule duck-quack)
  185.  
  186. You'll see 
  187.  
  188. (defrule duck-quack "The First Rule"
  189.    (duck)
  190.    =>
  191.    (assert (quack)))
  192.  
  193.    CLIPS puts different parts of the rule on different lines for the sake of 
  194. readability for you.  You can enter rules on one line, if there's 
  195.  enough room, or multiple lines.  From now on, we'll show rules on multiple 
  196. lines for the sake of readability.
  197.    Using a word processor is very convenient for typing programs because of all 
  198. the editing commands that are available.  The terms before the 
  199.  arrow are still considered the LHS and those after the arrow are the RHS of 
  200. the rule.
  201.    What if you want to print a rule but can't remember the name of the rule?  
  202. No problem.  Just use the rules command in response to a CLIPS 
  203.  prompt and CLIPS will print out the names of all the rules.  For example, enter
  204.  
  205. (rules)
  206.  
  207. and the duck-quack name will be printed since it's the only rule name.
  208.  
  209.  
  210. Getting Loaded
  211.  
  212.  
  213. You can load a file of rules made on a word processor into CLIPS using the load 
  214. command.  Let's assume you have made the duck rule on a word 
  215.  processor and stored it in a file called duck.clp on drive b.  Of course, the 
  216. actual device that you use for file storage will be system dependent 
  217.  and so this example should be taken only as a guide.  To load the rule into 
  218. CLIPS from the file duck.clp on drive b, enter the following in 
  219.  response to the CLIPS prompt.
  220.  
  221. (load "b:duck.clp")
  222.  
  223.    If you have typed the file using a word processor and loaded it in, you may 
  224. see an error message from CLIPS like
  225.  
  226. Illegal construct defrule
  227. Unidentified input character  (ASCII 229) found
  228.  
  229.    The problem is that you used document mode in typing the rule in the word 
  230. processor and so formatting characters like soft returns (ASCII 
  231.  character code 229) were put in.  CLIPS is designed to accept only standard 
  232. characters.  Be sure to type CLIPS programs in non-document mode 
  233.  to avoid error messages like the above.
  234.    You can use multiple files to save rules.  That is, you don't have to store 
  235. all your rules in one big file.  It does take time to read rules 
  236.  into CLIPS and if you have hundreds of rules, it could take a long time.
  237.    CLIPS also provides the opposite of the (load) command.  With the save 
  238. command, you can save rules in CLIPS to a disk file.  For example, 
  239.  to save the duck-quack rule to a file called duck.clp on drive b:, just enter 
  240. the following in response to a CLIPS prompt.
  241.  
  242. (save "b:duck.clp")
  243.  
  244.    The (save) command will save all the rules in CLIPS to the specified file.  
  245. It is not possible to save specified rules to a file.
  246.  
  247.  
  248. Comments, Anyone
  249.  
  250.  
  251. It's a good idea to include comments in your CLIPS program.  Sometimes, rules 
  252. can be difficult to understand and comments can be used to explain 
  253.  to the reader what the rule is doing.  Comments are also used for good 
  254. documentation of a program and will be very helpful in lengthy programs.
  255.   A comment in CLIPS is any text that begins with semicolon and ends with a 
  256. Return.  The following is an example of comments in the duck program.
  257.  
  258. ;***********************************
  259. ;*                                 *
  260. ;* Programmer: J. Giarratano       *
  261. ;*                                 *
  262. ;* Title     : The Duck Program    *
  263. ;*                                 *
  264. ;* Date      : 7/7/86              *
  265. ;*                                 *
  266. ;***********************************
  267. ;
  268. ;The purpose of this rule is to
  269. ;make a quack if there is a duck
  270. (defrule duck-quack "The First Rule" ;IF
  271.    (duck)                            ;there is a duck
  272.    =>                                ;THEN
  273.    (assert (quack)))                 ;quack
  274.  
  275.    If you type this in a word processor and then load it into CLIPS, you'll 
  276. notice that all the comments are eliminated in the CLIPS program. 
  277.   That is, CLIPS will ignore the program comments as it's loading the program.
  278.  
  279. Write To Me
  280.  
  281.  
  282. Besides asserting facts in the RHS of rules, you can also print out information 
  283. using the printout keyword.  For example, enter a (clear) command 
  284.  and then the following.
  285.  
  286. (defrule duck-quack
  287.    (duck)
  288. =>
  289.    (printout "quack"))
  290.  
  291. The argument of (printout) is enclosed in double quotes, as shown.
  292.    Assert a new (duck) and then enter a (run) command.  You'll see the output
  293.  
  294. quack1 rules fired
  295.  
  296. where the quack is from the (printout) statement and the informational message 
  297. "1 rules fired" is from CLIPS.
  298.    CLIPS has a carriage return keyword called crlf which is very useful in 
  299. improving the appearance of output by formatting it on different lines. 
  300.   For a change, the crlf is not included in parentheses. The following example 
  301. of crlf shows how it's used to separate the program output from 
  302.  the informational message of CLIPS.
  303.  
  304. (defrule duck-quack (duck) => (printout "quack" crlf))
  305.  
  306.    When you enter a (run) command, you'll see
  307.  
  308. quack
  309. 1 rules fired
  310.  
  311. Notice how the quack is separated now from the CLIPS message.
  312. .PA
  313.                             Problems
  314.  
  315.  
  316. 1.  Write a program that will print your name and address when you assert your 
  317. first name.  For example, if you enter (assert (Bob)), the program 
  318.  could print out
  319.  
  320.                             Bob Jones
  321.                             1000 Main St.
  322.                             Houston, TX 77058
  323.  
  324. 2.  Write a program that will print out the following if an assertion of 
  325. (holidays) is made.
  326.  
  327.                                 *
  328.                                * *
  329.                               *   *
  330.                              *     *
  331.                                | |
  332.                          Happy Holidays
  333.  
  334.  
  335.