globle void TagRuleNetwork( long int *moduleCount, long int *ruleCount, long int *joinCount) { struct defmodule *modulePtr; struct defrule *rulePtr; struct joinNode *joinPtr; *moduleCount = 0; *ruleCount = 0; *joinCount = 0; MarkRuleNetwork(0); /*===========================*/ /* Loop through each module. */ /*===========================*/ for (modulePtr = (struct defmodule *) GetNextDefmodule(NULL); modulePtr != NULL; modulePtr = (struct defmodule *) GetNextDefmodule(modulePtr)) { (*moduleCount)++; SetCurrentModule((void *) modulePtr); /*=========================*/ /* Loop through each rule. */ /*=========================*/ rulePtr = (struct defrule *) GetNextDefrule(NULL); while (rulePtr != NULL) { rulePtr->header.bsaveID = *ruleCount; (*ruleCount)++; /*=========================*/ /* Loop through each join. */ /*=========================*/ for (joinPtr = rulePtr->lastJoin; joinPtr != NULL; joinPtr = GetPreviousJoin(joinPtr)) { if (joinPtr->marked == 0) { joinPtr->marked = 1; joinPtr->bsaveID = *joinCount; (*joinCount)++; } } if (rulePtr->disjunct != NULL) rulePtr = rulePtr->disjunct; else rulePtr = (struct defrule *) GetNextDefrule(rulePtr); } } }
static void CheckForPrimableJoins( void *theEnv, struct defrule *tempRule) { struct joinNode *joinPtr; struct partialMatch *theList; /*========================================*/ /* Loop through each of the rule's joins. */ /*========================================*/ for (joinPtr = tempRule->lastJoin; joinPtr != NULL; joinPtr = GetPreviousJoin(joinPtr)) { /*===============================*/ /* Update the join if necessary. */ /*===============================*/ if ((joinPtr->initialize) && (! joinPtr->marked)) /* GDR 6.05 */ { if (joinPtr->firstJoin == TRUE) { if (((struct patternNodeHeader *) GetPatternForJoin(joinPtr))->initialize == FALSE) { PrimeJoin(theEnv,joinPtr); joinPtr->marked = TRUE; /* GDR 6.05 */ } } else if (joinPtr->lastLevel->initialize == FALSE) { PrimeJoin(theEnv,joinPtr); joinPtr->marked = TRUE; /* GDR 6.05 */ } } /*================================================================*/ /* If the join is associated with a rule activation (i.e. partial */ /* matches that reach this join cause an activation to be placed */ /* on the agenda), then add activations to the agenda for the */ /* rule being incrementally reset. */ /*================================================================*/ else if (joinPtr->ruleToActivate == tempRule) { for (theList = joinPtr->beta; theList != NULL; theList = theList->next) { AddActivation(theEnv,tempRule,theList); } } } }
globle void MarkRuleNetwork( int value) { struct defrule *rulePtr; struct joinNode *joinPtr; struct defmodule *modulePtr; /*===========================*/ /* Loop through each module. */ /*===========================*/ SaveCurrentModule(); for (modulePtr = (struct defmodule *) GetNextDefmodule(NULL); modulePtr != NULL; modulePtr = (struct defmodule *) GetNextDefmodule(modulePtr)) { SetCurrentModule((void *) modulePtr); /*=========================*/ /* Loop through each rule. */ /*=========================*/ rulePtr = (struct defrule *) GetNextDefrule(NULL); while (rulePtr != NULL) { /*=============================*/ /* Mark each join for the rule */ /* with the specified value. */ /*=============================*/ joinPtr = rulePtr->lastJoin; while (joinPtr != NULL) { joinPtr->marked = value; joinPtr = GetPreviousJoin(joinPtr); } /*=================================*/ /* Move on to the next rule or the */ /* next disjunct for this rule. */ /*=================================*/ if (rulePtr->disjunct != NULL) rulePtr = rulePtr->disjunct; else rulePtr = (struct defrule *) GetNextDefrule(rulePtr); } } RestoreCurrentModule(); }
static void BsaveJoins( void *theEnv, FILE *fp) { struct defrule *rulePtr; struct joinNode *joinPtr; struct defmodule *theModule; /*===========================*/ /* Loop through each module. */ /*===========================*/ for (theModule = (struct defmodule *) EnvGetNextDefmodule(theEnv,NULL); theModule != NULL; theModule = (struct defmodule *) EnvGetNextDefmodule(theEnv,theModule)) { EnvSetCurrentModule(theEnv,(void *) theModule); /*===========================================*/ /* Loop through each rule and its disjuncts. */ /*===========================================*/ rulePtr = (struct defrule *) EnvGetNextDefrule(theEnv,NULL); while (rulePtr != NULL) { /*=========================================*/ /* Loop through each join of the disjunct. */ /*=========================================*/ for (joinPtr = rulePtr->lastJoin; joinPtr != NULL; joinPtr = GetPreviousJoin(joinPtr)) { if (joinPtr->marked) BsaveJoin(theEnv,fp,joinPtr); } /*=======================================*/ /* Move on to the next rule or disjunct. */ /*=======================================*/ if (rulePtr->disjunct != NULL) rulePtr = rulePtr->disjunct; else rulePtr = (struct defrule *) EnvGetNextDefrule(theEnv,rulePtr); } } }
static void MarkNetworkForIncrementalReset( void *theEnv, struct defrule *tempRule, int value) { struct joinNode *joinPtr; struct patternNodeHeader *patternPtr; /*============================================*/ /* Loop through each of the rule's disjuncts. */ /*============================================*/ for (; tempRule != NULL; tempRule = tempRule->disjunct) { /*============================================*/ /* Loop through each of the disjunct's joins. */ /*============================================*/ for (joinPtr = tempRule->lastJoin; joinPtr != NULL; joinPtr = GetPreviousJoin(joinPtr)) { /*================*/ /* Mark the join. */ /*================*/ joinPtr->marked = FALSE; /* GDR 6.05 */ if ((joinPtr->initialize) && (joinPtr->joinFromTheRight == FALSE)) { joinPtr->initialize = value; patternPtr = (struct patternNodeHeader *) GetPatternForJoin(joinPtr); MarkPatternForIncrementalReset(theEnv,(int) joinPtr->rhsType,patternPtr,value); } } } }
static int ConstructToCode( char *fileName, int fileID, FILE *headerFP, int imageID, int maxIndices) { int fileCount = 1; struct defmodule *theModule; struct defrule *theDefrule; struct joinNode *theJoin; int joinArrayCount = 0, joinArrayVersion = 1; int moduleCount = 0, moduleArrayCount = 0, moduleArrayVersion = 1; int defruleArrayCount = 0, defruleArrayVersion = 1; FILE *joinFile = NULL, *moduleFile = NULL, *defruleFile = NULL; #if FUZZY_DEFTEMPLATES struct fzSlotLocator *thePatternFv; int patternFvArrayCount = 0, patternFvArrayVersion = 1; FILE *patternFvFile = NULL; #endif /*==============================================*/ /* Include the appropriate defrule header file. */ /*==============================================*/ fprintf(headerFP,"#include \"ruledef.h\"\n"); /*=========================================================*/ /* Loop through all the modules, all the defrules, and all */ /* the join nodes writing their C code representation to */ /* the file as they are traversed. */ /*=========================================================*/ #if FUZZY_DEFTEMPLATES /*=========================================================*/ /* ALSO write the patternFv arrays as required if Fuzzy */ /*=========================================================*/ #endif for (theModule = (struct defmodule *) GetNextDefmodule(NULL); theModule != NULL; theModule = (struct defmodule *) GetNextDefmodule(theModule)) { /*=========================*/ /* Set the current module. */ /*=========================*/ SetCurrentModule((void *) theModule); /*==========================*/ /* Save the defrule module. */ /*==========================*/ moduleFile = OpenFileIfNeeded(moduleFile,fileName,fileID,imageID,&fileCount, moduleArrayVersion,headerFP, "struct defruleModule",ModulePrefix(DefruleCodeItem), FALSE,NULL); if (moduleFile == NULL) { #if FUZZY_DEFTEMPLATES CloseDefruleFiles(moduleFile,defruleFile,joinFile,patternFvFile,maxIndices); #else CloseDefruleFiles(moduleFile,defruleFile,joinFile,maxIndices); #endif return(0); } DefruleModuleToCode(moduleFile,theModule,imageID,maxIndices,moduleCount); moduleFile = CloseFileIfNeeded(moduleFile,&moduleArrayCount,&moduleArrayVersion, maxIndices,NULL,NULL); /*=========================================*/ /* Loop through all of the defrules (and */ /* their disjuncts) in the current module. */ /*=========================================*/ theDefrule = (struct defrule *) GetNextDefrule(NULL); while (theDefrule != NULL) { /*===================================*/ /* Save the defrule data structures. */ /*===================================*/ defruleFile = OpenFileIfNeeded(defruleFile,fileName,fileID,imageID,&fileCount, defruleArrayVersion,headerFP, "struct defrule",ConstructPrefix(DefruleCodeItem), FALSE,NULL); if (defruleFile == NULL) { #if FUZZY_DEFTEMPLATES CloseDefruleFiles(moduleFile,defruleFile,joinFile,patternFvFile,maxIndices); #else CloseDefruleFiles(moduleFile,defruleFile,joinFile,maxIndices); #endif return(0); } #if FUZZY_DEFTEMPLATES DefruleToCode(defruleFile,theDefrule,imageID,maxIndices, moduleCount, patternFvArrayCount, patternFvArrayVersion); #else DefruleToCode(defruleFile,theDefrule,imageID,maxIndices, moduleCount); #endif defruleArrayCount++; defruleFile = CloseFileIfNeeded(defruleFile,&defruleArrayCount,&defruleArrayVersion, maxIndices,NULL,NULL); #if FUZZY_DEFTEMPLATES /* write out the patternFv array of fzSlotLocator structs */ thePatternFv = theDefrule->pattern_fv_arrayPtr; if (thePatternFv != NULL) { int i; int numFzSlots = theDefrule->numberOfFuzzySlots; patternFvFile = OpenFileIfNeeded(patternFvFile,fileName,fileID,imageID,&fileCount, patternFvArrayVersion,headerFP, "struct fzSlotLocator",PatternFvPrefix(),FALSE,NULL); if (patternFvFile == NULL) { CloseDefruleFiles(moduleFile,defruleFile,joinFile,patternFvFile,maxIndices); return(0); } for (i=0; i< numFzSlots; i++) { fprintf(patternFvFile, "{%d, %d, ", thePatternFv[i].patternNum, thePatternFv[i].slotNum); PrintFuzzyValueReference(patternFvFile,thePatternFv[i].fvhnPtr); fprintf(patternFvFile, "}"); if (i != numFzSlots-1) fprintf(patternFvFile, ","); } patternFvArrayCount += numFzSlots; patternFvFile = CloseFileIfNeeded(patternFvFile,&patternFvArrayCount,&patternFvArrayVersion, maxIndices,NULL,NULL); } #endif /*================================*/ /* Save the join data structures. */ /*================================*/ for (theJoin = theDefrule->lastJoin; theJoin != NULL; theJoin = GetPreviousJoin(theJoin)) { if (theJoin->marked) { joinFile = OpenFileIfNeeded(joinFile,fileName,fileID,imageID,&fileCount, joinArrayVersion,headerFP, "struct joinNode",JoinPrefix(),FALSE,NULL); if (joinFile == NULL) { #if FUZZY_DEFTEMPLATES CloseDefruleFiles(moduleFile,defruleFile,joinFile,patternFvFile,maxIndices); #else CloseDefruleFiles(moduleFile,defruleFile,joinFile,maxIndices); #endif return(0); } JoinToCode(joinFile,theJoin,imageID,maxIndices); joinArrayCount++; joinFile = CloseFileIfNeeded(joinFile,&joinArrayCount,&joinArrayVersion, maxIndices,NULL,NULL); } } /*==========================================*/ /* Move on to the next disjunct or defrule. */ /*==========================================*/ if (theDefrule->disjunct != NULL) theDefrule = theDefrule->disjunct; else theDefrule = (struct defrule *) GetNextDefrule(theDefrule); } moduleCount++; moduleArrayCount++; } #if FUZZY_DEFTEMPLATES CloseDefruleFiles(moduleFile,defruleFile,joinFile,patternFvFile,maxIndices); #else CloseDefruleFiles(moduleFile,defruleFile,joinFile,maxIndices); #endif return(1); }