📄 rulebin.c
字号:
/*******************************************************/
/* "C" Language Integrated Production System */
/* */
/* CLIPS Version 6.24 05/17/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. */
/* */
/*************************************************************/
#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 *);
#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 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)
unsigned long space;
long i;
struct defruleModule *theModuleItem;
struct activation *theActivation, *tmpActivation;
for (i = 0; i < DefruleBinaryData(theEnv)->NumberOfJoins; i++)
{ DestroyAlphaBetaMemory(theEnv,DefruleBinaryData(theEnv)->JoinArray[i].beta); }
for (i = 0; i < DefruleBinaryData(theEnv)->NumberOfDefruleModules; i++)
{
theModuleItem = &DefruleBinaryData(theEnv)->ModuleArray[i];
theActivation = theModuleItem->agenda;
while (theActivation != NULL)
{
tmpActivation = theActivation->next;
if (theActivation->sortedBasis != NULL)
{ DestroyPartialMatch(theEnv,theActivation->sortedBasis); }
rtn_struct(theEnv,activation,theActivation);
theActivation = tmpActivation;
}
}
space = DefruleBinaryData(theEnv)->NumberOfDefruleModules * sizeof(struct defruleModule);
if (space != 0) genlongfree(theEnv,(void *) DefruleBinaryData(theEnv)->ModuleArray,space);
space = DefruleBinaryData(theEnv)->NumberOfDefrules * sizeof(struct defrule);
if (space != 0) genlongfree(theEnv,(void *) DefruleBinaryData(theEnv)->DefruleArray,space);
space = DefruleBinaryData(theEnv)->NumberOfJoins * sizeof(struct joinNode);
if (space != 0) genlongfree(theEnv,(void *) DefruleBinaryData(theEnv)->JoinArray,space);
#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);
/*====================================================*/
/* 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);
/*===========================*/
/* 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 */
/*****************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -