📄 factcom.c
字号:
struct fact *theFact; FILE *filePtr; struct defmodule *theModule; DATA_OBJECT_PTR theDOArray; int count, i, printFact, error; /*======================================================*/ /* Open the file. Use either "fast save" or I/O Router. */ /*======================================================*/ if ((filePtr = fopen(fileName,"w")) == NULL) { OpenErrorMessage("save-facts",fileName); return(CLIPS_FALSE); } SetFastSave(filePtr); /*===========================================*/ /* Set the print flags so that addresses and */ /* strings are printed properly to the file. */ /*===========================================*/ tempValue1 = PreserveEscapedCharacters; PreserveEscapedCharacters = CLIPS_TRUE; tempValue2 = AddressesToStrings; AddressesToStrings = CLIPS_TRUE; tempValue3 = InstanceAddressesToNames; InstanceAddressesToNames = CLIPS_TRUE; /*===================================================*/ /* Determine the list of specific facts to be saved. */ /*===================================================*/ theDOArray = GetSaveFactsDeftemplateNames(theList,saveCode,&count,&error); if (error) { PreserveEscapedCharacters = tempValue1; AddressesToStrings = tempValue2; InstanceAddressesToNames = tempValue3; fclose(filePtr); SetFastSave(NULL); return(CLIPS_FALSE); } /*=================*/ /* Save the facts. */ /*=================*/ theModule = ((struct defmodule *) GetCurrentModule()); for (theFact = (struct fact *) GetNextFactInScope(NULL); theFact != NULL; theFact = (struct fact *) GetNextFactInScope(theFact)) { /*===========================================================*/ /* If we're doing a local save and the facts's corresponding */ /* deftemplate isn't in the current module, then don't save */ /* the fact. */ /*===========================================================*/ if ((saveCode == LOCAL_SAVE) && (theFact->whichDeftemplate->header.whichModule->theModule != theModule)) { printFact = CLIPS_FALSE; } /*=====================================================*/ /* Otherwise, if the list of facts to be printed isn't */ /* restricted, then set the print flag to TRUE. */ /*=====================================================*/ else if (theList == NULL) { printFact = CLIPS_TRUE; } /*=======================================================*/ /* Otherwise see if the fact's corresponding deftemplate */ /* is in the list of deftemplates whose facts are to be */ /* saved. If it's in the list, then set the print flag */ /* to TRUE, otherwise set it to FALSE. */ /*=======================================================*/ else { printFact = CLIPS_FALSE; for (i = 0; i < count; i++) { if (theDOArray[i].value == (VOID *) theFact->whichDeftemplate) { printFact = CLIPS_TRUE; break; } } } /*===================================*/ /* If the print flag is set to TRUE, */ /* then save the fact to the file. */ /*===================================*/ if (printFact) { PrintFact((char *) filePtr,theFact); PrintCLIPS((char *) filePtr,"\n"); } } /*==========================*/ /* Restore the print flags. */ /*==========================*/ PreserveEscapedCharacters = tempValue1; AddressesToStrings = tempValue2; InstanceAddressesToNames = tempValue3; /*=================*/ /* Close the file. */ /*=================*/ fclose(filePtr); SetFastSave(NULL); /*==================================*/ /* Free the deftemplate name array. */ /*==================================*/ if (theList != NULL) rm3(theDOArray,(long) sizeof(DATA_OBJECT) * count); /*===================================*/ /* Return TRUE to indicate no errors */ /* occurred while saving the facts. */ /*===================================*/ return(CLIPS_TRUE); } /*******************************************************************//* GetSaveFactsDeftemplateNames: Retrieves the list of deftemplate *//* names for saving specific facts with the save-facts command. *//*******************************************************************/static DATA_OBJECT_PTR GetSaveFactsDeftemplateNames(theList,saveCode,count,error) struct expr *theList; int saveCode; int *count; int *error; { struct expr *tempList; DATA_OBJECT_PTR theDOArray; int i, tempCount; struct deftemplate *theDeftemplate; /*=============================*/ /* Initialize the error state. */ /*=============================*/ *error = CLIPS_FALSE; /*=====================================================*/ /* If no deftemplate names were specified as arguments */ /* then the deftemplate name list is empty. */ /*=====================================================*/ if (theList == NULL) { *count = 0; return(NULL); } /*======================================*/ /* Determine the number of deftemplate */ /* names to be stored in the name list. */ /*======================================*/ for (tempList = theList, *count = 0; tempList != NULL; tempList = tempList->nextArg, (*count)++) { /* Do Nothing */ } /*=========================================*/ /* Allocate the storage for the name list. */ /*=========================================*/ theDOArray = (DATA_OBJECT_PTR) gm3((long) sizeof(DATA_OBJECT) * *count); /*=====================================*/ /* Loop through each of the arguments. */ /*=====================================*/ for (tempList = theList, i = 0; i < *count; tempList = tempList->nextArg, i++) { /*========================*/ /* Evaluate the argument. */ /*========================*/ EvaluateExpression(tempList,&theDOArray[i]); if (EvaluationError) { *error = CLIPS_TRUE; rm3(theDOArray,(long) sizeof(DATA_OBJECT) * *count); return(NULL); } /*======================================*/ /* A deftemplate name must be a symbol. */ /*======================================*/ if (theDOArray[i].type != SYMBOL) { *error = CLIPS_TRUE; ExpectedTypeError1("save-facts",3+i,"symbol"); rm3(theDOArray,(long) sizeof(DATA_OBJECT) * *count); return(NULL); } /*===================================================*/ /* Find the deftemplate. For a local save, look only */ /* in the current module. For a visible save, look */ /* in all visible modules. */ /*===================================================*/ if (saveCode == LOCAL_SAVE) { theDeftemplate = (struct deftemplate *) FindDeftemplate(ValueToString(theDOArray[i].value)); if (theDeftemplate == NULL) { *error = CLIPS_TRUE; ExpectedTypeError1("save-facts",3+i,"local deftemplate name"); rm3(theDOArray,(long) sizeof(DATA_OBJECT) * *count); return(NULL); } } else if (saveCode == VISIBLE_SAVE) { theDeftemplate = (struct deftemplate *) FindImportedConstruct("deftemplate",NULL, ValueToString(theDOArray[i].value), &tempCount,CLIPS_TRUE,NULL); if (theDeftemplate == NULL) { *error = CLIPS_TRUE; ExpectedTypeError1("save-facts",3+i,"visible deftemplate name"); rm3(theDOArray,(long) sizeof(DATA_OBJECT) * *count); return(NULL); } } /*==================================*/ /* Add a pointer to the deftemplate */ /* to the array being created. */ /*==================================*/ theDOArray[i].type = DEFTEMPLATE_PTR; theDOArray[i].value = (VOID *) theDeftemplate; } /*===================================*/ /* Return the array of deftemplates. */ /*===================================*/ return(theDOArray); } /***********************************************************//* LoadFacts: C access routine for the load-facts command. *//***********************************************************/globle BOOLEAN LoadFacts(fileName) char *fileName; { FILE *filePtr; struct token theToken; struct expr *testPtr; DATA_OBJECT rv; /*======================================================*/ /* Open the file. Use either "fast save" or I/O Router. */ /*======================================================*/ if ((filePtr = fopen(fileName,"r")) == NULL) { OpenErrorMessage("load-facts",fileName); return(CLIPS_FALSE); } SetFastLoad(filePtr); /*=================*/ /* Load the facts. */ /*=================*/ theToken.type = LPAREN; while (theToken.type != STOP) { testPtr = StandardLoadFact((char *) filePtr,&theToken); if (testPtr == NULL) theToken.type = STOP; else EvaluateExpression(testPtr,&rv); ReturnExpression(testPtr); } /*=================*/ /* Close the file. */ /*=================*/ SetFastLoad(NULL); fclose(filePtr); /*================================================*/ /* Return TRUE if no error occurred while loading */ /* the facts, otherwise return FALSE. */ /*================================================*/ if (EvaluationError) return(CLIPS_FALSE); return(CLIPS_TRUE); }/***********************************************************//* LoadFactsFromString: C access routine *//***********************************************************/globle BOOLEAN LoadFactsFromString(theString,theMax) char * theString; int theMax; { char * theStrRouter = "*** load-facts-from-string ***"; struct token theToken; struct expr *testPtr; DATA_OBJECT rv; /*==========================*/ /* Initialize string router */ /*==========================*/ if ((theMax == -1) ? (!OpenStringSource(theStrRouter,theString,0)) : (!OpenTextSource(theStrRouter,theString,0,theMax))) return(CLIPS_FALSE); /*=================*/ /* Load the facts. */ /*=================*/ theToken.type = LPAREN; while (theToken.type != STOP) { testPtr = StandardLoadFact(theStrRouter,&theToken); if (testPtr == NULL) theToken.type = STOP; else EvaluateExpression(testPtr,&rv); ReturnExpression(testPtr); } /*=================*/ /* Close router. */ /*=================*/ CloseStringSource(theStrRouter); /*================================================*/ /* Return TRUE if no error occurred while loading */ /* the facts, otherwise return FALSE. */ /*================================================*/ if (EvaluationError) return(CLIPS_FALSE); return(CLIPS_TRUE); }/**************************************************************************//* StandardLoadFact: Loads a single fact from the specified logical name. *//**************************************************************************/static struct expr *StandardLoadFact(logicalName,theToken) char *logicalName; struct token *theToken; { int error = CLIPS_FALSE; struct expr *temp; GetToken(logicalName,theToken); if (theToken->type != LPAREN) return(NULL); temp = GenConstant(FCALL,FindFunction("assert")); temp->argList = GetRHSPattern(logicalName,theToken,&error, CLIPS_TRUE,CLIPS_FALSE,CLIPS_TRUE,RPAREN); if (error == CLIPS_TRUE) { PrintCLIPS(WERROR,"Function load-facts encountered an error\n"); SetEvaluationError(CLIPS_TRUE); ReturnExpression(temp); return(NULL); } if (ExpressionContainsVariables(temp,CLIPS_TRUE)) { ReturnExpression(temp); return(NULL); } return(temp); }#if (! RUN_TIME)/****************************************************************//* AssertParse: Driver routine for parsing the assert function. *//****************************************************************/static struct expr *AssertParse(top,logicalName) struct expr *top; char *logicalName; { int error; struct expr *rv; struct token theToken; ReturnExpression(top); SavePPBuffer(" "); IncrementIndentDepth(8); rv = BuildRHSAssert(logicalName,&theToken,&error,CLIPS_TRUE,CLIPS_TRUE,"assert command"); DecrementIndentDepth(8); return(rv); }#endif /* (! RUN_TIME) */#endif /* DEFTEMPLATE_CONSTRUCT */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -