📄 reteutil.c
字号:
/*****************************************************************//* DestroyAlphaMemory: *//*****************************************************************/globle void DestroyAlphaMemory( void *theEnv, struct patternNodeHeader *theHeader, int unlink) { struct alphaMemoryHash *theAlphaMemory, *tempMemory; theAlphaMemory = theHeader->firstHash; while (theAlphaMemory != NULL) { tempMemory = theAlphaMemory->nextHash; DestroyAlphaBetaMemory(theEnv,theAlphaMemory->alphaMemory); if (unlink) { UnlinkAlphaMemoryBucketSiblings(theEnv,theAlphaMemory); } rtn_struct(theEnv,alphaMemoryHash,theAlphaMemory); theAlphaMemory = tempMemory; } theHeader->firstHash = NULL; theHeader->lastHash = NULL; }/*****************************************************************//* FlushAlphaMemory: *//*****************************************************************/globle void FlushAlphaMemory( void *theEnv, struct patternNodeHeader *theHeader) { struct alphaMemoryHash *theAlphaMemory, *tempMemory; theAlphaMemory = theHeader->firstHash; while (theAlphaMemory != NULL) { tempMemory = theAlphaMemory->nextHash; FlushAlphaBetaMemory(theEnv,theAlphaMemory->alphaMemory); UnlinkAlphaMemoryBucketSiblings(theEnv,theAlphaMemory); rtn_struct(theEnv,alphaMemoryHash,theAlphaMemory); theAlphaMemory = tempMemory; } theHeader->firstHash = NULL; theHeader->lastHash = NULL; }/*****************************************************************//* FindAlphaMemory: *//*****************************************************************/static struct alphaMemoryHash *FindAlphaMemory( void *theEnv, struct patternNodeHeader *theHeader, unsigned long hashValue) { struct alphaMemoryHash *theAlphaMemory; theAlphaMemory = DefruleData(theEnv)->AlphaMemoryTable[hashValue]; if (theAlphaMemory != NULL) { while ((theAlphaMemory != NULL) && (theAlphaMemory->owner != theHeader)) { theAlphaMemory = theAlphaMemory->next; } } return theAlphaMemory; } /*****************************************************************//* AlphaMemoryHashValue: *//*****************************************************************/static unsigned long AlphaMemoryHashValue( struct patternNodeHeader *theHeader, unsigned long hashOffset) { unsigned long hashValue; union { void *vv; unsigned uv; } fis; fis.uv = 0; fis.vv = theHeader; hashValue = fis.uv + hashOffset; hashValue = hashValue % ALPHA_MEMORY_HASH_SIZE; return hashValue; } /*****************************************************************//* UnlinkAlphaMemory: *//*****************************************************************/static void UnlinkAlphaMemory( void *theEnv, struct patternNodeHeader *theHeader, struct alphaMemoryHash *theAlphaMemory) { /*======================*/ /* Unlink the siblings. */ /*======================*/ UnlinkAlphaMemoryBucketSiblings(theEnv,theAlphaMemory); /*================================*/ /* Update firstHash and lastHash. */ /*================================*/ if (theAlphaMemory == theHeader->firstHash) { theHeader->firstHash = theAlphaMemory->nextHash; } if (theAlphaMemory == theHeader->lastHash) { theHeader->lastHash = theAlphaMemory->prevHash; } /*===============================*/ /* Update nextHash and prevHash. */ /*===============================*/ if (theAlphaMemory->prevHash != NULL) { theAlphaMemory->prevHash->nextHash = theAlphaMemory->nextHash; } if (theAlphaMemory->nextHash != NULL) { theAlphaMemory->nextHash->prevHash = theAlphaMemory->prevHash; } rtn_struct(theEnv,alphaMemoryHash,theAlphaMemory); } /*****************************************************************//* UnlinkAlphaMemoryBucketSiblings: *//*****************************************************************/static void UnlinkAlphaMemoryBucketSiblings( void *theEnv, struct alphaMemoryHash *theAlphaMemory) { if (theAlphaMemory->prev == NULL) { DefruleData(theEnv)->AlphaMemoryTable[theAlphaMemory->bucket] = theAlphaMemory->next; } else { theAlphaMemory->prev->next = theAlphaMemory->next; } if (theAlphaMemory->next != NULL) { theAlphaMemory->next->prev = theAlphaMemory->prev; } } /********************************************//* ComputeRightHashValue: *//********************************************/ unsigned long ComputeRightHashValue( void *theEnv, struct patternNodeHeader *theHeader) { struct expr *tempExpr; unsigned long hashValue = 0; unsigned long multiplier = 1; if (theHeader->rightHash == NULL) { return hashValue; } for (tempExpr = theHeader->rightHash; tempExpr != NULL; tempExpr = tempExpr->nextArg, multiplier = multiplier * 509) { DATA_OBJECT theResult; struct expr *oldArgument; oldArgument = EvaluationData(theEnv)->CurrentExpression; EvaluationData(theEnv)->CurrentExpression = tempExpr; (*EvaluationData(theEnv)->PrimitivesArray[tempExpr->type]->evaluateFunction)(theEnv,tempExpr->value,&theResult); EvaluationData(theEnv)->CurrentExpression = oldArgument; switch (theResult.type) { case STRING: case SYMBOL: case INSTANCE_NAME: hashValue += (((SYMBOL_HN *) theResult.value)->bucket * multiplier); break; case INTEGER: hashValue += (((INTEGER_HN *) theResult.value)->bucket * multiplier); break; case FLOAT: hashValue += (((FLOAT_HN *) theResult.value)->bucket * multiplier); break; } } return hashValue; }/***********************************************************//* ResizeBetaMemory: *//***********************************************************/globle void ResizeBetaMemory( void *theEnv, struct betaMemory *theMemory) { struct partialMatch **oldArray, **lastAdd, *thePM, *nextPM; unsigned long i, oldSize, betaLocation; oldSize = theMemory->size; oldArray = theMemory->beta; theMemory->size = oldSize * 11; theMemory->beta = genalloc(theEnv,sizeof(struct partialMatch *) * theMemory->size); lastAdd = genalloc(theEnv,sizeof(struct partialMatch *) * theMemory->size); memset(theMemory->beta,0,sizeof(struct partialMatch *) * theMemory->size); memset(lastAdd,0,sizeof(struct partialMatch *) * theMemory->size); for (i = 0; i < oldSize; i++) { thePM = oldArray[i]; while (thePM != NULL) { nextPM = thePM->nextInMemory; thePM->nextInMemory = NULL; betaLocation = thePM->hashValue % theMemory->size; thePM->prevInMemory = lastAdd[betaLocation]; if (lastAdd[betaLocation] != NULL) { lastAdd[betaLocation]->nextInMemory = thePM; } else { theMemory->beta[betaLocation] = thePM; } lastAdd[betaLocation] = thePM; thePM = nextPM; } } if (theMemory->last != NULL) { genfree(theEnv,theMemory->last,sizeof(struct partialMatch *) * oldSize); theMemory->last = lastAdd; } else { genfree(theEnv,lastAdd,sizeof(struct partialMatch *) * theMemory->size); } genfree(theEnv,oldArray,sizeof(struct partialMatch *) * oldSize); }/***********************************************************//* ResetBetaMemory: *//***********************************************************/static void ResetBetaMemory( void *theEnv, struct betaMemory *theMemory) { struct partialMatch **oldArray, **lastAdd; unsigned long oldSize; if ((theMemory->size == 1) || (theMemory->size == INITIAL_BETA_HASH_SIZE)) { return; } oldSize = theMemory->size; oldArray = theMemory->beta; theMemory->size = INITIAL_BETA_HASH_SIZE; theMemory->beta = genalloc(theEnv,sizeof(struct partialMatch *) * theMemory->size); memset(theMemory->beta,0,sizeof(struct partialMatch *) * theMemory->size); genfree(theEnv,oldArray,sizeof(struct partialMatch *) * oldSize); if (theMemory->last != NULL) { lastAdd = genalloc(theEnv,sizeof(struct partialMatch *) * theMemory->size); memset(lastAdd,0,sizeof(struct partialMatch *) * theMemory->size); genfree(theEnv,theMemory->last,sizeof(struct partialMatch *) * oldSize); theMemory->last = lastAdd; } } #if (CONSTRUCT_COMPILER || BLOAD_AND_BSAVE) && (! RUN_TIME)/*************************************************************//* TagRuleNetwork: Assigns each join in the join network and *//* each defrule data structure with a unique integer ID. *//* Also counts the number of defrule and joinNode data *//* structures currently in use. *//*************************************************************/globle void TagRuleNetwork( void *theEnv, long int *moduleCount, long int *ruleCount, long int *joinCount, long int *linkCount) { struct defmodule *modulePtr; struct defrule *rulePtr; struct joinLink *theLink; *moduleCount = 0; *ruleCount = 0; *joinCount = 0; *linkCount = 0; MarkRuleNetwork(theEnv,0); for (theLink = DefruleData(theEnv)->LeftPrimeJoins; theLink != NULL; theLink = theLink->next) { theLink->bsaveID = *linkCount; (*linkCount)++; } for (theLink = DefruleData(theEnv)->RightPrimeJoins; theLink != NULL; theLink = theLink->next) { theLink->bsaveID = *linkCount; (*linkCount)++; } /*===========================*/ /* Loop through each module. */ /*===========================*/ for (modulePtr = (struct defmodule *) EnvGetNextDefmodule(theEnv,NULL); modulePtr != NULL; modulePtr = (struct defmodule *) EnvGetNextDefmodule(theEnv,modulePtr)) { (*moduleCount)++; EnvSetCurrentModule(theEnv,(void *) modulePtr); /*=========================*/ /* Loop through each rule. */ /*=========================*/ rulePtr = (struct defrule *) EnvGetNextDefrule(theEnv,NULL); while (rulePtr != NULL) { rulePtr->header.bsaveID = *ruleCount; (*ruleCount)++; /*=========================*/ /* Loop through each join. */ /*=========================*/ TagNetworkTraverseJoins(theEnv,joinCount,linkCount,rulePtr->lastJoin); if (rulePtr->disjunct != NULL) rulePtr = rulePtr->disjunct; else rulePtr = (struct defrule *) EnvGetNextDefrule(theEnv,rulePtr); } } }/*******************************************************************//* TagNetworkTraverseJoins: Traverses the join network for a rule. *//*******************************************************************/static void TagNetworkTraverseJoins( void *theEnv, long int *joinCount, long int *linkCount, struct joinNode *joinPtr) { struct joinLink *theLink; for (; joinPtr != NULL; joinPtr = joinPtr->lastLevel) { if (joinPtr->marked == 0) { joinPtr->marked = 1; joinPtr->bsaveID = *joinCount; (*joinCount)++; for (theLink = joinPtr->nextLinks; theLink != NULL; theLink = theLink->next) { theLink->bsaveID = *linkCount; (*linkCount)++; } } if (joinPtr->joinFromTheRight) { TagNetworkTraverseJoins(theEnv,joinCount,linkCount,joinPtr->rightSideEntryStructure); } } }#endif /* (CONSTRUCT_COMPILER || BLOAD_AND_BSAVE) && (! RUN_TIME) */#endif /* DEFRULE_CONSTRUCT */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -