?? agenda.c
字號:
/*******************************************************/ /* "C" Language Integrated Production System */ /* */ /* CLIPS Version 6.30 10/19/06 */ /* */ /* AGENDA MODULE */ /*******************************************************//*************************************************************//* Purpose: *//* Provides functionality for examining, manipulating, *//* adding, and removing activations from the agenda. *//* *//* Principal Programmer(s): *//* Gary D. Riley *//* *//* Contributing Programmer(s): *//* Brian L. Donnell *//* *//* Revision History: *//* 6.23: Corrected compilation errors for files *//* generated by constructs-to-c. DR0861 *//* *//* 6.24: Removed CONFLICT_RESOLUTION_STRATEGIES *//* and DYNAMIC_SALIENCE compilation flags. *//* *//* Renamed BOOLEAN macro type to intBool. *//* *//* Added EnvGetActivationBasisPPForm function. *//* *//* 6.30: Added salience groups to improve performance *//* with large numbers of activations of different *//* saliences. *//* *//*************************************************************/#define _AGENDA_SOURCE_#include <stdio.h>#define _STDIO_INCLUDED_#include <string.h>#include "setup.h"#if DEFRULE_CONSTRUCT#include "argacces.h"#include "constant.h"#include "crstrtgy.h"#include "engine.h"#include "envrnmnt.h"#include "extnfunc.h"#include "memalloc.h"#include "moduldef.h"#include "modulutl.h"#include "multifld.h"#include "reteutil.h"#include "retract.h"#include "router.h"#include "rulebsc.h"#include "ruledef.h"#include "strngrtr.h"#include "sysdep.h"#include "watch.h"#include "agenda.h"/***************************************//* LOCAL INTERNAL FUNCTION DEFINITIONS *//***************************************/ static void PrintActivation(void *,char *,void *); static void AgendaClearFunction(void *); static char *SalienceEvaluationName(int); static int EvaluateSalience(void *,void *); static struct salienceGroup *ReuseOrCreateSalienceGroup(void *,struct defruleModule *,int); static struct salienceGroup *FindSalienceGroup(struct defruleModule *,int); static void RemoveActivationFromGroup(void *,struct activation *,struct defruleModule *); /*************************************************//* InitializeAgenda: Initializes the activations *//* watch item and the H/L commands for *//* manipulating the agenda. *//*************************************************/globle void InitializeAgenda( void *theEnv) { AllocateEnvironmentData(theEnv,AGENDA_DATA,sizeof(struct agendaData),NULL); AgendaData(theEnv)->SalienceEvaluation = WHEN_DEFINED; AgendaData(theEnv)->Strategy = DEFAULT_STRATEGY; EnvAddClearFunction(theEnv,"agenda",AgendaClearFunction,0);#if DEBUGGING_FUNCTIONS AddWatchItem(theEnv,"activations",1,&AgendaData(theEnv)->WatchActivations,40,DefruleWatchAccess,DefruleWatchPrint);#endif#if ! RUN_TIME EnvDefineFunction2(theEnv,"refresh", 'v', PTIEF RefreshCommand, "RefreshCommand", "11w"); EnvDefineFunction2(theEnv,"refresh-agenda",'v', PTIEF RefreshAgendaCommand,"RefreshAgendaCommand", "01w"); EnvDefineFunction2(theEnv,"get-salience-evaluation",'w', PTIEF GetSalienceEvaluationCommand, "GetSalienceEvaluationCommand", "00"); EnvDefineFunction2(theEnv,"set-salience-evaluation",'w', PTIEF SetSalienceEvaluationCommand, "SetSalienceEvaluationCommand", "11w");#if DEBUGGING_FUNCTIONS EnvDefineFunction2(theEnv,"agenda", 'v', PTIEF AgendaCommand, "AgendaCommand", "01w");#endif#endif }/*****************************************************************//* AddActivation: Creates a rule activation to be added to the *//* Agenda and links the activation with its associated partial *//* match. The function PlaceActivation is then called to place *//* the activation on the Agenda. Typically called when all *//* patterns on the LHS of a rule have been satisfied. *//*****************************************************************/globle void AddActivation( void *theEnv, void *vTheRule, void *vBinds) { struct activation *newActivation; struct defrule *theRule = (struct defrule *) vTheRule; struct partialMatch *binds = (struct partialMatch *) vBinds; struct defruleModule *theModuleItem; struct salienceGroup *theGroup; /*=======================================*/ /* Focus on the module if the activation */ /* is from an auto-focus rule. */ /*=======================================*/ if (theRule->autoFocus) { EnvFocus(theEnv,(void *) theRule->header.whichModule->theModule); } /*=======================================================*/ /* Create the activation. The activation stores pointers */ /* to its associated partial match and defrule. The */ /* activation is given a time tag, its salience is */ /* evaluated, and it is assigned a random number for use */ /* with the random conflict resolution strategy. */ /*=======================================================*/ newActivation = get_struct(theEnv,activation); newActivation->theRule = theRule; newActivation->basis = binds; newActivation->timetag = AgendaData(theEnv)->CurrentTimetag++; newActivation->salience = EvaluateSalience(theEnv,theRule); newActivation->randomID = genrand(); newActivation->prev = NULL; newActivation->next = NULL; AgendaData(theEnv)->NumberOfActivations++; /*=======================================================*/ /* Point the partial match to the activation to complete */ /* the link between the join network and the agenda. */ /*=======================================================*/ binds->marker = (void *) newActivation; /*====================================================*/ /* If activations are being watch, display a message. */ /*====================================================*/#if DEBUGGING_FUNCTIONS if (newActivation->theRule->watchActivation) { EnvPrintRouter(theEnv,WTRACE,"==> Activation "); PrintActivation(theEnv,WTRACE,(void *) newActivation); EnvPrintRouter(theEnv,WTRACE,"\n"); }#endif /*=====================================*/ /* Place the activation on the agenda. */ /*=====================================*/ theModuleItem = (struct defruleModule *) theRule->header.whichModule; theGroup = ReuseOrCreateSalienceGroup(theEnv,theModuleItem,newActivation->salience); PlaceActivation(theEnv,&(theModuleItem->agenda),newActivation,theGroup); }/***************************************************************//* ReuseOrCreateSalienceGroup: *//***************************************************************/static struct salienceGroup *ReuseOrCreateSalienceGroup( void *theEnv, struct defruleModule *theRuleModule, int salience) { struct salienceGroup *theGroup, *lastGroup, *newGroup; for (lastGroup = NULL, theGroup = theRuleModule->groupings; theGroup != NULL; lastGroup = theGroup, theGroup = theGroup->next) { if (theGroup->salience == salience) { return(theGroup); } if (theGroup->salience < salience) { break; } } newGroup = get_struct(theEnv,salienceGroup); newGroup->salience = salience; newGroup->first = NULL; newGroup->last = NULL; newGroup->next = theGroup; newGroup->prev = lastGroup; if (newGroup->next != NULL) { newGroup->next->prev = newGroup; } if (newGroup->prev != NULL) { newGroup->prev->next = newGroup; } if (lastGroup == NULL) { theRuleModule->groupings = newGroup; } return newGroup; }/***************************************************************//* FindSalienceGroup: *//***************************************************************/static struct salienceGroup *FindSalienceGroup( struct defruleModule *theRuleModule, int salience) { struct salienceGroup *theGroup; for (theGroup = theRuleModule->groupings; theGroup != NULL; theGroup = theGroup->next) { if (theGroup->salience == salience) { return(theGroup); } if (theGroup->salience < salience) { break; } } return NULL; } /***************************************************************//* ClearRuleFromAgenda: Clears the agenda of a specified rule. *//***************************************************************/globle void ClearRuleFromAgenda( void *theEnv, void *vTheRule) { struct defrule *theRule = (struct defrule *) vTheRule; struct defrule *tempRule; struct activation *agendaPtr, *agendaNext; /*============================================*/ /* Get a pointer to the agenda for the module */ /* in which the rule is contained. */ /*============================================*/ agendaPtr = ((struct defruleModule *) theRule->header.whichModule)->agenda; /*==============================================*/ /* Loop through every activation on the agenda. */ /*==============================================*/ while (agendaPtr != NULL) { agendaNext = agendaPtr->next; /*========================================================*/ /* Check each disjunct of the rule against the activation */ /* to determine if the activation points to the rule. If */ /* it does, then remove the activation from the agenda. */ /*========================================================*/ for (tempRule = theRule; tempRule != NULL; tempRule = tempRule->disjunct) { if (agendaPtr->theRule == tempRule) { RemoveActivation(theEnv,agendaPtr,TRUE,TRUE); break; } } agendaPtr = agendaNext; } }/****************************************************************//* EnvGetNextActivation: Returns an activation from the Agenda. *//* If its argument is NULL, then the first activation on the *//* Agenda is returned. If its argument is not NULL, the next *//* activation after the argument is returned. *//****************************************************************/globle void *EnvGetNextActivation( void *theEnv, void *actPtr) { struct defruleModule *theModuleItem; if (actPtr == NULL) { theModuleItem = (struct defruleModule *) GetModuleItem(theEnv,NULL,DefruleData(theEnv)->DefruleModuleIndex); if (theModuleItem == NULL) return(NULL); return((void *) theModuleItem->agenda); } else { return((void *) (((struct activation *) actPtr)->next)); } }/*********************************************//* EnvGetActivationName: Returns the name of *//* the rule associated with an activation. *//*********************************************/#if IBM_TBC
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -