📄 insfile.c
字号:
SetEvaluationError(CLIPS_TRUE); return(i); } } FreeReadBuffer(); FreeAtomicValueStorage(); GenClose(); return(instanceCount); }#endif/******************************************************* NAME : SaveInstances DESCRIPTION : Saves current instances to named file INPUTS : 1) The name of the output file 2) A flag indicating whether to save local (current module only) or visible instances LOCAL_SAVE or VISIBLE_SAVE 3) A list of expressions containing the names of classes for which instances are to be saved 4) A flag indicating if the subclasses of specified classes shoudl also be processed RETURNS : The number of instances saved SIDE EFFECTS : Instances saved to file NOTES : None *******************************************************/globle long SaveInstances(file,saveCode,classExpressionList,inheritFlag) char *file; int saveCode; EXPRESSION *classExpressionList; BOOLEAN inheritFlag; { FILE *sfile = NULL; int oldPEC,oldATS,oldIAN; DATA_OBJECT *classList; long instanceCount; classList = ProcessSaveClassList("save-instances",classExpressionList, saveCode,inheritFlag); if ((classList == NULL) && (classExpressionList != NULL)) return(0L); SaveOrMarkInstances((VOID *) sfile,saveCode,classList, inheritFlag,CLIPS_TRUE,NULL); if ((sfile = fopen(file,"w")) == NULL) { OpenErrorMessage("save-instances",file); ReturnSaveClassList(classList); SetEvaluationError(CLIPS_TRUE); return(0L); } oldPEC = PreserveEscapedCharacters; PreserveEscapedCharacters = CLIPS_TRUE; oldATS = AddressesToStrings; AddressesToStrings = CLIPS_TRUE; oldIAN = InstanceAddressesToNames; InstanceAddressesToNames = CLIPS_TRUE; SetFastSave(sfile); instanceCount = SaveOrMarkInstances((VOID *) sfile,saveCode,classList, inheritFlag,CLIPS_TRUE,SaveSingleInstanceText); fclose(sfile); SetFastSave(NULL); PreserveEscapedCharacters = oldPEC; AddressesToStrings = oldATS; InstanceAddressesToNames = oldIAN; ReturnSaveClassList(classList); return(instanceCount); } #if BSAVE_INSTANCES/**************************************************************************** NAME : BinarySaveInstancesCommand DESCRIPTION : CLIPS interface for saving current instances to a binary file INPUTS : None RETURNS : The number of instances saved SIDE EFFECTS : Instances saved (in binary format) to named file NOTES : CLIPS Syntax : (bsave-instances <file> [local|visible [[inherit] <class>+]]) *****************************************************************************/globle long BinarySaveInstancesCommand() { return(InstancesSaveCommandParser("bsave-instances",BinarySaveInstances)); }/******************************************************* NAME : BinarySaveInstances DESCRIPTION : Saves current instances to binary file INPUTS : 1) The name of the output file 2) A flag indicating whether to save local (current module only) or visible instances LOCAL_SAVE or VISIBLE_SAVE 3) A list of expressions containing the names of classes for which instances are to be saved 4) A flag indicating if the subclasses of specified classes shoudl also be processed RETURNS : The number of instances saved SIDE EFFECTS : Instances saved to file NOTES : None *******************************************************/globle long BinarySaveInstances(file,saveCode,classExpressionList,inheritFlag) char *file; int saveCode; EXPRESSION *classExpressionList; BOOLEAN inheritFlag; { DATA_OBJECT *classList; FILE *bsaveFP; long instanceCount; classList = ProcessSaveClassList("bsave-instances",classExpressionList, saveCode,inheritFlag); if ((classList == NULL) && (classExpressionList != NULL)) return(0L); BinaryInstanceFileSize = 0L; InitAtomicValueNeededFlags(); instanceCount = SaveOrMarkInstances(NULL,saveCode,classList,inheritFlag, CLIPS_FALSE,MarkSingleInstance); if ((bsaveFP = fopen(file,"wb")) == NULL) { OpenErrorMessage("bsave-instances",file); ReturnSaveClassList(classList); SetEvaluationError(CLIPS_TRUE); return(0L); } WriteBinaryHeader(bsaveFP); WriteNeededAtomicValues(bsaveFP); fwrite((VOID *) &BinaryInstanceFileSize,sizeof(unsigned long),1,bsaveFP); fwrite((VOID *) &instanceCount,sizeof(long),1,bsaveFP); SetAtomicValueIndices(CLIPS_FALSE); SaveOrMarkInstances((VOID *) bsaveFP,saveCode,classList, inheritFlag,CLIPS_FALSE,SaveSingleInstanceBinary); RestoreAtomicValueBuckets(); fclose(bsaveFP); ReturnSaveClassList(classList); return(instanceCount); } #endif/* ========================================= ***************************************** INTERNALLY VISIBLE FUNCTIONS ========================================= ***************************************** *//****************************************************** NAME : InstancesSaveCommandParser DESCRIPTION : Argument parser for save-instances and bsave-instances INPUTS : 1) The name of the calling function 2) A pointer to the support function to call for the save/bsave RETURNS : The number of instances saved SIDE EFFECTS : Instances saved/bsaved NOTES : None ******************************************************/static long InstancesSaveCommandParser(functionName,saveFunction) char *functionName;#if ANSI_COMPILER long (*saveFunction)(char *,int,EXPRESSION *,BOOLEAN);#else long (*saveFunction)();#endif { char *fileFound; DATA_OBJECT temp; int argCount,saveCode = LOCAL_SAVE; EXPRESSION *classList = NULL; BOOLEAN inheritFlag = CLIPS_FALSE; if (ArgTypeCheck(functionName,1,SYMBOL_OR_STRING,&temp) == CLIPS_FALSE) return(0L); fileFound = DOToString(temp); argCount = RtnArgCount(); if (argCount > 1) { if (ArgTypeCheck(functionName,2,SYMBOL,&temp) == CLIPS_FALSE) { ExpectedTypeError1(functionName,2,"symbol \"local\" or \"visible\""); SetEvaluationError(CLIPS_TRUE); return(0L); } if (strcmp(DOToString(temp),"local") == 0) saveCode = LOCAL_SAVE; else if (strcmp(DOToString(temp),"visible") == 0) saveCode = VISIBLE_SAVE; else { ExpectedTypeError1(functionName,2,"symbol \"local\" or \"visible\""); SetEvaluationError(CLIPS_TRUE); return(0L); } classList = GetFirstArgument()->nextArg->nextArg; /* =========================== Check for "inherit" keyword Must be at least one class name following =========================== */ if ((classList != NULL) ? (classList->nextArg != NULL) : CLIPS_FALSE) { if ((classList->type != SYMBOL) ? CLIPS_FALSE : (strcmp(ValueToString(classList->value),"inherit") == 0)) { inheritFlag = CLIPS_TRUE; classList = classList->nextArg; } } } return((*saveFunction)(fileFound,saveCode,classList,inheritFlag)); }/**************************************************** NAME : ProcessSaveClassList DESCRIPTION : Evaluates a list of class name expressions and stores them in a data object list INPUTS : 1) The name of the calling function 2) The class expression list 3) A flag indicating if only local or all visible instances are being saved 4) A flag indicating if inheritance relationships should be checked between classes RETURNS : The evaluated class pointer data objects - NULL on errors SIDE EFFECTS : Data objects allocated and classes validated NOTES : None ****************************************************/static DATA_OBJECT *ProcessSaveClassList(functionName,classExps,saveCode,inheritFlag) char *functionName; EXPRESSION *classExps; int saveCode; BOOLEAN inheritFlag; { DATA_OBJECT *head = NULL,*prv,*new,tmp; DEFCLASS *theDefclass; struct defmodule *currentModule; int argIndex = inheritFlag ? 4 : 3; currentModule = ((struct defmodule *) GetCurrentModule()); while (classExps != NULL) { if (EvaluateExpression(classExps,&tmp)) goto ProcessClassListError; if (tmp.type != SYMBOL) goto ProcessClassListError; if (saveCode == LOCAL_SAVE) theDefclass = LookupDefclassAnywhere(currentModule,DOToString(tmp)); else theDefclass = LookupDefclassInScope(DOToString(tmp)); if (theDefclass == NULL) goto ProcessClassListError; else if (theDefclass->abstract && (inheritFlag == CLIPS_FALSE)) goto ProcessClassListError; prv = new = head; while (new != NULL) { if (new->value == (VOID *) theDefclass) goto ProcessClassListError; else if (inheritFlag) { if (HasSuperclass((DEFCLASS *) new->value,theDefclass) || HasSuperclass(theDefclass,(DEFCLASS *) new->value)) goto ProcessClassListError; } prv = new; new = new->next; } new = get_struct(dataObject); new->type = DEFCLASS_PTR; new->value = (VOID *) theDefclass; new->next = NULL; if (prv == NULL) head = new; else prv->next = new; argIndex++; classExps = classExps->nextArg; } return(head); ProcessClassListError: ExpectedTypeError1(functionName,argIndex, inheritFlag ? "valid class name" : "valid concrete class name"); ReturnSaveClassList(head); SetEvaluationError(CLIPS_TRUE); return(NULL); }/**************************************************** NAME : ReturnSaveClassList DESCRIPTION : Deallocates the class data object list created by ProcessSaveClassList INPUTS : The class data object list RETURNS : Nothing useful SIDE EFFECTS : Class data object returned NOTES : None ****************************************************/static VOID ReturnSaveClassList(classList) DATA_OBJECT *classList; { DATA_OBJECT *tmp; while (classList != NULL) { tmp = classList; classList = classList->next; rtn_struct(dataObject,tmp); } }/*************************************************** NAME : SaveOrMarkInstances DESCRIPTION : Iterates through all specified instances either marking needed atoms or writing instances in binary/text format INPUTS : 1) NULL (for marking), logical name (for text saves) file pointer (for binary saves) 2) A cope flag indicating LOCAL or VISIBLE saves only 3) A list of data objects containing the names of classes of instances to be saved 4) A flag indicating whether to include subclasses of arg #3 5) A flag indicating if the iteration can be interrupted or not 6) The access function to mark or save an instance (can be NULL if only counting instances) RETURNS : The number of instances saved SIDE EFFECTS : Instances amrked or saved NOTES : None ***************************************************/static long SaveOrMarkInstances(theOutput,saveCode,classList, inheritFlag,interruptOK,saveInstanceFunc) VOID *theOutput; int saveCode; DATA_OBJECT *classList; BOOLEAN inheritFlag,interruptOK;#if ANSI_COMPILER VOID (*saveInstanceFunc)(VOID *,INSTANCE_TYPE *);#else VOID (*saveInstanceFunc)();#endif { struct defmodule *currentModule; int traversalID; DATA_OBJECT *tmp; INSTANCE_TYPE *ins; long instanceCount = 0L; currentModule = ((struct defmodule *) GetCurrentModule()); if (classList != NULL) { traversalID = GetTraversalID(); if (traversalID != -1) { for (tmp = classList ; (! ((tmp == NULL) || (HaltExecution && interruptOK))) ; tmp = tmp->next) instanceCount += SaveOrMarkInstancesOfClass(theOutput,currentModule,saveCode, (DEFCLASS *) tmp->value,inheritFlag, traversalID,saveInstanceFunc); ReleaseTraversalID(); } } else { for (ins = (INSTANCE_TYPE *) GetNextInstanceInScope(NULL) ; (ins != NULL) && (HaltExecution != CLIPS_TRUE) ; ins = (INSTANCE_TYPE *) GetNextInstanceInScope((VOID *) ins)) { if ((saveCode == VISIBLE_SAVE) ? CLIPS_TRUE : (ins->cls->header.whichModule->theModule == currentModule)) { if (saveInstanceFunc != NULL) (*saveInstanceFunc)(theOutput,ins); instanceCount++; } } } return(instanceCount); } /*************************************************** NAME : SaveOrMarkInstancesOfClass DESCRIPTION : Saves off the direct (and indirect) instance of the specified class INPUTS : 1) The logical name of the output
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -