void PrintFact(Factor *f) { if (f->left) { putchar('('); PrintFact(f->left); printf(" x "); PrintFact(f->right); putchar(')'); } else { printf("A%d", f->me); } }
void Print(int combine[], int n) { Factor **factors, *newf; int i, j; factors = (Factor**)malloc(sizeof(Factor*) * n); for (i=0; i<n; i++) { factors[i] = (Factor*)malloc(sizeof(Factor)); factors[i]->left = factors[i]->right = 0; factors[i]->me = i+1; } for (i=0; i<n-1; i++) { newf = (Factor*)malloc(sizeof(Factor)); newf->left = factors[combine[i]]; newf->right = factors[combine[i]+1]; factors[combine[i]] = newf; for (j=combine[i]+1; j<n-1; j++) factors[j] = factors[j+1]; } printf("Case %d: ", ++case_no); PrintFact(factors[0]); putchar('\n'); FreeFact(factors[0]); }
globle void PrintFactWithIdentifier( char *logicalName, struct fact *factPtr) { char printSpace[20]; sprintf(printSpace,"f-%-5ld ",factPtr->factIndex); PrintRouter(logicalName,printSpace); PrintFact(logicalName,factPtr); }
static void PatternNetErrorMessage( void *theEnv, struct factPatternNode *patternPtr) { char buffer[60]; struct templateSlot *theSlots; int i; /*=======================================*/ /* Print the fact being pattern matched. */ /*=======================================*/ PrintErrorID(theEnv,"FACTMCH",1,TRUE); EnvPrintRouter(theEnv,WERROR,"This error occurred in the fact pattern network\n"); EnvPrintRouter(theEnv,WERROR," Currently active fact: "); PrintFact(theEnv,WERROR,FactData(theEnv)->CurrentPatternFact,FALSE,FALSE); EnvPrintRouter(theEnv,WERROR,"\n"); /*==============================================*/ /* Print the field position or slot name of the */ /* pattern from which the error originated. */ /*==============================================*/ if (FactData(theEnv)->CurrentPatternFact->whichDeftemplate->implied) { sprintf(buffer," Problem resides in field #%d\n",patternPtr->whichField); } else { theSlots = FactData(theEnv)->CurrentPatternFact->whichDeftemplate->slotList; for (i = 0; i < (int) patternPtr->whichSlot; i++) theSlots = theSlots->next; sprintf(buffer," Problem resides in slot %s\n",ValueToString(theSlots->slotName)); } EnvPrintRouter(theEnv,WERROR,buffer); /*==========================================================*/ /* Trace the pattern to its entry point to the join network */ /* (which then traces to the defrule data structure so that */ /* the name(s) of the rule(s) utilizing the patterns can be */ /* printed). */ /*==========================================================*/ TraceErrorToJoin(theEnv,patternPtr,FALSE); EnvPrintRouter(theEnv,WERROR,"\n"); }
globle void EnvPPFact( void *theEnv, void *vTheFact, const char *logicalName, int ignoreDefaults) { #if MAC_XCD #pragma unused(theEnv) #endif struct fact *theFact = (struct fact *) vTheFact; if (theFact == NULL) return; if (theFact->garbage) return; PrintFact(theEnv,logicalName,theFact,TRUE,ignoreDefaults); EnvPrintRouter(theEnv,logicalName,"\n"); }
globle intBool EnvSaveFacts( void *theEnv, char *fileName, int saveCode, struct expr *theList) { 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,(char*)"w")) == NULL) { OpenErrorMessage(theEnv,(char*)"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,(char*)"\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); }