?? objrtbld.c
字號:
/*******************************************************/ /* "C" Language Integrated Production System */ /* */ /* CLIPS Version 6.30 10/19/06 */ /* */ /* OBJECT PATTERN MATCHER MODULE */ /*******************************************************//**************************************************************//* Purpose: RETE Network Parsing Interface for Objects *//* *//* Principal Programmer(s): *//* Brian L. Donnell *//* *//* Contributing Programmer(s): *//* *//* Revision History: *//* *//* 6.24: Removed INCREMENTAL_RESET compilation flag. *//* *//* Converted INSTANCE_PATTERN_MATCHING to *//* DEFRULE_CONSTRUCT. *//* *//* Renamed BOOLEAN macro type to intBool. *//* *//* 6.30: Added support for hashed alpha memories. *//* *//**************************************************************//* ========================================= ***************************************** EXTERNAL DEFINITIONS ========================================= ***************************************** */#include "setup.h"#if DEFRULE_CONSTRUCT && OBJECT_SYSTEM#if (! BLOAD_ONLY) && (! RUN_TIME)#include <string.h>#include <stdlib.h>#include "classcom.h"#include "classfun.h"#include "cstrnutl.h"#include "constrnt.h"#include "cstrnchk.h"#include "cstrnops.h"#include "drive.h"#include "envrnmnt.h"#include "exprnpsr.h"#include "inscom.h"#include "insfun.h"#include "insmngr.h"#include "memalloc.h"#include "network.h"#include "object.h"#include "pattern.h"#include "reteutil.h"#include "ruledef.h"#include "rulepsr.h"#include "scanner.h"#include "symbol.h"#include "utility.h"#endif#include "constrct.h"#include "objrtmch.h"#include "objrtgen.h"#include "objrtfnx.h"#include "reorder.h"#include "router.h"#if CONSTRUCT_COMPILER && (! RUN_TIME)#include "objrtcmp.h"#endif#if BLOAD_AND_BSAVE || BLOAD || BLOAD_ONLY#include "objrtbin.h"#endif#define _OBJRTBLD_SOURCE_#include "objrtbld.h"#if ! DEFINSTANCES_CONSTRUCT#include "extnfunc.h"#include "classfun.h"#include "classcom.h"#endif#if (! BLOAD_ONLY) && (! RUN_TIME)/* ========================================= ***************************************** CONSTANTS ========================================= ***************************************** */#define OBJECT_PATTERN_INDICATOR "object"/* ========================================= ***************************************** INTERNALLY VISIBLE FUNCTION HEADERS ========================================= ***************************************** */static intBool PatternParserFind(SYMBOL_HN *);static struct lhsParseNode *ObjectLHSParse(void *,char *,struct token *);static intBool ReorderAndAnalyzeObjectPattern(void *,struct lhsParseNode *);static struct patternNodeHeader *PlaceObjectPattern(void *,struct lhsParseNode *);static OBJECT_PATTERN_NODE *FindObjectPatternNode(OBJECT_PATTERN_NODE *,struct lhsParseNode *, OBJECT_PATTERN_NODE **,unsigned,unsigned);static OBJECT_PATTERN_NODE *CreateNewObjectPatternNode(void *,struct lhsParseNode *,OBJECT_PATTERN_NODE *, OBJECT_PATTERN_NODE *,unsigned,unsigned);static void DetachObjectPattern(void *,struct patternNodeHeader *);static void ClearObjectPatternMatches(void *,OBJECT_ALPHA_NODE *);static void RemoveObjectPartialMatches(void *,INSTANCE_TYPE *,struct patternNodeHeader *);static intBool CheckDuplicateSlots(void *,struct lhsParseNode *,SYMBOL_HN *);static struct lhsParseNode *ParseClassRestriction(void *,char *,struct token *);static struct lhsParseNode *ParseNameRestriction(void *,char *,struct token *);static struct lhsParseNode *ParseSlotRestriction(void *,char *,struct token *,CONSTRAINT_RECORD *,int);static CLASS_BITMAP *NewClassBitMap(void *,int,int);static void InitializeClassBitMap(void *,CLASS_BITMAP *,int);static void DeleteIntermediateClassBitMap(void *,CLASS_BITMAP *);static void *CopyClassBitMap(void *,void *);static void DeleteClassBitMap(void *,void *);static void MarkBitMapClassesBusy(void *,BITMAP_HN *,int);static intBool EmptyClassBitMap(CLASS_BITMAP *);static intBool IdenticalClassBitMap(CLASS_BITMAP *,CLASS_BITMAP *);static intBool ProcessClassRestriction(void *,CLASS_BITMAP *,struct lhsParseNode **,int);static CONSTRAINT_RECORD *ProcessSlotRestriction(void *,CLASS_BITMAP *,SYMBOL_HN *,int *);static void IntersectClassBitMaps(CLASS_BITMAP *,CLASS_BITMAP *);static void UnionClassBitMaps(CLASS_BITMAP *,CLASS_BITMAP *);static CLASS_BITMAP *PackClassBitMap(void *,CLASS_BITMAP *);static struct lhsParseNode *FilterObjectPattern(void *,struct patternParser *, struct lhsParseNode *,struct lhsParseNode **, struct lhsParseNode **,struct lhsParseNode **);static BITMAP_HN *FormSlotBitMap(void *,struct lhsParseNode *);static struct lhsParseNode *RemoveSlotExistenceTests(void *,struct lhsParseNode *,BITMAP_HN **);static struct lhsParseNode *CreateInitialObjectPattern(void *);static EXPRESSION *ObjectMatchDelayParse(void *,EXPRESSION *,char *);static void MarkObjectPtnIncrementalReset(void *,struct patternNodeHeader *,int);static void ObjectIncrementalReset(void *);#endif#if ! DEFINSTANCES_CONSTRUCTstatic void ResetInitialObject(void *);#endif/* ========================================= ***************************************** EXTERNALLY VISIBLE FUNCTIONS ========================================= ***************************************** *//******************************************************** NAME : SetupObjectPatternStuff DESCRIPTION : Installs the parsers and other items necessary for recognizing and processing object patterns in defrules INPUTS : None RETURNS : Nothing useful SIDE EFFECTS : Rete network interfaces for objects initialized NOTES : None ********************************************************/globle void SetupObjectPatternStuff( void *theEnv) {#if (! BLOAD_ONLY) && (! RUN_TIME) struct patternParser *newPtr; if (ReservedPatternSymbol(theEnv,"object",NULL) == TRUE) { SystemError(theEnv,"OBJRTBLD",1); EnvExitRouter(theEnv,EXIT_FAILURE); } AddReservedPatternSymbol(theEnv,"object",NULL); /* =========================================================================== The object pattern parser needs to have a higher priority than deftemplates or regular facts so that the "object" keyword is always recognized first =========================================================================== */ newPtr = get_struct(theEnv,patternParser); newPtr->name = "objects"; newPtr->priority = 20; newPtr->entityType = &InstanceData(theEnv)->InstanceInfo; newPtr->recognizeFunction = PatternParserFind; newPtr->parseFunction = ObjectLHSParse; newPtr->postAnalysisFunction = ReorderAndAnalyzeObjectPattern; newPtr->addPatternFunction = PlaceObjectPattern; newPtr->removePatternFunction = DetachObjectPattern; newPtr->genJNConstantFunction = NULL; newPtr->replaceGetJNValueFunction = ReplaceGetJNObjectValue; newPtr->genGetJNValueFunction = GenGetJNObjectValue; newPtr->genCompareJNValuesFunction = ObjectJNVariableComparison; newPtr->genPNConstantFunction = GenObjectPNConstantCompare; newPtr->replaceGetPNValueFunction = ReplaceGetPNObjectValue; newPtr->genGetPNValueFunction = GenGetPNObjectValue; newPtr->genComparePNValuesFunction = ObjectPNVariableComparison; newPtr->returnUserDataFunction = DeleteClassBitMap; newPtr->copyUserDataFunction = CopyClassBitMap; newPtr->markIRPatternFunction = MarkObjectPtnIncrementalReset; newPtr->incrementalResetFunction = ObjectIncrementalReset; newPtr->initialPatternFunction = CreateInitialObjectPattern;#if CONSTRUCT_COMPILER && (! RUN_TIME) newPtr->codeReferenceFunction = ObjectPatternNodeReference;#else newPtr->codeReferenceFunction = NULL;#endif AddPatternParser(theEnv,newPtr); EnvDefineFunction2(theEnv,"object-pattern-match-delay",'u', PTIEF ObjectMatchDelay,"ObjectMatchDelay",NULL); AddFunctionParser(theEnv,"object-pattern-match-delay",ObjectMatchDelayParse); FuncSeqOvlFlags(theEnv,"object-pattern-match-delay",FALSE,FALSE);#endif InstallObjectPrimitives(theEnv);#if CONSTRUCT_COMPILER && (! RUN_TIME) ObjectPatternsCompilerSetup(theEnv);#endif#if ! DEFINSTANCES_CONSTRUCT EnvAddResetFunction(theEnv,"reset-initial-object",ResetInitialObject,0);#endif#if BLOAD_AND_BSAVE || BLOAD || BLOAD_ONLY SetupObjectPatternsBload(theEnv);#endif }/* ========================================= ***************************************** INTERNALLY VISIBLE FUNCTIONS ========================================= ***************************************** */#if ! DEFINSTANCES_CONSTRUCTstatic void ResetInitialObject( void *theEnv) { EXPRESSION *tmp; DATA_OBJECT rtn; tmp = GenConstant(theEnv,FCALL,(void *) FindFunction(theEnv,"make-instance")); tmp->argList = GenConstant(theEnv,INSTANCE_NAME,(void *) DefclassData(theEnv)->INITIAL_OBJECT_SYMBOL); tmp->argList->nextArg = GenConstant(theEnv,DEFCLASS_PTR,(void *) LookupDefclassInScope(theEnv,INITIAL_OBJECT_CLASS_NAME)); EvaluateExpression(theEnv,tmp,&rtn); ReturnExpression(theEnv,tmp); }#endif#if (! BLOAD_ONLY) && (! RUN_TIME)/***************************************************** NAME : PatternParserFind DESCRIPTION : Determines if a pattern CE is an object pattern (i.e. the first field is the constant symbol "object") INPUTS : 1) The type of the first field 2) The value of the first field RETURNS : TRUE if it is an object pattern, FALSE otherwise SIDE EFFECTS : None NOTES : Used by AddPatternParser() *****************************************************/static intBool PatternParserFind( SYMBOL_HN *value) { if (strcmp(ValueToString(value),OBJECT_PATTERN_INDICATOR) == 0) return(TRUE); return(FALSE); }/************************************************************************************ NAME : ObjectLHSParse DESCRIPTION : Scans and parses an object pattern for a rule INPUTS : 1) The logical name of the input source 2) A buffer holding the last token read RETURNS : The address of struct lhsParseNodes, NULL on errors SIDE EFFECTS : A series of struct lhsParseNodes are created to represent the intermediate parse of the pattern Pretty-print form for the pattern is saved NOTES : Object Pattern Syntax: (object [<class-constraint>] [<name-constraint>] <slot-constraint>*) <class-constraint> ::= (is-a <constraint>) <name-constraint> ::= (name <constraint>) <slot-constraint> ::= (<slot-name> <constraint>*) ************************************************************************************/#if IBM_TBC#pragma argsused#endifstatic struct lhsParseNode *ObjectLHSParse( void *theEnv, char *readSource, struct token *lastToken) {#if MAC_MCW || IBM_MCW || MAC_XCD#pragma unused(lastToken)#endif struct token theToken; struct lhsParseNode *firstNode = NULL,*lastNode = NULL,*tmpNode; CLASS_BITMAP *clsset,*tmpset; CONSTRAINT_RECORD *slotConstraints; int ppbackupReqd = FALSE,multip; /* ======================================================== Get a bitmap big enough to mark the ids of all currently existing classes - and set all bits, since the initial set of applicable classes is everything. ======================================================== */ clsset = NewClassBitMap(theEnv,((int) DefclassData(theEnv)->MaxClassID) - 1,1); if (EmptyClassBitMap(clsset)) { PrintErrorID(theEnv,"OBJRTBLD",1,FALSE); EnvPrintRouter(theEnv,WERROR,"No objects of existing classes can satisfy pattern.\n"); DeleteIntermediateClassBitMap(theEnv,clsset); return(NULL); } tmpset = NewClassBitMap(theEnv,((int) DefclassData(theEnv)->MaxClassID) - 1,1); IncrementIndentDepth(theEnv,7); /* =========================================== Parse the class, name and slot restrictions =========================================== */ GetToken(theEnv,readSource,&theToken); while (theToken.type != RPAREN) { ppbackupReqd = TRUE; PPBackup(theEnv); SavePPBuffer(theEnv," "); SavePPBuffer(theEnv,theToken.printForm); if (theToken.type != LPAREN)
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -