home *** CD-ROM | disk | FTP | other *** search
/ Piper's Pit BBS/FTP: ibm 0020 - 0029 / ibm0020-0029 / ibm0028.tar / ibm0028 / KAPA2.ZIP / EXAMPLES.ZIP / BACKWARD.KAL < prev    next >
Encoding:
Text File  |  1990-05-25  |  11.6 KB  |  275 lines

  1. /***********************************************
  2. **
  3. ** BACKWARD.KAL: contains an example of using Backward Chaining.
  4. **
  5. ***********************************************/
  6.  
  7. /*********************************************************
  8. ** The purpose of this example is to demonstrate:
  9. ** 
  10. **            a) backward rule-processing capabilities of KAPPA,
  11. **            b) usage of Meter and StateBox Images
  12. ** 
  13. ** The example contains five rules and a goal for determining whether a given
  14. ** car is of type racing or a family car.  This depends on three main 
  15. ** parameters: the power of the car (in horsepower), its coefficient of drag 
  16. ** (CD), and its origin (the country where it's made). 
  17. **
  18. *********************************************************/
  19.  
  20. /*********************************************************
  21. **      Steps to Load
  22. **
  23. ** To load the example, start KAPPA, and select "Interpret KAL File" from the
  24. ** File menu.  Type in "backward.kal" as the name of the file and click on
  25. ** "Open" to initiate the interpretation.
  26. **
  27. ** Once interpretation is completed, you will see the "Session" window 
  28. ** positioned in the top center, and the "Inference Browser" at the bottom.
  29. ** The main KAPPA window at this time is iconified (you can find the icon
  30. ** under the Inference Browser window). 
  31. ** 
  32. *********************************************************/
  33.  
  34. /*********************************************************
  35. **      Steps to Run
  36. ** 
  37. ** To start running the problem, click on the "Solve" button in the "Session"
  38. ** window.  This will cause the invocation of the "Solve" function, which
  39. ** in turn calls KAPPA's "BackChain" function.  The tracing of the backward
  40. ** chaining is seen in the "Inference Browser".  The tracing tree will grow 
  41. ** until the system stops and prompts you for the value of Car:Power. In order
  42. ** to develop a better understanding of KAPPA's backward chaining process, 
  43. ** let's take a close look at the trace inside the "Inference Browser" window.
  44. **
  45. ** Since the purpose of this backchaining is to satisfy the conditions in
  46. ** Goal1, the system needs to determine the value for Car:Type.  Among the 
  47. ** five rules in the system, Rule4 and Rule5 are the only ones which make 
  48. ** assertions about Car:Type in their "Then" part.  Therefore, the backward 
  49. ** chaining, first selects one of these rules, Rule4, and expands its "if" 
  50. ** part, to see if the validity of this rule can be proven. In order to prove
  51. ** its validity, it needs to have the values for Car:Speed and Car:Origin. 
  52. ** Since the value of these slots are unknown,  the system temporarily
  53. ** puts evaluation of Rule4 on hold and moves on to Rule5.  To prove the 
  54. ** validity of Rule5, the system runs into the same unknown variables. The 
  55. ** backward chaining engine, hence, returns to Rule4 and decides to expand 
  56. ** on the two key variables and see if it can find other rules which can 
  57. ** assist in determinig their values. It determines that Rule1, Rule2, and 
  58. ** Rule3 make assertions about Car:Speed in their "Then" part.  Therefore, 
  59. ** it selects Rule1 and tries to prove its validity.  Rule1's "If" part can 
  60. ** only be evaluated if the values for Car:Power and Car:CD were known.  
  61. ** Since they are not,  KAPPA moves on to Rule2 and Rule3 to see if 
  62. ** determining their validity is a more subtle task.  Since these two
  63. ** rules also depend on the same two variables, only at this time,  having 
  64. ** exhausted all other possibilities, KAPPA decides to prompt you with the
  65. ** question concerning the Car:Power.
  66. **
  67. ** The system at this point should have prompted you for a value for 
  68. ** Car:Power.  Since in the definition of Car:Power slot, we have specified 
  69. ** zero as the minimum allowable value, this information is automatically 
  70. ** integrated into the prompt line of the query form.  At this point, you can 
  71. ** type in a horsepower value for the car which is greater than zero, and 
  72. ** then click on the OK button.  Let's for example type in 180 and click on 
  73. ** OK.  KAPPA will then ask for a value for Car:CD.  Type in 0.3. Having the 
  74. ** values for Car:Power and Car:CD, Rule1's "If" part is now satisified and 
  75. ** is fired.  As a result, Car:Speed is calculated to be 153.9. 
  76. **
  77. ** Having the value for Car:Speed, the system needs a value for Car:Origin 
  78. ** in order to be able to evaluate Rule4.  Since they are no rules which 
  79. ** will assist in determining this value, KAPPA prompts you for a value.  
  80. ** Note that the query form for Car:Origin is slightly different than that 
  81. ** for Car:CD and Car:Power.  This is due to the ALLOWABLE_VALUES
  82. ** option that has been specified for Car:Origin.  The list box in the bottom 
  83. ** of the form allows you to select any one of the options by just clicking 
  84. ** the left mouse button on the desired option.  As the name suggests, these 
  85. ** are only options.  You can type any reply dirrectly into the edit box.  
  86. ** As an example, click over the option "Italy".  You will notice that 
  87. ** "Italy" is now displayed in the edit box.  Click on OK.  
  88. **
  89. ** Given values for Car:Origin and Car:Speed,  Rule4 is then evaluated.  Since
  90. ** both of the condition's in its "If" part are valid, the "Then" part of the
  91. ** rule sets the Car:Type to "Sport".  Since Goal1 was directly depended on
  92. ** Car:Type, it is then evaluated and found to be TRUE.  The backward chaining
  93. ** at this point stops.
  94.  
  95. ** At any time you can re-run the problem, by first, clicking on the "Reset"
  96. ** button and then clicking on "Solve".  The "Reset" button calls a KAPPA
  97. ** function named "Reset" which sets the values of Car:CD, Car:Power, 
  98. ** Car:Speed,  and Car:Type to unknown.  This will allow for re-running of 
  99. ** the entire problem.
  100. **
  101. ** The "Explain" button calls KAPPA's Explain function with Car:Type as 
  102. ** argument.  Try clicking on this button and view the explanation.  See how 
  103. ** the explanation uses the text specified in the "Info" field of the 
  104. ** corresponding rule.  From the Explain form, you can again click on the 
  105. ** "Explain" button, and get explanation on, for example, Car:Speed.
  106. ** 
  107. *********************************************************/
  108.  
  109. /***********************************************************/
  110. /**            ALL FUNCTIONS ARE SAVED BELOW             ***/
  111. /***********************************************************/
  112.  
  113.        /**************************************
  114.         *******  FUNCTION: Reset
  115.         **************************************/
  116. MakeFunction( Reset, [ ],
  117.               {
  118.               ResetValue( Car:Speed );
  119.               ResetValue( Car:Origin );
  120.               ResetValue( Car:CD );
  121.               ResetValue( Car:Power );
  122.               ResetValue( Car:Type );
  123.               } );
  124.  
  125.        /**************************************
  126.         *******  FUNCTION: Solve
  127.         **************************************/
  128. MakeFunction( Solve, [ ], BackwardChain( Goal1 ) );
  129.  
  130.        /**************************************
  131.         *******  FUNCTION: ExpType
  132.         **************************************/
  133. MakeFunction( ExpType, [ ], Explain( Car:Type ) );
  134.  
  135.  
  136.  
  137. /***********************************************************/
  138. /***       ALL OBJECTS ARE SAVED BELOW             ***/
  139. /***********************************************************/
  140.  
  141.        /**************************************
  142.         *******  OBJECT: Car
  143.         **************************************/
  144. MakeInstance( Car, Root );
  145. MakeSlot( Car:Speed );
  146. SetSlotOption( Car:Speed, VALUE_TYPE, NUMBER );
  147. SetSlotOption( Car:Speed, MINIMUM_VALUE, 0 );
  148. MakeSlot( Car:Power );
  149. SetSlotOption( Car:Power, VALUE_TYPE, NUMBER );
  150. SetSlotOption( Car:Power, MINIMUM_VALUE, 0 );
  151. MakeSlot( Car:CD );
  152. SetSlotOption( Car:CD, VALUE_TYPE, NUMBER );
  153. SetSlotOption( Car:CD, MINIMUM_VALUE, 0 );
  154. MakeSlot( Car:Origin );
  155. SetSlotOption( Car:Origin, ALLOWABLE_VALUES, Italy, Germany, France,
  156.                USA, Japan );
  157. MakeSlot( Car:Type );
  158. SetSlotOption( Car:Type, ALLOWABLE_VALUES, Family, Sports );
  159. SetSlotOption( Car:Type, IMAGE, Status );
  160.  
  161.        /**************************************
  162.         *******  OBJECT: Reset
  163.         **************************************/
  164. MakeInstance( Reset, Button );
  165. Reset:X = 10;
  166. Reset:Y = 40;
  167. Reset:Width = 100;
  168. Reset:Height = 25;
  169. Reset:Action = Reset;
  170. Reset:Visible = TRUE;
  171. ResetImage( Reset );  
  172.  
  173.        /**************************************
  174.         *******  OBJECT: Solve
  175.         **************************************/
  176. MakeInstance( Solve, Button );
  177. Solve:X = 10;
  178. Solve:Y = 10;
  179. Solve:Width = 100;
  180. Solve:Height = 25;
  181. Solve:Action = Solve;
  182. Solve:Visible = TRUE;
  183. ResetImage( Solve );  
  184.  
  185.        /**************************************
  186.         *******  OBJECT: Explain
  187.         **************************************/
  188. MakeInstance( Explain, Button );
  189. Explain:X = 10;
  190. Explain:Y = 70;
  191. Explain:Width = 100;
  192. Explain:Height = 25;
  193. Explain:Action = ExpType;
  194. Explain:Visible = TRUE;
  195. ResetImage( Explain );  
  196.  
  197.        /**************************************
  198.         *******  OBJECT: Status
  199.         **************************************/
  200. MakeInstance( Status, StateBox );
  201. Status:X = 120;
  202. Status:Y = 2;
  203. Status:Width = 150;
  204. Status:Height = 120;
  205. Status:Visible = TRUE;
  206. Status:Title = "Car Type";
  207. MakeSlot( Status:Value);
  208. Status:Owner = Car;
  209. Status:OwnerSlot = Type;
  210. ResetImage( Status );  
  211.  
  212.  
  213. /***********************************************************/
  214. /***         ALL RULES ARE SAVED BELOW             ***/
  215. /***********************************************************/
  216.  
  217.        /**************************************
  218.         *******  RULE: Rule1
  219.         **************************************/
  220. MakeRule( Rule1, [ ], ( Car:CD < 0.4 ) And ( Car:Power > 150 ),
  221.           SetValue( Car:Speed, Sqrt( Car:Power / Car:CD * 40 ) )
  222.           );
  223.        /**************************************
  224.         *******  RULE: Rule2
  225.         **************************************/
  226. MakeRule( Rule2, [ ], 
  227.           ( Car:CD < 0.6 ) And ( Car:Power > 70 ) And ( Car:Power <= 150 ),
  228.           SetValue( Car:Speed, Sqrt( Car:Power / Car:CD * 50 ) )
  229.           );
  230.  
  231.        /**************************************
  232.         *******  RULE: Rule3
  233.         **************************************/
  234. MakeRule( Rule3, [ ], ( Car:CD < 0.4 ) And ( Car:Power <= 150 ),
  235.           {
  236.           SetValue( Car:Speed, Sqrt( Car:Power / Car:CD * 45 ) );
  237.           } );
  238. SetRulePriority( Rule3, 3 );
  239.  
  240.        /**************************************
  241.         *******  RULE: Rule4
  242.         **************************************/
  243. MakeRule( Rule4, [ ], 
  244.           ( Car:Origin #= Italy ) And ( Car:Speed > 150 ),
  245.           SetValue( Car:Type, Sports ) );
  246. SetRuleComment( Rule4, "If Car is made in Italy and has a speed over 150, then it's a Sports Car." );
  247.  
  248.        /**************************************
  249.         *******  RULE: Rule5
  250.         **************************************/
  251. MakeRule( Rule5, [ ], 
  252.      ( Not( Car:Origin #= Italy ) ) And ( Car:Speed <= 150 ),
  253.          SetValue( Car:Type, Family ) );
  254. SetRuleComment( Rule5, "If Car is not made in Italy or has a speed less than 150, then it's a Family  Car." );
  255.  
  256.  
  257. /***********************************************************/
  258. /***         ALL GOALS ARE SAVED BELOW             ***/
  259. /***********************************************************/
  260.  
  261.        /**************************************
  262.         *******  GOAL: Goal1
  263.         **************************************/
  264. MakeGoal( Goal1, KnownValue?( Car:Type ) );
  265.  
  266.  
  267. /***************  SPECIAL  *****************/
  268. Reset(  );
  269. IconifyWindow( KTOOLS );
  270. IconifyWindow( KAPPA );
  271. ShowWindow( SESSION );
  272. ShowWindow( INFERENCE );
  273. PositionWindow( SESSION, 100, 0, 320, 200 );
  274. PositionWindow( INFERENCE, 0, 200, 640, 250 );
  275.