⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 engine.c

📁 NASA 开发使用的一个专家系统
💻 C
📖 第 1 页 / 共 3 页
字号:
   /* limit was reached, then print a message.             */   /*======================================================*/      if (runLimit == rulesFired)     { PrintCLIPS(WDIALOG,"rule firing limit reached\n"); }   /*==============================*/   /* Restore execution variables. */   /*==============================*/   ExecutingRule = NULL;   HaltRules = CLIPS_FALSE;   /*=================================================*/   /* Print out statistics if they are being watched. */   /*=================================================*/   #if DEBUGGING_FUNCTIONS   if (WatchStatistics)     {      char printSpace[60];      endTime = gentime();      PrintLongInteger(WDIALOG,rulesFired);      PrintCLIPS(WDIALOG," rules fired");#if (! GENERIC)      if (startTime != endTime)        {         PrintCLIPS(WDIALOG,"        Run time is ");         PrintFloat(WDIALOG,endTime - startTime);         PrintCLIPS(WDIALOG," seconds.\n");         PrintFloat(WDIALOG,(double) rulesFired / (endTime - startTime));         PrintCLIPS(WDIALOG," rules per second.\n");        }      else        { PrintCLIPS(WDIALOG,"\n"); }#endif#if DEFTEMPLATE_CONSTRUCT      sprintf(printSpace,"%ld mean number of facts (%ld maximum).\n",                          (long) (((double) sumFacts / (rulesFired + 1)) + 0.5),                          maxFacts);      PrintCLIPS(WDIALOG,printSpace);#endif#if OBJECT_SYSTEM      sprintf(printSpace,"%ld mean number of instances (%ld maximum).\n",                          (long) (((double) sumInstances / (rulesFired + 1)) + 0.5),                          maxInstances);      PrintCLIPS(WDIALOG,printSpace);#endif      sprintf(printSpace,"%ld mean number of activations (%ld maximum).\n",                          (long) (((double) sumActivations / (rulesFired + 1)) + 0.5),                          maxActivations);      PrintCLIPS(WDIALOG,printSpace);     }#endif   /*==========================================*/   /* The current module should be the current */   /* focus when the run finishes.             */   /*==========================================*/      if (CurrentFocus != NULL)     {      if (CurrentFocus->theModule != ((struct defmodule *) GetCurrentModule()))        { SetCurrentModule((VOID *) CurrentFocus->theModule); }     }           /*===================================*/   /* Return the number of rules fired. */   /*===================================*/   alreadyRunning = CLIPS_FALSE;   return(rulesFired);  }  /***********************************************************//* NextActivationToFire: Returns the next activation which *//*   should be executed based on the current focus.        *//***********************************************************/static struct activation *NextActivationToFire()  {   struct activation *theActivation;   struct defmodule *theModule;      /*====================================*/   /* If there is no current focus, then */   /* focus on the MAIN module.          */   /*====================================*/                        if (CurrentFocus == NULL)     {       theModule = (struct defmodule *) FindDefmodule("MAIN");      Focus(theModule);     }        /*===========================================================*/   /* Determine the top activation on the agenda of the current */   /* focus. If the current focus has no activations on its     */   /* agenda, then pop the focus off the focus stack until      */   /* a focus that has an activation on its agenda is found.    */   /*===========================================================*/      theActivation = CurrentFocus->theDefruleModule->agenda;   while ((theActivation == NULL) && (CurrentFocus != NULL))     {      if (CurrentFocus != NULL) PopFocus();       if (CurrentFocus != NULL) theActivation = CurrentFocus->theDefruleModule->agenda;     }        /*=========================================*/   /* Return the next activation to be fired. */   /*=========================================*/      return(theActivation);  }  /***************************************************//* RemoveFocus: Removes the first occurence of the *//*   specified module from the focus stack.        *//***************************************************/static struct defmodule *RemoveFocus(theModule)  struct defmodule *theModule;  {   struct focus *tempFocus,*prevFocus, *nextFocus;   int found = CLIPS_FALSE;   int currentFocusRemoved = CLIPS_FALSE;   /*====================================*/   /* Return NULL if there is nothing on */   /* the focus stack to remove.         */   /*====================================*/      if (CurrentFocus == NULL) return(NULL);   /*=============================================*/   /* Remove the first occurence of the specified */   /* module from the focus stack.                */   /*=============================================*/      prevFocus = NULL;   tempFocus = CurrentFocus;   while ((tempFocus != NULL) && (! found))     {      if (tempFocus->theModule == theModule)        {         found = CLIPS_TRUE;         nextFocus = tempFocus->next;         rtn_struct(focus,tempFocus);         tempFocus = nextFocus;         if (prevFocus == NULL)           {            currentFocusRemoved = CLIPS_TRUE;            CurrentFocus = tempFocus;           }         else           { prevFocus->next = tempFocus; }        }      else        {         prevFocus = tempFocus;         tempFocus = tempFocus->next;        }     }      /*=========================================*/   /* If the given module is not in the focus */   /* stack, simply return the current focus  */   /*=========================================*/      if (! found) return(CurrentFocus->theModule);      /*========================================*/   /* If the current focus is being watched, */   /* then print an informational message.   */   /*========================================*/#if DEBUGGING_FUNCTIONS   if (WatchFocus)     {      PrintCLIPS(WTRACE,"<== Focus ");      PrintCLIPS(WTRACE,ValueToString(theModule->name));            if ((CurrentFocus != NULL) && currentFocusRemoved)        {         PrintCLIPS(WTRACE," to ");         PrintCLIPS(WTRACE,ValueToString(CurrentFocus->theModule->name));        }              PrintCLIPS(WTRACE,"\n");     }#endif      /*======================================================*/   /* Set the current module to the module associated with */   /* the current focus (if it changed) and set a boolean  */   /* flag indicating that the focus has changed.          */   /*======================================================*/      if ((CurrentFocus != NULL) && currentFocusRemoved)     { SetCurrentModule((VOID *) CurrentFocus->theModule); }   FocusChanged = CLIPS_TRUE;      /*====================================*/   /* Return the module that was removed */   /* from the focus stack.              */   /*====================================*/      return(theModule);  }  /**********************************************************//* PopFocus: C access routine for the pop-focus function. *//**********************************************************/globle VOID *PopFocus()  {   if (CurrentFocus == NULL) return(NULL);   return((VOID *) RemoveFocus(CurrentFocus->theModule));  }  /*************************************************************//* GetNextFocus: Returns the next focus on the focus stack. *//*************************************************************/globle VOID *GetNextFocus(theFocus)  VOID *theFocus;  {   /*==================================================*/   /* If NULL is passed as an argument, return the top */   /* focus on the focus stack (the current focus).    */   /*==================================================*/      if (theFocus == NULL) return((VOID *) CurrentFocus);      /*=======================================*/   /* Otherwise, return the focus following */   /* the focus passed as an argument.      */   /*=======================================*/      return((VOID *) ((struct focus *) theFocus)->next);  }  /***************************************************//* Focus: C access routine for the focus function. *//***************************************************/globle VOID Focus(vTheModule)  VOID *vTheModule;  {   struct defmodule *theModule = (struct defmodule *) vTheModule;   struct focus *tempFocus;      /*==================================================*/   /* Make the specified module be the current module. */   /* If the specified module is the current focus,    */   /* then no further action is needed.                */   /*==================================================*/      SetCurrentModule((VOID *) theModule);   if (CurrentFocus != NULL)     { if (CurrentFocus->theModule == theModule) return; }     /*=====================================*/   /* If the focus is being watched, then */   /* print an information message.       */   /*=====================================*/   #if DEBUGGING_FUNCTIONS      if (WatchFocus)     {      PrintCLIPS(WTRACE,"==> Focus ");      PrintCLIPS(WTRACE,ValueToString(theModule->name));      if (CurrentFocus != NULL)        {         PrintCLIPS(WTRACE," from ");         PrintCLIPS(WTRACE,ValueToString(CurrentFocus->theModule->name));        }      PrintCLIPS(WTRACE,"\n");     }#endif      /*=======================================*/   /* Add the new focus to the focus stack. */   /*=======================================*/      tempFocus = get_struct(focus);   tempFocus->theModule = theModule;   tempFocus->theDefruleModule = GetDefruleModuleItem(theModule);   tempFocus->next = CurrentFocus;   CurrentFocus = tempFocus;   FocusChanged = CLIPS_TRUE;  }/************************************************//* ClearFocusStackCommand: CLIPS access routine *//*   for the clear-focus-stack command.         *//************************************************/globle VOID ClearFocusStackCommand()  {   if (ArgCountCheck("list-focus-stack",EXACTLY,0) == -1) return;   ClearFocusStack();  }  /****************************************//* ClearFocusStack: C access routine    *//*   for the clear-focus-stack command. *//****************************************/globle VOID ClearFocusStack()  {   while (CurrentFocus != NULL) PopFocus();        FocusChanged = CLIPS_TRUE;  }  /**************************************************************//* AddRunFunction: Adds a function to the ListOfRunFunctions. *//**************************************************************/globle BOOLEAN AddRunFunction(name,functionPtr,priority)  char *name;#if ANSI_COMPILER   VOID (*functionPtr)(void);#else   VOID (*functionPtr)();#endif  int priority;  {   ListOfRunFunctions = AddFunctionToCallList(name,priority,                                              functionPtr,                                              ListOfRunFunctions);   return(1);  }/**********************************************************************//* RemoveRunFunction: Removes a function from the ListOfRunFunctions. *//**********************************************************************/globle BOOLEAN RemoveRunFunction(name)  char *name;  {   int found;   ListOfRunFunctions = RemoveFunctionFromCallList(name,ListOfRunFunctions,&found);   if (found) return(CLIPS_TRUE);      return(CLIPS_FALSE);  }/*****************************************************************************//* InitializeEngine: Initializes the activations and statistics watch items. *//*****************************************************************************/globle VOID InitializeEngine()  {#if DEBUGGING_FUNCTIONS   AddWatchItem("statistics",0,&WatchStatistics,20,NULL,NULL);   AddWatchItem("focus",0,&WatchFocus,0,NULL,NULL);#endif  }  /*********************************************************//* RunCommand: CLIPS access routine for the run command. *//*********************************************************/globle VOID RunCommand()  {   int numArgs;   long int runLimit = -1;   DATA_OBJECT argPtr;   if ((numArgs = ArgCountCheck("run",NO_MORE_THAN,1)) == -1) return;   if (numArgs == 0)     { runLimit = -1; }   else if (numArgs == 1)     {      if (ArgTypeCheck("run",1,INTEGER,&argPtr) == CLIPS_FALSE) return;      runLimit = DOToLong(argPtr);     }   Run(runLimit);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -