📄 dffctbin.c
字号:
/*******************************************************/
/* "C" Language Integrated Production System */
/* */
/* CLIPS Version 6.21 06/15/03 */
/* */
/* DEFFACTS BSAVE/BLOAD MODULE */
/*******************************************************/
/*************************************************************/
/* Purpose: Implements the binary save/load feature for the */
/* deffacts construct. */
/* */
/* Principal Programmer(s): */
/* Gary D. Riley */
/* */
/* Contributing Programmer(s): */
/* Brian L. Donnell */
/* */
/* Revision History: */
/* */
/*************************************************************/
#define _DFFCTBIN_SOURCE_
#include "setup.h"
#if DEFFACTS_CONSTRUCT && (BLOAD || BLOAD_ONLY || BLOAD_AND_BSAVE) && (! RUN_TIME)
#include <stdio.h>
#define _STDIO_INCLUDED_
#include "memalloc.h"
#include "dffctdef.h"
#include "moduldef.h"
#include "bload.h"
#include "bsave.h"
#include "envrnmnt.h"
#include "dffctbin.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 *);
#endif
static void BloadStorage(void *);
static void BloadBinaryItem(void *);
static void UpdateDeffactsModule(void *,void *,long);
static void UpdateDeffacts(void *,void *,long);
static void ClearBload(void *);
static void DeallocateDeffactsBloadData(void *);
/********************************************/
/* DeffactsBinarySetup: Installs the binary */
/* save/load feature for deffacts. */
/********************************************/
globle void DeffactsBinarySetup(
void *theEnv)
{
AllocateEnvironmentData(theEnv,DFFCTBIN_DATA,sizeof(struct deffactsBinaryData),DeallocateDeffactsBloadData);
#if BLOAD_AND_BSAVE
AddBinaryItem(theEnv,"deffacts",0,BsaveFind,BsaveExpressions,
BsaveStorage,BsaveBinaryItem,
BloadStorage,BloadBinaryItem,
ClearBload);
#endif
#if (BLOAD || BLOAD_ONLY)
AddBinaryItem(theEnv,"deffacts",0,NULL,NULL,NULL,NULL,
BloadStorage,BloadBinaryItem,
ClearBload);
#endif
}
/********************************************************/
/* DeallocateDeffactsBloadData: Deallocates environment */
/* data for the deffacts bsave functionality. */
/********************************************************/
static void DeallocateDeffactsBloadData(
void *theEnv)
{
unsigned long space;
space = DeffactsBinaryData(theEnv)->NumberOfDeffacts * sizeof(struct deffacts);
if (space != 0) genlongfree(theEnv,(void *) DeffactsBinaryData(theEnv)->DeffactsArray,space);
space = DeffactsBinaryData(theEnv)->NumberOfDeffactsModules * sizeof(struct deffactsModule);
if (space != 0) genlongfree(theEnv,(void *) DeffactsBinaryData(theEnv)->ModuleArray,space);
}
#if BLOAD_AND_BSAVE
/*********************************************************/
/* BsaveFind: Counts the number of data structures which */
/* must be saved in the binary image for the deffacts */
/* in the current environment. */
/*********************************************************/
static void BsaveFind(
void *theEnv)
{
struct deffacts *theDeffacts;
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,DeffactsBinaryData(theEnv)->NumberOfDeffactsModules);
SaveBloadCount(theEnv,DeffactsBinaryData(theEnv)->NumberOfDeffacts);
/*========================================*/
/* Set the count of deffacts and deffacts */
/* module data structures to zero. */
/*========================================*/
DeffactsBinaryData(theEnv)->NumberOfDeffacts = 0;
DeffactsBinaryData(theEnv)->NumberOfDeffactsModules = 0;
/*===========================*/
/* 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 and increment the number of deffacts */
/* modules encountered. */
/*===============================================*/
EnvSetCurrentModule(theEnv,(void *) theModule);
DeffactsBinaryData(theEnv)->NumberOfDeffactsModules++;
/*===================================================*/
/* Loop through each deffacts in the current module. */
/*===================================================*/
for (theDeffacts = (struct deffacts *) EnvGetNextDeffacts(theEnv,NULL);
theDeffacts != NULL;
theDeffacts = (struct deffacts *) EnvGetNextDeffacts(theEnv,theDeffacts))
{
/*======================================================*/
/* Initialize the construct header for the binary save. */
/*======================================================*/
MarkConstructHeaderNeededItems(&theDeffacts->header,DeffactsBinaryData(theEnv)->NumberOfDeffacts++);
/*============================================================*/
/* Count the number of expressions contained in the deffacts' */
/* assertion list and mark any atomic values contained there */
/* as in use. */
/*============================================================*/
ExpressionData(theEnv)->ExpressionCount += ExpressionSize(theDeffacts->assertList);
MarkNeededItems(theEnv,theDeffacts->assertList);
}
}
}
/************************************************/
/* BsaveExpressions: Saves the expressions used */
/* by deffacts to the binary save file. */
/************************************************/
static void BsaveExpressions(
void *theEnv,
FILE *fp)
{
struct deffacts *theDeffacts;
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 deffacts in the current module */
/* and save the assertion list expression. */
/*==================================================*/
for (theDeffacts = (struct deffacts *) EnvGetNextDeffacts(theEnv,NULL);
theDeffacts != NULL;
theDeffacts = (struct deffacts *) EnvGetNextDeffacts(theEnv,theDeffacts))
{ BsaveExpression(theEnv,theDeffacts->assertList,fp); }
}
}
/******************************************************/
/* BsaveStorage: Writes out the storage requirements */
/* for all deffacts structures to the binary file. */
/******************************************************/
static void BsaveStorage(
void *theEnv,
FILE *fp)
{
unsigned long space;
/*=================================================================*/
/* Only two data structures are saved as part of a deffacts binary */
/* image: the deffacts data structure and the deffactsModule data */
/* structure. The assertion list expressions are not save with the */
/* deffacts portion of the binary image. */
/*=================================================================*/
space = sizeof(long) * 2;
GenWrite(&space,(unsigned long) sizeof(unsigned long int),fp);
GenWrite(&DeffactsBinaryData(theEnv)->NumberOfDeffacts,(unsigned long) sizeof(long int),fp);
GenWrite(&DeffactsBinaryData(theEnv)->NumberOfDeffactsModules,(unsigned long) sizeof(long int),fp);
}
/********************************************/
/* BsaveBinaryItem: Writes out all deffacts */
/* structures to the binary file. */
/********************************************/
static void BsaveBinaryItem(
void *theEnv,
FILE *fp)
{
unsigned long int space;
struct deffacts *theDeffacts;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -