📄 factcom.c
字号:
{
int tempValue1, tempValue2, tempValue3;
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 = GenOpen(theEnv,fileName,"w")) == NULL)
{
OpenErrorMessage(theEnv,"save-facts",fileName);
return(FALSE);
}
SetFastSave(theEnv,filePtr);
/*===========================================*/
/* Set the print flags so that addresses and */
/* strings are printed properly to the file. */
/*===========================================*/
tempValue1 = PrintUtilityData(theEnv)->PreserveEscapedCharacters;
PrintUtilityData(theEnv)->PreserveEscapedCharacters = TRUE;
tempValue2 = PrintUtilityData(theEnv)->AddressesToStrings;
PrintUtilityData(theEnv)->AddressesToStrings = TRUE;
tempValue3 = PrintUtilityData(theEnv)->InstanceAddressesToNames;
PrintUtilityData(theEnv)->InstanceAddressesToNames = TRUE;
/*===================================================*/
/* Determine the list of specific facts to be saved. */
/*===================================================*/
theDOArray = GetSaveFactsDeftemplateNames(theEnv,theList,saveCode,&count,&error);
if (error)
{
PrintUtilityData(theEnv)->PreserveEscapedCharacters = tempValue1;
PrintUtilityData(theEnv)->AddressesToStrings = tempValue2;
PrintUtilityData(theEnv)->InstanceAddressesToNames = tempValue3;
GenClose(theEnv,filePtr);
SetFastSave(theEnv,NULL);
return(FALSE);
}
/*=================*/
/* Save the facts. */
/*=================*/
theModule = ((struct defmodule *) EnvGetCurrentModule(theEnv));
for (theFact = (struct fact *) GetNextFactInScope(theEnv,NULL);
theFact != NULL;
theFact = (struct fact *) GetNextFactInScope(theEnv,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 = 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 = 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 = FALSE;
for (i = 0; i < count; i++)
{
if (theDOArray[i].value == (void *) theFact->whichDeftemplate)
{
printFact = TRUE;
break;
}
}
}
/*===================================*/
/* If the print flag is set to TRUE, */
/* then save the fact to the file. */
/*===================================*/
if (printFact)
{
PrintFact(theEnv,(char *) filePtr,theFact,FALSE,FALSE);
EnvPrintRouter(theEnv,(char *) filePtr,"\n");
}
}
/*==========================*/
/* Restore the print flags. */
/*==========================*/
PrintUtilityData(theEnv)->PreserveEscapedCharacters = tempValue1;
PrintUtilityData(theEnv)->AddressesToStrings = tempValue2;
PrintUtilityData(theEnv)->InstanceAddressesToNames = tempValue3;
/*=================*/
/* Close the file. */
/*=================*/
GenClose(theEnv,filePtr);
SetFastSave(theEnv,NULL);
/*==================================*/
/* Free the deftemplate name array. */
/*==================================*/
if (theList != NULL) rm3(theEnv,theDOArray,(long) sizeof(DATA_OBJECT) * count);
/*===================================*/
/* Return TRUE to indicate no errors */
/* occurred while saving the facts. */
/*===================================*/
return(TRUE);
}
/*******************************************************************/
/* GetSaveFactsDeftemplateNames: Retrieves the list of deftemplate */
/* names for saving specific facts with the save-facts command. */
/*******************************************************************/
static DATA_OBJECT_PTR GetSaveFactsDeftemplateNames(
void *theEnv,
struct expr *theList,
int saveCode,
int *count,
int *error)
{
struct expr *tempList;
DATA_OBJECT_PTR theDOArray;
int i, tempCount;
struct deftemplate *theDeftemplate = NULL;
/*=============================*/
/* Initialize the error state. */
/*=============================*/
*error = 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(theEnv,(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(theEnv,tempList,&theDOArray[i]);
if (EvaluationData(theEnv)->EvaluationError)
{
*error = TRUE;
rm3(theEnv,theDOArray,(long) sizeof(DATA_OBJECT) * *count);
return(NULL);
}
/*======================================*/
/* A deftemplate name must be a symbol. */
/*======================================*/
if (theDOArray[i].type != SYMBOL)
{
*error = TRUE;
ExpectedTypeError1(theEnv,"save-facts",3+i,"symbol");
rm3(theEnv,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 *)
EnvFindDeftemplate(theEnv,ValueToString(theDOArray[i].value));
if (theDeftemplate == NULL)
{
*error = TRUE;
ExpectedTypeError1(theEnv,"save-facts",3+i,"local deftemplate name");
rm3(theEnv,theDOArray,(long) sizeof(DATA_OBJECT) * *count);
return(NULL);
}
}
else if (saveCode == VISIBLE_SAVE)
{
theDeftemplate = (struct deftemplate *)
FindImportedConstruct(theEnv,"deftemplate",NULL,
ValueToString(theDOArray[i].value),
&tempCount,TRUE,NULL);
if (theDeftemplate == NULL)
{
*error = TRUE;
ExpectedTypeError1(theEnv,"save-facts",3+i,"visible deftemplate name");
rm3(theEnv,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);
}
/**************************************************************/
/* EnvLoadFacts: C access routine for the load-facts command. */
/**************************************************************/
globle intBool EnvLoadFacts(
void *theEnv,
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 = GenOpen(theEnv,fileName,"r")) == NULL)
{
OpenErrorMessage(theEnv,"load-facts",fileName);
return(FALSE);
}
SetFastLoad(theEnv,filePtr);
/*=================*/
/* Load the facts. */
/*=================*/
theToken.type = LPAREN;
while (theToken.type != STOP)
{
testPtr = StandardLoadFact(theEnv,(char *) filePtr,&theToken);
if (testPtr == NULL) theToken.type = STOP;
else EvaluateExpression(theEnv,testPtr,&rv);
ReturnExpression(theEnv,testPtr);
}
/*=================*/
/* Close the file. */
/*=================*/
SetFastLoad(theEnv,NULL);
GenClose(theEnv,filePtr);
/*================================================*/
/* Return TRUE if no error occurred while loading */
/* the facts, otherwise return FALSE. */
/*================================================*/
if (EvaluationData(theEnv)->EvaluationError) return(FALSE);
return(TRUE);
}
/*********************************************/
/* EnvLoadFactsFromString: C access routine. */
/*********************************************/
globle intBool EnvLoadFactsFromString(
void *theEnv,
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(theEnv,theStrRouter,theString,0)) :
(!OpenTextSource(theEnv,theStrRouter,theString,0,(unsigned) theMax)))
return(FALSE);
/*=================*/
/* Load the facts. */
/*=================*/
theToken.type = LPAREN;
while (theToken.type != STOP)
{
testPtr = StandardLoadFact(theEnv,theStrRouter,&theToken);
if (testPtr == NULL) theToken.type = STOP;
else EvaluateExpression(theEnv,testPtr,&rv);
ReturnExpression(theEnv,testPtr);
}
/*=================*/
/* Close router. */
/*=================*/
CloseStringSource(theEnv,theStrRouter);
/*================================================*/
/* Return TRUE if no error occurred while loading */
/* the facts, otherwise return FALSE. */
/*================================================*/
if (EvaluationData(theEnv)->EvaluationError) return(FALSE);
return(TRUE);
}
/**************************************************************************/
/* StandardLoadFact: Loads a single fact from the specified logical name. */
/**************************************************************************/
static struct expr *StandardLoadFact(
void *theEnv,
char *logicalName,
struct token *theToken)
{
int error = FALSE;
struct expr *temp;
GetToken(theEnv,logicalName,theToken);
if (theToken->type != LPAREN) return(NULL);
temp = GenConstant(theEnv,FCALL,FindFunction(theEnv,"assert"));
temp->argList = GetRHSPattern(theEnv,logicalName,theToken,&error,
TRUE,FALSE,TRUE,RPAREN);
if (error == TRUE)
{
EnvPrintRouter(theEnv,WERROR,"Function load-facts encountered an error\n");
SetEvaluationError(theEnv,TRUE);
ReturnExpression(theEnv,temp);
return(NULL);
}
if (ExpressionContainsVariables(temp,TRUE))
{
ReturnExpression(theEnv,temp);
return(NULL);
}
return(temp);
}
#if (! RUN_TIME)
/****************************************************************/
/* AssertParse: Driver routine for parsing the assert function. */
/****************************************************************/
static struct expr *AssertParse(
void *theEnv,
struct expr *top,
char *logicalName)
{
int error;
struct expr *rv;
struct token theToken;
ReturnExpression(theEnv,top);
SavePPBuffer(theEnv," ");
IncrementIndentDepth(theEnv,8);
rv = BuildRHSAssert(theEnv,logicalName,&theToken,&error,TRUE,TRUE,"assert command");
DecrementIndentDepth(theEnv,8);
return(rv);
}
#endif /* (! RUN_TIME) */
#endif /* DEFTEMPLATE_CONSTRUCT */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -