static void ResetDefrulesPrime( void *theEnv) { struct joinLink *theLink; struct partialMatch *notParent; for (theLink = DefruleData(theEnv)->RightPrimeJoins; theLink != NULL; theLink = theLink->next) { NetworkAssert(theEnv,theLink->join->rightMemory->beta[0],theLink->join); } for (theLink = DefruleData(theEnv)->LeftPrimeJoins; theLink != NULL; theLink = theLink->next) { if ((theLink->join->patternIsNegated || theLink->join->joinFromTheRight) && (! theLink->join->patternIsExists)) { notParent = theLink->join->leftMemory->beta[0]; if (theLink->join->secondaryNetworkTest != NULL) { if (EvaluateSecondaryNetworkTest(theEnv,notParent,theLink->join) == FALSE) { continue; } } notParent->marker = NULL; EPMDrive(theEnv,notParent,theLink->join); } } }
void DefruleRunTimeInitialize( Environment *theEnv, struct joinLink *rightPrime, struct joinLink *leftPrime) { Defmodule *theModule; Defrule *theRule, *theDisjunct; DefruleData(theEnv)->RightPrimeJoins = rightPrime; DefruleData(theEnv)->LeftPrimeJoins = leftPrime; SaveCurrentModule(theEnv); for (theModule = GetNextDefmodule(theEnv,NULL); theModule != NULL; theModule = GetNextDefmodule(theEnv,theModule)) { SetCurrentModule(theEnv,theModule); for (theRule = GetNextDefrule(theEnv,NULL); theRule != NULL; theRule = GetNextDefrule(theEnv,theRule)) { for (theDisjunct = theRule; theDisjunct != NULL; theDisjunct = theDisjunct->disjunct) { theDisjunct->header.env = theEnv; AddBetaMemoriesToRule(theEnv,theDisjunct->lastJoin); } } } RestoreCurrentModule(theEnv); }
globle void DefruleRunTimeInitialize( void *theEnv, struct joinLink *rightPrime, struct joinLink *leftPrime) { struct defmodule *theModule; struct defrule *theRule, *theDisjunct; DefruleData(theEnv)->RightPrimeJoins = rightPrime; DefruleData(theEnv)->LeftPrimeJoins = leftPrime; SaveCurrentModule(theEnv); for (theModule = (struct defmodule *) EnvGetNextDefmodule(theEnv,NULL); theModule != NULL; theModule = (struct defmodule *) EnvGetNextDefmodule(theEnv,theModule)) { EnvSetCurrentModule(theEnv,(void *) theModule); for (theRule = EnvGetNextDefrule(theEnv,NULL); theRule != NULL; theRule = EnvGetNextDefrule(theEnv,theRule)) { for (theDisjunct = theRule; theDisjunct != NULL; theDisjunct = theDisjunct->disjunct) { AddBetaMemoriesToRule(theEnv,theDisjunct->lastJoin); } } } RestoreCurrentModule(theEnv); }
globle intBool EnvSetBetaMemoryResizing( void *theEnv, int value) { int ov; ov = DefruleData(theEnv)->BetaMemoryResizingFlag; DefruleData(theEnv)->BetaMemoryResizingFlag = value; return(ov); }
globle unsigned DefruleWatchAccess( void *theEnv, int code, unsigned newState, struct expr *argExprs) { if (code) return(ConstructSetWatchAccess(theEnv,DefruleData(theEnv)->DefruleConstruct,newState,argExprs, EnvGetDefruleWatchActivations,EnvSetDefruleWatchActivations)); else return(ConstructSetWatchAccess(theEnv,DefruleData(theEnv)->DefruleConstruct,newState,argExprs, EnvGetDefruleWatchFirings,EnvSetDefruleWatchFirings)); }
globle unsigned DefruleWatchPrint( void *theEnv, char *log, int code, struct expr *argExprs) { if (code) return(ConstructPrintWatchAccess(theEnv,DefruleData(theEnv)->DefruleConstruct,log,argExprs, EnvGetDefruleWatchActivations,EnvSetDefruleWatchActivations)); else return(ConstructPrintWatchAccess(theEnv,DefruleData(theEnv)->DefruleConstruct,log,argExprs, EnvGetDefruleWatchActivations,EnvSetDefruleWatchActivations)); }
static void DeallocateDefruleData( Environment *theEnv) { struct defruleModule *theModuleItem; Defmodule *theModule; Activation *theActivation, *tmpActivation; struct salienceGroup *theGroup, *tmpGroup; #if BLOAD || BLOAD_AND_BSAVE if (Bloaded(theEnv)) { return; } #endif DoForAllConstructs(theEnv,DestroyDefruleAction, DefruleData(theEnv)->DefruleModuleIndex,false,NULL); for (theModule = GetNextDefmodule(theEnv,NULL); theModule != NULL; theModule = GetNextDefmodule(theEnv,theModule)) { theModuleItem = (struct defruleModule *) GetModuleItem(theEnv,theModule, DefruleData(theEnv)->DefruleModuleIndex); theActivation = theModuleItem->agenda; while (theActivation != NULL) { tmpActivation = theActivation->next; rtn_struct(theEnv,activation,theActivation); theActivation = tmpActivation; } theGroup = theModuleItem->groupings; while (theGroup != NULL) { tmpGroup = theGroup->next; rtn_struct(theEnv,salienceGroup,theGroup); theGroup = tmpGroup; } #if ! RUN_TIME rtn_struct(theEnv,defruleModule,theModuleItem); #endif } rm(theEnv,DefruleData(theEnv)->AlphaMemoryTable,sizeof (ALPHA_MEMORY_HASH *) * ALPHA_MEMORY_HASH_SIZE); }
globle unsigned DefruleWatchPrint( void *theEnv, EXEC_STATUS, char *logName, int code, struct expr *argExprs) { if (code) return(ConstructPrintWatchAccess(theEnv,execStatus,DefruleData(theEnv,execStatus)->DefruleConstruct,logName,argExprs, EnvGetDefruleWatchActivations,EnvSetDefruleWatchActivations)); else return(ConstructPrintWatchAccess(theEnv,execStatus,DefruleData(theEnv,execStatus)->DefruleConstruct,logName,argExprs, EnvGetDefruleWatchActivations,EnvSetDefruleWatchActivations)); }
void ListDefrulesCommand( UDFContext *context, CLIPSValue *returnValue) { void *theEnv = UDFContextEnvironment(context); ListConstructCommand(context,"list-defrules",DefruleData(theEnv)->DefruleConstruct); }
void PPDefruleCommand( UDFContext *context, CLIPSValue *returnValue) { void *theEnv = UDFContextEnvironment(context); PPConstructCommand(context,"ppdefrule",DefruleData(theEnv)->DefruleConstruct); }
void DefruleModuleFunction( UDFContext *context, CLIPSValue *returnValue) { void *theEnv = UDFContextEnvironment(context); CVSetCLIPSSymbol(returnValue,GetConstructModuleCommand(context,"defrule-module",DefruleData(theEnv)->DefruleConstruct)); }
void GetDefruleListFunction( UDFContext *context, CLIPSValue *returnValue) { void *theEnv = UDFContextEnvironment(context); GetConstructListFunction(context,"get-defrule-list",returnValue,DefruleData(theEnv)->DefruleConstruct); }
globle void GetDefruleListFunction( void *theEnv, EXEC_STATUS, DATA_OBJECT_PTR returnValue) { GetConstructListFunction(theEnv,execStatus,"get-defrule-list",returnValue,DefruleData(theEnv,execStatus)->DefruleConstruct); }
globle intBool EnvUndefrule( void *theEnv, EXEC_STATUS, void *theDefrule) { return(Undefconstruct(theEnv,execStatus,theDefrule,DefruleData(theEnv,execStatus)->DefruleConstruct)); }
globle void DefruleBasicCommands( void *theEnv) { EnvAddResetFunction(theEnv,"defrule",ResetDefrules,70); AddSaveFunction(theEnv,"defrule",SaveDefrules,0); #if (! RUN_TIME) AddClearReadyFunction(theEnv,"defrule",ClearDefrulesReady,0); EnvAddClearFunction(theEnv,"defrule",ClearDefrules,0); #endif #if DEBUGGING_FUNCTIONS AddWatchItem(theEnv,"rules",0,&DefruleData(theEnv)->WatchRules,70,DefruleWatchAccess,DefruleWatchPrint); #endif #if ! RUN_TIME EnvDefineFunction2(theEnv,"get-defrule-list",'m',PTIEF GetDefruleListFunction,"GetDefruleListFunction","01w"); EnvDefineFunction2(theEnv,"undefrule",'v',PTIEF UndefruleCommand,"UndefruleCommand","11w"); EnvDefineFunction2(theEnv,"defrule-module",'w',PTIEF DefruleModuleFunction,"DefruleModuleFunction","11w"); #if DEBUGGING_FUNCTIONS EnvDefineFunction2(theEnv,"rules",'v', PTIEF ListDefrulesCommand,"ListDefrulesCommand","01w"); EnvDefineFunction2(theEnv,"list-defrules",'v', PTIEF ListDefrulesCommand,"ListDefrulesCommand","01w"); EnvDefineFunction2(theEnv,"ppdefrule",'v',PTIEF PPDefruleCommand,"PPDefruleCommand","11w"); #endif #if (BLOAD || BLOAD_ONLY || BLOAD_AND_BSAVE) DefruleBinarySetup(theEnv); #endif #if CONSTRUCT_COMPILER && (! RUN_TIME) DefruleCompilerSetup(theEnv); #endif #endif }
globle void EnvListDefrules( void *theEnv, char *logicalName, void *theModule) { ListConstruct(theEnv,DefruleData(theEnv)->DefruleConstruct,logicalName,(struct defmodule *) theModule); }
globle void DefruleCompilerSetup( void *theEnv, EXEC_STATUS) { DefruleData(theEnv,execStatus)->DefruleCodeItem = AddCodeGeneratorItem(theEnv,execStatus,"defrules",0,BeforeDefrulesCode, InitDefruleCode,ConstructToCode,4); }
globle void EnvGetDefruleList( void *theEnv, DATA_OBJECT_PTR returnValue, void *theModule) { GetConstructList(theEnv,returnValue,DefruleData(theEnv)->DefruleConstruct,(struct defmodule *) theModule); }
globle int PPDefrule( void *theEnv, char *defruleName, char *logicalName) { return(PPConstruct(theEnv,defruleName,logicalName,DefruleData(theEnv)->DefruleConstruct)); }
static void ReturnModule( void *theEnv, void *theItem) { FreeConstructHeaderModule(theEnv,(struct defmoduleItemHeader *) theItem,DefruleData(theEnv)->DefruleConstruct); rtn_struct(theEnv,defruleModule,theItem); }
static void SaveDefrules( void *theEnv, void *theModule, char *logicalName) { SaveConstruct(theEnv,theModule,logicalName,DefruleData(theEnv)->DefruleConstruct); }
static void UpdateDefrule( void *theEnv, void *buf, long obji) { struct bsaveDefrule *br; br = (struct bsaveDefrule *) buf; UpdateConstructHeader(theEnv,&br->header,&DefruleBinaryData(theEnv)->DefruleArray[obji].header, (int) sizeof(struct defruleModule),(void *) DefruleBinaryData(theEnv)->ModuleArray, (int) sizeof(struct defrule),(void *) DefruleBinaryData(theEnv)->DefruleArray); DefruleBinaryData(theEnv)->DefruleArray[obji].dynamicSalience = ExpressionPointer(br->dynamicSalience); DefruleBinaryData(theEnv)->DefruleArray[obji].actions = ExpressionPointer(br->actions); DefruleBinaryData(theEnv)->DefruleArray[obji].logicalJoin = BloadJoinPointer(br->logicalJoin); DefruleBinaryData(theEnv)->DefruleArray[obji].lastJoin = BloadJoinPointer(br->lastJoin); DefruleBinaryData(theEnv)->DefruleArray[obji].disjunct = BloadDefrulePointer(DefruleBinaryData(theEnv)->DefruleArray,br->disjunct); DefruleBinaryData(theEnv)->DefruleArray[obji].salience = br->salience; DefruleBinaryData(theEnv)->DefruleArray[obji].localVarCnt = br->localVarCnt; DefruleBinaryData(theEnv)->DefruleArray[obji].complexity = br->complexity; DefruleBinaryData(theEnv)->DefruleArray[obji].autoFocus = br->autoFocus; DefruleBinaryData(theEnv)->DefruleArray[obji].executing = 0; DefruleBinaryData(theEnv)->DefruleArray[obji].afterBreakpoint = 0; #if DEBUGGING_FUNCTIONS DefruleBinaryData(theEnv)->DefruleArray[obji].watchActivation = AgendaData(theEnv)->WatchActivations; DefruleBinaryData(theEnv)->DefruleArray[obji].watchFiring = DefruleData(theEnv)->WatchRules; #endif }
globle void InitializeDefrules( void *theEnv) { AllocateEnvironmentData(theEnv,DEFRULE_DATA,sizeof(struct defruleData),DeallocateDefruleData); InitializeEngine(theEnv); InitializeAgenda(theEnv); InitializePatterns(theEnv); InitializeDefruleModules(theEnv); AddReservedPatternSymbol(theEnv,"and",NULL); AddReservedPatternSymbol(theEnv,"not",NULL); AddReservedPatternSymbol(theEnv,"or",NULL); AddReservedPatternSymbol(theEnv,"test",NULL); AddReservedPatternSymbol(theEnv,"logical",NULL); AddReservedPatternSymbol(theEnv,"exists",NULL); AddReservedPatternSymbol(theEnv,"forall",NULL); DefruleBasicCommands(theEnv); DefruleCommands(theEnv); DefruleData(theEnv)->DefruleConstruct = AddConstruct(theEnv,"defrule","defrules", ParseDefrule,EnvFindDefrule, GetConstructNamePointer,GetConstructPPForm, GetConstructModuleItem,EnvGetNextDefrule,SetNextConstruct, EnvIsDefruleDeletable,EnvUndefrule,ReturnDefrule); }
static void SaveDefrules( void *theEnv, EXEC_STATUS, void *theModule, char *logicalName) { SaveConstruct(theEnv,execStatus,theModule,logicalName,DefruleData(theEnv,execStatus)->DefruleConstruct); }
globle void EnvGetDefruleList( void *theEnv, EXEC_STATUS, DATA_OBJECT_PTR returnValue, void *theModule) { GetConstructList(theEnv,execStatus,returnValue,DefruleData(theEnv,execStatus)->DefruleConstruct,(struct defmodule *) theModule); }
globle int PPDefrule( void *theEnv, EXEC_STATUS, char *defruleName, char *logicalName) { return(PPConstruct(theEnv,execStatus,defruleName,logicalName,DefruleData(theEnv,execStatus)->DefruleConstruct)); }
globle void EnvListDefrules( void *theEnv, EXEC_STATUS, char *logicalName, void *theModule) { ListConstruct(theEnv,execStatus,DefruleData(theEnv,execStatus)->DefruleConstruct,logicalName,(struct defmodule *) theModule); }
static void ResetDefrules( void *theEnv, EXEC_STATUS) { struct defmodule *theModule; struct joinLink *theLink; struct partialMatch *notParent; DefruleData(theEnv,execStatus)->CurrentEntityTimeTag = 1L; EnvClearFocusStack(theEnv,execStatus); theModule = (struct defmodule *) EnvFindDefmodule(theEnv,execStatus,"MAIN"); EnvFocus(theEnv,execStatus,(void *) theModule); for (theLink = DefruleData(theEnv,execStatus)->RightPrimeJoins; theLink != NULL; theLink = theLink->next) { PosEntryRetractAlpha(theEnv,execStatus,theLink->join->rightMemory->beta[0]); } for (theLink = DefruleData(theEnv,execStatus)->LeftPrimeJoins; theLink != NULL; theLink = theLink->next) { if ((theLink->join->patternIsNegated || theLink->join->joinFromTheRight) && (! theLink->join->patternIsExists)) { notParent = theLink->join->leftMemory->beta[0]; if (notParent->marker) { RemoveBlockedLink(notParent); } /*==========================================================*/ /* Prevent any retractions from generating partial matches. */ /*==========================================================*/ notParent->marker = notParent; if (notParent->children != NULL) { PosEntryRetractBeta(theEnv,execStatus,notParent,notParent->children); } /* if (notParent->dependents != NULL) { RemoveLogicalSupport(theEnv,execStatus,notParent); } */ } } }
static void ResetDefrules( void *theEnv) { struct defmodule *theModule; DefruleData(theEnv)->CurrentEntityTimeTag = 0L; EnvClearFocusStack(theEnv); theModule = (struct defmodule *) EnvFindDefmodule(theEnv,"MAIN"); EnvFocus(theEnv,(void *) theModule); }
globle void ShowAlphaHashTable( void *theEnv) { int i, count; long totalCount = 0; struct alphaMemoryHash *theEntry; struct partialMatch *theMatch; char buffer[40]; for (i = 0; i < ALPHA_MEMORY_HASH_SIZE; i++) { for (theEntry = DefruleData(theEnv)->AlphaMemoryTable[i], count = 0; theEntry != NULL; theEntry = theEntry->next) { count++; } if (count != 0) { totalCount += count; gensprintf(buffer,"%4d: %4d ->",i,count); EnvPrintRouter(theEnv,WDISPLAY,buffer); for (theEntry = DefruleData(theEnv)->AlphaMemoryTable[i], count = 0; theEntry != NULL; theEntry = theEntry->next) { for (theMatch = theEntry->alphaMemory; theMatch != NULL; theMatch = theMatch->nextInMemory) { count++; } gensprintf(buffer," %4d",count); EnvPrintRouter(theEnv,WDISPLAY,buffer); if (theEntry->owner->rightHash == NULL) { EnvPrintRouter(theEnv,WDISPLAY,"*"); } } EnvPrintRouter(theEnv,WDISPLAY,"\n"); } } gensprintf(buffer,"Total Count: %ld\n",totalCount); EnvPrintRouter(theEnv,WDISPLAY,buffer); }