home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Interactive Guide / c-cplusplus-interactive-guide.iso / c_ref / csource4 / 241_01 / inferenc.ci < prev    next >
Encoding:
Text File  |  1987-08-31  |  6.0 KB  |  244 lines

  1. /*
  2. HEADER:         CUG241;
  3. TITLE:          Inference Engine for Expert System;
  4. DATE:           12/30/85;
  5. VERSION:
  6. DESCRIPTION:   "Source code for inference engine for an Expert System.";
  7. KEYWORDS:       Artificial Intelligence, expert systems, inference engine;
  8. SYSTEM:         MS-DOS or UNIX System V;
  9. FILENAME:       INFERENC.C;
  10. WARNINGS:      "User-supported, author requests non-commercial use"
  11. AUTHORS:        George Hageman - Softman Enterprizes; 
  12. COMPILERS:      Microsoft C V3.00 or UNIX System V Portable C Compiler;
  13. REFERENCES:     AUTHOR:   William F. Clocksin, Christopher S. Mellish;
  14.                 TITLE:    "Programming in Prolog";
  15.  
  16.                 AUTHOR:   Mitch Derick, Linda Derick;
  17.                 TITLE:    "MVP-FORTH Expert-2 Tutorial";
  18.  
  19.                 AUTHOR:   Jack Park;
  20.                 TITLE:    "Expert Systems & the Weather - Dr. Dobb's Journal";
  21.                 CITATION:  "April 1984, pp. 24-28."
  22.  
  23.                 AUTHOR:   Jack Park;
  24.                 TITLE:    "MVP-FORTH Expert System Toolkit";
  25.  
  26.                 AUTHOR:   C. H. Ting;
  27.                 TITLE:    "Inside F83";
  28.  
  29.                 AUTHOR:   Patrick H. Winston, Berthold Klaus, Paul Horn;
  30.                 TITLE:    "LISP"
  31. ENDREF
  32. */
  33.  
  34. /*****************************************************************
  35. **                                **
  36. **      Inference -- (C) Copyright 1985 George Hageman    **
  37. **                                **
  38. **        user-supported software:                **
  39. **                                **
  40. **            George Hageman                **
  41. **            P.O. Box 11234                **
  42. **            Boulder, Colorado 80302            **
  43. **                                **
  44. *****************************************************************/
  45.  
  46. /*******************************************************************
  47. **
  48. **    inference engine main routine
  49. **
  50. *******************************************************************/
  51.  
  52. #include <stdio.h>
  53. #include <string.h>
  54.  
  55. #include "expert.h"
  56. #include "routine.h"
  57.  
  58. #define    MAX_KNOWN    100
  59.  
  60. int    numHypot, hypStack[MAX_HYPS],strBuffOfst ;
  61. char    strBuff[MAX_STRING_BUFF] ;
  62. int    ruleBuffOfst ;
  63. int    knownTrue[MAX_KNOWN], knownFalse[MAX_KNOWN] ;
  64. int    numTrue, numFalse ;
  65. struct  rule_statement_r ruleBuff[MAX_RULE_STATEMENTS] ;
  66.  
  67. int    main(argc,argv)
  68. int argc ;
  69. char **argv ;
  70.  
  71. {
  72. int    i,consequent ;
  73. int    proved ;
  74. int    p_value ;
  75. FILE    *infile ;
  76.  
  77. for( proved = 0; proved < MAX_STRING_BUFF; proved ++ )
  78.     strBuff[proved] = 0 ;
  79.  
  80. proved = FALSE ;
  81.  
  82. #ifdef    DEBUG
  83.     fprintf(stdout,"\nDEBUG-RULECOMP argc=%d ",argc) ;
  84. #endif
  85.  
  86. if(argc != 2 )
  87.     {
  88.     fprintf(stdout, "\n\n Usage: inference in.file \n " ) ;
  89.     exit(RETURN_ROUTINE_FALSE) ;
  90.     }
  91. infile = fopen( argv[1], "rb" );
  92. if(infile == NULL)
  93.     {
  94.     fprintf(stdout,"\n\n Cannot open input file %s \n ", argv[1]);
  95.     exit(RETURN_ROUTINE_FALSE) ;
  96.     }
  97. for(i=0;i<MAX_KNOWN;i++)
  98.     knownTrue[i]=knownFalse[i]=0 ;
  99. /*
  100. **
  101. **    read in the compiled rules
  102. **
  103. */
  104.  
  105. fread(&numHypot,sizeof(int),1,infile) ;
  106. fread(hypStack,sizeof(int),numHypot,infile) ;
  107. fread(&ruleBuffOfst,sizeof(int),1,infile) ;
  108. fread(ruleBuff,2*sizeof(int),ruleBuffOfst,infile) ;
  109. fread(&strBuffOfst,sizeof(int),1,infile) ;
  110. fread(strBuff,1,strBuffOfst,infile) ;
  111.  
  112. #ifdef    DEBUG
  113.     
  114. printf("\nDEBUG-main, numHypot = %d",numHypot) ;
  115. for(i=0;i<numHypot;i++)
  116.     {
  117.     printf("\nDEBUG-main, consequent[%d]= %d, string=%s",i,hypStack[i],
  118.             &strBuff[(ruleBuff[hypStack[i]].string)] ) ;
  119.     }
  120. printf("\nDEBUG-main, number of rules = %d",ruleBuffOfst) ;
  121. printf("\nDEBUG-main, number of bytes in stringbuffer=%d",strBuffOfst) ;
  122. for(i=0;i<ruleBuffOfst;i++)
  123.     {
  124.     if(ruleBuff[i].flag !=0)
  125.         {
  126.         printf("\nDEBUG-main, rule[%d].flag=%d, rule[%d].string= %d->%s"
  127.                   ,i,ruleBuff[i].flag,i,ruleBuff[i].string,&strBuff[ruleBuff[i].string]) ;
  128.         }
  129.     }
  130. printf("\n") ;
  131.  
  132. #endif
  133.  
  134. for( i=0 ; i < numHypot ; i++ )
  135.  
  136.     {
  137.     consequent = hypStack[i] ;
  138.  
  139. #ifdef DEBUG
  140.     printf("\nDEBUG-main, consequent = %d ",consequent ) ;
  141. #endif
  142.  
  143.     if(weKnow(consequent,&p_value) == TRUE)
  144.         {
  145. #ifdef DEBUG
  146.     printf("\nDEBUG-main, we knew this consequent was ") ;
  147.     if(p_value == TRUE)
  148.         printf("-- TRUE") ;
  149.     else
  150.         printf("-- FALSE") ;
  151. #endif
  152.         continue ;
  153.         }
  154.         
  155.     if ( verify(consequent) == TRUE )
  156.         {
  157.         
  158. #ifdef DEBUG
  159.             printf("\nDEBUG-main, consequent verified TRUE " ) ;
  160. #endif
  161.         
  162.         if( (ruleBuff[consequent].flag == ROUTINE_TRUE) ||
  163.             (ruleBuff[consequent].flag == ROUTINE_TRUE_HYP) )
  164.             {
  165.             if(weKnow(consequent,&p_value) == TRUE)
  166.                 continue ;
  167. #ifdef DEBUG
  168.             printf("\nRuning Routine %s",&strBuff[ruleBuff[consequent].string]) ;
  169. #endif
  170.             if (runRoutine(consequent) == TRUE)
  171.                 {
  172.                 knownTrue[numTrue++]=ruleBuff[consequent].string ;
  173. #ifdef DEBUG
  174.                 printf(" -- TRUE\n") ;
  175. #endif
  176.                 if(ruleBuff[consequent].flag == ROUTINE_TRUE_HYP)
  177.                     {
  178.                     printf("\nCONCLUSION\n") ;
  179.                     exit(RETURN_ROUTINE_TRUE) ;
  180.                     }
  181.                 }
  182.             else
  183.                 {
  184.                 knownFalse[numFalse++]=ruleBuff[consequent].string ;
  185. #ifdef DEBUG
  186.                 printf(" -- FALSE\n") ;
  187. #endif
  188.                 }
  189.             }
  190.         else
  191.             {
  192.             knownTrue[numTrue++]=ruleBuff[consequent].string ;
  193.             proved = TRUE ;
  194.             printf("\nI infer that : %s\n",&strBuff[ruleBuff[consequent].string]) ;
  195.             if(ruleBuff[consequent].flag == STRING_TRUE_HYP)
  196.                 {
  197.                 printf("\nCONCLUSION\n") ;
  198.                 exit(RETURN_ROUTINE_TRUE) ;
  199.                 }
  200.             }
  201.         }
  202.     else
  203.         {
  204. #ifdef DEBUG
  205.             printf("\nDEBUG-main, consequent not proved " ) ;
  206. #endif
  207.         }
  208.     }
  209. if(proved == FALSE)
  210.     {
  211.     printf("\n I can't prove anything\n") ;
  212.     exit(RETURN_ROUTINE_FALSE) ;
  213.     }
  214. exit(RETURN_ROUTINE_TRUE) ;
  215. }
  216.  
  217. /* - -<<hidden tof */
  218.  
  219. /******************************************************
  220. **
  221. **    known(hypot,knownFile,numKnown)
  222. **
  223. **    checks to see if hypot is in the stack knownFile of length
  224. **        numKnown
  225. **
  226. **    returns true if known, false otherwise
  227. **
  228. ******************************************************/
  229.  
  230.  
  231.  
  232. known(hypot,knownFile,numKnown)
  233.     int     hypot,numKnown ;
  234.     int    knownFile[] ;
  235. {
  236. int i ;
  237. for (i = 0 ; i < numKnown ; i++ )
  238.     if (ruleBuff[hypot].string == knownFile[i] )
  239.         return(TRUE) ;
  240. return(FALSE) ;
  241. }
  242.  
  243.  
  244.