home *** CD-ROM | disk | FTP | other *** search
/ ftp.umcs.maine.edu / 2015-02-07.ftp.umcs.maine.edu.tar / ftp.umcs.maine.edu / pub / thesis / zhongy / rule / rule_based.cc < prev    next >
C/C++ Source or Header  |  1994-12-15  |  39KB  |  1,504 lines

  1. #include <stdio.h>
  2. #include <stdlib.h> 
  3. #include <iostream.h>
  4. #include <string.h>
  5. #include <ctype.h>
  6.  
  7. #include "rule.h"
  8.     
  9.  
  10. int                   NumberofObjects;
  11. /*ObjectType            *FirstObject; */
  12. int                   Length1;
  13. char                  *List[20];
  14. int                   option, option1;
  15. int                   Finish,Finished,Explanation; /*True--1, False--0*/
  16. ObjectNameType        ObjectName;
  17. ObjectValueType       ObjectValue;
  18. ObjectType            *PreviousPtr, *ObjectPtr;
  19. struct RuleType       *PreviousRulePtr, *RulePtr;
  20. RuleNameType          RuleName;
  21. int                   NumberofRules;
  22. RuleType              *FirstRule;
  23. PremiseType           *PremisePtr, *PreviousPremisePtr;
  24. ConclusionType        *ConclusionPtr;
  25. RuleType              *NewRule;
  26. int                   Line;
  27. char                  d;
  28. char                  *str1, *Title;
  29.  
  30. /**************Function 1 **********************************************/
  31. static void EraseScreen() 
  32. {
  33.   system("clear");
  34. };
  35.  
  36. /**************Function 2 **********************************************/
  37. /*
  38.   change C to C++
  39.  
  40.   we put all paramater to the ()
  41.  
  42. */
  43. static void GenerateList(char *List[20],int *Length1,int ListNumber)
  44. /*
  45.  used in C
  46.  
  47. int ListNumber;
  48. char *List[20];
  49. int *Length1;
  50. */
  51. {
  52.   int j;
  53.  
  54.   j=ListNumber;
  55.   switch (j)  {
  56.     case 1:
  57.          List[1]="Objects";
  58.          List[2]="Rules";
  59.          List[3]="Facts";
  60.          List[4]="Backward chaining";
  61.          List[5]="Forward chaining";
  62.          List[6]="Save knowledge base";
  63.          List[7]="Load knowledge base from file";
  64.          List[8]="Select explanation";
  65.          List[9]="End";
  66.          *Length1=9;
  67.          break;
  68.    case 2:
  69.          List[1]="Add";
  70.          List[2]="Erase";
  71.          List[3]="List";
  72.          *Length1=3;
  73.          break;
  74.    case 3:
  75.          List[1]="Initialize";
  76.          List[2]="Give values";
  77.          List[3]="List";
  78.          *Length1=3;
  79.          break;
  80.    default:
  81.          break;
  82.    }
  83. }
  84.  
  85. /**************Function 3**********************************************/
  86. /*
  87.   Same thing
  88.  
  89.   change from C to C++, we must change the position of paramater
  90. */
  91. static void OptionList(char *List[20],int Number,int *Option)
  92. /*
  93.   used in C
  94.  
  95. char *List[20];
  96. int Number;
  97. int *Option;
  98. */
  99. {
  100.   int i;
  101.   
  102.   EraseScreen();
  103.   for (i=1; i<=Number; i++)
  104.       printf("  %d   %s \n",i,List[i]);
  105.   printf("\n\n");
  106.   printf("Select one option :");
  107.   do {
  108.      scanf("%d",Option);
  109.      if (((*Option) > Number) || ((*Option) <1))
  110.         printf("\n Invalid option, reenter the option: ");
  111.   } while (((*Option) > Number) || ((*Option) <1));
  112.  
  113. }
  114.  
  115. /****************Function 4******************************************/
  116. void Trim(char *str1)
  117. {
  118.   char c;
  119.   int len,i,counter;
  120.   char str2[MaxCharactersObjectName];
  121.   
  122.   printf("\n Enter the Trim");
  123.   len=strlen(str1);
  124.   counter=-1;
  125.   for (i=0;i<len;i++) {
  126.       c=str1[i];
  127.       if (c==' ') c='\0';
  128.       if (isupper(c)) tolower(c);
  129.       if (c != '\0') {
  130.          counter++;
  131.          str2[counter]=c;    
  132.          };
  133.       }
  134.   counter++;
  135.   str2[counter]='\0';
  136.   
  137.   strncpy(str1,str2,strlen(str2));
  138. }
  139.  
  140. /****************Function 5******************************************/
  141. void GiveNameofObject(char *Name)
  142. {
  143.   char str[MaxCharactersObjectName];
  144.  
  145.   printf("\nGive name of the object(if no more enter quit):");
  146.   scanf("%s",str);
  147.  /* Trim(str);  /*****Fun 4, not necessary ***************/
  148.   printf("Here comes the object:%s", str); 
  149.  
  150.   strncpy(Name,str,strlen(str));
  151. }
  152.  
  153.  
  154. /**************Function  6******************************************/
  155. /*
  156.   Found--return point to ObjectType
  157.  
  158.   Not Found--return NULL
  159. */
  160.  
  161. ObjectType *SearchObject(char *ObjectName)
  162. {
  163.   int Found;
  164.  
  165.   Found=0;
  166.   PreviousPtr=NULL;
  167.   ObjectPtr=FirstObject;
  168.  
  169.   while ((ObjectPtr != NULL) && (Found == 0)) {
  170.      if (!strcmp(ObjectName,(*ObjectPtr).Name())) {
  171.         Found=1;
  172.         return(ObjectPtr);
  173.         }
  174.      else {
  175.         PreviousPtr=ObjectPtr;
  176.         ObjectPtr=(*ObjectPtr).Next();
  177.         };
  178.      };
  179.   return(NULL);
  180. }
  181.  
  182.  
  183. /************Function 7*******************************************/
  184.  
  185. ObjectType *CreateObject(char *ObjectName)
  186. /*
  187.   used in C
  188.  
  189. char *ObjectName;
  190. */
  191. {
  192.   ObjectType *Object_Ptr;
  193.  
  194.   /*
  195.   Object_Ptr=malloc(sizeof(struct ObjectType));
  196.   */
  197.   Object_Ptr=new ObjectType;
  198.  
  199.   NumberofObjects++;
  200.   
  201.   (*Object_Ptr).set_Name(ObjectName);
  202.   (*Object_Ptr).set_Value("Unknown");
  203.   (*Object_Ptr).set_Known(0);
  204.   (*Object_Ptr).set_Next(FirstObject);
  205.   (*Object_Ptr).set_Type(PROBLEM);
  206.  
  207. /*
  208.   used in C
  209.  
  210.   strcpy(Object_Ptr.Name(),ObjectName);
  211.   strcpy(Object_Ptr.Value(),"Unknown");
  212.   Object_Ptr->Known=0;
  213.   Object_Ptr->Next=FirstObject;
  214. */
  215.  
  216.   FirstObject=Object_Ptr;
  217.   /*
  218.   printf("\n first");
  219.   printf("\nObjectName is:%s",(*FirstObject).Name());
  220.   printf("\nObjectName is:%s",(*FirstObject).Value());
  221.   printf("\nKnown:%d, Next is:%d",(*FirstObject).Known(),(*FirstObject).Next());
  222.   */
  223.  
  224.   return(Object_Ptr);
  225. }
  226.  
  227. ObjectType *CreateObject2(int type)
  228. {
  229.  
  230.   ObjectType *ObjPtr;
  231.   ObjectType *ObjectPtr;
  232.   char Hostname[50];
  233.   char Obj_name[50];
  234.   char Probname[50];
  235.   char Eventname[50];
  236.   long time_interval;
  237.   struct timeval t;
  238.  
  239.  
  240.       switch (type) {
  241.       case MIB_VALUE:
  242.            bzero(Hostname, sizeof(Hostname));
  243.            bzero(Obj_name, sizeof(Obj_name));
  244.  
  245.            printf("\nEnter Hostname Name:");
  246.            scanf("%s",Hostname);
  247.            printf("\nEnter Objectname:");
  248.            scanf("%s",Obj_name);
  249.            printf("\nEnter time interval:");
  250.            scanf("%d", &time_interval);
  251.  
  252.            ObjectPtr=SearchObject(strcat(Hostname,Obj_name));
  253.  
  254.            if (ObjectPtr ==NULL) {
  255.                ObjPtr=new ObjectType;
  256.                (*ObjPtr).set_Type(MIB_VALUE);
  257.                (*ObjPtr).set_hostname(Hostname);
  258.                (*ObjPtr).set_objname(Obj_name);
  259.                (*ObjPtr).set_Name(strcat(Hostname,Obj_name));
  260.                t.tv_sec=time_interval;
  261.                (*ObjPtr).set_timeleft(t);
  262.                (*ObjPtr).set_timeinterval(t);
  263.                (*ObjPtr).set_Known(0);
  264.                }
  265.            else {
  266.                printf("\nObject already existed");
  267.                return(NULL);
  268.                };
  269.            break;
  270.  
  271.       case MIB_VARIANCE:
  272.            bzero(Hostname, sizeof(Hostname));
  273.            bzero(Obj_name, sizeof(Obj_name));
  274.  
  275.            printf("\nEnter Hostname Name:");
  276.            scanf("%s",Hostname);
  277.            printf("\nEnter Objectname:");
  278.            scanf("%s",Obj_name);
  279.            printf("\nEnter time interval:");
  280.            scanf("%d", &time_interval);
  281.  
  282.            ObjectPtr=SearchObject(strcat(Hostname,Obj_name)); 
  283.            if (ObjectPtr ==NULL) {
  284.                ObjPtr=new ObjectType;
  285.                (*ObjPtr).set_Type(MIB_VARIANCE);
  286.                (*ObjPtr).set_hostname(Hostname);
  287.                (*ObjPtr).set_objname(Obj_name);
  288.                (*ObjPtr).set_Name(strcat(Hostname,Obj_name));
  289.                t.tv_sec=time_interval;
  290.                (*ObjPtr).set_timeleft(t);
  291.                (*ObjPtr).set_timeinterval(t);
  292.                (*ObjPtr).set_Known(0);
  293.                }
  294.            else {
  295.                printf("\nObject already existed");
  296.                return(NULL);
  297.                };
  298.            break;
  299.       case PROBLEM:
  300.            bzero(Probname,sizeof(Probname));
  301.            printf("\nEnter problem name:");
  302.            scanf("%s", Probname);
  303.  
  304.            ObjectPtr=SearchObject(Probname); 
  305.            if (ObjectPtr ==NULL) {
  306.                ObjPtr=new ObjectType;
  307.                (*ObjPtr).set_Type(PROBLEM);
  308.                (*ObjPtr).set_Known(0);
  309.                (*ObjPtr).set_Name(Probname);
  310.                }
  311.            else {
  312.                printf("\nObject already existed");
  313.                return(NULL);
  314.                };
  315.            break;
  316.       case EVENT:
  317.            bzero(Eventname,sizeof(Eventname));
  318.            printf("\nEnter event name:");
  319.            scanf("%s",Eventname);
  320.  
  321.            ObjectPtr=SearchObject(Eventname); 
  322.            if (ObjectPtr ==NULL) {
  323.               ObjPtr=new ObjectType;
  324.               (*ObjPtr).set_Type(EVENT);
  325.               (*ObjPtr).set_Known(0);
  326.               (*ObjPtr).set_Name(Eventname);
  327.               }
  328.            else {
  329.                printf("\nObject already existed");
  330.                return(NULL);
  331.                };
  332.            break;
  333.       default:
  334.            break;
  335.       }
  336.  
  337.     (*ObjPtr).set_Next(FirstObject);
  338.     FirstObject=ObjPtr;
  339.  
  340.   return(ObjPtr);
  341.  
  342. }  
  343.  
  344. /**********Function 8*******************************************/
  345.  
  346. static void ControlScr() 
  347. {
  348.   char s[MaxCharactersObjectName];
  349.  
  350.   Line=Line+1;
  351.   if (Line > Nlines) {
  352.      printf("\n----Type character 'c' to cancel and other key to continue:");
  353.      scanf("%s",s);
  354.      d=s[0];
  355.      EraseScreen();
  356.      Line=0;
  357.      }
  358. }
  359.  
  360. /**********Function 9*******************************************/
  361.  
  362. void ListObjects()
  363. {
  364.   char s[MaxCharactersObjectName];
  365.   ObjectType *ObjectPtr;
  366.  
  367.   EraseScreen();
  368.   printf("\n *List of Objects*(type 0:MIB_VALUE,1:MIB_VARIANCE,2:PROBLEM,3:EVENT*\n");
  369.   printf("\n ***Type**          **Name***\n");
  370.   ObjectPtr=FirstObject;
  371.   Line=2;
  372.   d='B';
  373.   while ((ObjectPtr != NULL)&&(d != 'c'))  {
  374.         printf("  %d           %s \n",(*ObjectPtr).Type(), (*ObjectPtr).Name());
  375.         ObjectPtr=(*ObjectPtr).Next();
  376.         ControlScr();
  377.         }
  378.   if ( d != 'c') {
  379.     printf("\n *****End of List****Hit 'q' <Enter> to exit:");
  380.     scanf("%s",s);
  381.     while ( strcmp(s,"q")) {
  382.       printf("\nHit 'q' <Enter> to exit:");
  383.       scanf("%s",s);
  384.       }
  385.     }
  386. }
  387.  
  388.  
  389. /********Function 10******************************************/
  390.  
  391.   
  392. int GiveRuleName(char *Name)
  393. {
  394.   char str[MaxCharactersObjectName];
  395.  
  396.   printf("\n Enter Rule Name?:(y/n)");
  397.   scanf("%s",str);
  398.   if (!strncmp(str,"y",1)) {
  399.      printf("    Give name of the rule:");
  400.      scanf("%s",str);
  401.      /* Trim(str);  /*****Fun 4, not necessary ***************/
  402.      printf("\n $$Begin to set up rule:%s", str);
  403.      strncpy(Name,str,strlen(str));
  404.      return(1);
  405.      }
  406.   else
  407.      return(0);
  408. }
  409.  
  410.  
  411. /*******Function 11****************************************/
  412.  
  413. /*
  414.   return value:
  415.     Point to Found Rule--Found,  NULL--Not Found
  416. */
  417.  
  418. struct RuleType *SearchRule(char *RuleName)
  419. {
  420.   int Found;
  421.  
  422.   Found=0;
  423.   PreviousRulePtr=NULL;
  424.   RulePtr=FirstRule;
  425.  
  426.   while ((RulePtr != NULL) && (Found == 0)) {
  427.      if (!strcmp(RuleName,(*RulePtr).Name())) {
  428.         Found=1;
  429.         return(RulePtr);
  430.         }
  431.      else {
  432.         PreviousRulePtr=RulePtr;
  433.         RulePtr=(*RulePtr).Next();
  434.         };
  435.      };
  436.   return(NULL);
  437. }
  438.  
  439. /******Function 12********************************************/
  440. /*
  441.   Reads a premise (object and value) and checks whether or not the object
  442.   is in the objects list. If not the object is created
  443.  
  444.   return value:   1--sucess, 0--failure
  445. */
  446.  
  447. int GiveExpression()
  448. {
  449.  char str[MaxCharactersObjectName];
  450.  char ObjectName[MaxCharactersObjectName];
  451.  
  452.  
  453.  
  454.  printf("\n Enter the expression:(y/n) <Enter> ");
  455.  scanf("%s",str);
  456.  if ( !strncmp(str,"y",1)) {
  457.     printf("        Give object:");
  458.     scanf("%s",str);
  459.     strcpy(ObjectName,str);
  460.     ObjectPtr=SearchObject(ObjectName);
  461.     if (ObjectPtr == NULL) {
  462.        printf("\n$$This object does not exist, but it is created as problem");
  463.        ObjectPtr=CreateObject(ObjectName);
  464.        }
  465.     printf("\n      Give value of the object:");
  466.     scanf("%s",str);
  467.     strcpy(ObjectValue,str);
  468.     return(1);
  469.     }
  470.  else {
  471.     return(0);
  472.     };
  473. }
  474.    
  475.  
  476. /******Function 13 *******************************************/
  477.  
  478. struct RuleType *CreateRule()
  479. {
  480.   char *Name1;
  481.   int c;
  482.   int Premtype;
  483.  
  484.   char PremObjName[50];
  485.   char Premvalue[50];
  486.   char ObjectName[50];
  487.   ObjectType *Object_Ptr;
  488.   int  firstPrem;
  489.   int  Int_value;
  490.   PremiseType *PremPtr;
  491.  
  492.   /*
  493.   NewRule=malloc(sizeof(struct RuleType));
  494.   */
  495.  
  496.   NewRule=new RuleType;
  497.   NumberofRules++;
  498.  
  499.   /*
  500.     C
  501.   strcpy(NewRule->Name,RuleName);
  502.   NewRule->Premise=NULL;
  503.   NewRule->Conclusion=NULL;
  504.   NewRule->Next=FirstRule;
  505.   */
  506.   (*NewRule).set_Name(RuleName);
  507.   (*NewRule).set_Premise(NULL);
  508.   (*NewRule).set_Conclusion(NULL);
  509.   (*NewRule).set_Next(FirstRule);
  510.   
  511.   /*
  512.    Enter the premise
  513.   */
  514.      printf("\nEnter the type of Premtype:");
  515.      scanf("%d",&Premtype);
  516.      firstPrem=1;
  517.      while (Premtype != 9999) {
  518.        PremPtr=new PremiseType;
  519.        (*PremPtr).set_Operation(Premtype);
  520.  
  521.        printf("\nEnter the object name of this premise:");
  522.        scanf("%s",PremObjName);
  523.        strcpy(ObjectName,PremObjName);
  524.        Object_Ptr=SearchObject(ObjectName);
  525.        if (Object_Ptr == NULL) {
  526.           printf("\n$$This object does not exist,but it is created as problem");
  527.           Object_Ptr=CreateObject(ObjectName);
  528.           }
  529.        (*PremPtr).set_Object(Object_Ptr);
  530.  
  531.        switch (Premtype) {
  532.          case EQUAL_STR:
  533.               printf("\nEnter the Premise string value:");
  534.               scanf("%s",Premvalue);
  535.               (*PremPtr).set_Value(Premvalue);
  536.               break;
  537.          case EQUAL_INT:
  538.          case BIGGER:
  539.          case LESSER:
  540.               printf("\nEnter the Premise int value:");
  541.               scanf("%d",&Int_value);
  542.               (*PremPtr).set_Int(Int_value);
  543.               break;
  544.          default:
  545.               break;
  546.          };
  547.  
  548.        if (firstPrem == 0) {
  549.             (*PremPtr).set_Next((*NewRule).Premise());
  550.             (*NewRule).set_Premise(PremPtr);
  551.             }
  552.        else {
  553.             firstPrem=0;
  554.             (*PremPtr).set_Next(NULL);
  555.             (*NewRule).set_Premise(PremPtr);
  556.             }
  557.        printf("\n Enter the type of Premtype(no more 9999):");
  558.        scanf("%d",&Premtype);
  559.        }
  560.  
  561.   
  562.  
  563.    /*
  564.    Enter the conclusion
  565.    */
  566.  
  567.    do {
  568.       printf("\n Give conclusion of the rule:");
  569.       c=GiveExpression();
  570.       if (c ==0)
  571.          printf("\n No Conclusion Entered");
  572.       }
  573.    while (c==0);
  574.    /*
  575.    ConclusionPtr=malloc(sizeof(struct ConclusionType));
  576.    */
  577.    ConclusionPtr=new ConclusionType;
  578.    /* C
  579.    NewRule->Conclusion=ConclusionPtr;
  580.    */
  581.    (*NewRule).set_Conclusion(ConclusionPtr);
  582.  
  583.    /*  C
  584.    ConclusionPtr->Object=ObjectPtr;
  585.    strcpy(ConclusionPtr->Value,ObjectValue);
  586.  
  587.    */
  588.    (*ConclusionPtr).set_Object(ObjectPtr);
  589.    (*ConclusionPtr).set_Value(ObjectValue);
  590.  
  591.    FirstRule=NewRule;
  592.     
  593.    return(NewRule);
  594. }
  595.  
  596.  
  597. /****   Function 14***************************************************/
  598. /*
  599.   search the Rulename, Found--1,founded, Found--0,not founded
  600.  
  601.   if (Found)
  602.      Erases one rule from the list of rules and disposes the memory space
  603.      occupied by its premises, conclusion and the rule itself
  604.  
  605. */
  606.  
  607. int EraseRule(char *RuleName)
  608. {
  609.   int Found;
  610.   struct PremiseType *PtrP;
  611.  
  612.   RulePtr=FirstRule;
  613.   PreviousRulePtr=NULL;
  614.   Found=0;
  615.   while ((RulePtr != NULL) && (Found == 0)) {
  616.      if ( !strcmp(RuleName,(*RulePtr).Name()))
  617.         Found=1;
  618.      else {
  619.         PreviousRulePtr=RulePtr;
  620.         RulePtr=(*RulePtr).Next();
  621.         }
  622.      }
  623.  
  624.   if (Found ==1) {
  625.      PremisePtr=(*RulePtr).Premise();
  626.      while (PremisePtr != NULL) {
  627.         PtrP=(*PremisePtr).Next();
  628.         /*
  629.         free(PremisePtr);
  630.         */
  631.         delete PremisePtr;
  632.         PremisePtr=PtrP;
  633.         }
  634.      /*
  635.      free((*RulePtr).Conclusion());
  636.      */
  637.      delete (*RulePtr).Conclusion();
  638.      if (PreviousRulePtr == NULL)
  639.         FirstRule=(*RulePtr).Next();
  640.      else {
  641.         /*
  642.           C
  643.         PreviousRulePtr->Next=RulePtr->Next;
  644.         */
  645.         (*PreviousRulePtr).set_Next((*RulePtr).Next());
  646.  
  647.         /*
  648.         free(RulePtr);
  649.         */
  650.         delete RulePtr;
  651.         NumberofRules=NumberofRules-1;
  652.         }
  653.      return(1);
  654.      }
  655.   else
  656.      return(0);
  657.  
  658. }
  659.         
  660.  
  661. /************ Function 15**************************************************/
  662.  
  663. void ListRules()
  664. {
  665.   char s[MaxCharactersObjectName];
  666.   struct ObjectType *ObjPtr;
  667.   
  668.   EraseScreen();
  669.   printf("\n       List of Rules           ");
  670.   printf("\n______________________________________________________________");
  671.   RulePtr=FirstRule;
  672.   Line=2;
  673.   d='B';
  674.   while ((RulePtr != NULL) && (d != 'c')) {
  675.      printf("\n %s:",(*RulePtr).Name());
  676.      printf("\n IF");
  677.      PremisePtr=(*RulePtr).Premise();
  678.      while (PremisePtr != NULL) {
  679.         ObjPtr=(*PremisePtr).Object();
  680.         switch ((*PremisePtr).Op()) {
  681.           case EQUAL_STR:
  682.            printf("\n EQUAL_STR  %s=%s",(*ObjPtr).Name(),(*PremisePtr).Value());
  683.            break;
  684.           case EQUAL_INT:
  685.             printf("\n INT %s = %d",(*ObjPtr).Name(),(*PremisePtr).INT_Value());
  686.             break;
  687.           case BIGGER:
  688.             printf("\n INT %s > %d",(*ObjPtr).Name(),(*PremisePtr).INT_Value());
  689.             break;
  690.           case LESSER:
  691.             printf("\n INT %s < %d",(*ObjPtr).Name(),(*PremisePtr).INT_Value());
  692.             break;
  693.           default:
  694.             break;
  695.           }
  696.         PremisePtr=(*PremisePtr).Next();
  697.         }
  698.      printf("\n THEN");
  699.      /*
  700.        C
  701.      printf("\n  %s",(*RulePtr->Conclusion->Object).Name());
  702.      printf("=%s",RulePtr->Conclusion->Value);
  703.      */
  704.      printf("\n %s", (*((*((*RulePtr).Conclusion())).Object())).Name() );
  705.      printf("=%s",(*(*RulePtr).Conclusion()).Value()  );
  706.      ControlScr();
  707.      RulePtr=(*RulePtr).Next();
  708.      printf("\n");
  709.      }
  710.  
  711.   if ( d != 'c') {
  712.     printf("\n *****End of Rule List****Hit 'q' <Enter> to exit:");
  713.     scanf("%s",s);
  714.     while ( strcmp(s,"q")) {
  715.       printf("\nHit 'q' <Enter> to exit:");
  716.       scanf("%s",s);
  717.       }
  718.     }
  719. }
  720.  
  721. /***********  Function 16 ************************************************/
  722.  
  723. void InitializeObjects()
  724. {
  725.   struct ObjectType *Object_Ptr;
  726.  
  727.   Object_Ptr=FirstObject;
  728.   while (Object_Ptr != NULL ) {
  729.      (*Object_Ptr).set_Value("Unknown");
  730.      (*Object_Ptr).set_Known(0);
  731.      Object_Ptr=(*Object_Ptr).Next();
  732.      
  733.      /*
  734.        used in C
  735.  
  736.      strcpy(Object_Ptr->Value,"Unknown");
  737.      Object_Ptr->Known=0;
  738.      Object_Ptr=Object_Ptr=Object_Ptr->Next;
  739.      */
  740.      }
  741. }
  742.   
  743.   
  744. /*********  Function 17 ************************************************/
  745.  
  746. void GiveObjectValue()
  747. {
  748.   char str[MaxCharactersObjectValue];
  749.   struct ObjectType *ObjPtr;
  750.   char  Objectname[MaxCharactersObjectName];
  751.  
  752.   printf("\nGive name of the object(if no more enter quit):");
  753.   scanf("%s",ObjectName);
  754.   while (strncmp(ObjectName,"quit",4)) {
  755.      ObjPtr=SearchObject(ObjectName);
  756.      if (ObjPtr != NULL ) {
  757.         printf("\nGive Value of Object (%s):",ObjectName);
  758.         scanf("%s",str);
  759.         (*ObjPtr).set_Value(str);
  760.         (*ObjPtr).set_Known(1);
  761.         }
  762.      else {
  763.         printf("\n The object (%s) does not exist",ObjectName);
  764.         }
  765.      printf("\nGive name of the object(if no more enter quit):");
  766.      scanf("%s",ObjectName);
  767.      }
  768. }
  769.         
  770.  
  771. /******   Functiopn 18 *********************************************/
  772.  
  773. void ListFacts()
  774. {
  775.   char s[MaxCharactersObjectName];
  776.  
  777.   EraseScreen();
  778.   printf("\n ******List of Objects Facts**********************\n");
  779.   ObjectPtr=FirstObject;
  780.   Line=2;
  781.   d='B';
  782.   while ((ObjectPtr != NULL)&&(d != 'c'))  {
  783.         if ((*ObjectPtr).Known() == 1) {
  784.            printf("Objname=%s,value=%s\n",(*ObjectPtr).Name(),(*ObjectPtr).Value());
  785.             }
  786.         else
  787.             printf("Objname=%s,value=UNKNOWN\n",(*ObjectPtr).Name());
  788.         ObjectPtr=(*ObjectPtr).Next();
  789.         ControlScr();
  790.         }
  791.   if ( d != 'c') {
  792.     printf("\n *****End of List****Hit 'q' <Enter> to exit:");
  793.     scanf("%s",s);
  794.     while ( strcmp(s,"q")) {
  795.       printf("\nHit 'q' <Enter> to exit:");
  796.       scanf("%s",s);
  797.       }
  798.     }
  799. }
  800.  
  801.  
  802. /********* Function 19 ****************************************/
  803. /*
  804.   return 1-- Yes
  805.  
  806.   return 0-- No 
  807. */
  808. int IsObjectInRules(struct ObjectType *ObjPtr)
  809. {
  810.   struct RuleType *Rule_Ptr;
  811.   struct PremiseType *Premise_Ptr;
  812.   int Found,Isin;
  813.  
  814.  
  815.   Isin=0;
  816.   Rule_Ptr=FirstRule;
  817.   while (Rule_Ptr != NULL ) {
  818.      Found=0;
  819.      Premise_Ptr=(*Rule_Ptr).Premise();
  820.  
  821.      while ((Premise_Ptr != NULL) && (Found == 0)) {
  822.         if ((*Premise_Ptr).Object() == ObjPtr) {
  823.            Found=1;
  824.            Isin=1;
  825.            }
  826.         Premise_Ptr=(*Premise_Ptr).Next();
  827.         };
  828.  
  829.      if ((*(*Rule_Ptr).Conclusion()).Object() == ObjPtr) {
  830.            Isin=1;
  831.            Found=1;
  832.            }
  833.  
  834.      if (Found == 1) 
  835.         printf("\n Object (%s) is in Rule (%s)",ObjPtr->Name,Rule_Ptr->Name);
  836.  
  837.      Rule_Ptr=(*Rule_Ptr).Next();
  838.      }
  839.   return(Isin);
  840. }
  841.  
  842. /********** Function 20 ****************************************/
  843.  
  844.  
  845. void EraseObject()
  846. {
  847.   char  Objectname[MaxCharactersObjectName];
  848.   struct ObjectType *ObjPtr;
  849.  
  850.  
  851.   printf("\nGive name of the object(if no more enter quit):");
  852.   scanf("%s",ObjectName);
  853.  
  854.   while (strncmp(ObjectName,"quit",4)) {
  855.      ObjPtr=SearchObject(ObjectName);
  856.      if (ObjPtr != NULL ) {
  857.         if (IsObjectInRules(ObjPtr) == 0 ) {
  858.            if (ObjPtr == FirstObject)
  859.               (*FirstObject).set_Next((*ObjPtr).Next());
  860.            else
  861.               (*PreviousPtr).set_Next((*ObjPtr).Next());
  862.            /*
  863.            free(ObjPtr);
  864.            */
  865.            delete ObjPtr;
  866.            }
  867.         else
  868.            printf("\n The object (%s) can not be deleted",ObjectName);
  869.         }
  870.      else
  871.         printf("\n The object (%s) does not exist",ObjectName);
  872.      printf("\nGive name of the object(if no more enter quit):");
  873.      scanf("%s",ObjectName);
  874.      }
  875.  
  876. /*************** Function 21 ******************************************/
  877.  
  878. void ForwardChaining()
  879. {
  880.   int Bad,Concludes;
  881.   struct RuleType *Rule_Ptr;
  882.   struct PremiseType *Premise_Ptr;
  883.  
  884.   do {
  885.      /* printf("\n First run"); */
  886.      Concludes=0;
  887.      Rule_Ptr=FirstRule;
  888.      do {
  889.        /*  printf("\n second run"); */
  890.         if ((*(*(*Rule_Ptr).Conclusion()).Object()).Known() == 0) {
  891.            Premise_Ptr=(*Rule_Ptr).Premise();
  892.            Bad=0;
  893.            while ((Premise_Ptr != NULL) && (Bad == 0)) {
  894.  
  895.              /*test whether premise is OK or not****************************/
  896.              switch ((*Premise_Ptr).Op()) {
  897.                case EQUAL_STR:
  898.                     if (strcmp((*(*Premise_Ptr).Object()).Value(),(*Premise_Ptr).Value()) != 0) 
  899.                     Bad=1;
  900.                     break;
  901.                case EQUAL_INT:
  902.                     if ((*Premise_Ptr).INT_Value() != (*(*Premise_Ptr).Object()).Diff() ) 
  903.                     Bad=1;
  904.                     if ( (*(*Premise_Ptr).Object()).Time_Left().tv_sec != (*(*Premise_Ptr).Object()).Time_Interval().tv_sec ) /*make sure it is the first time*/
  905.                     Bad=1; 
  906.                     
  907.                     break;
  908.                case BIGGER:
  909.                     if ((*Premise_Ptr).INT_Value() >= (*(*Premise_Ptr).Object()).Diff() ) 
  910.                     Bad=1;
  911.                     if ( (*(*Premise_Ptr).Object()).Time_Left().tv_sec != (*(*Premise_Ptr).Object()).Time_Interval().tv_sec ) /*make sure it is the first time*/
  912.                     Bad=1;
  913.                     
  914.                     break;
  915.                case LESSER:
  916.                     if ((*Premise_Ptr).INT_Value() <= (*(*Premise_Ptr).Object()).Diff() )
  917.                     Bad=1;
  918.                     if ( (*(*Premise_Ptr).Object()).Time_Left().tv_sec != (*(*Premise_Ptr).Object()).Time_Interval().tv_sec ) /*make sure it is the first time*/
  919.                     Bad=1;
  920.                     
  921.                     break;
  922.                default:
  923.                     break;
  924.                }
  925.               Premise_Ptr=(*Premise_Ptr).Next();
  926.               }
  927.            if (Bad == 0) {
  928.           /*    printf("\n A conclusion here");
  929.           */
  930.               Concludes=1;
  931.               (*(*(*Rule_Ptr).Conclusion()).Object()).set_Known(1);
  932.  
  933.               (*(*(*Rule_Ptr).Conclusion()).Object()).set_Value((*(*Rule_Ptr).Conclusion()).Value());
  934.  
  935.               if (Explanation == 1) {
  936.            /*   printf("\n Conclusions: (%s)",(*(*(*Rule_Ptr).Conclusion()).Object()).Name());
  937.            */
  938.                   printf("=(%s)",(*(*Rule_Ptr).Conclusion()).Value());
  939.                   printf(" based on rule (%s)",(*Rule_Ptr).Name());
  940.                   }
  941.               else {
  942.                   printf("\n Conclusions:(%s)",(*(*(*Rule_Ptr).Conclusion()).Object()).Name());
  943.                   printf("=(%s)",(*(*Rule_Ptr).Conclusion()).Value());
  944.                   }
  945.               }
  946.            }
  947.         Rule_Ptr=(*Rule_Ptr).Next();
  948.         } while (Rule_Ptr != NULL); 
  949.      } while (Concludes == 1);
  950. }
  951.        
  952.  
  953. /******* Function 22 ***************************************************/
  954.  
  955. struct RuleType *FindRule(ObjectType *ObjPtr,struct RuleType *RulePtr)
  956.  
  957. {
  958.   int Found;
  959.  
  960.   printf("\n in find function");
  961.   Found=False;
  962.   while ((RulePtr != NULL) && (Found == False)) {
  963.      if ((*(*RulePtr).Conclusion()).Object() == ObjPtr) {
  964.         Found=True;
  965.         return(RulePtr);
  966.         }
  967.      else
  968.         RulePtr=(*RulePtr).Next();
  969.      }
  970.   return(NULL);
  971. }
  972.  
  973. /****** Function 23 *************************************************/
  974.  
  975.  
  976. void BackwardChaining(ObjectType *ObjPtr)
  977. {
  978.   struct PremiseType *PremisePtr1;
  979.   struct RuleType *RulePtr1;
  980.   int Bad,Known;
  981.   char str[MaxCharactersObjectName];
  982.   
  983.   
  984.   printf("\n in Backwardchaining");
  985.   if ((*ObjPtr).Known() == False) {
  986.      Known=False;
  987.      (*ObjPtr).set_Known(True);
  988.      RulePtr1=FindRule(ObjPtr,FirstRule);
  989.      while ((RulePtr1 != NULL) && (Known == False)) {
  990.         printf("\n first loop");
  991.         PremisePtr1 =(*RulePtr1).Premise();
  992.         Bad=False;
  993.         while ((PremisePtr1 != NULL) && (Bad == False)) {
  994.            printf("\nsecond loop");
  995.            BackwardChaining((*PremisePtr1).Object());
  996.            if (strcmp((*(*PremisePtr1).Object()).Value(),(*PremisePtr1).Value()) != 0)
  997.               Bad=True;
  998.            else
  999.               PremisePtr1=(*PremisePtr1).Next();
  1000.            };
  1001.     
  1002.         if (Bad == False) {
  1003.            printf("\n third place");
  1004.            (*ObjPtr).set_Value((*(*RulePtr1).Conclusion()).Value());
  1005.            (*ObjPtr).set_Known(True);
  1006.            Known=True;
  1007.            if (Explanation == True) {
  1008.               printf("\nConcludes:(%s)=(%s)",(*ObjPtr).Name(),(*ObjPtr).Value());
  1009.               printf("based on rule (%s)",(*RulePtr1).Name());
  1010.               }
  1011.            }
  1012.         RulePtr1=FindRule(ObjPtr,(*RulePtr1).Next());
  1013.         }
  1014.  
  1015.     if (Known == False) {
  1016.        printf("\nGive value of the object (%s):",(*ObjPtr).Name());
  1017.        scanf("%s",str);
  1018.        /*
  1019.        strcpy(ObjPtr->Value,str);
  1020.        */
  1021.        (*ObjPtr).set_Value(str);
  1022.        }
  1023.     }
  1024.   else 
  1025.     printf("\n object (%s)=(%s)",(*ObjPtr).Name(),(*ObjPtr).Value());
  1026. }
  1027.         
  1028. /*************Function 24***************************************************/
  1029. void Load_DB(void)
  1030. {
  1031.   char Hostname[50];
  1032.   char Obj_name[50];
  1033.   char Probname[50];
  1034.   char Eventname[50];
  1035.   FILE *var_file;
  1036.   long time_interval;
  1037.   int typecode;
  1038.   struct timeval t;
  1039.  
  1040.   ObjectType *ObjPtr;
  1041.   int i;
  1042.  
  1043.   int firsttime;
  1044.   int Current_Value;
  1045.  
  1046.   char Rulname[50];
  1047.   char PremObjName[50];
  1048.   char Premvalue[50];
  1049.   char ConcObjName[50];
  1050.   char Concvalue[50];
  1051.   char ObjectName[50];
  1052.   ObjectType *ObjectPtr;
  1053.   int firstrule,firstPrem;
  1054.   RuleType *RulePtr;
  1055.   int Premtype, Int_value;
  1056.   PremiseType *PremPtr;
  1057.   ConclusionType *ConcPtr;
  1058.  
  1059.  
  1060.  
  1061.   /*
  1062.    initialize database
  1063.   */
  1064.  
  1065.  
  1066.   if ((var_file=fopen("VAR.TXT","r")) == NULL) {
  1067.      printf("\n VAR.TXT can not be opened");
  1068.      exit(0);
  1069.      };
  1070.  
  1071.   firsttime=1;
  1072.   while (1) {
  1073.     bzero(Hostname,sizeof(Hostname));
  1074.     bzero(Obj_name,sizeof(Obj_name));
  1075.     fscanf(var_file,"%d",&typecode);
  1076.     if (typecode == 9999){
  1077.        break;
  1078.        }
  1079.  
  1080.     switch (typecode) {
  1081.       case MIB_VARIANCE:
  1082.            fscanf(var_file,"%s",Hostname);
  1083.            fscanf(var_file,"%s",Obj_name);
  1084.            fscanf(var_file,"%d",&time_interval);
  1085.  
  1086.            ObjPtr=new ObjectType;
  1087.            (*ObjPtr).set_Type(MIB_VARIANCE);
  1088.            (*ObjPtr).set_hostname(Hostname);
  1089.            (*ObjPtr).set_objname(Obj_name);
  1090.            (*ObjPtr).set_Name(strcat(Hostname,Obj_name));
  1091.            t.tv_sec=time_interval;
  1092.            (*ObjPtr).set_timeleft(t);
  1093.            (*ObjPtr).set_timeinterval(t);
  1094.            (*ObjPtr).set_Known(0);
  1095.            
  1096.            break;
  1097.       case MIB_VALUE:
  1098.            fscanf(var_file,"%s",Hostname);
  1099.            fscanf(var_file,"%s",Obj_name);
  1100.            fscanf(var_file,"%d",&time_interval);
  1101.  
  1102.            ObjPtr=new ObjectType;
  1103.            (*ObjPtr).set_Type(MIB_VALUE);
  1104.            (*ObjPtr).set_hostname(Hostname);
  1105.            (*ObjPtr).set_objname(Obj_name);
  1106.            (*ObjPtr).set_Name(strcat(Hostname,Obj_name));
  1107.            t.tv_sec=time_interval;
  1108.            (*ObjPtr).set_timeleft(t);
  1109.            (*ObjPtr).set_timeinterval(t);
  1110.            (*ObjPtr).set_Known(0);
  1111.  
  1112.            break;
  1113.       case PROBLEM:
  1114.            bzero(Probname,sizeof(Probname));
  1115.            fscanf(var_file,"%s",Probname);
  1116.            
  1117.            ObjPtr=new ObjectType;
  1118.            (*ObjPtr).set_Type(PROBLEM);
  1119.            (*ObjPtr).set_Known(0);
  1120.            (*ObjPtr).set_Name(Probname);
  1121.  
  1122.            break;
  1123.       case EVENT:
  1124.            bzero(Eventname,sizeof(Eventname));
  1125.            fscanf(var_file,"%s",Eventname);
  1126.            
  1127.            ObjPtr=new ObjectType;
  1128.            (*ObjPtr).set_Type(EVENT);
  1129.            (*ObjPtr).set_Known(0);
  1130.            (*ObjPtr).set_Name(Eventname);
  1131.  
  1132.            break;
  1133.       default:
  1134.            break;
  1135.       }
  1136.  
  1137.     if (firsttime ==0 ) {
  1138.         (*ObjPtr).set_Next(FirstObject);
  1139.         FirstObject=ObjPtr;
  1140.         }
  1141.     else {
  1142.         firsttime=0;
  1143.         (*ObjPtr).set_Next(NULL);
  1144.         FirstObject=ObjPtr;
  1145.         }
  1146.     }
  1147.  
  1148.    /* initialize rule  */
  1149.  
  1150.    firstrule=1;
  1151.    while (1) {
  1152.      bzero(Rulname,sizeof(Rulname));
  1153.      fscanf(var_file,"%s",Rulname);
  1154.  
  1155.      if (!strcmp(Rulname,"END")) {
  1156.         break;
  1157.         }
  1158.  
  1159.      RulePtr=new RuleType;
  1160.      (*RulePtr).set_Name(Rulname);
  1161.      
  1162.      /* set premise   **********/
  1163.  
  1164.      fscanf(var_file,"%d",&Premtype);
  1165. printf("\n Premtype is %d",Premtype);
  1166.      firstPrem=1;
  1167.      while (Premtype != 8888) {
  1168.        PremPtr=new PremiseType; 
  1169.        (*PremPtr).set_Operation(Premtype);
  1170.  
  1171.        fscanf(var_file,"%s",PremObjName);
  1172.        strcpy(ObjectName,PremObjName);              
  1173.        ObjectPtr=SearchObject(ObjectName);
  1174.        if (ObjectPtr == NULL) {
  1175.           printf("\n$$This object does not exist, but it is created");
  1176.           ObjectPtr=CreateObject(ObjectName); 
  1177.           }
  1178.        (*PremPtr).set_Object(ObjectPtr);
  1179.  
  1180.        switch (Premtype) {
  1181.          case EQUAL_STR:
  1182.               fscanf(var_file,"%s",Premvalue);
  1183.               (*PremPtr).set_Value(Premvalue);
  1184.               break;
  1185.          case EQUAL_INT:
  1186.          case BIGGER:
  1187.          case LESSER:
  1188.               fscanf(var_file,"%d",&Int_value);
  1189.               (*PremPtr).set_Int(Int_value);
  1190.               break;
  1191.          default:
  1192.               printf("\n file is in wrong format");
  1193.               break;
  1194.          };
  1195.  
  1196.        if (firstPrem == 0) {
  1197.             (*PremPtr).set_Next((*RulePtr).Premise());
  1198.             (*RulePtr).set_Premise(PremPtr);
  1199.             }
  1200.        else {
  1201.             firstPrem=0;
  1202.             (*PremPtr).set_Next(NULL);
  1203.             (*RulePtr).set_Premise(PremPtr);
  1204.             }
  1205.        fscanf(var_file,"%d",&Premtype);
  1206.        }
  1207.  
  1208.      /***set conclusion********************************************/
  1209.      fscanf(var_file,"%s",ConcObjName);
  1210.      strcpy(ObjectName,ConcObjName);
  1211.      ObjectPtr=SearchObject(ObjectName);
  1212.      if (ObjectPtr == NULL) {
  1213.         printf("\n$$This object does not exist, but it is created");
  1214.         ObjectPtr=CreateObject(ObjectName);
  1215.         }
  1216.      ConcPtr=new ConclusionType;
  1217.      (*ConcPtr).set_Object(ObjectPtr);
  1218.  
  1219.      fscanf(var_file,"%s",Concvalue);
  1220.  
  1221.      (*ConcPtr).set_Value(Concvalue);
  1222.      
  1223.      (*RulePtr).set_Conclusion(ConcPtr);
  1224.  
  1225.  
  1226.      /**next Rule************************************************/
  1227.      if (firstrule == 0) {
  1228.         (*RulePtr).set_Next(FirstRule);
  1229.         FirstRule=RulePtr;
  1230.         }
  1231.      else {
  1232.         firstrule=0;
  1233.         (*RulePtr).set_Next(NULL);
  1234.         FirstRule=RulePtr;
  1235.         }
  1236.      }
  1237.    close(var_file);
  1238. }
  1239.  
  1240. void Save_DB()
  1241. {
  1242.   ObjectType *ObjectPtr;
  1243.   RuleType *RulePtr;
  1244.   PremiseType *PremPtr;
  1245.   FILE *var_file;
  1246.  
  1247.     if ((var_file=fopen("VAR.TXT","w")) == NULL) {
  1248.      printf("\n VAR.TXT can not be opened");
  1249.      exit(0);
  1250.      };
  1251.  
  1252.     /*
  1253.       write object
  1254.     */
  1255.  
  1256.     ObjectPtr=FirstObject;
  1257.     while (ObjectPtr != NULL) {
  1258.        fprintf(var_file,"%d  ",(*ObjectPtr).Type());
  1259.        switch((*ObjectPtr).Type()) {
  1260.          case MIB_VARIANCE:
  1261.               fprintf(var_file,"%s  ",(*ObjectPtr).Hostname());
  1262.               fprintf(var_file,"%s  ",(*ObjectPtr).Obj_Name());
  1263.               fprintf(var_file,"%d  ",(*ObjectPtr).Time_Interval().tv_sec);
  1264.               break;
  1265.          case PROBLEM:
  1266.               fprintf(var_file,"%s  ",(*ObjectPtr).Name());
  1267.               break;
  1268.          case EVENT:
  1269.               fprintf(var_file,"%s  ",(*ObjectPtr).Name()); 
  1270.               break;
  1271.          case MIB_VALUE:
  1272.               fprintf(var_file,"%s  ",(*ObjectPtr).Hostname());
  1273.               fprintf(var_file,"%s  ",(*ObjectPtr).Obj_Name());
  1274.               fprintf(var_file,"%d  ",(*ObjectPtr).Time_Interval().tv_sec);
  1275.  
  1276.               break;
  1277.          default:
  1278.               break;
  1279.          }
  1280.       fprintf(var_file,"\n");
  1281.       ObjectPtr =(*ObjectPtr).Next();
  1282.       }
  1283.     fprintf(var_file,"9999\n");
  1284.  
  1285.     /*
  1286.      write rule
  1287.     */
  1288.  
  1289.     RulePtr=FirstRule;
  1290.     while (RulePtr != NULL) {
  1291.       fprintf(var_file,"%s ",(*RulePtr).Name());   
  1292.       PremPtr=(*RulePtr).Premise();
  1293.       while (PremPtr != NULL) {
  1294.          fprintf(var_file,"%d ",(*PremPtr).Op());
  1295.          fprintf(var_file,"%s ",(*(*PremPtr).Object()).Name());
  1296.          switch ((*PremPtr).Op()) {
  1297.            case EQUAL_STR:
  1298.               fprintf(var_file,"%s ",(*PremPtr).Value());
  1299.               break;
  1300.            case EQUAL_INT:
  1301.            case BIGGER:
  1302.            case LESSER:
  1303.               fprintf(var_file,"%d ",(*PremPtr).INT_Value());
  1304.               break;
  1305.            default:
  1306.               break;
  1307.            };
  1308.          PremPtr=(*PremPtr).Next();
  1309.          }
  1310.       fprintf(var_file,"8888 ");
  1311.  
  1312.       /* conclusion*/
  1313.       fprintf(var_file,"%s ",(*(*(*RulePtr).Conclusion()).Object()).Name());   
  1314.       fprintf(var_file,"%s ",(*(*RulePtr).Conclusion()).Value());   
  1315.  
  1316.       fprintf(var_file,"\n");
  1317.       RulePtr=(*RulePtr).Next();
  1318.       }
  1319.     fprintf(var_file,"END\n");
  1320.  
  1321. }
  1322.  
  1323. /* set all Event and Problem as unknow */
  1324. void Clear_DB()
  1325. {
  1326.   ObjectType *ObjPtr;
  1327.  
  1328.     ObjPtr=FirstObject;
  1329.     while (ObjPtr != NULL ) {
  1330.        switch ((*ObjPtr).Type()) {
  1331.          case MIB_VARIANCE:
  1332.            break;
  1333.          case MIB_VALUE:
  1334.            break;
  1335.          case PROBLEM:
  1336.            (*ObjPtr).set_Known(0);
  1337.            (*ObjPtr).set_Value("");
  1338.            break;
  1339.          case EVENT:
  1340.            (*ObjPtr).set_Known(0);
  1341.            (*ObjPtr).set_Value("");
  1342.            break;
  1343.          default:
  1344.            break;
  1345.          }
  1346.        ObjPtr=(*ObjPtr).Next();
  1347.        };
  1348. }
  1349.         
  1350.  
  1351. void ITRDB()
  1352. {
  1353.   int c;
  1354.   int type;
  1355.   char str[MaxCharactersObjectName];
  1356.   char ObjName[MaxCharactersObjectName];
  1357.   struct ObjectType *ObjPtr; 
  1358.  
  1359.   
  1360.   Explanation=True;   
  1361.   NumberofObjects=0;
  1362.   NumberofRules=0;
  1363.   FirstObject=NULL;
  1364.   Finish=False;
  1365.   EraseScreen();
  1366.  
  1367.   do {
  1368.     GenerateList(List,&Length1,1); /***** Fun 2 ********/
  1369.     OptionList(List,Length1,&option); /****** Fun 3 ********/
  1370.     switch (option) {
  1371.       case 1:
  1372.            GenerateList(List,&Length1,2); /******** Fun 2 **********/
  1373.            OptionList(List,Length1,&option1); /******* Fun 3 *******/
  1374.            switch (option1) {
  1375.              case 1:
  1376.               /* old version
  1377.    
  1378.                 GiveNameofObject(ObjectName); 
  1379.                 while (strncmp(ObjectName,"quit",4)) {
  1380.                   ObjectPtr=SearchObject(ObjectName); 
  1381.                   if (ObjectPtr ==NULL) 
  1382.                       ObjectPtr=CreateObject(ObjectName); 
  1383.                   GiveNameofObject(ObjectName);
  1384.                   };
  1385.               */
  1386.  
  1387.                 printf("\nGive type of the object(9999 end):");
  1388.                 scanf("%d", &type);
  1389.                 while (type != 9999) {
  1390.                   ObjectPtr=CreateObject2(type);
  1391.                   printf("\nGive type of the object(9999 end):");
  1392.                   scanf("%d", &type);
  1393.                   }
  1394.                 break;
  1395.              case 2:
  1396.                  /****Erase an Object from the object list******/
  1397.                  EraseObject();        /*****Fun 20*******/
  1398.                  break;
  1399.              case 3:
  1400.                  ListObjects(); /*** Fun 9****/
  1401.                  break;
  1402.              default:
  1403.                  break;
  1404.              };
  1405.            break;
  1406.       case 2:
  1407.            GenerateList(List,&Length1,2);      /**** Fun 2 *************/      
  1408.            OptionList(List,Length1,&option1);  /**** Fun 3 *************/
  1409.            switch (option1) {
  1410.              case 1:
  1411.                   c=GiveRuleName(RuleName);     /***** Fun 10 ***********/     
  1412.                   while (c==1)  {
  1413.                     if(SearchRule(RuleName) == NULL); /*Fun 11*/
  1414.                        RulePtr=CreateRule(); /*Fun 13*/
  1415.                     c=GiveRuleName(RuleName); /*Fun 10*/
  1416.                     }
  1417.                   break;
  1418.              case 2:
  1419.                   c=GiveRuleName(RuleName);
  1420.                   while( c== 1) {
  1421.                      if ( EraseRule(RuleName))
  1422.                         printf("\n %s is erased",RuleName);
  1423.                      else
  1424.                         printf("\n No such Rule");
  1425.                      c=GiveRuleName(RuleName);
  1426.                      }
  1427.                   break;
  1428.              case 3:
  1429.                   ListRules();      
  1430.                   break;
  1431.              default:
  1432.                   break;
  1433.              }
  1434.            break;
  1435.       case 3:
  1436.            GenerateList(List,&Length1,3);
  1437.            OptionList(List,Length1,&option1);
  1438.            switch (option1) {
  1439.                case 1:
  1440.                      InitializeObjects();   /***Fun 16***/
  1441.                      break;
  1442.                case 2:
  1443.                      GiveObjectValue();     /***Fun 17***/
  1444.                      break;
  1445.                case 3:
  1446.                      ListFacts();
  1447.                      break;
  1448.                default:
  1449.                      break;
  1450.                }
  1451.            break;
  1452.       case 4:
  1453.            do {
  1454.               printf("\nGive name of the object(if no more enter quit):");
  1455.               scanf("%s",ObjName);
  1456.               printf("\n something");
  1457.               ObjPtr=SearchObject(ObjName);
  1458.               if (ObjPtr != NULL ) {
  1459.                  BackwardChaining(ObjPtr);
  1460.                  if (Explanation == False)
  1461.                     printf("\nConcludes: (%s)=(%s)",ObjPtr->Name,ObjPtr->Value);
  1462.                  }
  1463.               else {
  1464.                  printf("\n This object does not exist");
  1465.                  }
  1466.               printf("\n Done? (y/n)");
  1467.               scanf("%s",str);
  1468.               } while (strcmp(str,"y"));
  1469.            break;
  1470.       case 5:
  1471.            do {
  1472.               ForwardChaining();
  1473.               printf("\n Done? (y/n)");
  1474.               scanf("%s",str);
  1475.               } while (strcmp(str,"y"));
  1476.            break;
  1477.       case 6:
  1478.            Save_DB();
  1479.            break;
  1480.       case 7:
  1481.            Load_DB();
  1482.            break;
  1483.       case 8:
  1484.            printf("\n Explanation? (y/n)");
  1485.            scanf("%s",str);
  1486.            if (!strcmp(str,"y")) 
  1487.               Explanation=True;
  1488.            else
  1489.               Explanation=False;
  1490.            break;
  1491.       case 9:
  1492.  
  1493.            Finish=1;
  1494.            break;
  1495.       default:
  1496.  
  1497.            break;
  1498.       };
  1499.   } while (Finish == 0);
  1500.  
  1501. }
  1502.  
  1503.