📄 insfile.c
字号:
/*******************************************************/ /* "C" Language Integrated Production System */ /* */ /* CLIPS Version 6.05 04/09/97 */ /* */ /* INSTANCE LOAD/SAVE (ASCII/BINARY) MODULE */ /*******************************************************//*************************************************************//* Purpose: File load/save routines for instances *//* *//* Principal Programmer(s): *//* Brian L. Donnell *//* *//* Contributing Programmer(s): *//* *//* Revision History: *//* *//*************************************************************//* ========================================= ***************************************** EXTERNAL DEFINITIONS ========================================= ***************************************** */#include "setup.h"#if OBJECT_SYSTEM#include "argacces.h"#include "classcom.h"#include "classfun.h"#include "clipsmem.h"#include "extnfunc.h"#include "inscom.h"#include "insfun.h"#include "insmngr.h"#include "inspsr.h"#include "object.h"#include "router.h"#include "strngrtr.h"#include "symblbin.h"#include "sysdep.h"#if DEFTEMPLATE_CONSTRUCT && DEFRULE_CONSTRUCT#include "factmngr.h"#endif#define _INSFILE_SOURCE_#include "insfile.h"extern struct token ObjectParseToken;/* ========================================= ***************************************** CONSTANTS ========================================= ***************************************** */#define MAX_BLOCK_SIZE 10240/* ========================================= ***************************************** MACROS AND TYPES ========================================= ***************************************** */struct bsaveSlotValue { long slotName; int valueCount; };struct bsaveSlotValueAtom { int type; long value; };/* ========================================= ***************************************** INTERNALLY VISIBLE FUNCTION HEADERS ========================================= ***************************************** */#if ANSI_COMPILERstatic long InstancesSaveCommandParser(char *,long (*)(char *,int, EXPRESSION *,BOOLEAN));static DATA_OBJECT *ProcessSaveClassList(char *,EXPRESSION *,int,BOOLEAN);static VOID ReturnSaveClassList(DATA_OBJECT *);static long SaveOrMarkInstances(VOID *,int,DATA_OBJECT *,BOOLEAN,BOOLEAN, VOID (*)(VOID *,INSTANCE_TYPE *));static long SaveOrMarkInstancesOfClass(VOID *,struct defmodule *,int,DEFCLASS *, BOOLEAN,int,VOID (*)(VOID *,INSTANCE_TYPE *));static VOID SaveSingleInstanceText(VOID *,INSTANCE_TYPE *);static VOID ProcessFileErrorMessage(char *,char *);#if BSAVE_INSTANCESstatic VOID WriteBinaryHeader(FILE *);static VOID MarkSingleInstance(VOID *,INSTANCE_TYPE *);static VOID MarkNeededAtom(int,VOID *);static VOID SaveSingleInstanceBinary(VOID *,INSTANCE_TYPE *);static VOID SaveAtomBinary(int,VOID *,FILE *);#endifstatic long LoadOrRestoreInstances(char *,int,int);#if BLOAD_INSTANCESstatic BOOLEAN VerifyBinaryHeader(char *);static BOOLEAN LoadSingleBinaryInstance(void);static VOID BinaryLoadInstanceError(SYMBOL_HN *,DEFCLASS *);static VOID CreateSlotValue(DATA_OBJECT *,struct bsaveSlotValueAtom *,long); /* 6.04 Bug Fix */static VOID *GetBinaryAtomValue(struct bsaveSlotValueAtom *);static VOID BufferedRead(VOID *,unsigned long);static VOID FreeReadBuffer(void);#endif#elsestatic long InstancesSaveCommandParser();static DATA_OBJECT *ProcessSaveClassList();static VOID ReturnSaveClassList();static long SaveOrMarkInstances();static long SaveOrMarkInstancesOfClass();static VOID SaveSingleInstanceText();static VOID ProcessFileErrorMessage();#if BSAVE_INSTANCESstatic VOID WriteBinaryHeader();static VOID MarkSingleInstance();static VOID MarkNeededAtom();static VOID SaveSingleInstanceBinary();static VOID SaveAtomBinary();#endifstatic long LoadOrRestoreInstances();#if BLOAD_INSTANCESstatic BOOLEAN VerifyBinaryHeader();static BOOLEAN LoadSingleBinaryInstance();static VOID BinaryLoadInstanceError();static VOID CreateSlotValue();static VOID *GetBinaryAtomValue();static VOID BufferedRead();static VOID FreeReadBuffer();#endif#endif/* ========================================= ***************************************** INTERNALLY VISIBLE GLOBAL VARIABLES ========================================= ***************************************** */ #if BLOAD_INSTANCES || BSAVE_INSTANCESstatic char *InstanceBinaryPrefixID = "\5\6\7CLIPS";static char *InstanceBinaryVersionID = "V6.00";static unsigned long BinaryInstanceFileSize;#if BLOAD_INSTANCESstatic unsigned long BinaryInstanceFileOffset;static char HUGE_ADDR *CurrentReadBuffer = NULL;static unsigned long CurrentReadBufferSize = 0L;#endif#endif/* ========================================= ***************************************** EXTERNALLY VISIBLE FUNCTIONS ========================================= ***************************************** */#if (! RUN_TIME)/*************************************************** NAME : SetupInstanceFileCommands DESCRIPTION : Defines function interfaces for saving instances to files INPUTS : None RETURNS : Nothing useful SIDE EFFECTS : Functions defined to CLIPS NOTES : None ***************************************************/globle VOID SetupInstanceFileCommands() { DefineFunction2("save-instances",'l',PTIF SaveInstancesCommand, "SaveInstancesCommand","1*wk"); DefineFunction2("load-instances",'l',PTIF LoadInstancesCommand, "LoadInstancesCommand","11k"); DefineFunction2("restore-instances",'l',PTIF RestoreInstancesCommand, "RestoreInstancesCommand","11k"); #if BSAVE_INSTANCES DefineFunction2("bsave-instances",'l',PTIF BinarySaveInstancesCommand, "BinarySaveInstancesCommand","1*wk");#endif#if BLOAD_INSTANCES DefineFunction2("bload-instances",'l',PTIF BinaryLoadInstancesCommand, "BinaryLoadInstancesCommand","11k");#endif }#endif/**************************************************************************** NAME : SaveInstancesCommand DESCRIPTION : CLIPS interface for saving current instances to a file INPUTS : None RETURNS : The number of instances saved SIDE EFFECTS : Instances saved to named file NOTES : CLIPS Syntax : (save-instances <file> [local|visible [[inherit] <class>+]]) ****************************************************************************/globle long SaveInstancesCommand() { return(InstancesSaveCommandParser("save-instances",SaveInstances)); }/****************************************************** NAME : LoadInstancesCommand DESCRIPTION : CLIPS interface for loading instances from a file INPUTS : None RETURNS : The number of instances loaded SIDE EFFECTS : Instances loaded from named file NOTES : CLIPS Syntax : (load-instances <file>) ******************************************************/globle long LoadInstancesCommand() { char *fileFound; DATA_OBJECT temp; long instanceCount; if (ArgTypeCheck("load-instances",1,SYMBOL_OR_STRING,&temp) == CLIPS_FALSE) return(0L); fileFound = DOToString(temp); instanceCount = LoadInstances(fileFound); if (EvaluationError) ProcessFileErrorMessage("load-instances",fileFound); return(instanceCount); }/*************************************************** NAME : LoadInstances DESCRIPTION : Loads instances from named file INPUTS : The name of the input file RETURNS : The number of instances loaded SIDE EFFECTS : Instances loaded from file NOTES : None ***************************************************/globle long LoadInstances(file) char *file; { return(LoadOrRestoreInstances(file,CLIPS_TRUE,CLIPS_TRUE)); }/*************************************************** NAME : LoadInstancesFromString DESCRIPTION : Loads instances from given string INPUTS : 1) The input string 2) Index of char in string after last valid char (-1 for all chars) RETURNS : The number of instances loaded SIDE EFFECTS : Instances loaded from string NOTES : Uses string routers ***************************************************/globle long LoadInstancesFromString(theString,theMax) char * theString; int theMax; { long theCount; char * theStrRouter = "*** load-instances-from-string ***"; if ((theMax == -1) ? (!OpenStringSource(theStrRouter,theString,0)) : (!OpenTextSource(theStrRouter,theString,0,theMax))) return(-1L); theCount = LoadOrRestoreInstances(theStrRouter,CLIPS_TRUE,CLIPS_FALSE); CloseStringSource(theStrRouter); return(theCount); }/********************************************************* NAME : RestoreInstancesCommand DESCRIPTION : CLIPS interface for loading instances from a file w/o messages INPUTS : None RETURNS : The number of instances restored SIDE EFFECTS : Instances loaded from named file NOTES : CLIPS Syntax : (restore-instances <file>) *********************************************************/globle long RestoreInstancesCommand() { char *fileFound; DATA_OBJECT temp; long instanceCount; if (ArgTypeCheck("restore-instances",1,SYMBOL_OR_STRING,&temp) == CLIPS_FALSE) return(0L); fileFound = DOToString(temp); instanceCount = RestoreInstances(fileFound); if (EvaluationError) ProcessFileErrorMessage("restore-instances",fileFound); return(instanceCount); }/*************************************************** NAME : RestoreInstances DESCRIPTION : Restores instances from named file INPUTS : The name of the input file RETURNS : The number of instances restored SIDE EFFECTS : Instances restored from file NOTES : None ***************************************************/globle long RestoreInstances(file) char *file; { return(LoadOrRestoreInstances(file,CLIPS_FALSE,CLIPS_TRUE)); }/*************************************************** NAME : RestoreInstancesFromString DESCRIPTION : Restores instances from given string INPUTS : 1) The input string 2) Index of char in string after last valid char (-1 for all chars) RETURNS : The number of instances loaded SIDE EFFECTS : Instances loaded from string NOTES : Uses string routers ***************************************************/globle long RestoreInstancesFromString(theString,theMax) char * theString; int theMax; { long theCount; char * theStrRouter = "*** load-instances-from-string ***"; if ((theMax == -1) ? (!OpenStringSource(theStrRouter,theString,0)) : (!OpenTextSource(theStrRouter,theString,0,theMax))) return(-1L); theCount = LoadOrRestoreInstances(theStrRouter,CLIPS_FALSE,CLIPS_FALSE); CloseStringSource(theStrRouter); return(theCount); }#if BLOAD_INSTANCES/******************************************************* NAME : BinaryLoadInstancesCommand DESCRIPTION : CLIPS interface for loading instances from a binary file INPUTS : None RETURNS : The number of instances loaded SIDE EFFECTS : Instances loaded from named binary file NOTES : CLIPS Syntax : (bload-instances <file>) *******************************************************/globle long BinaryLoadInstancesCommand() { char *fileFound; DATA_OBJECT temp; long instanceCount; if (ArgTypeCheck("bload-instances",1,SYMBOL_OR_STRING,&temp) == CLIPS_FALSE) return(0L); fileFound = DOToString(temp); instanceCount = BinaryLoadInstances(fileFound); if (EvaluationError) ProcessFileErrorMessage("bload-instances",fileFound); return(instanceCount); }/**************************************************** NAME : BinaryLoadInstances DESCRIPTION : Loads instances quickly from a binary file INPUTS : The file name RETURNS : The number of instances loaded SIDE EFFECTS : Instances loaded w/o message-passing NOTES : None ****************************************************/globle long BinaryLoadInstances(theFile) char *theFile; { long i,instanceCount; if (GenOpen("bload-instances",theFile) == 0) { SetEvaluationError(CLIPS_TRUE); return(-1L); } if (VerifyBinaryHeader(theFile) == CLIPS_FALSE) { GenClose(); SetEvaluationError(CLIPS_TRUE); return(-1L); } ReadNeededAtomicValues(); BinaryInstanceFileOffset = 0L; GenRead((VOID *) &BinaryInstanceFileSize,(unsigned long) sizeof(unsigned long)); GenRead((VOID *) &instanceCount,(unsigned long) sizeof(long)); for (i = 0L ; i < instanceCount ; i++) { if (LoadSingleBinaryInstance() == CLIPS_FALSE) { FreeReadBuffer(); FreeAtomicValueStorage(); GenClose();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -