📄 factcom.c
字号:
/*******************************************************/ /* "C" Language Integrated Production System */ /* */ /* CLIPS Version 6.05 04/09/97 */ /* */ /* FACT COMMANDS MODULE */ /*******************************************************//*************************************************************//* Purpose: Provides the facts, assert, retract, save-facts, *//* load-facts, set-fact-duplication, get-fact-duplication, *//* assert-string, and fact-index commands and functions. *//* *//* Principal Programmer(s): *//* Gary D. Riley *//* *//* Contributing Programmer(s): *//* Brian L. Donnell *//* *//* Revision History: *//* *//*************************************************************/#include <stdio.h>#define _CLIPS_STDIO_#include <string.h>#include "setup.h"#if DEFTEMPLATE_CONSTRUCT#define _FACTCOM_SOURCE_#include "clipsmem.h"#include "exprnpsr.h"#include "factmngr.h"#include "argacces.h"#include "match.h"#include "router.h"#include "scanner.h"#include "constant.h"#include "factrhs.h"#include "factmch.h"#include "extnfunc.h"#include "tmpltpsr.h"#include "tmpltutl.h"#include "facthsh.h"#include "modulutl.h"#include "strngrtr.h"#include "tmpltdef.h"#include "tmpltfun.h"#if BLOAD_AND_BSAVE || BLOAD || BLOAD_ONLY#include "bload.h"#endif#include "factcom.h"#define INVALID -2L#define UNSPECIFIED -1L/***************************************//* LOCAL INTERNAL FUNCTION DEFINITIONS *//***************************************/#if ANSI_COMPILER#if (! RUN_TIME) static struct expr *AssertParse(struct expr *,char *);#endif#if DEBUGGING_FUNCTIONS static long int GetFactsArgument(int,int);#endif static struct expr *StandardLoadFact(char *,struct token *); static DATA_OBJECT_PTR GetSaveFactsDeftemplateNames(struct expr *,int,int *,int *);#else#if (! RUN_TIME) static struct expr *AssertParse();#endif#if DEBUGGING_FUNCTIONS static long int GetFactsArgument();#endif static struct expr *StandardLoadFact(); static DATA_OBJECT_PTR GetSaveFactsDeftemplateNames();#endif/***************************************//* FactCommandDefinitions: Initializes *//* fact commands and functions. *//***************************************/globle VOID FactCommandDefinitions() {#if ! RUN_TIME#if DEBUGGING_FUNCTIONS DefineFunction2("facts", 'v', PTIF FactsCommand, "FactsCommand", "*4iu");#endif DefineFunction("assert", 'u', PTIF AssertCommand, "AssertCommand"); DefineFunction2("retract", 'v', PTIF RetractCommand, "RetractCommand","1*z"); DefineFunction2("assert-string", 'u', PTIF AssertStringFunction, "AssertStringFunction", "11s"); DefineFunction2("str-assert", 'u', PTIF AssertStringFunction, "AssertStringFunction", "11s"); DefineFunction2("get-fact-duplication",'b', GetFactDuplicationCommand,"GetFactDuplicationCommand", "00"); DefineFunction2("set-fact-duplication",'b', SetFactDuplicationCommand,"SetFactDuplicationCommand", "11"); DefineFunction2("save-facts", 'b', PTIF SaveFactsCommand, "SaveFactsCommand", "1*wk"); DefineFunction2("load-facts", 'b', PTIF LoadFactsCommand, "LoadFactsCommand", "11k"); DefineFunction2("fact-index", 'l', PTIF FactIndexFunction,"FactIndexFunction", "11y");#endif#if (! RUN_TIME) AddFunctionParser("assert",AssertParse); FuncSeqOvlFlags("assert",CLIPS_FALSE,CLIPS_FALSE);#endif }/***************************************//* AssertCommand: CLIPS access routine *//* for the assert function. *//***************************************/globle VOID AssertCommand(rv) DATA_OBJECT_PTR rv; { struct deftemplate *theDeftemplate; struct field *theField; DATA_OBJECT theValue; struct expr *theExpression; struct templateSlot *slotPtr; struct fact *newFact; int error = CLIPS_FALSE; int i; struct fact *theFact; /*===================================================*/ /* Set the default return value to the symbol FALSE. */ /*===================================================*/ SetpType(rv,SYMBOL); SetpValue(rv,CLIPSFalseSymbol); /*================================*/ /* Get the deftemplate associated */ /* with the fact being asserted. */ /*================================*/ theExpression = GetFirstArgument(); theDeftemplate = (struct deftemplate *) theExpression->value; /*=======================================*/ /* Create the fact and store the name of */ /* the deftemplate as the 1st field. */ /*=======================================*/ if (theDeftemplate->implied == CLIPS_FALSE) { newFact = CreateFactBySize((int) theDeftemplate->numberOfSlots); slotPtr = theDeftemplate->slotList; } else { newFact = CreateFactBySize(1); if (theExpression->nextArg == NULL) { newFact->theProposition.theFields[0].type = MULTIFIELD; newFact->theProposition.theFields[0].value = CreateMultifield2(0L); } slotPtr = NULL; } newFact->whichDeftemplate = theDeftemplate; /*===================================================*/ /* Evaluate the expression associated with each slot */ /* and store the result in the appropriate slot of */ /* the newly created fact. */ /*===================================================*/ theField = newFact->theProposition.theFields; for (theExpression = theExpression->nextArg, i = 0; theExpression != NULL; theExpression = theExpression->nextArg, i++) { /*===================================================*/ /* Evaluate the expression to be stored in the slot. */ /*===================================================*/ EvaluateExpression(theExpression,&theValue); /*============================================================*/ /* A multifield value can't be stored in a single field slot. */ /*============================================================*/ if ((slotPtr != NULL) ? (slotPtr->multislot == CLIPS_FALSE) && (theValue.type == MULTIFIELD) : CLIPS_FALSE) { MultiIntoSingleFieldSlotError(slotPtr,theDeftemplate); theValue.type = SYMBOL; theValue.value = CLIPSFalseSymbol; error = CLIPS_TRUE; } /*==============================*/ /* Store the value in the slot. */ /*==============================*/ theField[i].type = (short) theValue.type; theField[i].value = theValue.value; /*========================================*/ /* Get the information for the next slot. */ /*========================================*/ if (slotPtr != NULL) slotPtr = slotPtr->next; } /*============================================*/ /* If an error occured while generating the */ /* fact's slot values, then abort the assert. */ /*============================================*/ if (error) { ReturnFact(newFact); return; } /*================================*/ /* Add the fact to the fact-list. */ /*================================*/ theFact = (struct fact *) Assert((VOID *) newFact); /*========================================*/ /* The asserted fact is the return value. */ /*========================================*/ if (theFact != NULL) { SetpType(rv,FACT_ADDRESS); SetpValue(rv,(VOID *) theFact); } return; } /****************************************//* RetractCommand: CLIPS access routine *//* for the retract command. *//****************************************/globle VOID RetractCommand() { long int factIndex; struct fact *ptr; struct expr *theArgument; DATA_OBJECT theResult; int argNumber; /*================================*/ /* Iterate through each argument. */ /*================================*/ for (theArgument = GetFirstArgument(), argNumber = 1; theArgument != NULL; theArgument = GetNextArgument(theArgument), argNumber++) { /*========================*/ /* Evaluate the argument. */ /*========================*/ EvaluateExpression(theArgument,&theResult); /*===============================================*/ /* If the argument evaluates to an integer, then */ /* it's assumed to be the fact index of the fact */ /* to be retracted. */ /*===============================================*/ if (theResult.type == INTEGER) { /*==========================================*/ /* A fact index must be a positive integer. */ /*==========================================*/ factIndex = ValueToLong(theResult.value); if (factIndex < 0) { ExpectedTypeError1("retract",argNumber,"fact-address, fact-index, or the symbol *"); return; } /*================================================*/ /* See if a fact with the specified index exists. */ /*================================================*/ ptr = FindIndexedFact(factIndex); /*=====================================*/ /* If the fact exists then retract it, */ /* otherwise print an error message. */ /*=====================================*/ if (ptr != NULL) { Retract((VOID *) ptr); } else { char tempBuffer[20]; sprintf(tempBuffer,"f-%ld",factIndex); CantFindItemErrorMessage("fact",tempBuffer); } } /*===============================================*/ /* Otherwise if the argument evaluates to a fact */ /* address, we can directly retract it. */ /*===============================================*/ else if (theResult.type == FACT_ADDRESS) { Retract(theResult.value); } /*============================================*/ /* Otherwise if the argument evaluates to the */ /* symbol *, then all facts are retracted. */ /*============================================*/ else if ((theResult.type == SYMBOL) ? (strcmp(ValueToString(theResult.value),"*") == 0) : CLIPS_FALSE) { RemoveAllFacts(); return; } /*============================================*/ /* Otherwise the argument has evaluated to an */ /* illegal value for the retract command. */ /*============================================*/ else { ExpectedTypeError1("retract",argNumber,"fact-address, fact-index, or the symbol *"); SetEvaluationError(TRUE); } } }/***************************************************//* SetFactDuplicationCommand: CLIPS access routine *//* for the set-fact-duplication command. *//***************************************************/globle int SetFactDuplicationCommand() { int oldValue; DATA_OBJECT theValue; /*=====================================================*/ /* Get the old value of the fact duplication behavior. */ /*=====================================================*/ oldValue = GetFactDuplication(); /*============================================*/ /* Check for the correct number of arguments. */ /*============================================*/ if (ArgCountCheck("set-fact-duplication",EXACTLY,1) == -1) { return(oldValue); } /*========================*/ /* Evaluate the argument. */ /*========================*/ RtnUnknown(1,&theValue); /*===============================================================*/ /* If the argument evaluated to FALSE, then the fact duplication */ /* behavior is disabled, otherwise it is enabled. */ /*===============================================================*/ if ((theValue.value == CLIPSFalseSymbol) && (theValue.type == SYMBOL)) { SetFactDuplication(CLIPS_FALSE); } else { SetFactDuplication(CLIPS_TRUE); } /*========================================================*/ /* Return the old value of the fact duplication behavior. */ /*========================================================*/ return(oldValue); }/***************************************************//* GetFactDuplicationCommand: CLIPS access routine *//* for the get-fact-duplication command. *//***************************************************/globle int GetFactDuplicationCommand() { int currentValue; /*=========================================================*/ /* Get the current value of the fact duplication behavior. */ /*=========================================================*/ currentValue = GetFactDuplication(); /*============================================*/ /* Check for the correct number of arguments. */ /*============================================*/ if (ArgCountCheck("get-fact-duplication",EXACTLY,0) == -1) { return(currentValue); } /*============================================================*/ /* Return the current value of the fact duplication behavior. */ /*============================================================*/ return(currentValue); }/*******************************************//* FactIndexFunction: CLIPS access routine *//* for the fact-index function. *//*******************************************/globle long int FactIndexFunction() { DATA_OBJECT item;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -