home *** CD-ROM | disk | FTP | other *** search
- /***********************************************
- **
- ** BACKWARD.KAL: contains an example of using Backward Chaining.
- **
- ***********************************************/
-
- /*********************************************************
- ** The purpose of this example is to demonstrate:
- **
- ** a) backward rule-processing capabilities of KAPPA,
- ** b) usage of Meter and StateBox Images
- **
- ** The example contains five rules and a goal for determining whether a given
- ** car is of type racing or a family car. This depends on three main
- ** parameters: the power of the car (in horsepower), its coefficient of drag
- ** (CD), and its origin (the country where it's made).
- **
- *********************************************************/
-
- /*********************************************************
- ** Steps to Load
- **
- ** To load the example, start KAPPA, and select "Interpret KAL File" from the
- ** File menu. Type in "backward.kal" as the name of the file and click on
- ** "Open" to initiate the interpretation.
- **
- ** Once interpretation is completed, you will see the "Session" window
- ** positioned in the top center, and the "Inference Browser" at the bottom.
- ** The main KAPPA window at this time is iconified (you can find the icon
- ** under the Inference Browser window).
- **
- *********************************************************/
-
- /*********************************************************
- ** Steps to Run
- **
- ** To start running the problem, click on the "Solve" button in the "Session"
- ** window. This will cause the invocation of the "Solve" function, which
- ** in turn calls KAPPA's "BackChain" function. The tracing of the backward
- ** chaining is seen in the "Inference Browser". The tracing tree will grow
- ** until the system stops and prompts you for the value of Car:Power. In order
- ** to develop a better understanding of KAPPA's backward chaining process,
- ** let's take a close look at the trace inside the "Inference Browser" window.
- **
- ** Since the purpose of this backchaining is to satisfy the conditions in
- ** Goal1, the system needs to determine the value for Car:Type. Among the
- ** five rules in the system, Rule4 and Rule5 are the only ones which make
- ** assertions about Car:Type in their "Then" part. Therefore, the backward
- ** chaining, first selects one of these rules, Rule4, and expands its "if"
- ** part, to see if the validity of this rule can be proven. In order to prove
- ** its validity, it needs to have the values for Car:Speed and Car:Origin.
- ** Since the value of these slots are unknown, the system temporarily
- ** puts evaluation of Rule4 on hold and moves on to Rule5. To prove the
- ** validity of Rule5, the system runs into the same unknown variables. The
- ** backward chaining engine, hence, returns to Rule4 and decides to expand
- ** on the two key variables and see if it can find other rules which can
- ** assist in determinig their values. It determines that Rule1, Rule2, and
- ** Rule3 make assertions about Car:Speed in their "Then" part. Therefore,
- ** it selects Rule1 and tries to prove its validity. Rule1's "If" part can
- ** only be evaluated if the values for Car:Power and Car:CD were known.
- ** Since they are not, KAPPA moves on to Rule2 and Rule3 to see if
- ** determining their validity is a more subtle task. Since these two
- ** rules also depend on the same two variables, only at this time, having
- ** exhausted all other possibilities, KAPPA decides to prompt you with the
- ** question concerning the Car:Power.
- **
- ** The system at this point should have prompted you for a value for
- ** Car:Power. Since in the definition of Car:Power slot, we have specified
- ** zero as the minimum allowable value, this information is automatically
- ** integrated into the prompt line of the query form. At this point, you can
- ** type in a horsepower value for the car which is greater than zero, and
- ** then click on the OK button. Let's for example type in 180 and click on
- ** OK. KAPPA will then ask for a value for Car:CD. Type in 0.3. Having the
- ** values for Car:Power and Car:CD, Rule1's "If" part is now satisified and
- ** is fired. As a result, Car:Speed is calculated to be 153.9.
- **
- ** Having the value for Car:Speed, the system needs a value for Car:Origin
- ** in order to be able to evaluate Rule4. Since they are no rules which
- ** will assist in determining this value, KAPPA prompts you for a value.
- ** Note that the query form for Car:Origin is slightly different than that
- ** for Car:CD and Car:Power. This is due to the ALLOWABLE_VALUES
- ** option that has been specified for Car:Origin. The list box in the bottom
- ** of the form allows you to select any one of the options by just clicking
- ** the left mouse button on the desired option. As the name suggests, these
- ** are only options. You can type any reply dirrectly into the edit box.
- ** As an example, click over the option "Italy". You will notice that
- ** "Italy" is now displayed in the edit box. Click on OK.
- **
- ** Given values for Car:Origin and Car:Speed, Rule4 is then evaluated. Since
- ** both of the condition's in its "If" part are valid, the "Then" part of the
- ** rule sets the Car:Type to "Sport". Since Goal1 was directly depended on
- ** Car:Type, it is then evaluated and found to be TRUE. The backward chaining
- ** at this point stops.
-
- ** At any time you can re-run the problem, by first, clicking on the "Reset"
- ** button and then clicking on "Solve". The "Reset" button calls a KAPPA
- ** function named "Reset" which sets the values of Car:CD, Car:Power,
- ** Car:Speed, and Car:Type to unknown. This will allow for re-running of
- ** the entire problem.
- **
- ** The "Explain" button calls KAPPA's Explain function with Car:Type as
- ** argument. Try clicking on this button and view the explanation. See how
- ** the explanation uses the text specified in the "Info" field of the
- ** corresponding rule. From the Explain form, you can again click on the
- ** "Explain" button, and get explanation on, for example, Car:Speed.
- **
- *********************************************************/
-
- /***********************************************************/
- /** ALL FUNCTIONS ARE SAVED BELOW ***/
- /***********************************************************/
-
- /**************************************
- ******* FUNCTION: Reset
- **************************************/
- MakeFunction( Reset, [ ],
- {
- ResetValue( Car:Speed );
- ResetValue( Car:Origin );
- ResetValue( Car:CD );
- ResetValue( Car:Power );
- ResetValue( Car:Type );
- } );
-
- /**************************************
- ******* FUNCTION: Solve
- **************************************/
- MakeFunction( Solve, [ ], BackwardChain( Goal1 ) );
-
- /**************************************
- ******* FUNCTION: ExpType
- **************************************/
- MakeFunction( ExpType, [ ], Explain( Car:Type ) );
-
-
-
- /***********************************************************/
- /*** ALL OBJECTS ARE SAVED BELOW ***/
- /***********************************************************/
-
- /**************************************
- ******* OBJECT: Car
- **************************************/
- MakeInstance( Car, Root );
- MakeSlot( Car:Speed );
- SetSlotOption( Car:Speed, VALUE_TYPE, NUMBER );
- SetSlotOption( Car:Speed, MINIMUM_VALUE, 0 );
- MakeSlot( Car:Power );
- SetSlotOption( Car:Power, VALUE_TYPE, NUMBER );
- SetSlotOption( Car:Power, MINIMUM_VALUE, 0 );
- MakeSlot( Car:CD );
- SetSlotOption( Car:CD, VALUE_TYPE, NUMBER );
- SetSlotOption( Car:CD, MINIMUM_VALUE, 0 );
- MakeSlot( Car:Origin );
- SetSlotOption( Car:Origin, ALLOWABLE_VALUES, Italy, Germany, France,
- USA, Japan );
- MakeSlot( Car:Type );
- SetSlotOption( Car:Type, ALLOWABLE_VALUES, Family, Sports );
- SetSlotOption( Car:Type, IMAGE, Status );
-
- /**************************************
- ******* OBJECT: Reset
- **************************************/
- MakeInstance( Reset, Button );
- Reset:X = 10;
- Reset:Y = 40;
- Reset:Width = 100;
- Reset:Height = 25;
- Reset:Action = Reset;
- Reset:Visible = TRUE;
- ResetImage( Reset );
-
- /**************************************
- ******* OBJECT: Solve
- **************************************/
- MakeInstance( Solve, Button );
- Solve:X = 10;
- Solve:Y = 10;
- Solve:Width = 100;
- Solve:Height = 25;
- Solve:Action = Solve;
- Solve:Visible = TRUE;
- ResetImage( Solve );
-
- /**************************************
- ******* OBJECT: Explain
- **************************************/
- MakeInstance( Explain, Button );
- Explain:X = 10;
- Explain:Y = 70;
- Explain:Width = 100;
- Explain:Height = 25;
- Explain:Action = ExpType;
- Explain:Visible = TRUE;
- ResetImage( Explain );
-
- /**************************************
- ******* OBJECT: Status
- **************************************/
- MakeInstance( Status, StateBox );
- Status:X = 120;
- Status:Y = 2;
- Status:Width = 150;
- Status:Height = 120;
- Status:Visible = TRUE;
- Status:Title = "Car Type";
- MakeSlot( Status:Value);
- Status:Owner = Car;
- Status:OwnerSlot = Type;
- ResetImage( Status );
-
-
- /***********************************************************/
- /*** ALL RULES ARE SAVED BELOW ***/
- /***********************************************************/
-
- /**************************************
- ******* RULE: Rule1
- **************************************/
- MakeRule( Rule1, [ ], ( Car:CD < 0.4 ) And ( Car:Power > 150 ),
- SetValue( Car:Speed, Sqrt( Car:Power / Car:CD * 40 ) )
- );
- /**************************************
- ******* RULE: Rule2
- **************************************/
- MakeRule( Rule2, [ ],
- ( Car:CD < 0.6 ) And ( Car:Power > 70 ) And ( Car:Power <= 150 ),
- SetValue( Car:Speed, Sqrt( Car:Power / Car:CD * 50 ) )
- );
-
- /**************************************
- ******* RULE: Rule3
- **************************************/
- MakeRule( Rule3, [ ], ( Car:CD < 0.4 ) And ( Car:Power <= 150 ),
- {
- SetValue( Car:Speed, Sqrt( Car:Power / Car:CD * 45 ) );
- } );
- SetRulePriority( Rule3, 3 );
-
- /**************************************
- ******* RULE: Rule4
- **************************************/
- MakeRule( Rule4, [ ],
- ( Car:Origin #= Italy ) And ( Car:Speed > 150 ),
- SetValue( Car:Type, Sports ) );
- SetRuleComment( Rule4, "If Car is made in Italy and has a speed over 150, then it's a Sports Car." );
-
- /**************************************
- ******* RULE: Rule5
- **************************************/
- MakeRule( Rule5, [ ],
- ( Not( Car:Origin #= Italy ) ) And ( Car:Speed <= 150 ),
- SetValue( Car:Type, Family ) );
- SetRuleComment( Rule5, "If Car is not made in Italy or has a speed less than 150, then it's a Family Car." );
-
-
- /***********************************************************/
- /*** ALL GOALS ARE SAVED BELOW ***/
- /***********************************************************/
-
- /**************************************
- ******* GOAL: Goal1
- **************************************/
- MakeGoal( Goal1, KnownValue?( Car:Type ) );
-
-
- /*************** SPECIAL *****************/
- Reset( );
- IconifyWindow( KTOOLS );
- IconifyWindow( KAPPA );
- ShowWindow( SESSION );
- ShowWindow( INFERENCE );
- PositionWindow( SESSION, 100, 0, 320, 200 );
- PositionWindow( INFERENCE, 0, 200, 640, 250 );
-