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

  1. /***********************************************
  2. **
  3. ** CREDIT.KAL: contains an example for classifying credit applicants
  4. **             based on their previous credit history.
  5. **
  6. ***********************************************/
  7.  
  8. /********************************************************
  9. **     The problem examplifies the followings:
  10. **              
  11. **              a) usage of inputforms
  12. **              b) usage of popup menus
  13. **              c) customizing OBJECT Menu in the Class-browser
  14. **              d) descending objects down the class hierarchy
  15. **              e) forward chaining
  16. **      
  17. **     
  18. **     Individuals are created as INSTANCES.  Clicking on an individual
  19. **     posts up an input-form, asking for that persons CreditEvent and 
  20. **     PastRating.  The data typed in for CreditEvent has to be a kind
  21. **     of BadCredit or GoodCredit (the name of an Object underneath 
  22. **     either of these classes).  The PastRating has to be either Good or
  23. **     Bad.  
  24. **
  25. **     Once the rating information is provided for one or more of the Humans,
  26. **     they can be classified as Ok or NotOk by typing (ProcessRules) on the 
  27. **     KAPPA window.  This invokes the forward chaining process which descends
  28. **     people (moves them down the hierarchy) to the appropriate subClasses.
  29. **
  30. **     To load the example, start KAPPA, and select "Open..." from the File menu. 
  31. **     Type in "credit.kal" as the name of the file
  32. **     and click on "Open" to initiate the interpretation.
  33. **
  34. **     You are now ready to classify the persons in the system by clicking
  35. **     on the "ProcessIndividuals" button, or you may add new persons by
  36. **     clicking on "NewIndividual".
  37. **
  38. *******************************************************************/
  39.  
  40.  
  41. /***********************************************************/
  42. /**            ALL FUNCTIONS ARE SAVED BELOW             ***/
  43. /***********************************************************/
  44.  
  45.        /**************************************
  46.         *******  FUNCTION: RequestPersonInfo
  47.         ** Takes the name of the person clicked on, and puts up an input 
  48.         ** form asking for the CreditEvent and PastRating of the individual.
  49.         **************************************/
  50. MakeFunction( RequestPersonInfo, [ person ],  
  51.       PostInputForm( person, person:CreditEvent, Event,
  52.                              person:PastRating, Rating ) );
  53.  
  54.        /**************************************
  55.         *******  FUNCTION: ShowInstnaceMenu
  56.         **
  57.         ** When an INSTANCE is clicked on in the browser, the system looks
  58.         ** for a function called ShowInstanceMenu.  If the function exists,
  59.         ** it is called, passing in the name of the instance as the argument,
  60.         ** instead of posting the default menu.
  61.         ** ShowInstanceMenu in this case posts a popup menu of two choices.
  62.         ** Based on the user's selection, the code uses an IF statement to 
  63.         ** perform the appropriate actions.  If the user selected 
  64.         ** "Request Person Information", the system calls the 
  65.         ** RequestPersonInfo function passing along the name of the instance.  
  66.         ** Otherwise, it sends a message to the person to Classify.
  67.        **************************************/
  68. MakeFunction( ShowInstanceMenu, [ person ], 
  69.    If IsAKindOf? (person, Human)
  70.      Then
  71.        {
  72.        If (PostMenu(person, "Request Person Information", "Classify Person")
  73.                      #= "Request Person Information")
  74.          Then RequestPersonInfo( person )
  75.          Else SendMessage( person, Classify );
  76.        } );
  77.  
  78.        /**************************************
  79.         *******  FUNCTION: ProcessIndividuals
  80.         ** Called when the user clicks on the button by the same
  81.         ** name, and calls the inference engine.
  82.         ** All key slots are ASSERT'ED to provide starting point for
  83.         ** the Forward Chainer. 
  84.         **************************************/
  85. MakeFunction( ProcessIndividuals, [ ], 
  86.       {
  87.       ForAll [person | Human]
  88.          {
  89.          Assert (person:CreditEvent);
  90.          Assert (person:PastRating);
  91.          };
  92.       ForwardChain();
  93.       }
  94. );
  95.  
  96.        /**************************************
  97.         *******  FUNCTION: NewIndividual
  98.         ** Called when the user clicks on the button by the same
  99.         ** name, and creates a new person.
  100.         **************************************/
  101. MakeFunction( NewIndividual, [ ], 
  102.         CatchError( {
  103.                     PostInputForm( "Enter a new person's name",
  104.                                        Global:Name, " " );
  105.                     MakeInstance( Global:Name, Human );
  106.                     RequestPersonInfo( Global:Name );
  107.                     }, 
  108.                     PostMessage( "Cannot Create " # Global:Name ) ) );
  109.  
  110.  
  111. /***********************************************************/
  112. /***       ALL CLASSES ARE SAVED BELOW             ***/
  113. /***********************************************************/
  114.  
  115.        /**************************************
  116.         *******  CLASS: Human
  117.         **
  118.         ** Defines the Human class with its slots
  119.         **************************************/
  120. MakeClass( Human, Root );
  121. MakeSlot( Human:Age );
  122. MakeSlot( Human:YearsInTraining );
  123. MakeSlot( Human:License );
  124. MakeSlot( Human:Car );
  125. MakeSlot( Human:CreditEvent );
  126. MakeSlot( Human:PastRating );
  127. SetSlotOption( Human:PastRating, ALLOWABLE_VALUES, Good, Bad );
  128. MakeSlot( Human:Rating );
  129.  
  130.         /*** Defines a default Classify Method for all classes and
  131.          ** Objects of type Human.  The message, for now, is a NO-OP.
  132.          ** It posts a message box and specifies how to start the
  133.          ** forward chaining process 
  134.          ***/
  135.  
  136. MakeMethod( Human, Classify, [ ], 
  137.         PostMessage( FormatValue( "%s\n%s",
  138.              "Option to classify individuals not available",
  139.              "Click Over [Process Individuals] to classify all humans" ) ) );
  140.  
  141.        /**************************************
  142.         *******  CLASS: OK
  143.         ** 
  144.         ** This is the class where all Humans' of good credit
  145.         ** get descended.
  146.         **
  147.         **************************************/
  148. MakeClass( OK, Human );
  149. SetValue( OK:Rating, OK );
  150.  
  151.        /**************************************
  152.         *******  CLASS: NotOK
  153.         **
  154.         ** This is the class where all Humans' of bad credit
  155.         ** get descended.
  156.         **
  157.         **************************************/
  158. MakeClass( NotOK, Human );
  159. SetValue( NotOK:Rating, NotOK );
  160.  
  161.        /**************************************
  162.         *******  CLASS: CreditEvents
  163.         **
  164.         ** Defines the general class for all Credit Events
  165.         **************************************/
  166. MakeClass( CreditEvents, Root );
  167.  
  168.        /**************************************
  169.         *******  CLASS: BadCredit
  170.         **
  171.         ** Defines the general BadCredit class as a specialization
  172.         ** of CreditEvents.
  173.         **************************************/
  174. MakeClass( BadCredit, CreditEvents );
  175.  
  176.        /**************************************
  177.         *******  CLASS: GoodCredit
  178.         ** Defines the general GoodCredit class as a specialization
  179.         ** of CreditEvents.
  180.         **************************************/
  181. MakeClass( GoodCredit, CreditEvents );
  182.  
  183.  
  184.  
  185. /***********************************************************/
  186. /***       ALL OBJECTS ARE SAVED BELOW             ***/
  187. /***********************************************************/
  188.  
  189.        /**************************************
  190.         *******  OBJECT: Anthony
  191.         **************************************/
  192. MakeSlot( Global:Name );
  193.  
  194.  
  195.        /**************************************
  196.         *******  OBJECT: Anthony
  197.         **************************************/
  198. MakeInstance( Anthony, Human );
  199. SetValue( Anthony:Age, 29 );
  200. SetValue( Anthony:YearsInTraining, 2 );
  201. SetValue( Anthony:CreditEvent, NoBankruptcy );
  202. SetValue( Anthony:PastRating, Good );
  203.  
  204.        /**************************************
  205.         *******  OBJECT: Lisa
  206.         **************************************/
  207. MakeInstance( Lisa, Human );
  208. SetValue( Lisa:Age, 47 );
  209. SetValue( Lisa:YearsInTraining, 4 );
  210. SetValue( Lisa:CreditEvent, BouncedCheck );
  211. SetValue( Lisa:PastRating, Good );
  212.  
  213.        /**************************************
  214.         *******  OBJECT: Robert
  215.         **************************************/
  216. MakeInstance( Robert, Human );
  217. SetValue( Robert:Age, 49 );
  218. SetValue( Robert:YearsInTraining, 7 );
  219. SetValue( Robert:License, RACING );
  220. SetValue( Robert:Car, RX7_T_Race );
  221. SetValue( Robert:CreditEvent, Bankruptcy );
  222. SetValue( Robert:PastRating, Bad );
  223.  
  224.        /**************************************
  225.         *******  OBJECT: Annette
  226.         **************************************/
  227. MakeInstance( Annette, Human );
  228. SetValue( Annette:Age, 21 );
  229. SetValue( Annette:YearsInTraining, 4 );
  230. SetValue( Annette:CreditEvent, NoBankruptcy );
  231. SetValue( Annette:PastRating, Good );
  232.  
  233.        /**************************************
  234.         *******  OBJECT: BouncedCheck
  235.         ** 
  236.         ** Defines an INSTANCE of BadCredit
  237.         **************************************/
  238. MakeInstance( BouncedCheck, BadCredit );
  239.  
  240.        /**************************************
  241.         *******  OBJECT: Bankruptcy
  242.         **
  243.         ** Defines an INSTANCE of BadCredit
  244.         **************************************/
  245. MakeInstance( Bankruptcy, BadCredit );
  246.  
  247.        /**************************************
  248.         *******  OBJECT: SatisfactoryAccount
  249.         **
  250.         ** Defines an INSTANCE of GoodCredit
  251.         **************************************/
  252. MakeInstance( SatisfactoryAccount, GoodCredit );
  253.  
  254.        /**************************************
  255.         *******  OBJECT: NoBankruptcy
  256.         ** 
  257.         ** Defines an INSTANCE of GoodCredit
  258.         **************************************/
  259. MakeInstance( NoBankruptcy, GoodCredit );
  260.  
  261.  
  262.  
  263. /***********************************************************/
  264. /***         ALL RULES ARE SAVED BELOW             ***/
  265. /***********************************************************/
  266.  
  267.        /**************************************
  268.         *******  RULE: BadBad
  269.         **
  270.         ** If person's CreditEvent is Bad
  271.         **    and person's PastRating is Bad
  272.         ** Then Classify the person as NotOK
  273.         **************************************/
  274. MakeRule( BadBad, [ person | Human ], ( Not( KnownValue?( person:Rating ) ) )
  275.                                         And ( IsAKindOf?( person:CreditEvent,
  276.                                                           BadCredit ) )
  277.                                         And ( person:PastRating
  278.                                                 #= Bad ), MoveInstance( person,
  279.                                                                       NotOK ) );
  280.  
  281.        /**************************************
  282.         *******  RULE: GoodGood
  283.         **
  284.         ** If person's CreditEvent is Good
  285.         **    and person's PastRating is Good
  286.         ** Then Classify the person as OK
  287.         **************************************/
  288. MakeRule( GoodGood, [ person | Human ], ( Not( KnownValue?( person:Rating ) ) )
  289.                                           And ( IsAKindOf?( person:CreditEvent,
  290.                                                             GoodCredit ) )
  291.                                           And ( person:PastRating
  292.                                                   #= Good ), MoveInstance( person,
  293.                                                                          OK ) );
  294.  
  295.  
  296.  
  297. /***********************************************************/
  298. /***         ALL GOALS ARE SAVED BELOW             ***/
  299. /***********************************************************/
  300.  
  301.  
  302. /*****  Special; Buttons to add people and to process individuals  *****/
  303.  
  304. MakeImage( NewIndividual, Button );
  305. PositionImage( NewIndividual, 5, 5, 160, 25 );
  306. SetValue( NewIndividual:Action, NewIndividual );
  307. ShowImage( NewIndividual );
  308.  
  309. MakeImage( ProcessIndividuals, Button );
  310. PositionImage( ProcessIndividuals, 5, 40, 160, 25 );
  311. SetValue( ProcessIndividuals:Action, ProcessIndividuals );
  312. ShowImage( ProcessIndividuals );
  313.  
  314. IconifyWindow( KTOOLS );
  315. IconifyWindow( KAPPA );
  316. ShowWindow( BROWSER );
  317. ShowWindow( SESSION );
  318. PositionWindow( BROWSER, 200, 0, 440, 450 );
  319. PositionWindow( SESSION, 0, 0, 200, 200 );
  320.  
  321.