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

  1.         INFERENCE          -- SOFTMAN ENTERPRIZES --        Dec. 30, 1985
  2.  
  3.  
  4.         INTRODUCTION: 
  5.  
  6.         The  software contained in this distribution is copyright (C)  by 
  7.         George   Hageman   1985   and  is  released   into   the   public               
  8.         domain with the following restrictions:                         
  9.  
  10.              (1)   This  software  is intended  for  non-commertial usage.                                              
  11.              (2)    I   am   held  save  from  damages   resulting   from               
  12.              its use, and
  13.              (3)   The following concepts and legal jargon are agreed  to 
  14.              by the user of this software.
  15.  
  16.              User-supported software concept:
  17.  
  18.                   IF          you find use for this software
  19.                   ANDIF       it saves you some development time
  20.                   THEN            send me $10.00
  21.                   ANDTHENHYP      you will feel good!
  22.  
  23.         This source code is provided on an "as is" basis without warranty 
  24.         of any kind,  expressed or implied,  including but not limited to 
  25.         the  implied  warranties  of merchantability and  fitness  for  a 
  26.         particular   purpose.    The  entire  risk  as  to  quality   and 
  27.         performance  of this software is with you.   Should the  software 
  28.         prove  defective,  you  assume the entire cost of  all  necessary 
  29.         repair, servicing, or correction.  In no event will the author be 
  30.         liable to you for any damages,  including any lost profits,  lost 
  31.         savings, or other incidental or consequential damages arising out 
  32.         of  the   use  of inability to use this software.   In  short  my 
  33.         friends,  I  have done  a reasonable amount of work in  debugging 
  34.         this  software and I think it is pretty good but,  as  you  know, 
  35.         there  is always some chance that a bug is still lurking  around. 
  36.         If you should happen to be lucky enough to  find one,  please let 
  37.         me know so I    can make an attempt to fix it.          
  38.  
  39.              The  following  is  a short description of how  to  use  the 
  40.         inference  engine  and rule-compiler contained in  this  software 
  41.         release.    The source and object files for the rule compiler and 
  42.         the inference engine are contained in the files  named  *.?R  and
  43.         *.?I, respectively.   There  are  common files in  each  library.  
  44.         These  common  files  are header files which are used  to  define 
  45.         common terms between the different sources.    The most important 
  46.         header file is the file named "expert.h" which not only  contains 
  47.         common  definitions  used  between  the  rule  compiler  and  the 
  48.         inference  engine,  but has a short description of their usage as 
  49.         well.    This inference engine, and its associated rule compiler, 
  50.         represents  a  significant  time investment for  me,  so  if  you 
  51.         believe in the  shareware concept please remember my address.
  52.  
  53.                             George W. Hageman
  54.                             P.O. Box 11234
  55.                             Boulder, Colorado  80301
  56.  
  57.  
  58.  
  59.  
  60.  
  61.         George W. Hageman            --1--
  62.  
  63.  
  64.  
  65.  
  66.  
  67.         INFERENCE          -- SOFTMAN ENTERPRIZES --        Dec. 30, 1985
  68.  
  69.  
  70.              This  software compiles using the Microsoft C  Compiler  Rev 
  71.         3.0  using the make function which comes with the Microsoft Macro 
  72.         Assembler Rev.  4.0.  I have nothing but good things to say about 
  73.         these  two products and suggest that you consider their  purchase 
  74.         if  you are into serious software development for the  PC.   This 
  75.         software  also compiles and runs under UNIX system  V.   Use  the 
  76.         UNIXSV  flag  in the makefile or use a -DUNIXSV when you  compile 
  77.         it.
  78.  
  79.  
  80.  
  81.  
  82.  
  83.  
  84.  
  85.  
  86.  
  87.  
  88.  
  89.  
  90.  
  91.  
  92.  
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.         George W. Hageman            --2--
  128.  
  129.  
  130.  
  131.  
  132.  
  133.         INFERENCE          -- SOFTMAN ENTERPRIZES --        Dec. 30, 1985
  134.  
  135.  
  136.         INFERENCE ENGINES:
  137.  
  138.              An  inference engine is merely a program which  attempts  to 
  139.         prove consequents given a certain set of antecedents and a set of 
  140.         rules  which define the TRUTH or FALSEness of each consequent  in 
  141.         terms  of the antecedents.     The consequents,  antecedents  and 
  142.         rules  for  this  inference engine are contained in a  text  file 
  143.         which is compiled by the rule-compiler into a form the  inference 
  144.         engine  can understand.   Often these two functions are contained 
  145.         in the same executeable,  but I have decided to split them up  to 
  146.         make the inference engine as small as possible.
  147.  
  148.              Rules  are collections of ANTECEDENTS and CONSEQUENTS formed 
  149.         into  TRUTH statements.   Each rule is an attempt state that  "If 
  150.         all of the antecedents for this particular RULE are  TRUE,   then 
  151.         all  of the consequents connected to this rule are TRUE.   If one 
  152.         or  more  of the antecedents for a RULE are  FALSE,  then  it  is 
  153.         assumed that this rule cannot prove the TRUTH of the consequents, 
  154.         but  this does not necessarily prove the consequents FALSE  since 
  155.         some  other rule may prove them TRUE."  Rules must have at  least 
  156.         one  ANTECEDENT  and  at least one CONSEQUENT  to  be  considered 
  157.         valid. 
  158.  
  159.              Each   ANTECEDENT  and  CONSEQUENT  is  a  simple  statement 
  160.         consisting  of  a  leading  KEYWORD,   and  a  FOLLOWING  STRING.  
  161.         KEYWORDS tell the inference engine what the FOLLOWING STRING will 
  162.         mean or what is to be done with it.  The FOLLOWING STRINGs may be 
  163.         either  in upper or lower case and are either statements such  as 
  164.         "THE  ANIMAL IS A BAT",  or,  a pathname to an executeable  which 
  165.         will  be loaded and executed depending on what is defined by  the 
  166.         KEYWORD. Strings, except for the number of leading blanks, can be 
  167.         considered equal only if they are identical.  The reason for this 
  168.         rule  will  become  apparent later.   An example  of  a  pathname 
  169.         FOLLOWING STRING is "/b1/hageman/expert/storm/gt_3200  data.fil".  
  170.         Note  that  the strings denoting pathnames should be  exactly  as 
  171.         they  would  be if the pathname were to be given at  a  terminal, 
  172.         also,  you  may  include  parameters with  any  pathname.   These 
  173.         parameters  are no different than the parameters that  you  would 
  174.         use  if  you  were initiating the executeable from  the  terminal 
  175.         rather than via the inference engine.    Under MS_DOS these  path 
  176.         names  can  either  be  upper or lower case,  and  for  the  UNIX 
  177.         operating system, they must correspond to the exact path name.
  178.  
  179.  
  180.  
  181.  
  182.  
  183.  
  184.  
  185.  
  186.  
  187.  
  188.  
  189.  
  190.  
  191.  
  192.  
  193.         George W. Hageman            --3--
  194.  
  195.  
  196.  
  197.  
  198.  
  199.         INFERENCE          -- SOFTMAN ENTERPRIZES --        Dec. 30, 1985
  200.  
  201.  
  202.         QUICK AND DIRTY:
  203.  
  204.              Impatient?   Well  here  are some quick ways to get  started 
  205.         with the inference engine,  leave all that reading till later!
  206.  
  207.         ONE:
  208.  
  209.              Use  your  text  editor (WS in the  Non-document  mode  only 
  210.         Please) to create a quick rule file,  or use the animals  example 
  211.         contained in this release.   Skip to the next page if you want to 
  212.         find out what the KEYWORDS are and how to use them.
  213.  
  214.         TWO:
  215.  
  216.              Compile the rules with the rule-compiler by typing..
  217.  
  218.              rulecomp inputfile outputfile 
  219.  
  220.              where  the inputfile is the filename of the file  containing 
  221.         your  rules,  and the outputfile is the file inwhich you want the 
  222.         compiled rules to be written to.  Caution, the rule compiler does 
  223.         not  check  for the equivalence of the inputfile  and  outputfile 
  224.         filenames,   if they are identical you will probably have to type 
  225.         in your rules again.
  226.  
  227.         THREE:
  228.  
  229.              Run the inference engine by typing ..
  230.  
  231.              inference outputfile
  232.  
  233.              answer the questions and go back to step ONE if you found an 
  234.         error with your rules or you want to expand them.
  235.  
  236.  
  237.  
  238.  
  239.  
  240.  
  241.  
  242.  
  243.  
  244.  
  245.  
  246.  
  247.  
  248.  
  249.  
  250.  
  251.  
  252.  
  253.  
  254.  
  255.  
  256.  
  257.  
  258.  
  259.         George W. Hageman            --4--
  260.  
  261.  
  262.  
  263.  
  264.  
  265.         INFERENCE          -- SOFTMAN ENTERPRIZES --        Dec. 30, 1985
  266.  
  267.  
  268.         KEYWORDS:
  269.  
  270.              The  following are the KEYWORDS which the rule compiler  can 
  271.         recognize,  and  a short description of what each means  and  how 
  272.         each  would be used.    Note that the members of each group  have 
  273.         identical meaning and can therefore be substituted for each other 
  274.         without  effecting the sense of the rule.   Latter examples  will 
  275.         attempt to demonstrate this fact.
  276.  
  277.  
  278.  
  279.         IF, AND, ANDIF:
  280.  
  281.              These KEYWORDS define the FOLLOWING STRING as an ANTECEDENT, 
  282.              and  the TRUTH sense of the string is TRUE if the string  is 
  283.              TRUE.    These  KEYWORDS can be used interchangeable without 
  284.              effecting the sense of the rule being expressed.
  285.  
  286.                   Example:
  287.  
  288.                        IF the animal is a mammal
  289.                        ANDIF the animal has hooves
  290.                        AND the animal has horns         
  291.  
  292.                   This is equivalent to:
  293.  
  294.                        AND the animal is a mammal
  295.                        IF the animal has hooves
  296.                        ANDIF the animal has horns
  297.  
  298.              Note that since the antecedent part of a rule is essentially 
  299.              a  large AND statement,  the order in which  the  individual 
  300.              statements  are  arranged  is a matter  of  esthetics  only.  
  301.              However,  it may be more readable to the human, if a certain 
  302.              order is maintained.
  303.  
  304.         IFNOT, ANDNOT
  305.  
  306.              These  KEYWORDS  are  essentially  identical  to  the  above 
  307.              KEYWORDS except that the sense of the statement is reversed.   
  308.              That is to say that if the following string is TRUE then the 
  309.              truth   value   of   the  statement  is  FALSE.              
  310.  
  311.                   Example:
  312.  
  313.                        IFNOT the animal is a mammal
  314.                        ANDNOT the animal has smooth skin
  315.                        ANDNOT the animal breaths air
  316.                        THEN animal is a fish 
  317.  
  318.  
  319.  
  320.  
  321.  
  322.  
  323.  
  324.  
  325.         George W. Hageman            --5--
  326.  
  327.  
  328.  
  329.  
  330.  
  331.         INFERENCE          -- SOFTMAN ENTERPRIZES --        Dec. 30, 1985
  332.  
  333.  
  334.         IFRUN, ANDRUN, ANDIFRUN
  335.  
  336.              These KEYWORDS tell the inference engine that the  FOLLOWING 
  337.              STRING is to be used as a pathname to an executeable.   This 
  338.              executeable  is to be loaded and run and the resultant TRUTH 
  339.              value returned when the routine exits is the TRUTH value  of 
  340.              the ANTECEDENT statement.   Like the AND, IF, ANDIF KEYWORDS 
  341.              above,  each  of  these  may be substituted for any  of  the 
  342.              others  without effecting the sense of the  rule  statement.   
  343.              Note  that  the  full path name of the  executeable  is  not 
  344.              needed  if  the  executeable  file resides  in  the  working 
  345.              directory from which the inference engine was initiated.
  346.  
  347.                   Example:
  348.  
  349.                        IFRUN /b1/hageman/expert/gt3000
  350.                        ANDRUN /b1/hageman/expert/sedir direction.dat
  351.                        ANDIFRUN falling barompress
  352.                        THEN sorry about the picnic
  353.                
  354.         IFNOTRUN, ANDNOTRUN     
  355.  
  356.              These are used as the KEYWORDS above are used -- to initiate 
  357.              the execution of an ANTECEDENT executeable file, except that 
  358.              the truth value of the result is reversed as with the IFNOT, 
  359.              ANDNOT and the ANDIFNOT KEYWORDS.
  360.  
  361.                   Example:
  362.                        
  363.                        IFNOTRUN gt3000
  364.                        ANDNOTRUN sedir direction.dat
  365.                        ANDNOTRUN falling barompress
  366.                        THEN how about a picnic?
  367.          
  368.         THEN, ANDTHEN, THENHYP, ANDTHENHYP    
  369.  
  370.              These KEYWORDS tell the inference engine that the  FOLLOWING 
  371.              STRING  is  a  CONSEQUENT.     If  all  of  the  immediately 
  372.              proceeding ANTECEDENTS have a truth value of TRUE,  then the 
  373.              inference  engine INFERS that the CONSEQUENT is  TRUE.   The 
  374.              THEN KEYWORDS ending in "HYP" tell the inference engine that 
  375.              the  FOLLOWING STRING is an ending CONCLUSION and no further 
  376.              processing  or inferencing is required.   The  routine  will 
  377.              exit  when one of the THENHYP or ANDTHENHYP CONSEQUENTS  are 
  378.              proven   TRUE.     Therefore,   one  should  use  the  "HYP" 
  379.              CONSEQUENT KEYWORDS with care. 
  380.  
  381.                   Examples:
  382.  
  383.                        IF you have an aunt
  384.                        ANDIF your aunt has a child
  385.                        THEN you have a cousin
  386.  
  387.                        IF you have a cousin
  388.                        THENHYP you have at least two relatives
  389.  
  390.  
  391.         George W. Hageman            --6--
  392.  
  393.  
  394.  
  395.  
  396.  
  397.         INFERENCE          -- SOFTMAN ENTERPRIZES --        Dec. 30, 1985
  398.  
  399.  
  400.         THENRUN, ANDTHENRUN, THENRUNHYP, ANDTHENRUNHYP 
  401.  
  402.              These are similar to the THEN,  ANDTHEN etc. KEYWORDS except 
  403.              they  perform  the  loading and execution of  the  FOLLOWING 
  404.              STRING  path  name  if they  are  proven  TRUE.   The  value 
  405.              returned  by  the executeable file loaded becomes the  value 
  406.              remembered  for  the  CONSEQUENT.   The truth  value  for  a 
  407.              CONSEQUENT proceeded with the KEYWORDS of THEN or ANDTHEN is 
  408.              only remembered if it is proven TRUE.  However, with the RUN 
  409.              type of CONSEQUENT since it is initiated only when found  to 
  410.              be proven, the predicate value it returns is remembered even 
  411.              if  it is FALSE.  This prevents the rerunning of  CONSEQUENT 
  412.              routines.  
  413.  
  414.              In this manner one can use rules to determine weather or not 
  415.              a particular routine should be executed,  then use the truth 
  416.              value  returned  by the routine upon its exit in other  rule 
  417.              statements.   An obvious use for such a function would be in 
  418.              the field of diagnostics.   Through a set of rules it  could 
  419.              be  determined  that a particular diagnostic should be  run,  
  420.              once  the diagnostic has been run,  further rules could  use 
  421.              the  fact  of whether the diagnostic test passed  (TRUE)  or 
  422.              failed (FALSE) to make decisions about the further isolation 
  423.              of the hardware failure.
  424.  
  425.                   Example:
  426.                        
  427.                        !
  428.                        IFNOTRUN isdev1
  429.                        IFNOTRUN isdev2
  430.                        IFNOTRUN isdev3
  431.                        THENHYP there are no devices to run diagnostics on
  432.                        !     
  433.                        ! see note below for the following rule
  434.                        !
  435.                        IF isdev1
  436.                        ANDIFRUN dev1diag
  437.                        THENHYP device one is faulty
  438.                        !
  439.                        IFRUN isdev2
  440.                        ANDIFRUN dev2diag
  441.                        THENHYP device two is faulty
  442.                        !
  443.                        IFRUN isdev3
  444.                        ANDIFRUN dev3diag
  445.                        THENHYP device three is faulty
  446.                        !
  447.                        IFNOTRUN dev1diag
  448.                        IFNOTRUN dev2diag
  449.                        IFNOTRUN dev3diag
  450.                        IFRUN isdev1
  451.                        ANDRUN isdev2
  452.                        THENRUN diag12
  453.                        !
  454.                        !
  455.  
  456.  
  457.         George W. Hageman            --7--
  458.  
  459.  
  460.  
  461.  
  462.  
  463.         INFERENCE          -- SOFTMAN ENTERPRIZES --        Dec. 30, 1985
  464.  
  465.  
  466.                        !
  467.                        IFRUN diag12
  468.                        THENHYP device two is suspect remove and re-run test
  469.                        !
  470.                        IFNOTRUN diag12
  471.                        THENHYP device one is suspect, remove and re-run test 
  472.                        !
  473.  
  474.              Note:
  475.  
  476.              You   have  probably  noticed  that  the  FOLLOWING  STRINGS 
  477.              associated  with  the  RUN KEYWORDS and  the  IF,  AND  etc. 
  478.              KEYWORDS are interchangeable.   This is due to fact that the 
  479.              TRUTH  of  a  string is kept as a pointer  into  the  string 
  480.              buffer,  and therefore have no information concerning  their 
  481.              nature and only have meaning when used in conjunction with a 
  482.              KEYWORD.  If you are sure that the string "isdev1" will have 
  483.              its   truth  determined  earlier  by  running  the   routine 
  484.              "isdev1",   Then you may use it as a regular string in later 
  485.              rules,  however,  if its truth has not been determined  then 
  486.              the  inference  engine  will ask you for the  truth  of  the 
  487.              statement  "isdev1" rather than running the routine.   To be 
  488.              sure  use  the "RUN" form for the strings  which  relate  to 
  489.              executeables,   they will only be run once as it is,  so you 
  490.              don't really have to keep them straight.
  491.  
  492.                   Notice also that if the first rule is not proved by the 
  493.              fact  that  isdev1  turns up being TRUE  and  therefore  the 
  494.              antecedent  statement  is FALSE due to the reverse sense  of 
  495.              the IFNOTRUN KEYWORD,  then the others will not be run until 
  496.              they are encountered in other rules containing them.   So be 
  497.              safe and use the RUN forms of the KEYWORDS if you intend the 
  498.              execution of an object.
  499.          
  500.  
  501.  
  502.  
  503.  
  504.  
  505.  
  506.  
  507.  
  508.  
  509.  
  510.  
  511.  
  512.  
  513.  
  514.  
  515.  
  516.  
  517.  
  518.  
  519.  
  520.  
  521.  
  522.  
  523.         George W. Hageman            --8--
  524.  
  525.  
  526.  
  527.  
  528.  
  529.         INFERENCE          -- SOFTMAN ENTERPRIZES --        Dec. 30, 1985
  530.  
  531.  
  532.         USAGE:
  533.  
  534.              Ok,  now  that we know what all the key words are,  how does 
  535.         one go about using an inference engine, and more specifically how 
  536.         does one use this inference engine and what for?  
  537.              
  538.              People  usually use inference engines  as part of  what  are 
  539.         known  as expert systems.   Expert systems are a study associated 
  540.         with  a  branch  of  software  engineering  known  as  Artificial 
  541.         Intelligence (AI).    (I am probably going to get some heat  from 
  542.         that  statement).    Expert systems are supposed to mimic the way 
  543.         in which human experts deal with a particular physical or  mental 
  544.         problem.    A  clear  example  is an expert  system  which  could 
  545.         isolate  a  fault within a complex computer system as well as  or 
  546.         almost  as well as its human counterpart.    It is apparent  that 
  547.         the  computer  expert  system  would lack  much  of  the  tactile 
  548.         resources  the human expert would have,  but the computer  expert 
  549.         could still be of great value when coupled with a novice computer 
  550.         user.    In  this way the computer expert would rely on the human 
  551.         to  perform  actions  and observations  the  computer  expert  is 
  552.         incapable  of  doing.   The computer expert and the  novice  then 
  553.         could form a team which might perform as well as the human expert 
  554.         alone and at a probably much lower per/hour labor rate.
  555.  
  556.              Unfortunately,  in  order to develop an expert  system,  one 
  557.         must  either  be  an expert in the area one wants to  develop  an 
  558.         expert system for or have a ready access to one.   Assuming  that 
  559.         you are or have found one,  the following is a description of how 
  560.         one  would  use  the inference engine and the  rule  compiler  to 
  561.         produce an expert system.    Fortunately one of the sticker tasks 
  562.         of  developing  the inference engine has been done,  and all  you 
  563.         have  to do is develop the rule base,  compile it with  the  rule 
  564.         compiler  and  use  the resultant compiled rule-base  file  as  a 
  565.         parameter  when  you initiate the inference engine.   In  reality 
  566.         this is a much tougher job than developing the inference engine.
  567.  
  568.              Expert   systems  generally  consist  of  three  parts,    a 
  569.         KNOWLEDGE BASE,  HUMAN INTERFACE,  and an INFERENCE ENGINE.   The 
  570.         following is a short description of each:
  571.  
  572.         THE RULE or KNOWLEDGE BASE:
  573.  
  574.              The  rules  consisting of ANTECEDENTS  and  CONSEQUENTS  are 
  575.         known  as  the  "KNOWLEDGE  BASE"  of  the  expert  system.    An 
  576.         additional part of the KNOWLEDGE BASE consists of the executeable 
  577.         files  and  their  shared data files.   The  way  the  "knowledge 
  578.         engineer"  puts  these rules together determines how good  and/or 
  579.         effective  the resultant expert system becomes.    The  KNOWLEDGE 
  580.         BASE  is the smarts of the expert system,  and the basic data  on 
  581.         which the inference engine is to operate upon.
  582.  
  583.  
  584.  
  585.  
  586.  
  587.  
  588.  
  589.         George W. Hageman            --9--
  590.  
  591.  
  592.  
  593.  
  594.  
  595.         INFERENCE          -- SOFTMAN ENTERPRIZES --        Dec. 30, 1985
  596.  
  597.  
  598.         THE HUMAN INTERFACE:
  599.  
  600.              The  expert system needs a way to ask the human  user  about 
  601.         the  TRUTH or FALSENESS of the antecedents contained in the  rule 
  602.         base.   This  is  contained  as a part of  the  inference  engine 
  603.         associated with this release.    These routines will ask the user 
  604.         whether or not FOLLOWING STRINGS are TRUE or FALSE.    Additional 
  605.         human interfaces may be contained in the executeable files.
  606.  
  607.         THE INFERENCE ENGINE:
  608.  
  609.              The inference engine released with this software is known as 
  610.         a backwards chaining inference engine.    It works by identifying 
  611.         consequents  and  attempting  to find rules to  prove  that  each 
  612.         consequent  is TRUE.   Once a consequent is proved  TRUE,  it  is 
  613.         remembered  as being TRUE.    Each antecedent which is determined 
  614.         either TRUE or FALSE is remembered so that the user does not have 
  615.         to  be  asked more than once to verify  a  particular  statement.  
  616.         Remember  that if a consequent is not proved TRUE,  then it  does 
  617.         not  necessarily  mean that it is FALSE.    For more  information 
  618.         consult  the "inference.str" file which is a  preliminary  pseudo 
  619.         code  description  of the inference engine.   It is  not  correct 
  620.         because   I  have  not  gone  back  and  up  dated  it  from  the 
  621.         implementation effort (Tom De Marco please forgive me) but, it is 
  622.         close  enough to provide a basis for understanding the  code.   I 
  623.         suggest  looking  at the code to understand  how  this  inference 
  624.         engine  works.    I have also left in all of the debug statements 
  625.         which I found helpful so by modifying the makefile to include the 
  626.         DEBUG FLAGS, you can observe the inference engine working.
  627.  
  628.              This   inference  engine  attempts  to  prove  all  of   the 
  629.         consequents  from the top of the rule base through to the  bottom 
  630.         in a linear way if possible.  If however, an antecedent is really 
  631.         a  consequent  of a rule,  the inference engine will  attempt  to 
  632.         prove  that consequent even if it occurs later in the rule  base.  
  633.         In  this sense,  the inference engine will exhibit  some  forward 
  634.         chaining characteristics.
  635.  
  636.              There  are  several good books on the development of  expert 
  637.         systems,  inference  engines  and the like.   I have  included  a 
  638.         bibliography  which contain the books I consulted to  build  this 
  639.         one.
  640.  
  641.  
  642.  
  643.  
  644.  
  645.  
  646.  
  647.  
  648.  
  649.  
  650.  
  651.  
  652.  
  653.  
  654.  
  655.         George W. Hageman            --10--
  656.  
  657.  
  658.  
  659.  
  660.  
  661.         INFERENCE          -- SOFTMAN ENTERPRIZES --        Dec. 30, 1985
  662.  
  663.  
  664.         BUILDING EXPERT SYSTEMS:
  665.  
  666.              The  builders  of  expert systems  are  sometimes  known  as 
  667.         "knowledge engineers".    As the name implies, they deal with the 
  668.         manipulation  and  representation  of knowledge  leading  to  the 
  669.         development  of the Knowledge base the inference engine  operates 
  670.         upon.    In  order  to develop the knowledge base  the  knowledge 
  671.         engineer  needs either to be an expert in the area for which  the 
  672.         expert system is to be developed or, have ready access to a human 
  673.         expert in the field.   
  674.  
  675.              The  inference engine in this distribution accepts knowledge 
  676.         in  many  ways.   The first of which are the rules  as  discussed 
  677.         above which are relatively simple logical or predicate statements 
  678.         about the TRUTH of well defined consequents.    The other is  the 
  679.         somewhat  complex  knowledge representation as contained  in  the 
  680.         executeable files which may be initiated by the inference engine.  
  681.         In  fact,  the inference engine and the rule_compiler each can be 
  682.         one  of these executeable files so you can have recursive  expert 
  683.         systems  if  you  have a mind to!    You  can  even  use  another 
  684.         executeable  to produce a text file of rules,  which then can  be 
  685.         operated  on  by  the  rule-compiler  and  then  fed  to  another 
  686.         inference  engine  producing  self-modifying or  learning  expert 
  687.         system.   So even though the inference engine is only 12K and the 
  688.         rule  compiler 10K these are sufficient enough to produce  rather 
  689.         powerful expert systems.
  690.  
  691.              To  develop the knowledge base the knowledge engineer  first 
  692.         must  under  stand  the limitations which are acceptable  to  the 
  693.         expert  system  user.   For example,  if the user  of  an  animal 
  694.         identification  expert  system is not concerned with whether  the 
  695.         expert  can  differentiate  between a snake and a  lizard  or  is 
  696.         not interested in reptiles at all,  then the expert system can be 
  697.         simplified  by leaving out this knowledge.    Once the boundaries 
  698.         of  an expert system are well known,  the expert system  designer 
  699.         can concentrate on how to define the particulars.   This software 
  700.         release  contains  two examples of simple  expert  systems.   The 
  701.         first is the more or less classical animal identification expert, 
  702.         and  the second is a weather predictor expert which  demonstrates 
  703.         the usage of the IFRUN, ANDTHENRUN, etc. KEYWORDS.
  704.  
  705.  
  706.  
  707.  
  708.  
  709.  
  710.  
  711.  
  712.  
  713.  
  714.  
  715.  
  716.  
  717.  
  718.  
  719.  
  720.  
  721.         George W. Hageman            --11--
  722.  
  723.  
  724.  
  725.  
  726.  
  727.         INFERENCE          -- SOFTMAN ENTERPRIZES --        Dec. 30, 1985
  728.  
  729.  
  730.         ANIMALS:
  731.  
  732.              This is a simple expert which can differentiate between only 
  733.         certain  types of animals -- Mammals and Birds.   And among these 
  734.         two groups it can only tell you that the animal you are  thinking 
  735.         about  is  either  a  Giraffe,  Zebra,  Cheeta,  Tiger,  Penguin, 
  736.         albatross,  Duck,  and  so on.   The example has been limited  to 
  737.         these  few  identifications  in  order to  greatly  simplify  the 
  738.         development  effort  and  to help  demonstrate  the  strategy  of 
  739.         developing the rules associated with this simple expert system.
  740.  
  741.              The strategy associated with the development of a consistent 
  742.         set  of  rules  which  will identify a  particular  animal  given 
  743.         certain  physical  characteristics  to work with  is  DIVIDE  AND 
  744.         CONQUER.    The  idea being to use rules which build  a  decision 
  745.         tree  where each branch of the tree is formed by a rule which can 
  746.         decide  which direction to go at that junction.    Since  we  are 
  747.         obviously  dealing  with birds and mammals then we can build  our 
  748.         root  branch  or  the  grossest division  as  a  rule  which  can 
  749.         differentiate between birds and mammals.   Like:
  750.                   
  751.                   !
  752.                   IF animal gives milk
  753.                   ANDIF animal has hair
  754.                   THEN animal is mammal
  755.                   !
  756.                   IFNOT animal is mammal
  757.                   THEN animal is bird
  758.                   !
  759.                   .
  760.                   .
  761.  
  762.              Notice  that if the animal is not a mammal we  automatically 
  763.         assume  that it is a bird since this is the domain of our  expert 
  764.         system  -- it  does not consider any other type of  animal  as  a 
  765.         possibility.   If  we  were to include perhaps reptiles then  the 
  766.         following might be used instead:
  767.  
  768.                   !
  769.                   IF animal gives milk
  770.                   ANDIF animal has hair
  771.                   THEN animal is mammal
  772.                   !
  773.                   IFNOT animal is mammal
  774.                   AND animal has feathers
  775.                   AND animal lays eggs
  776.                   THEN animal is bird
  777.                   !
  778.                   IFNOT animal is mammal
  779.                   IFNOT animal is bird
  780.                   THEN animal is reptile
  781.                   !
  782.  
  783.  
  784.  
  785.  
  786.  
  787.         George W. Hageman            --12--
  788.  
  789.  
  790.  
  791.  
  792.  
  793.         INFERENCE          -- SOFTMAN ENTERPRIZES --        Dec. 30, 1985
  794.  
  795.  
  796.              Again  the last category is the default and needs no further 
  797.         definitions  because it is within the limitations of  the  expert 
  798.         system.
  799.  
  800.  
  801.              Further  refinement of the decision tree associated with the 
  802.         animals  expert should build on the knowledge gained  by  earlier 
  803.         branching.   So  one should use the knowledge that the animal has 
  804.         been  identified  as a bird to further  define  the  animal.   An 
  805.         example of the further definition of the type of mammal follows:
  806.  
  807.                   !
  808.                   IF animal is mammal
  809.                   ANDIF animal eats meat
  810.                   ANDIF animal eats little vegetation
  811.                   THEN animal is carnivore
  812.                   !
  813.                   IFNOT animal is carnivore
  814.                   ANDNOT animal eats little vegetation
  815.                   THEN animal is vegetarian
  816.                   !
  817.  
  818.              Finer  and  finer branching is achieved by this  divide  and 
  819.         conquer strategy until the leaves of the tree are reached,  These 
  820.         leaves  are  the  actual hypothesis which end the  search  for  a 
  821.         particular animal.  
  822.  
  823.                   .
  824.                   .
  825.                   !
  826.                   IF animal is cat
  827.                   AND animal has tan color
  828.                   AND animal has stripes
  829.                   THENHYP animal is tiger
  830.                   !
  831.                   IF animal is cat
  832.                   AND animal has tan color
  833.                   AND animal has spots
  834.                   THENHYP animal is cheeta
  835.                   !
  836.                   .
  837.                   .
  838.  
  839.              Notice  that  "animal  is cat" should be a  THEN  Consequent 
  840.         somewhere or else the inference engine will simply ask you if the 
  841.         "animal  is  cat" statement is TRUE or not -- which  may  not  be 
  842.         construed  as  a  particularly intelligent thing  for  an  expert 
  843.         system to do.   The intelligence represented by the expert system 
  844.         is directly related to the intelligence you give it.  However, it 
  845.         is possible to get very confused when there are a large number of 
  846.         rules for a particular expert system and mistakes in logic or the 
  847.         development  of  circular  logic occurs.   The first  will  cause 
  848.         incorrect  conclusions to be drawn and the second will cause  the 
  849.         inference engine to crash.   Circular logic causes the  inference 
  850.         engine to run out of stack space.  
  851.  
  852.  
  853.         George W. Hageman            --13--
  854.  
  855.  
  856.  
  857.  
  858.  
  859.         INFERENCE          -- SOFTMAN ENTERPRIZES --        Dec. 30, 1985
  860.  
  861.  
  862.              A  circular argument is an argument which cannot be resolved 
  863.         because  its proof relies on another rule which in turn relies on 
  864.         proving the first statement before it can be proved such as in:
  865.  
  866.                        !
  867.                        IF the second one is true
  868.                        THEN the first one is true
  869.                        !
  870.                        IF the first one is true
  871.                        THEN the second one is true
  872.                        !
  873.  
  874.              Or to expand the concept:
  875.  
  876.                        !
  877.                        IF the third one is true
  878.                        THEN the first one is true
  879.                        !
  880.                        IF the first one is true
  881.                        THEN the second one is true
  882.                        !
  883.                        IF the second one is true
  884.                        THEN the third on is true
  885.                        !
  886.  
  887.              Usually,   when  the inference engine tells you that  "Stack 
  888.         overflow" has occurred -- this will be the problem.
  889.  
  890.              Look at the animal file contained in this release,  see  how 
  891.         the  rules are built and as an exercise add another animal to  be 
  892.         differentiated  like a platypus,  and later add a whole class  of 
  893.         animals such as domestic farm animals, or even a division such as 
  894.         reptiles or insects.
  895.  
  896.              You  will soon notice that your knowledge base will increase 
  897.         very  quickly  with each set of animals you want your  expert  to 
  898.         differentiate.   With this growth of the knowledge base come real 
  899.         difficulty  in keeping the rules correct  and  non-circular.    A 
  900.         strategy  for limiting the complexity for these rules is  to  use 
  901.         the IFRUN or THENRUN capability of this inference engine to fire-
  902.         up  a whole new inference engine which is an expert in one of the 
  903.         major branches of animals.   In this manner one only has to  make 
  904.         the  major decisions associated with a class of animals and  then 
  905.         run  the  expert system which knows how to  handle  the  specific 
  906.         class of animals.  
  907.  
  908.  
  909.  
  910.  
  911.  
  912.  
  913.  
  914.  
  915.  
  916.  
  917.  
  918.  
  919.         George W. Hageman            --14--
  920.  
  921.  
  922.  
  923.  
  924.  
  925.         INFERENCE          -- SOFTMAN ENTERPRIZES --        Dec. 30, 1985
  926.  
  927.  
  928.         For example:
  929.  
  930.                   !
  931.                   IF animal has feathers
  932.                   AND animal lays eggs
  933.                   THEN animal is bird
  934.                   THENRUNHYP  inference.exe birds.cmp
  935.                   !
  936.                   IFNOT animal is bird
  937.                   AND  animal has hair
  938.                   AND  animal gives milk
  939.                   THEN animal is mammal
  940.                   THENRUNHYP inference.exe mammals.cmp
  941.                   !
  942.  
  943.              The  files  "birds.cmp" and "mammals.cmp" are the  resultant 
  944.         compiled  files  from  the  text  knowledge  bases  "birds"   and 
  945.         "mammals"  which  the knowledge engineer would need  to  produce.   
  946.         But,  as stated above, these files would represent expert systems 
  947.         knowing  only the limited area of birds or mammals and  therefore 
  948.         can be greatly simplified.    NOTE:  inference.exe returns a TRUE 
  949.         value  if  it has proved anything while it ran,  and FALSE if  it 
  950.         could  not  prove anything.   With an IBM AT with 512  KBYTES  of 
  951.         memory  I  was able to load four copies of  an  inference  engine 
  952.         simultaneously.   A good way to see how many your system can deal 
  953.         with at the same time compile the following test:
  954.  
  955.                        !
  956.                        IFRUN INFERENCE.EXE TEST.CMP
  957.                        THENHYP I am done
  958.                        ! 
  959.  
  960.  
  961.         If this file is named TEST, then compile it using the following:
  962.  
  963.                   rulecomp test test.cmp
  964.  
  965.         Then, run the inference engine with the test.cmp file as follows:
  966.  
  967.                   inference test.cmp
  968.  
  969.  
  970.              Notice  that when any routine cannot be run for some  reason 
  971.         or another, an attempt is made to tell you what went wrong -- out 
  972.         of memory,  can't find it,  or some other reason, and the routine 
  973.         will  be assumed to have returned normally with a TRUE  predicate 
  974.         value.   This  is done so that the inference engine won't  simply 
  975.         die at some mysterious point.   The "I infer that : I am done" at 
  976.         the top was the last inference engine which could be loaded which 
  977.         could run but could not spawn a new process, the next one down is 
  978.         the  last one which could spawn one,  and any others  below  this 
  979.         line  plus  this  one will tell you the number of levels  of  the 
  980.         inference engine you can run on your system.  
  981.  
  982.  
  983.  
  984.  
  985.         George W. Hageman            --15--
  986.  
  987.  
  988.  
  989.  
  990.  
  991.         INFERENCE          -- SOFTMAN ENTERPRIZES --        Dec. 30, 1985
  992.  
  993.  
  994.         THE WEATHER EXPERT:
  995.  
  996.              The  other  expert system knowledge base included with  this 
  997.         release  is  a weather predicting  expert.   This  expert  system 
  998.         demonstrates   the   use  of  executeable  files  to  expand  the 
  999.         abilities  of  the inference engine to deal with other  forms  of 
  1000.         knowledge.   Look  a  the source files for  the  weather  expert, 
  1001.         notice how each member of this set of routines uses a common file 
  1002.         for  the  transfer of needed data.   The need of a file for  this 
  1003.         transfer of data is done because it was the only standard form of 
  1004.         data  transmission  which was not machine dependent according  to 
  1005.         MS-DOS and UNIX operating systems.   Specific methods for dealing 
  1006.         with this problem can be found for your machine which can greatly 
  1007.         speed up this cumbersome data transfer method -- but it works.   
  1008.  
  1009.              UNIX  and the Microsoft C compiler allows a routine to  exit 
  1010.         with a value which will be returned to the parent process.   This 
  1011.         method  is  used to allow each executeable to return  its  TRUTH-
  1012.         VALUE  or  PREDICATE-VALUE back to  the  inference  engine.   The 
  1013.         routine  runRoutine(consequent) performs this task.   If you look 
  1014.         at  the  file runrouti.c you can see that this is done  with  the 
  1015.         "exit(value) ;" statement.  The values used to indicate the TRUTH 
  1016.         and  FALSEness  of  the  routine  are  "RETURN_ROUTINE_TRUE"  and 
  1017.         "RETURN_ROUTINE_FALSE"   as   contained  in   the   header   file 
  1018.         "routine.h".   This header file should be included in any routine 
  1019.         which returns a TRUE/FALSE value to the inference engine.
  1020.  
  1021.              The   strategy  for  using  this  method  of  expanding  the 
  1022.         knowledge  base  is essentially the same as that for  the  animal 
  1023.         type  expert  system,  except that it is noticed that the  simple 
  1024.         form  of  the  knowledge base does not  have  the  capability  to 
  1025.         perform  some  of  the functions  needed.   Cases  where  complex 
  1026.         questions  or the manipulation of data are necessary must  resort 
  1027.         to  the use of executeable files which can deal with them.   Such 
  1028.         is the case for the weather predicting expert system.
  1029.  
  1030.              The  weather expert must deal with such data  as  barometric 
  1031.         pressure,  wind direction,  and the current rate of change of the 
  1032.         barometric  pressure.    Additional data such as cloud conditions 
  1033.         can  be  handled  by the predicate  functions  of  the  inference 
  1034.         engine.     Again  the  strategy  is  to divide  and  conquer  by 
  1035.         determining  which  information  needs  to  be  gathered  by  the 
  1036.         executeable portion of the expert system,  what routines must  be 
  1037.         used  to  convert  this data into a form which is usable  by  the 
  1038.         inference engine i.e.  TRUE or FALSE,  and,  what information can 
  1039.         be  gathered  directly  by the  inference  engine.    Once  these 
  1040.         decisions are made then a decision tree can be built in a similar 
  1041.         manner  to  the  one  built  for  the  animal  expert  but   also 
  1042.         incorporating the executeable files where appropriate.    Look at 
  1043.         the  file  "weather"  and  at each of the source  files  for  the 
  1044.         executeable portion of the weather expert.  
  1045.  
  1046.  
  1047.  
  1048.  
  1049.  
  1050.  
  1051.         George W. Hageman            --16--
  1052.  
  1053.  
  1054.  
  1055.  
  1056.  
  1057.         INFERENCE          -- SOFTMAN ENTERPRIZES --        Dec. 30, 1985
  1058.  
  1059.  
  1060.              Notice  that there is a "main" routine (even though  all  of 
  1061.         the  programs  are  stand-a-lone programs and are have  the  name 
  1062.         "main").   This routine is in the file "MESSAGE1.C", and provides 
  1063.         the  data entry for all of the data needed by the expert  system.  
  1064.         The  routine  explains to the user what  is  needed,  checks  for 
  1065.         reasonable  responses,  and  writes the data to a disk  file  for 
  1066.         later  use  by  the other routines associated with  this  system.  
  1067.         These other routines,  when initiated,  have the task of  reading 
  1068.         the  disk file produced by the "MESSAGE1.EXE" executeable,  check 
  1069.         for  some conditions,  and return "ROUTINE_RETURN_TRUE",  if  the 
  1070.         conditions are met and "ROUTINE_RETURN_FALSE" if they are not.
  1071.  
  1072.              In  this  manner  the knowledge  engineer  can  make  expert 
  1073.         systems   of  a  higher  complexity  and  usefulness  than  would 
  1074.         otherwise be possible.
  1075.  
  1076.         CONCLUSION:
  1077.  
  1078.              I hope that this short definition of the inference engine is 
  1079.         enough  to  get  you started into the fascinating  field  of  AI.   
  1080.         There are some useful additions the enthusiastic programmer could 
  1081.         make  to the inference engine to both enhance its usefulness  and 
  1082.         its  ability to assist in the debugging of knowledge  basses.   A 
  1083.         "Why"  function which shows the complete logical path which  lead 
  1084.         to  the  question being asked,  showing each statement  as  being 
  1085.         known or unknown and if known what its predicate value is,  would 
  1086.         be very helpful.    Further,  the addition of different  KEYWORDS 
  1087.         such  as  an  OR function,  might expand the  usefulness  of  the 
  1088.         inference engine.  I may include these in my next release of this 
  1089.         software  but If you have made such an addition -- I will include 
  1090.         them in the next release and refund your $10.00 if you have  been 
  1091.         so good as to send it to me.
  1092.  
  1093.              Good  luck  and if you have any questions or would  like  to 
  1094.         discuss this concept please drop me a line.
  1095.  
  1096.                                  Thanks,
  1097.  
  1098.                                       George W. Hageman
  1099.  
  1100.  
  1101.  
  1102.  
  1103.  
  1104.  
  1105.  
  1106.  
  1107.  
  1108.  
  1109.  
  1110.  
  1111.  
  1112.  
  1113.  
  1114.  
  1115.  
  1116.  
  1117.         George W. Hageman            --17--
  1118.  
  1119.  
  1120.  
  1121.  
  1122.  
  1123.         INFERENCE          -- SOFTMAN ENTERPRIZES --        Dec. 30, 1985
  1124.  
  1125.  
  1126.         BIBLIOGRAPHY:
  1127.  
  1128.              The  following  books  and articles  were  helpful  in  the 
  1129.         development of this inference engine.  
  1130.  
  1131.              MVP-FORTH EXPERT SYSTEM TOOLKIT,  Jack Park,   Mountain View 
  1132.         Press,  Inc.  P.O. Box 4656 Mountain View, CA 94040 Phone: (415)-
  1133.         961-4130.
  1134.  
  1135.              MVP-FORTH EXPERT-2 TUTORIAL,  Mitch Derick and Linda Derick, 
  1136.         Mountain View Press.
  1137.  
  1138.              INSIDE F83, C. H. Ting,  OFFETE ENTERPRISES, INC.  Available 
  1139.         from Moutain View Press. 
  1140.  
  1141.              Expert  Systems  and the Weather,  Jack  Park,   Dr.  Dobb's 
  1142.         Journal, April 1984, pp. 24-28.
  1143.  
  1144.              Programming in Prolog,  William F.  Clocksin and Christopher 
  1145.         S. Mellish, Springer-Verlag 
  1146.  
  1147.              LISP,  Patrick H.  Winston and Berthold Klaus and Paul Horn, 
  1148.         Addison Weseley.
  1149.  
  1150.              A  special  thankyou to Jack Park and the  MVP-FORTH  EXPERT 
  1151.         SYSTEM  TOOLKIT.   Many  of  the ideas in this document  and  the 
  1152.         development environment afforded by FORTH were the starting point 
  1153.         for many of the ideas developed in this inference engine.  If you 
  1154.         are into FORTH this is an excellent source of information on  the 
  1155.         subject.  
  1156.  
  1157.  
  1158.  
  1159.  
  1160.  
  1161.  
  1162.  
  1163.  
  1164.  
  1165.  
  1166.  
  1167.  
  1168.  
  1169.  
  1170.  
  1171.  
  1172.  
  1173.  
  1174.  
  1175.  
  1176.  
  1177.  
  1178.  
  1179.  
  1180.  
  1181.  
  1182.  
  1183.         George W. Hageman            --18--
  1184.  
  1185.  
  1186.  
  1187.  
  1188.  
  1189.