/*************************************************** NAME : InitObjectPatternsCode DESCRIPTION : Prints out run-time initialization code for object patterns INPUTS : 1) A pointer to the output file 2) The id of constructs-to-c image 3) The maximum number of indices allowed in any single array in the image RETURNS : Nothing useful SIDE EFFECTS : Initialization code written NOTES : None ***************************************************/ static void InitObjectPatternsCode( void *theEnv, FILE *initFP, int imageID, int maxIndices) { long firstIntermediateNode,firstAlphaNode; if (ObjectNetworkPointer(theEnv) != NULL) { firstIntermediateNode = ObjectNetworkPointer(theEnv)->bsaveID; firstAlphaNode = ObjectNetworkTerminalPointer(theEnv)->bsaveID; fprintf(initFP," SetObjectNetworkPointer(theEnv,&%s%d_%d[%d]);\n", ObjectPNPrefix(),imageID, (int) ((firstIntermediateNode / maxIndices) + 1), (int) (firstIntermediateNode % maxIndices)); fprintf(initFP," SetObjectNetworkTerminalPointer(theEnv,&%s%d_%d[%d]);\n", ObjectANPrefix(),imageID, (int) ((firstAlphaNode / maxIndices) + 1), (int) (firstAlphaNode % maxIndices)); } else { fprintf(initFP," SetObjectNetworkPointer(theEnv,NULL);\n"); fprintf(initFP," SetObjectNetworkTerminalPointer(theEnv,NULL);\n"); } }
/*************************************************** NAME : PrintObjectPatternNetwork DESCRIPTION : Displays an indented printout of the object pattern network INPUTS : None RETURNS : Nothing useful SIDE EFFECTS : Object pattern network displayed NOTES : None ***************************************************/ globle void PrintObjectPatternNetwork() { char indentbuf[80]; indentbuf[0] = '\0'; PrintOPNLevel(ObjectNetworkPointer(),indentbuf,0); }
/*************************************************** NAME : PrintObjectPatternNetwork DESCRIPTION : Displays an indented printout of the object pattern network INPUTS : None RETURNS : Nothing useful SIDE EFFECTS : Object pattern network displayed NOTES : None ***************************************************/ globle void PrintObjectPatternNetwork( void *theEnv) { char indentbuf[80]; indentbuf[0] = '\0'; PrintOPNLevel(theEnv,ObjectNetworkPointer(theEnv),indentbuf,0); }
/***************************************************** NAME : BeforeObjectPatternsToCode DESCRIPTION : Marks all object pattern intermediate and alpha memory nodes with a unique integer id prior to the constructs-to-c execution INPUTS : None RETURNS : Nothing useful SIDE EFFECTS : bsaveIDs of nodes set NOTES : None *****************************************************/ static void BeforeObjectPatternsToCode( void *theEnv) { long whichPattern; OBJECT_PATTERN_NODE *intermediateNode; OBJECT_ALPHA_NODE *alphaNode; struct defmodule *theModule; DEFCLASS *theDefclass; CLASS_ALPHA_LINK *theLink; whichPattern = 0L; intermediateNode = ObjectNetworkPointer(theEnv); while (intermediateNode != NULL) { intermediateNode->bsaveID = whichPattern++; intermediateNode = GetNextObjectPatternNode(intermediateNode); } whichPattern = 0L; alphaNode = ObjectNetworkTerminalPointer(theEnv); while (alphaNode != NULL) { alphaNode->bsaveID = whichPattern++; alphaNode = alphaNode->nxtTerminal; } whichPattern = 0L; for (theModule = (struct defmodule *) EnvGetNextDefmodule(theEnv,NULL); theModule != NULL; theModule = (struct defmodule *) EnvGetNextDefmodule(theEnv,theModule)) { EnvSetCurrentModule(theEnv,(void *) theModule); for (theDefclass = (DEFCLASS *) EnvGetNextDefclass(theEnv,NULL) ; theDefclass != NULL ; theDefclass = (DEFCLASS *) EnvGetNextDefclass(theEnv,(void *) theDefclass)) { for (theLink = theDefclass->relevant_terminal_alpha_nodes; theLink != NULL; theLink = theLink->next) { theLink->bsaveID = whichPattern++; } } } }
/*************************************************** NAME : BsaveObjectPatternsFind DESCRIPTION : Sets the Bsave IDs for the object pattern data structures and determines how much space (including padding) is necessary for the alpha node bitmPS INPUTS : None RETURNS : Nothing useful SIDE EFFECTS : Counts written NOTES : None ***************************************************/ static void BsaveObjectPatternsFind( void *theEnv, EXEC_STATUS) { OBJECT_ALPHA_NODE *alphaPtr; OBJECT_PATTERN_NODE *patternPtr; SaveBloadCount(theEnv,execStatus,ObjectReteBinaryData(theEnv,execStatus)->AlphaNodeCount); SaveBloadCount(theEnv,execStatus,ObjectReteBinaryData(theEnv,execStatus)->PatternNodeCount); ObjectReteBinaryData(theEnv,execStatus)->AlphaNodeCount = 0L; alphaPtr = ObjectNetworkTerminalPointer(theEnv,execStatus); while (alphaPtr != NULL) { alphaPtr->classbmp->neededBitMap = TRUE; if (alphaPtr->slotbmp != NULL) alphaPtr->slotbmp->neededBitMap = TRUE; alphaPtr->bsaveID = ObjectReteBinaryData(theEnv,execStatus)->AlphaNodeCount++; alphaPtr = alphaPtr->nxtTerminal; } ObjectReteBinaryData(theEnv,execStatus)->PatternNodeCount = 0L; patternPtr = ObjectNetworkPointer(theEnv,execStatus); while (patternPtr != NULL) { patternPtr->bsaveID = ObjectReteBinaryData(theEnv,execStatus)->PatternNodeCount++; if (patternPtr->nextLevel == NULL) { while (patternPtr->rightNode == NULL) { patternPtr = patternPtr->lastLevel; if (patternPtr == NULL) return; } patternPtr = patternPtr->rightNode; } else patternPtr = patternPtr->nextLevel; } }
/*************************************************** NAME : BsaveObjectPatternsFind DESCRIPTION : Sets the Bsave IDs for the object pattern data structures and determines how much space (including padding) is necessary for the alpha node bitmPS INPUTS : None RETURNS : Nothing useful SIDE EFFECTS : Counts written NOTES : None ***************************************************/ static void BsaveObjectPatternsFind() { OBJECT_ALPHA_NODE *alphaPtr; OBJECT_PATTERN_NODE *patternPtr; if (Bloaded()) { SaveBloadCount(AlphaNodeCount); SaveBloadCount(PatternNodeCount); } AlphaNodeCount = 0L; alphaPtr = ObjectNetworkTerminalPointer(); while (alphaPtr != NULL) { alphaPtr->classbmp->neededBitMap = TRUE; if (alphaPtr->slotbmp != NULL) alphaPtr->slotbmp->neededBitMap = TRUE; alphaPtr->bsaveID = AlphaNodeCount++; alphaPtr = alphaPtr->nxtTerminal; } PatternNodeCount = 0L; patternPtr = ObjectNetworkPointer(); while (patternPtr != NULL) { patternPtr->bsaveID = PatternNodeCount++; if (patternPtr->nextLevel == NULL) { while (patternPtr->rightNode == NULL) { patternPtr = patternPtr->lastLevel; if (patternPtr == NULL) return; } patternPtr = patternPtr->rightNode; } else patternPtr = patternPtr->nextLevel; } }
/***************************************************** NAME : BeforeObjectPatternsToCode DESCRIPTION : Marks all object pattern intermediate and alpha memory nodes with a unique integer id prior to the constructs-to-c execution INPUTS : None RETURNS : Nothing useful SIDE EFFECTS : bsaveIDs of nodes set NOTES : None *****************************************************/ static void BeforeObjectPatternsToCode( void *theEnv) { long whichPattern; OBJECT_PATTERN_NODE *intermediateNode; OBJECT_ALPHA_NODE *alphaNode; whichPattern = 0L; intermediateNode = ObjectNetworkPointer(theEnv); while (intermediateNode != NULL) { intermediateNode->bsaveID = whichPattern++; intermediateNode = GetNextObjectPatternNode(intermediateNode); } whichPattern = 0L; alphaNode = ObjectNetworkTerminalPointer(theEnv); while (alphaNode != NULL) { alphaNode->bsaveID = whichPattern++; alphaNode = alphaNode->nxtTerminal; } }
/*************************************************** NAME : BsaveObjectPatterns DESCRIPTION : Writes ouyt object pattern data structures to binary save file INPUTS : Bsave file stream pointer RETURNS : Nothing useful SIDE EFFECTS : Data structures written NOTES : Extra padding written with alpha node bitmaps to ensure correct alignment of structues on bload ***************************************************/ static void BsaveObjectPatterns( void *theEnv, EXEC_STATUS, FILE *fp) { size_t space; OBJECT_ALPHA_NODE *alphaPtr; OBJECT_PATTERN_NODE *patternPtr; BSAVE_OBJECT_ALPHA_NODE dummyAlpha; BSAVE_OBJECT_PATTERN_NODE dummyPattern; space = (sizeof(BSAVE_OBJECT_ALPHA_NODE) * ObjectReteBinaryData(theEnv,execStatus)->AlphaNodeCount) + (sizeof(BSAVE_OBJECT_PATTERN_NODE) * ObjectReteBinaryData(theEnv,execStatus)->PatternNodeCount); GenWrite(&space,sizeof(size_t),fp); /* ========================================== Write out the alpha terminal pattern nodes ========================================== */ alphaPtr = ObjectNetworkTerminalPointer(theEnv,execStatus); while (alphaPtr != NULL) { AssignBsavePatternHeaderValues(theEnv,execStatus,&dummyAlpha.header,&alphaPtr->header); dummyAlpha.classbmp = (long) alphaPtr->classbmp->bucket; if (alphaPtr->slotbmp != NULL) dummyAlpha.slotbmp = (long) alphaPtr->slotbmp->bucket; else dummyAlpha.slotbmp = -1L; dummyAlpha.patternNode = BsaveObjectPatternIndex(alphaPtr->patternNode); dummyAlpha.nxtInGroup = BsaveObjectAlphaIndex(alphaPtr->nxtInGroup); dummyAlpha.nxtTerminal = BsaveObjectAlphaIndex(alphaPtr->nxtTerminal); GenWrite(&dummyAlpha,sizeof(BSAVE_OBJECT_ALPHA_NODE),fp); alphaPtr = alphaPtr->nxtTerminal; } /* ======================================== Write out the intermediate pattern nodes ======================================== */ patternPtr = ObjectNetworkPointer(theEnv,execStatus); while (patternPtr != NULL) { dummyPattern.multifieldNode = patternPtr->multifieldNode; dummyPattern.whichField = patternPtr->whichField; dummyPattern.leaveFields = patternPtr->leaveFields; dummyPattern.endSlot = patternPtr->endSlot; dummyPattern.selector = patternPtr->selector; dummyPattern.slotNameID = patternPtr->slotNameID; dummyPattern.networkTest = HashedExpressionIndex(theEnv,execStatus,patternPtr->networkTest); dummyPattern.nextLevel = BsaveObjectPatternIndex(patternPtr->nextLevel); dummyPattern.lastLevel = BsaveObjectPatternIndex(patternPtr->lastLevel); dummyPattern.leftNode = BsaveObjectPatternIndex(patternPtr->leftNode); dummyPattern.rightNode = BsaveObjectPatternIndex(patternPtr->rightNode); dummyPattern.alphaNode = BsaveObjectAlphaIndex(patternPtr->alphaNode); GenWrite(&dummyPattern,sizeof(BSAVE_OBJECT_PATTERN_NODE),fp); if (patternPtr->nextLevel == NULL) { while (patternPtr->rightNode == NULL) { patternPtr = patternPtr->lastLevel; if (patternPtr == NULL) { RestoreBloadCount(theEnv,execStatus,&ObjectReteBinaryData(theEnv,execStatus)->AlphaNodeCount); RestoreBloadCount(theEnv,execStatus,&ObjectReteBinaryData(theEnv,execStatus)->PatternNodeCount); return; } } patternPtr = patternPtr->rightNode; } else patternPtr = patternPtr->nextLevel; } RestoreBloadCount(theEnv,execStatus,&ObjectReteBinaryData(theEnv,execStatus)->AlphaNodeCount); RestoreBloadCount(theEnv,execStatus,&ObjectReteBinaryData(theEnv,execStatus)->PatternNodeCount); }
/************************************************************* NAME : IntermediatePatternNodesToCode DESCRIPTION : Writes out data structures for run-time creation of object pattern intermediate nodes INPUTS : 1) The base image output file name 2) The base image file id 3) A pointer to the header output file 4) The id of constructs-to-c image 5) The maximum number of indices allowed in any single array in the image RETURNS : Next version file to open, 0 if error SIDE EFFECTS : Object patterns code written to files NOTES : None *************************************************************/ static int IntermediatePatternNodesToCode( void *theEnv, char *fileName, int fileID, FILE *headerFP, int imageID, int maxIndices, int version) { FILE *fp; int arrayVersion; int newHeader; int i; OBJECT_PATTERN_NODE *thePattern; /* ================ Create the file. ================ */ if (ObjectNetworkPointer(theEnv) == NULL) return(1); fprintf(headerFP,"#include \"objrtmch.h\"\n"); /* ================================= Dump the pattern node structures. ================================= */ if ((fp = NewCFile(theEnv,fileName,fileID,version,FALSE)) == NULL) return(0); newHeader = TRUE; arrayVersion = 1; i = 1; thePattern = ObjectNetworkPointer(theEnv); while (thePattern != NULL) { if (newHeader) { fprintf(fp,"OBJECT_PATTERN_NODE %s%d_%d[] = {\n", ObjectPNPrefix(),imageID,arrayVersion); fprintf(headerFP,"extern OBJECT_PATTERN_NODE %s%d_%d[];\n", ObjectPNPrefix(),imageID,arrayVersion); newHeader = FALSE; } fprintf(fp,"{0,%u,%u,%u,%u,0L,%u,",thePattern->multifieldNode, thePattern->endSlot, thePattern->whichField, thePattern->leaveFields, thePattern->slotNameID); PrintHashedExpressionReference(theEnv,fp,thePattern->networkTest,imageID,maxIndices); fprintf(fp,","); IntermediatePatternNodeReference(theEnv,thePattern->nextLevel,fp,imageID,maxIndices); fprintf(fp,","); IntermediatePatternNodeReference(theEnv,thePattern->lastLevel,fp,imageID,maxIndices); fprintf(fp,","); IntermediatePatternNodeReference(theEnv,thePattern->leftNode,fp,imageID,maxIndices); fprintf(fp,","); IntermediatePatternNodeReference(theEnv,thePattern->rightNode,fp,imageID,maxIndices); fprintf(fp,","); ObjectPatternNodeReference(theEnv,(void *) thePattern->alphaNode,fp,imageID,maxIndices); fprintf(fp,",0L}"); i++; thePattern = GetNextObjectPatternNode(thePattern); if ((i > maxIndices) || (thePattern == NULL)) { fprintf(fp,"};\n"); GenClose(fp); i = 1; version++; arrayVersion++; if (thePattern != NULL) { if ((fp = NewCFile(theEnv,fileName,fileID,version,FALSE)) == NULL) return(0); newHeader = TRUE; } } else if (thePattern != NULL) { fprintf(fp,",\n"); } } return(version); }
/*************************************************** NAME : BsaveObjectPatterns DESCRIPTION : Writes ouyt object pattern data structures to binary save file INPUTS : Bsave file stream pointer RETURNS : Nothing useful SIDE EFFECTS : Data structures written NOTES : Extra padding written with alpha node bitmaps to ensure correct alignment of structues on bload ***************************************************/ static void BsaveObjectPatterns( FILE *fp) { UNLN space; OBJECT_ALPHA_NODE *alphaPtr; OBJECT_PATTERN_NODE *patternPtr; BSAVE_OBJECT_ALPHA_NODE dummyAlpha; BSAVE_OBJECT_PATTERN_NODE dummyPattern; space = (sizeof(BSAVE_OBJECT_ALPHA_NODE) * AlphaNodeCount) + (sizeof(BSAVE_OBJECT_PATTERN_NODE) * PatternNodeCount); GenWrite(&space,(UNLN) sizeof(UNLN),fp); /* ========================================== Write out the alpha terminal pattern nodes ========================================== */ alphaPtr = ObjectNetworkTerminalPointer(); while (alphaPtr != NULL) { AssignBsavePatternHeaderValues(&dummyAlpha.header,&alphaPtr->header); dummyAlpha.classbmp = alphaPtr->classbmp->bucket; if (alphaPtr->slotbmp != NULL) dummyAlpha.slotbmp = alphaPtr->slotbmp->bucket; else dummyAlpha.slotbmp = -1L; dummyAlpha.patternNode = BsaveObjectPatternIndex(alphaPtr->patternNode); dummyAlpha.nxtInGroup = BsaveObjectAlphaIndex(alphaPtr->nxtInGroup); dummyAlpha.nxtTerminal = BsaveObjectAlphaIndex(alphaPtr->nxtTerminal); GenWrite(&dummyAlpha,(UNLN) sizeof(BSAVE_OBJECT_ALPHA_NODE),fp); alphaPtr = alphaPtr->nxtTerminal; } /* ======================================== Write out the intermediate pattern nodes ======================================== */ patternPtr = ObjectNetworkPointer(); while (patternPtr != NULL) { dummyPattern.multifieldNode = patternPtr->multifieldNode; dummyPattern.whichField = patternPtr->whichField; dummyPattern.leaveFields = patternPtr->leaveFields; dummyPattern.endSlot = patternPtr->endSlot; dummyPattern.slotNameID = patternPtr->slotNameID; dummyPattern.networkTest = HashedExpressionIndex(patternPtr->networkTest); dummyPattern.nextLevel = BsaveObjectPatternIndex(patternPtr->nextLevel); dummyPattern.lastLevel = BsaveObjectPatternIndex(patternPtr->lastLevel); dummyPattern.leftNode = BsaveObjectPatternIndex(patternPtr->leftNode); dummyPattern.rightNode = BsaveObjectPatternIndex(patternPtr->rightNode); dummyPattern.alphaNode = BsaveObjectAlphaIndex(patternPtr->alphaNode); GenWrite(&dummyPattern,(UNLN) sizeof(BSAVE_OBJECT_PATTERN_NODE),fp); if (patternPtr->nextLevel == NULL) { while (patternPtr->rightNode == NULL) { patternPtr = patternPtr->lastLevel; if (patternPtr == NULL) return; } patternPtr = patternPtr->rightNode; } else patternPtr = patternPtr->nextLevel; } if (Bloaded()) { RestoreBloadCount(&AlphaNodeCount); RestoreBloadCount(&PatternNodeCount); } }