home *** CD-ROM | disk | FTP | other *** search
- /***********************************************
- **
- ** CREDIT.KAL: contains an example for classifying credit applicants
- ** based on their previous credit history.
- **
- ***********************************************/
-
- /********************************************************
- ** The problem examplifies the followings:
- **
- ** a) usage of inputforms
- ** b) usage of popup menus
- ** c) customizing OBJECT Menu in the Class-browser
- ** d) descending objects down the class hierarchy
- ** e) forward chaining
- **
- **
- ** Individuals are created as INSTANCES. Clicking on an individual
- ** posts up an input-form, asking for that persons CreditEvent and
- ** PastRating. The data typed in for CreditEvent has to be a kind
- ** of BadCredit or GoodCredit (the name of an Object underneath
- ** either of these classes). The PastRating has to be either Good or
- ** Bad.
- **
- ** Once the rating information is provided for one or more of the Humans,
- ** they can be classified as Ok or NotOk by typing (ProcessRules) on the
- ** KAPPA window. This invokes the forward chaining process which descends
- ** people (moves them down the hierarchy) to the appropriate subClasses.
- **
- ** To load the example, start KAPPA, and select "Open..." from the File menu.
- ** Type in "credit.kal" as the name of the file
- ** and click on "Open" to initiate the interpretation.
- **
- ** You are now ready to classify the persons in the system by clicking
- ** on the "ProcessIndividuals" button, or you may add new persons by
- ** clicking on "NewIndividual".
- **
- *******************************************************************/
-
-
- /***********************************************************/
- /** ALL FUNCTIONS ARE SAVED BELOW ***/
- /***********************************************************/
-
- /**************************************
- ******* FUNCTION: RequestPersonInfo
- ** Takes the name of the person clicked on, and puts up an input
- ** form asking for the CreditEvent and PastRating of the individual.
- **************************************/
- MakeFunction( RequestPersonInfo, [ person ],
- PostInputForm( person, person:CreditEvent, Event,
- person:PastRating, Rating ) );
-
- /**************************************
- ******* FUNCTION: ShowInstnaceMenu
- **
- ** When an INSTANCE is clicked on in the browser, the system looks
- ** for a function called ShowInstanceMenu. If the function exists,
- ** it is called, passing in the name of the instance as the argument,
- ** instead of posting the default menu.
- ** ShowInstanceMenu in this case posts a popup menu of two choices.
- ** Based on the user's selection, the code uses an IF statement to
- ** perform the appropriate actions. If the user selected
- ** "Request Person Information", the system calls the
- ** RequestPersonInfo function passing along the name of the instance.
- ** Otherwise, it sends a message to the person to Classify.
- **************************************/
- MakeFunction( ShowInstanceMenu, [ person ],
- If IsAKindOf? (person, Human)
- Then
- {
- If (PostMenu(person, "Request Person Information", "Classify Person")
- #= "Request Person Information")
- Then RequestPersonInfo( person )
- Else SendMessage( person, Classify );
- } );
-
- /**************************************
- ******* FUNCTION: ProcessIndividuals
- ** Called when the user clicks on the button by the same
- ** name, and calls the inference engine.
- ** All key slots are ASSERT'ED to provide starting point for
- ** the Forward Chainer.
- **************************************/
- MakeFunction( ProcessIndividuals, [ ],
- {
- ForAll [person | Human]
- {
- Assert (person:CreditEvent);
- Assert (person:PastRating);
- };
- ForwardChain();
- }
- );
-
- /**************************************
- ******* FUNCTION: NewIndividual
- ** Called when the user clicks on the button by the same
- ** name, and creates a new person.
- **************************************/
- MakeFunction( NewIndividual, [ ],
- CatchError( {
- PostInputForm( "Enter a new person's name",
- Global:Name, " " );
- MakeInstance( Global:Name, Human );
- RequestPersonInfo( Global:Name );
- },
- PostMessage( "Cannot Create " # Global:Name ) ) );
-
-
- /***********************************************************/
- /*** ALL CLASSES ARE SAVED BELOW ***/
- /***********************************************************/
-
- /**************************************
- ******* CLASS: Human
- **
- ** Defines the Human class with its slots
- **************************************/
- MakeClass( Human, Root );
- MakeSlot( Human:Age );
- MakeSlot( Human:YearsInTraining );
- MakeSlot( Human:License );
- MakeSlot( Human:Car );
- MakeSlot( Human:CreditEvent );
- MakeSlot( Human:PastRating );
- SetSlotOption( Human:PastRating, ALLOWABLE_VALUES, Good, Bad );
- MakeSlot( Human:Rating );
-
- /*** Defines a default Classify Method for all classes and
- ** Objects of type Human. The message, for now, is a NO-OP.
- ** It posts a message box and specifies how to start the
- ** forward chaining process
- ***/
-
- MakeMethod( Human, Classify, [ ],
- PostMessage( FormatValue( "%s\n%s",
- "Option to classify individuals not available",
- "Click Over [Process Individuals] to classify all humans" ) ) );
-
- /**************************************
- ******* CLASS: OK
- **
- ** This is the class where all Humans' of good credit
- ** get descended.
- **
- **************************************/
- MakeClass( OK, Human );
- SetValue( OK:Rating, OK );
-
- /**************************************
- ******* CLASS: NotOK
- **
- ** This is the class where all Humans' of bad credit
- ** get descended.
- **
- **************************************/
- MakeClass( NotOK, Human );
- SetValue( NotOK:Rating, NotOK );
-
- /**************************************
- ******* CLASS: CreditEvents
- **
- ** Defines the general class for all Credit Events
- **************************************/
- MakeClass( CreditEvents, Root );
-
- /**************************************
- ******* CLASS: BadCredit
- **
- ** Defines the general BadCredit class as a specialization
- ** of CreditEvents.
- **************************************/
- MakeClass( BadCredit, CreditEvents );
-
- /**************************************
- ******* CLASS: GoodCredit
- ** Defines the general GoodCredit class as a specialization
- ** of CreditEvents.
- **************************************/
- MakeClass( GoodCredit, CreditEvents );
-
-
-
- /***********************************************************/
- /*** ALL OBJECTS ARE SAVED BELOW ***/
- /***********************************************************/
-
- /**************************************
- ******* OBJECT: Anthony
- **************************************/
- MakeSlot( Global:Name );
-
-
- /**************************************
- ******* OBJECT: Anthony
- **************************************/
- MakeInstance( Anthony, Human );
- SetValue( Anthony:Age, 29 );
- SetValue( Anthony:YearsInTraining, 2 );
- SetValue( Anthony:CreditEvent, NoBankruptcy );
- SetValue( Anthony:PastRating, Good );
-
- /**************************************
- ******* OBJECT: Lisa
- **************************************/
- MakeInstance( Lisa, Human );
- SetValue( Lisa:Age, 47 );
- SetValue( Lisa:YearsInTraining, 4 );
- SetValue( Lisa:CreditEvent, BouncedCheck );
- SetValue( Lisa:PastRating, Good );
-
- /**************************************
- ******* OBJECT: Robert
- **************************************/
- MakeInstance( Robert, Human );
- SetValue( Robert:Age, 49 );
- SetValue( Robert:YearsInTraining, 7 );
- SetValue( Robert:License, RACING );
- SetValue( Robert:Car, RX7_T_Race );
- SetValue( Robert:CreditEvent, Bankruptcy );
- SetValue( Robert:PastRating, Bad );
-
- /**************************************
- ******* OBJECT: Annette
- **************************************/
- MakeInstance( Annette, Human );
- SetValue( Annette:Age, 21 );
- SetValue( Annette:YearsInTraining, 4 );
- SetValue( Annette:CreditEvent, NoBankruptcy );
- SetValue( Annette:PastRating, Good );
-
- /**************************************
- ******* OBJECT: BouncedCheck
- **
- ** Defines an INSTANCE of BadCredit
- **************************************/
- MakeInstance( BouncedCheck, BadCredit );
-
- /**************************************
- ******* OBJECT: Bankruptcy
- **
- ** Defines an INSTANCE of BadCredit
- **************************************/
- MakeInstance( Bankruptcy, BadCredit );
-
- /**************************************
- ******* OBJECT: SatisfactoryAccount
- **
- ** Defines an INSTANCE of GoodCredit
- **************************************/
- MakeInstance( SatisfactoryAccount, GoodCredit );
-
- /**************************************
- ******* OBJECT: NoBankruptcy
- **
- ** Defines an INSTANCE of GoodCredit
- **************************************/
- MakeInstance( NoBankruptcy, GoodCredit );
-
-
-
- /***********************************************************/
- /*** ALL RULES ARE SAVED BELOW ***/
- /***********************************************************/
-
- /**************************************
- ******* RULE: BadBad
- **
- ** If person's CreditEvent is Bad
- ** and person's PastRating is Bad
- ** Then Classify the person as NotOK
- **************************************/
- MakeRule( BadBad, [ person | Human ], ( Not( KnownValue?( person:Rating ) ) )
- And ( IsAKindOf?( person:CreditEvent,
- BadCredit ) )
- And ( person:PastRating
- #= Bad ), MoveInstance( person,
- NotOK ) );
-
- /**************************************
- ******* RULE: GoodGood
- **
- ** If person's CreditEvent is Good
- ** and person's PastRating is Good
- ** Then Classify the person as OK
- **************************************/
- MakeRule( GoodGood, [ person | Human ], ( Not( KnownValue?( person:Rating ) ) )
- And ( IsAKindOf?( person:CreditEvent,
- GoodCredit ) )
- And ( person:PastRating
- #= Good ), MoveInstance( person,
- OK ) );
-
-
-
- /***********************************************************/
- /*** ALL GOALS ARE SAVED BELOW ***/
- /***********************************************************/
-
-
- /***** Special; Buttons to add people and to process individuals *****/
-
- MakeImage( NewIndividual, Button );
- PositionImage( NewIndividual, 5, 5, 160, 25 );
- SetValue( NewIndividual:Action, NewIndividual );
- ShowImage( NewIndividual );
-
- MakeImage( ProcessIndividuals, Button );
- PositionImage( ProcessIndividuals, 5, 40, 160, 25 );
- SetValue( ProcessIndividuals:Action, ProcessIndividuals );
- ShowImage( ProcessIndividuals );
-
- IconifyWindow( KTOOLS );
- IconifyWindow( KAPPA );
- ShowWindow( BROWSER );
- ShowWindow( SESSION );
- PositionWindow( BROWSER, 200, 0, 440, 450 );
- PositionWindow( SESSION, 0, 0, 200, 200 );
-
-