home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / INFO / C / INFERNG.ZIP / INFERENC.DOC < prev    next >
Encoding:
Text File  |  1986-02-01  |  44.6 KB  |  1,196 lines

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