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

  1.            Chapter 3   Adding Details
  2.  
  3.  
  4.  
  5.  
  6. In the first two chapters, you learned the fundamentals of CLIPS.  Now you will 
  7. see how to build on that foundation to create more powerful programs.
  8.  
  9.  
  10. Stop And Go
  11.  
  12.  
  13. Until now you've only seen the simplest type of program consisting of just one 
  14. rule.  However, expert systems consisting of only one rule are 
  15.  not too useful.  Practical expert systems may consist of hundreds or thousands 
  16. of rules.  Let's take a look now at an application requiring 
  17.  multiple rules.
  18.    Suppose you wanted to write an expert system to simulate how a robot should 
  19. respond to a traffic light.  This type of problem requires multiple 
  20.  rules.  For example, the rules for the red and green light situations can be 
  21. written as follows.
  22.  
  23. (defrule red-light
  24.    (red)
  25. =>
  26.    (printout "Stop" crlf))
  27.  
  28. (defrule green-light
  29.    (green)
  30. =>
  31.    (printout "Go" crlf))
  32.  
  33.    To test out this program, you can either enter the rules with a word 
  34. processor and (load) them into CLIPS, or type in each rule in response 
  35.  to a CLIPS prompt.
  36.    After the rules have been entered to CLIPS, assert a (red) fact and run.  
  37. You'll see "Stop" printed.  Now assert a (green) fact and run. You 
  38.  should see "Go" printed.
  39.    An alternative way of writing the conditional elements in the rules is as 
  40. follows.  By including the extra pattern "light", it is more clear 
  41.  to someone reading the program what is being matched.  The only disadvantage 
  42. is a slightly longer execution time by CLIPS because more work 
  43.  is required to find matches.
  44.  
  45. (defrule red-light
  46.    (light red)
  47. =>
  48.    (printout "Stop" crlf))
  49.  
  50. (defrule green-light
  51.    (light green)
  52. =>
  53.    (printout "Go" crlf))
  54.  
  55.  
  56. The Real Thing
  57.  
  58.  
  59. As long as our robot encounters only red and green lights, everything is 
  60. alright.  However, the real world is not quite that simple.  In addition 
  61.  to red and green lights, there are also yellow lights.  In order to make the 
  62. program more realistic, we need to add a rule for yellow lights. 
  63.   But is that enough ?
  64.    Other possibilities include blinking red and blinking yellow lights.  Rules 
  65. need to be made to cover these situations also.  Now have we covered 
  66.  all possibilties ?
  67.    Unfortunately, the answer is no.  In the real world, things don't always 
  68. operate perfectly.  What if the light is blinking green ?  What if 
  69.  none of the lights are on ? What if the lights don't change ?  What if the 
  70. light is red but a police officer waves you on ?
  71.  
  72.  
  73. Take A Walk
  74.  
  75.  
  76. If you think about it, there are other possibilities besides the simple red, 
  77. green, and yellow cases.  Some traffic lights also have a green 
  78.  arrow for protected left turns.  Some have a hand that lights up to indicate 
  79. whether a person can walk or not.  Some have signs that say walk 
  80.  or don't walk.  So depending on whether our robot is walking or driving, it 
  81. may have to pay attention to different signs.
  82.    The information about walking or driving must be asserted in addition to 
  83. information about the status of the light.  Rules can be made to 
  84.  cover these conditions but they must have more than one conditional element.  
  85. For example, suppose we want a rule to fire if the robot is walking 
  86.  and if the walk-sign says walk.  A rule could be written as follows.
  87.  
  88. (defrule take-a-walk
  89.    (status walking)
  90.    (walk-sign walk)
  91. =>
  92.    (printout "Go" crlf))
  93.  
  94.    The above rule has two conditional elements.  Both elements must be 
  95. satisfied by facts in the fact-list for the rule to fire.  To see how 
  96.  this works, enter the rule and then assert the facts (status walking) and 
  97. (walk-sign walk).  When you (run), the program will print out "Go" 
  98.  since both conditional elements are satisfied and the rule is fired.
  99.    You can have any number of conditional elements or actions in a rule.  The 
  100. important point to realize is that the rule is placed on the agenda 
  101.  only if all the conditional elements are satisfied by facts.  This type of 
  102. restriction is called a logical AND in reference to the AND relation 
  103.  of Boolean logic.  An AND relation is said to be true only if all its 
  104. conditions are true.
  105.    Because the conditional elements are of the logical AND type, the rule will 
  106. not fire if only one of the conditional elements are satisfied. 
  107.   All the facts must be present before the LHS of a rule is satisfied and the 
  108. rule is placed on the agenda.
  109.  
  110.  
  111. Gimme Deffacts
  112.  
  113.  
  114. As you work with CLIPS, you may become tired of typing in assertions.  Although 
  115. you cannot load assertions in from disk, there is a way to load 
  116.  in facts using the define facts keyword, deffacts.  For example, type the 
  117. following in response to a CLIPS prompt or enter it in a word processor 
  118.  and load it in from disk.
  119.  
  120. (deffacts walk
  121.    (status walking)
  122.    (walk-sign walk))
  123.  
  124.    Following the (deffacts) keyword is the required name of this deffacts 
  125. statement.  Any valid symbolic atom can be used as the name.  In this 
  126.  case, the name chosen was walk.  After the name are the facts that will be 
  127. asserted in the fact-list by this deffacts statement.
  128.    The facts in a deffacts statement are asserted using the CLIPS reset 
  129. command.  Just enter
  130.  
  131. (reset)
  132.  
  133. in response to a CLIPS prompt and then give a (facts) command.  You'll see the 
  134. facts from the deffacts statement and a new fact generated by 
  135.  the (reset) command called
  136.  
  137. f-0     initial-fact
  138.  
  139.    The (initial-fact) is automatically put in by a reset.  Even without any 
  140. deffact statements, a (reset) will always assert an (initial-fact). 
  141.   At first you may think this is pretty silly, but as you write more CLIPS 
  142. programs you'll appreciate (initial-fact) more.  The utility of (initial-fact) 
  143.  lies in starting programs running.  A CLIPS program will not start running 
  144. unless there are rules whose LHS are satisfied by facts.  Rather 
  145.  than your having to type in some fact to start things off, the (reset) command 
  146. asserts it for you as well as asserting the facts in deffacts 
  147.  statements.
  148.    The (reset) also has an advantage compared to a (clear) command in that 
  149. (reset) doesn't get rid of all the rules.  A (reset) leaves your rules 
  150.  intact.  Like (clear), it also removes all activated rules from the agenda.  
  151. Giving a (reset) command is a recommended way to start off program 
  152.  execution, especially if the program has been run before and the fact-list is 
  153. cluttered up with old facts.
  154.  
  155.  
  156. Selective Elimination
  157.  
  158.  
  159. There may be times when you don't want a (deffacts) to assert facts when you 
  160. issue a (reset) command.  You can use the undeffacts command to 
  161.  prevent a (deffacts) from asserting facts.  For example, enter
  162.  
  163. (undeffacts walk)
  164.  
  165. and then issue a (reset).  If you check the fact-list, you'll see no facts from 
  166. the (deffacts walk).
  167.    You can even get rid of initial-fact with undeffacts.  Just enter the command
  168.  
  169. (undeffacts initial-fact)
  170.  
  171. Now, whenever you do a (reset), the initial-fact will not appear.
  172.    Besides facts, CLIPS also allows you to selectively eliminate rules by using 
  173. the excise command.  For example, to get rid of the take-a-walk 
  174.  rule, just specify the name of the rule as argument, as in
  175.  
  176. (excise take-a-walk)
  177.  
  178. Enter this rule in response to a CLIPS prompt and then issue a (rules) command. 
  179.  No rule called take-a-walk will be listed since take-a-walk 
  180.  has been excised.
  181.  
  182.  
  183. A Matter Of Priority
  184.  
  185.  
  186. Up to this point, we haven't seen any cases of competing rules in the agenda.  
  187. But in most real situations, there is a need to specify which 
  188.  rules should have priority over other rules.
  189.    Let's consider the case of our robot trying to cross the street.  There may 
  190. be two rules in the agenda that are satisfied by the facts.  One 
  191.  fact may be (green) while the other fact is that a police officer says to 
  192. stop, even though the light is green.   For example, enter the following 
  193.  two rules.
  194.  
  195. (defrule green-light
  196.    (green)
  197. =>
  198.    (printout "Go" crlf))
  199.  
  200. (defrule police-officer
  201.    (police-officer stop)
  202. =>
  203.    (printout "Stop" crlf))
  204.  
  205. Now enter the following
  206.  
  207. (deffacts conditions
  208.    (green)
  209.    (police-officer stop))
  210.  
  211. Do a (reset) and check the agenda.  You'll see both rules are on the agenda 
  212. since both are satisfied by the facts.  When you (run), the "Stop" 
  213.  may appear before the "Go".  However, since the rules have equal priority, you 
  214. cannot count on the "Stop" always appearing first.
  215.    The correct thing to do is to assign a priority to the rules so that the 
  216. "Stop" rule will always fire before the "Go" rule.  CLIPS has a keyword 
  217.  called salience which is used to set the priority of rules.
  218.    The salience is set using a numeric value ranging from the smallest value of 
  219. -10000 to the highest of 10000.  If a rule has no salience explicitly 
  220.  assigned by the programmer, then CLIPS assumes a salience of 0.  Notice that a 
  221. salience of 0 is midway between the largest and smallest salience 
  222.  values.  A salience of 0 does not mean that the rule has no salience but 
  223. rather that it has an intermediate priority level.
  224.    Let's declare the salience of the police-officer rule to be higher than the 
  225. green-light rule.  We could select any values for the rules such 
  226.  that the value for police-officer is higher than green-light.  A simple way of 
  227. doing this is shown as follows.
  228.  
  229. (defrule police-officer
  230.    (declare (salience 10))
  231.    (police-officer stop)
  232. =>
  233.    (printout "Stop" crlf))
  234.  
  235.    The (declare (salience)) statement assigns a salience value to a rule and 
  236. must be the first pattern.  In this case, the declared value of 
  237.  10 for the police-officer rule is higher than the default value of 0 for the 
  238. green-light rule.  Although a salience value of 1 would also be 
  239.  higher, it's a good idea to spread out the salience values in case you later 
  240. decide to add a rule with intermediate priority to the police-officer 
  241.  and green-light rules.  That way, you won't have to modify the salience value 
  242. of the police-officer rule since there are unused values of 1 
  243.  to 9 that can be used for the intermediate rule.
  244.  
  245.  
  246. Order, Order
  247.  
  248.  
  249. Besides selecting competing rules in the agenda, salience has another important 
  250. application.  By assigning appropriate salience values, you can 
  251.  force rules to execute in a sequential fashion.  Let's first look at an 
  252. example in which salience is not used and then see how salience changes 
  253.  things.
  254.  
  255. (defrule first
  256.    (first)
  257. =>
  258.    (printout "Rule 1" crlf))
  259.  
  260. (defrule second
  261.    (second)
  262. =>
  263.    (printout "Rule 2" crlf))
  264.  
  265. (defrule third
  266.    (third)
  267. =>
  268.    (printout "Rule 3" crlf))
  269.  
  270. (deffacts test
  271.    (first)
  272.    (second)
  273.    (third))
  274.  
  275.    Enter these rules, then do a (reset) and check the agenda.  Now run the 
  276. program and you'll see this output.
  277.  
  278. Rule 3
  279. Rule 2
  280. Rule 1
  281.  
  282.    Notice the order of output statements.  First Rule 3 is printed, then Rule 
  283. 2, and finally Rule 1.  The reason this occurs is that the activated 
  284.  rules are put on a stack.  The first fact (first) activates the first rule, 
  285. Rule 1.  When the second fact is asserted, it activates the second 
  286.  rule, Rule 2, which is stacked on top of Rule 1.  Finally, the third fact is 
  287. asserted and its activated rule, Rule 3, is stacked on top of Rule 
  288.  2.
  289.    In CLIPS, rules of equal salience which are activated by different 
  290. conditional elements are prioritized based on the stack order of facts. 
  291.   Rules are fired from the agenda from the top of the stack down.  So first 
  292. Rule 3 is fired because it's on the top of the stack, then Rule 2 
  293.  and finally Rule 1. If you change the order of the facts in the deffacts, 
  294. you'll see that the last fact always produces the first rule to be 
  295.  fired and so forth.  However, if rules are written which are all activated by 
  296. the same conditional element, there is no guarantee of rule priority.
  297.    Now let's include salience.  Suppose you want the rules to fire in the order 
  298. Rule 1, Rule 2, and then Rule 3, no matter what the arrangement 
  299.  in deffacts.  To accomplish this, just add salience as follows.
  300.  
  301. (defrule first
  302.    (declare (salience 30))
  303.    (first)
  304. =>
  305.    (printout "Rule 1" crlf))
  306.  
  307. (defrule second
  308.    (declare (salience 20))
  309.    (second)
  310. =>
  311.    (printout "Rule 2" crlf))
  312.  
  313. (defrule third
  314.    (declare (salience 10))
  315.    (third)
  316. =>
  317.    (printout "Rule 3" crlf))
  318.  
  319.    Now do a reset and check the agenda.  Notice how the salience values have 
  320. rearranged the priority of rules in the agenda, as shown below.
  321.  
  322. 30  first  f-1
  323. 20  second  f-2
  324. 10  third  f-3
  325.  
  326. When you run the program, you'll see that the order of rule firing is now Rule 
  327. 1, Rule 2, and then Rule 3.
  328.  
  329.  
  330. Watch It
  331.  
  332.  
  333. CLIPS has several commands to help you debug programs.  These commands allow 
  334. you to watch rules, watch facts, and watch activations on the agenda 
  335.  as the program is executing.  To use these features, just enter the 
  336. appropriate command for the information that you want to see.  You can enter 
  337.  any or all of the commands.
  338.  
  339. (watch rules)
  340. (watch facts)
  341. (watch activations)
  342.  
  343. If you enter any one or all of these commands, CLIPS will print out the 
  344. appropriate information on the screen.
  345.    To turn off the effect of these commands, enter the appropriate following 
  346. command.
  347.  
  348. (unwatch rules)
  349. (unwatch facts)
  350. (unwatch activations)
  351.  
  352.    Another useful command in debugging is the (run) command which takes an 
  353. optional argument of the number of rule firings.  For example, (run 
  354.  21), would tell CLIPS to run the program and then stop after 21 rule firings.  
  355. A (run 1) command allows you to step through a program 1 rule 
  356.  firing at a time.
  357.    The major difference between a rule-based system like CLIPS and languages 
  358. such as Pascal or BASIC is that the rules of CLIPS can be activated 
  359.  in parallel while the statements of other languages are sequential in nature.  
  360. That is, multiple rules can be put on the agenda.
  361.    In procedural languages like Ada, Pascal and BASIC, execution normally 
  362. proceeds line by line in sequence.  In CLIPS, any of the rules can 
  363.  be executed if certain conditions for the rules are met.  So debugging an 
  364. expert system program is not quite as straight-forward as debugging 
  365.  a program in a procedural language.     This is why the (watch) commands are 
  366. so useful.  Except for very simple programs, you can't just read 
  367.  a CLIPS program listing and figure out how it executes.  Since the execution 
  368. can occur from any triggered rule, you really need to know what's 
  369.  on the agenda to know what rule will be fired next.  And to figure out why a 
  370. rule got on the agenda, you have to know what the facts are.
  371. .PA
  372.                             Problems
  373.  
  374.  
  375. 1.  Write a program to tell a robot what to do when it encounters a traffic 
  376. light.  The possible facts are
  377.    (light red)
  378.    (light green)
  379.    (light yellow)
  380.    (light blinking-red)
  381.    (light blinking-yellow)
  382.    (light blinking-green)
  383.    (light none)
  384.    (walk-sign walk)
  385.    (walk-sign dont-walk)
  386.  
  387.    The status of the robot can be
  388.    (status walking)
  389.    (status driving)
  390.  
  391. 2. Write a program to recognize and print the names of the following patterns.  
  392. Store the patterns in separate deffacts.  You'll have to include 
  393.  row numbers for some of the patterns because some rows are duplicates and 
  394. CLIPS won't assert duplicate facts.  Do not include row numbers in 
  395.  patterns that do not have duplicate patterns.
  396.  
  397.   1********   1    *       1            *      
  398.   2*      *   2   * *      2        *        *
  399.   3*      *   3  *   *     3      *     
  400.   4*      *   4 *     *    4*   
  401.   5********   5*********   5       *
  402.     Square      Triangle   6 *     
  403.                                  Big Dipper
  404.  1    *       1*****   
  405.  2   * *      2*    *
  406.  3  *****     3*****
  407.  4 *     *    4*    * 
  408.  5*       *   5*****
  409.       A          B
  410.  
  411.  
  412.