📄 factbin.c
字号:
/*******************************************************/
/* "C" Language Integrated Production System */
/* */
/* CLIPS Version 6.20 01/31/02 */
/* */
/* FACT BSAVE/BLOAD MODULE */
/*******************************************************/
/*************************************************************/
/* Purpose: Implements the binary save/load feature for the */
/* fact pattern network. */
/* */
/* Principal Programmer(s): */
/* Gary D. Riley */
/* */
/* Contributing Programmer(s): */
/* Brian L. Donnell */
/* */
/* Revision History: */
/* */
/*************************************************************/
#define _FACTBIN_SOURCE_
#include "setup.h"
#if DEFTEMPLATE_CONSTRUCT && (BLOAD || BLOAD_ONLY || BLOAD_AND_BSAVE) && (! RUN_TIME)
#include <stdio.h>
#define _STDIO_INCLUDED_
#include "memalloc.h"
#include "tmpltdef.h"
#include "bload.h"
#include "bsave.h"
#include "reteutil.h"
#include "rulebin.h"
#include "moduldef.h"
#include "envrnmnt.h"
#include "factbin.h"
/********************************************/
/* INTERNAL DATA STRUCTURES AND DEFINITIONS */
/********************************************/
struct bsaveFactPatternNode
{
struct bsavePatternNodeHeader header;
unsigned short whichSlot;
unsigned short whichField;
unsigned short leaveFields;
long networkTest;
long nextLevel;
long lastLevel;
long leftNode;
long rightNode;
};
#define BSAVE_FIND 0
#define BSAVE_PATTERNS 1
/***************************************/
/* LOCAL INTERNAL FUNCTION DEFINITIONS */
/***************************************/
#if BLOAD_AND_BSAVE
static void BsaveDriver(void *,int,FILE *,struct factPatternNode *);
static void BsaveFind(void *);
static void BsaveStorage(void *,FILE *);
static void BsaveFactPatterns(void *,FILE *);
static void BsavePatternNode(void *,struct factPatternNode *,FILE *);
#endif
static void BloadStorage(void *);
static void BloadBinaryItem(void *);
static void UpdateFactPatterns(void *,void *,long);
static void ClearBload(void *);
static void DeallocateFactBloadData(void *);
/*****************************************************/
/* FactBinarySetup: Initializes the binary load/save */
/* feature for the fact pattern network. */
/*****************************************************/
globle void FactBinarySetup(
void *theEnv)
{
AllocateEnvironmentData(theEnv,FACTBIN_DATA,sizeof(struct factBinaryData),DeallocateFactBloadData);
#if BLOAD_AND_BSAVE
AddBinaryItem(theEnv,"facts",0,BsaveFind,NULL,
BsaveStorage,BsaveFactPatterns,
BloadStorage,BloadBinaryItem,
ClearBload);
#endif
#if BLOAD || BLOAD_ONLY
AddBinaryItem(theEnv,"facts",0,NULL,NULL,NULL,NULL,
BloadStorage,BloadBinaryItem,
ClearBload);
#endif
}
/****************************************************/
/* DeallocateFactBloadData: Deallocates environment */
/* data for the fact bsave functionality. */
/****************************************************/
static void DeallocateFactBloadData(
void *theEnv)
{
unsigned long space;
int i;
for (i = 0; i < FactBinaryData(theEnv)->NumberOfPatterns; i++)
{ DestroyAlphaBetaMemory(theEnv,FactBinaryData(theEnv)->FactPatternArray[i].header.alphaMemory); }
space = FactBinaryData(theEnv)->NumberOfPatterns * sizeof(struct factPatternNode);
if (space != 0) genlongfree(theEnv,(void *) FactBinaryData(theEnv)->FactPatternArray,space);
}
#if BLOAD_AND_BSAVE
/*********************************************************/
/* BsaveFind: Counts the number of data structures which */
/* must be saved in the binary image for the fact */
/* pattern network in the current environment. */
/*********************************************************/
static void BsaveFind(
void *theEnv)
{
struct deftemplate *theDeftemplate;
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,FactBinaryData(theEnv)->NumberOfPatterns);
/*=======================================*/
/* Set the count of fact pattern network */
/* data structures to zero. */
/*=======================================*/
FactBinaryData(theEnv)->NumberOfPatterns = 0L;
/*===========================*/
/* 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 deftemplate in the current module */
/* and count the number of data structures which must */
/* be saved for its pattern network. */
/*=====================================================*/
for (theDeftemplate = (struct deftemplate *) EnvGetNextDeftemplate(theEnv,NULL);
theDeftemplate != NULL;
theDeftemplate = (struct deftemplate *) EnvGetNextDeftemplate(theEnv,theDeftemplate))
{ BsaveDriver(theEnv,BSAVE_FIND,NULL,theDeftemplate->patternNetwork); }
}
}
/**********************************************************/
/* BsaveDriver: Binary save driver routine which handles */
/* both finding/marking the data structures to be saved */
/* and saving the data structures to a file. */
/**********************************************************/
static void BsaveDriver(
void *theEnv,
int action,
FILE *fp,
struct factPatternNode *thePattern)
{
while (thePattern != NULL)
{
switch(action)
{
case BSAVE_FIND:
thePattern->bsaveID = FactBinaryData(theEnv)->NumberOfPatterns++;
break;
case BSAVE_PATTERNS:
BsavePatternNode(theEnv,thePattern,fp);
break;
default:
break;
}
if (thePattern->nextLevel == NULL)
{
while (thePattern->rightNode == NULL)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -