?? rulebin.c
字號:
/*******************************************************/ /* "C" Language Integrated Production System */ /* */ /* CLIPS Version 6.30 10/19/06 */ /* */ /* DEFRULE BSAVE/BLOAD MODULE */ /*******************************************************//*************************************************************//* Purpose: Implements the binary save/load feature for the *//* defrule construct. *//* *//* Principal Programmer(s): *//* Gary D. Riley *//* *//* Contributing Programmer(s): *//* Brian L. Donnell *//* Barry Cameron *//* *//* Revision History: *//* *//* 6.24: Removed CONFLICT_RESOLUTION_STRATEGIES, *//* DYNAMIC_SALIENCE, and LOGICAL_DEPENDENCIES *//* compilation flags. *//* *//* 6.30: Added support for hashed alpha memories. *//* *//* Added salience groups to improve performance *//* with large numbers of activations of different *//* saliences. *//* *//*************************************************************/#define _RULEBIN_SOURCE_#include "setup.h"#if DEFRULE_CONSTRUCT && (BLOAD || BLOAD_ONLY || BLOAD_AND_BSAVE) && (! RUN_TIME)#include <stdio.h>#define _STDIO_INCLUDED_#include <string.h>#include "memalloc.h"#include "bload.h"#include "bsave.h"#include "envrnmnt.h"#include "reteutil.h"#include "agenda.h"#include "engine.h"#include "retract.h"#include "rulebsc.h"#include "pattern.h"#include "moduldef.h"#include "rulebin.h"/***************************************//* LOCAL INTERNAL FUNCTION DEFINITIONS *//***************************************/#if BLOAD_AND_BSAVE static void BsaveFind(void *); static void BsaveExpressions(void *,FILE *); static void BsaveStorage(void *,FILE *); static void BsaveBinaryItem(void *,FILE *); static void BsaveJoins(void *,FILE *); static void BsaveJoin(void *,FILE *,struct joinNode *); static void BsaveDisjuncts(void *,FILE *,struct defrule *); static void BsaveTraverseJoins(void *,FILE *,struct joinNode *); static void BsaveLinks(void *,FILE *); static void BsaveTraverseLinks(void *,FILE *,struct joinNode *); static void BsaveLink(void *,FILE *,struct joinLink *);#endif static void BloadStorage(void *); static void BloadBinaryItem(void *); static void UpdateDefruleModule(void *,void *,long); static void UpdateDefrule(void *,void *,long); static void UpdateJoin(void *,void *,long); static void UpdateLink(void *,void *,long); static void ClearBload(void *); static void DeallocateDefruleBloadData(void *);/*****************************************************//* DefruleBinarySetup: Installs the binary save/load *//* feature for the defrule construct. *//*****************************************************/globle void DefruleBinarySetup( void *theEnv) { AllocateEnvironmentData(theEnv,RULEBIN_DATA,sizeof(struct defruleBinaryData),DeallocateDefruleBloadData);#if BLOAD_AND_BSAVE AddBinaryItem(theEnv,"defrule",20,BsaveFind,BsaveExpressions, BsaveStorage,BsaveBinaryItem, BloadStorage,BloadBinaryItem, ClearBload);#endif#if BLOAD || BLOAD_ONLY AddBinaryItem(theEnv,"defrule",20,NULL,NULL,NULL,NULL, BloadStorage,BloadBinaryItem, ClearBload);#endif }/*******************************************************//* DeallocateDefruleBloadData: Deallocates environment *//* data for the defrule bsave functionality. *//*******************************************************/static void DeallocateDefruleBloadData( void *theEnv) {#if (BLOAD || BLOAD_ONLY || BLOAD_AND_BSAVE) && (! RUN_TIME) size_t space; long i; struct defruleModule *theModuleItem; struct activation *theActivation, *tmpActivation; struct salienceGroup *theGroup, *tmpGroup; for (i = 0; i < DefruleBinaryData(theEnv)->NumberOfJoins; i++) { DestroyBetaMemory(theEnv,&DefruleBinaryData(theEnv)->JoinArray[i],LHS); DestroyBetaMemory(theEnv,&DefruleBinaryData(theEnv)->JoinArray[i],RHS); ReturnLeftMemory(theEnv,&DefruleBinaryData(theEnv)->JoinArray[i]); ReturnRightMemory(theEnv,&DefruleBinaryData(theEnv)->JoinArray[i]); } for (i = 0; i < DefruleBinaryData(theEnv)->NumberOfDefruleModules; i++) { theModuleItem = &DefruleBinaryData(theEnv)->ModuleArray[i]; 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; } } space = DefruleBinaryData(theEnv)->NumberOfDefruleModules * sizeof(struct defruleModule); if (space != 0) genfree(theEnv,(void *) DefruleBinaryData(theEnv)->ModuleArray,space); space = DefruleBinaryData(theEnv)->NumberOfDefrules * sizeof(struct defrule); if (space != 0) genfree(theEnv,(void *) DefruleBinaryData(theEnv)->DefruleArray,space); space = DefruleBinaryData(theEnv)->NumberOfJoins * sizeof(struct joinNode); if (space != 0) genfree(theEnv,(void *) DefruleBinaryData(theEnv)->JoinArray,space); space = DefruleBinaryData(theEnv)->NumberOfLinks * sizeof(struct joinLink); if (space != 0) genfree(theEnv,(void *) DefruleBinaryData(theEnv)->LinkArray,space); if (Bloaded(theEnv)) { rm3(theEnv,DefruleData(theEnv)->AlphaMemoryTable,sizeof(ALPHA_MEMORY_HASH *) * ALPHA_MEMORY_HASH_SIZE); }#endif }#if BLOAD_AND_BSAVE/*************************************************************//* BsaveFind: Determines the amount of memory needed to save *//* the defrule and joinNode data structures in addition to *//* the memory needed for their associated expressions. *//*************************************************************/static void BsaveFind( void *theEnv) { struct defrule *theDefrule, *theDisjunct; struct defmodule *theModule; /*=======================================================*/ /* If a binary image is already loaded, then temporarily */ /* save the count values since these will be overwritten */ /* in the process of saving the binary image. */ /*=======================================================*/ SaveBloadCount(theEnv,DefruleBinaryData(theEnv)->NumberOfDefruleModules); SaveBloadCount(theEnv,DefruleBinaryData(theEnv)->NumberOfDefrules); SaveBloadCount(theEnv,DefruleBinaryData(theEnv)->NumberOfJoins); SaveBloadCount(theEnv,DefruleBinaryData(theEnv)->NumberOfLinks); /*====================================================*/ /* Set the binary save ID for defrule data structures */ /* and count the number of each type. */ /*====================================================*/ TagRuleNetwork(theEnv,&DefruleBinaryData(theEnv)->NumberOfDefruleModules, &DefruleBinaryData(theEnv)->NumberOfDefrules, &DefruleBinaryData(theEnv)->NumberOfJoins, &DefruleBinaryData(theEnv)->NumberOfLinks); /*===========================*/ /* Loop through each module. */ /*===========================*/ for (theModule = (struct defmodule *) EnvGetNextDefmodule(theEnv,NULL); theModule != NULL; theModule = (struct defmodule *) EnvGetNextDefmodule(theEnv,theModule)) { /*============================*/ /* Set the current module to */ /* the module being examined. */ /*============================*/ EnvSetCurrentModule(theEnv,(void *) theModule); /*==================================================*/ /* Loop through each defrule in the current module. */ /*==================================================*/ for (theDefrule = (struct defrule *) EnvGetNextDefrule(theEnv,NULL); theDefrule != NULL; theDefrule = (struct defrule *) EnvGetNextDefrule(theEnv,theDefrule)) { /*================================================*/ /* Initialize the construct header for the binary */ /* save. The binary save ID has already been set. */ /*================================================*/ MarkConstructHeaderNeededItems(&theDefrule->header,theDefrule->header.bsaveID); /*===========================================*/ /* Count and mark data structures associated */ /* with dynamic salience. */ /*===========================================*/ ExpressionData(theEnv)->ExpressionCount += ExpressionSize(theDefrule->dynamicSalience); MarkNeededItems(theEnv,theDefrule->dynamicSalience); /*==========================================*/ /* Loop through each disjunct of the rule */ /* counting and marking the data structures */ /* associated with RHS actions. */ /*==========================================*/ for (theDisjunct = theDefrule; theDisjunct != NULL; theDisjunct = theDisjunct->disjunct) { ExpressionData(theEnv)->ExpressionCount += ExpressionSize(theDisjunct->actions); MarkNeededItems(theEnv,theDisjunct->actions); } } } /*===============================*/ /* Reset the bsave tags assigned */ /* to defrule data structures. */ /*===============================*/ MarkRuleNetwork(theEnv,1); }/************************************************//* BsaveExpressions: Saves the expressions used *//* by defrules to the binary save file. *//************************************************/static void BsaveExpressions( void *theEnv, FILE *fp) { struct defrule *theDefrule, *theDisjunct; struct defmodule *theModule; /*===========================*/ /* Loop through each module. */ /*===========================*/ for (theModule = (struct defmodule *) EnvGetNextDefmodule(theEnv,NULL); theModule != NULL; theModule = (struct defmodule *) EnvGetNextDefmodule(theEnv,theModule)) { /*======================================================*/ /* Set the current module to the module being examined. */ /*======================================================*/ EnvSetCurrentModule(theEnv,(void *) theModule); /*==================================================*/ /* Loop through each defrule in the current module. */ /*==================================================*/ for (theDefrule = (struct defrule *) EnvGetNextDefrule(theEnv,NULL); theDefrule != NULL; theDefrule = (struct defrule *) EnvGetNextDefrule(theEnv,theDefrule)) { /*===========================================*/ /* Save the dynamic salience of the defrule. */ /*===========================================*/ BsaveExpression(theEnv,theDefrule->dynamicSalience,fp); /*===================================*/ /* Loop through each disjunct of the */ /* defrule and save its RHS actions. */ /*===================================*/ for (theDisjunct = theDefrule; theDisjunct != NULL; theDisjunct = theDisjunct->disjunct) { BsaveExpression(theEnv,theDisjunct->actions,fp); } } } /*==============================*/ /* Set the marked flag for each */ /* join in the join network. */ /*==============================*/ MarkRuleNetwork(theEnv,1); }/*****************************************************//* BsaveStorage: Writes out storage requirements for *//* all defrule structures to the binary file *//*****************************************************/static void BsaveStorage( void *theEnv, FILE *fp) { size_t space; long int value; space = sizeof(long) * 5; GenWrite(&space,sizeof(size_t),fp); GenWrite(&DefruleBinaryData(theEnv)->NumberOfDefruleModules,sizeof(long int),fp); GenWrite(&DefruleBinaryData(theEnv)->NumberOfDefrules,sizeof(long int),fp); GenWrite(&DefruleBinaryData(theEnv)->NumberOfJoins,sizeof(long int),fp); GenWrite(&DefruleBinaryData(theEnv)->NumberOfLinks,sizeof(long int),fp); if (DefruleData(theEnv)->RightPrimeJoins == NULL) { value = -1; } else { value = DefruleData(theEnv)->RightPrimeJoins->bsaveID; } GenWrite(&value,sizeof(long int),fp); if (DefruleData(theEnv)->LeftPrimeJoins == NULL) { value = -1; } else { value = DefruleData(theEnv)->LeftPrimeJoins->bsaveID; } GenWrite(&value,sizeof(long int),fp); }/*******************************************//* BsaveBinaryItem: Writes out all defrule *//* structures to the binary file. *//*******************************************/static void BsaveBinaryItem( void *theEnv, FILE *fp) { size_t space; struct defrule *theDefrule; struct defmodule *theModule; struct defruleModule *theModuleItem; struct bsaveDefruleModule tempDefruleModule; /*===============================================*/ /* Write out the space required by the defrules. */ /*===============================================*/ space = (DefruleBinaryData(theEnv)->NumberOfDefrules * sizeof(struct bsaveDefrule)) + (DefruleBinaryData(theEnv)->NumberOfJoins * sizeof(struct bsaveJoinNode)) +
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -