📄 agenda.c
字号:
/*******************************************************/
/* "C" Language Integrated Production System */
/* */
/* CLIPS Version 6.24 05/17/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. */
/* */
/*************************************************************/
#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 *);
/*************************************************/
/* 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;
/*=======================================*/
/* 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->sortedBasis = NULL;
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->binds[binds->bcount].gm.theValue = (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;
PlaceActivation(theEnv,&(theModuleItem->agenda),newActivation);
}
/***************************************************************/
/* 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
#pragma argsused
#endif
globle char *EnvGetActivationName(
void *theEnv,
void *actPtr)
{
#if MAC_MCW || IBM_MCW || MAC_XCD
#pragma unused(theEnv)
#endif
return(ValueToString(((struct activation *) actPtr)->theRule->header.name));
}
/**************************************/
/* EnvSetActivationSalience: Sets the */
/* salience value of an activation. */
/**************************************/
#if IBM_TBC
#pragma argsused
#endif
globle int EnvSetActivationSalience(
void *theEnv,
void *actPtr,
int value)
{
int temp;
#if MAC_MCW || IBM_MCW || MAC_XCD
#pragma unused(theEnv)
#endif
temp = ((struct activation *) actPtr)->salience;
((struct activation *) actPtr)->salience = value;
return(temp);
}
/**********************************************/
/* EnvGetActivationPPForm: Returns the pretty */
/* print representation of an activation. */
/**********************************************/
globle void EnvGetActivationPPForm(
void *theEnv,
char *buffer,
unsigned bufferLength,
void *theActivation)
{
OpenStringDestination(theEnv,"ActPPForm",buffer,bufferLength);
PrintActivation(theEnv,"ActPPForm",(void *) theActivation);
CloseStringDestination(theEnv,"ActPPForm");
}
/****************************************************/
/* EnvGetActivationBasisPPForm: Returns the pretty */
/* print representation of an activation's basis. */
/****************************************************/
globle void EnvGetActivationBasisPPForm(
void *theEnv,
char *buffer,
unsigned bufferLength,
void *vTheActivation)
{
struct activation *theActivation = (struct activation *) vTheActivation;
OpenStringDestination(theEnv,"ActPPForm",buffer,bufferLength);
PrintPartialMatch(theEnv,"ActPPForm",theActivation->basis);
CloseStringDestination(theEnv,"ActPPForm");
}
/********************************************/
/* MoveActivationToTop: Moves the specified */
/* activation to the top of the agenda. */
/********************************************/
globle intBool MoveActivationToTop(
void *theEnv,
void *vtheActivation)
{
struct activation *prevPtr;
struct activation *theActivation = (struct activation *) vtheActivation;
struct defruleModule *theModuleItem;
/*====================================*/
/* Determine the module of the agenda */
/* in which the activation is stored. */
/*====================================*/
theModuleItem = (struct defruleModule *) theActivation->theRule->header.whichModule;
/*============================================*/
/* If the activation is already at the top of */
/* the agenda, then nothing needs to be done. */
/*============================================*/
if (theActivation == theModuleItem->agenda) return(FALSE);
/*=================================================*/
/* Update the pointers of the activation preceding */
/* and following the activation being moved. */
/*=================================================*/
prevPtr = theActivation->prev;
prevPtr->next = theActivation->next;
if (theActivation->next != NULL) theActivation->next->prev = prevPtr;
/*=======================================================*/
/* Move the activation and then update its pointers, the */
/* pointers of the activation following it, and the */
/* module pointer to the top activation on the agenda. */
/*=======================================================*/
theActivation->next = theModuleItem->agenda;
theModuleItem->agenda->prev = theActivation;
theActivation->prev = NULL;
theModuleItem->agenda = theActivation;
/*=============================*/
/* Mark the agenda as changed. */
/*=============================*/
AgendaData(theEnv)->AgendaChanged = TRUE;
return(TRUE);
}
/**********************************************/
/* EnvDeleteActivation: Removes the specified */
/* activation from the agenda. */
/**********************************************/
globle intBool EnvDeleteActivation(
void *theEnv,
void *theActivation)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -