00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 #define _FACTMNGR_SOURCE_
00039
00040 #include <stdio.h>
00041 #define _STDIO_INCLUDED_
00042
00043 #include "setup.h"
00044
00045 #if DEFTEMPLATE_CONSTRUCT && DEFRULE_CONSTRUCT
00046
00047 #include "constant.h"
00048 #include "symbol.h"
00049 #include "memalloc.h"
00050 #include "exprnpsr.h"
00051 #include "argacces.h"
00052 #include "scanner.h"
00053 #include "router.h"
00054 #include "strngrtr.h"
00055 #include "match.h"
00056 #include "factbld.h"
00057 #include "factqury.h"
00058 #include "reteutil.h"
00059 #include "retract.h"
00060 #include "factcmp.h"
00061 #include "filecom.h"
00062 #include "factfun.h"
00063 #include "factcom.h"
00064 #include "constrct.h"
00065 #include "factrhs.h"
00066 #include "factmch.h"
00067 #include "watch.h"
00068 #include "utility.h"
00069 #include "factbin.h"
00070 #include "factmngr.h"
00071 #include "facthsh.h"
00072 #include "default.h"
00073 #include "commline.h"
00074 #include "envrnmnt.h"
00075 #include "sysdep.h"
00076
00077 #include "engine.h"
00078 #include "lgcldpnd.h"
00079 #include "drive.h"
00080 #include "ruledlt.h"
00081
00082 #include "tmpltbsc.h"
00083 #include "tmpltdef.h"
00084 #include "tmpltutl.h"
00085 #include "tmpltfun.h"
00086
00087
00088
00089
00090
00091 static void ResetFacts(void *);
00092 static int ClearFactsReady(void *);
00093 static void RemoveGarbageFacts(void *);
00094 static void DeallocateFactData(void *);
00095
00096
00097
00098
00099
00100
00101 globle void InitializeFacts(
00102 void *theEnv)
00103 {
00104 struct patternEntityRecord factInfo = { { "FACT_ADDRESS", FACT_ADDRESS,1,0,0,
00105 PrintFactIdentifier,
00106 PrintFactIdentifierInLongForm,
00107 EnvRetract,
00108 NULL,
00109 EnvGetNextFact,
00110 EnvIncrementFactCount,
00111 EnvDecrementFactCount,NULL,NULL,NULL,NULL,NULL
00112 },
00113 DecrementFactBasisCount,
00114 IncrementFactBasisCount,
00115 MatchFactFunction,
00116 NULL
00117 };
00118
00119 struct fact dummyFact = { { NULL, NULL, 0, 0L }, NULL, NULL, -1L, 0, 0, 1,
00120 NULL, NULL, NULL, NULL, { 1, 0, 0UL, NULL, { { 0, NULL } } } };
00121
00122 AllocateEnvironmentData(theEnv,FACTS_DATA,sizeof(struct factsData),DeallocateFactData);
00123
00124 memcpy(&FactData(theEnv)->FactInfo,&factInfo,sizeof(struct patternEntityRecord));
00125 dummyFact.factHeader.theInfo = &FactData(theEnv)->FactInfo;
00126 memcpy(&FactData(theEnv)->DummyFact,&dummyFact,sizeof(struct fact));
00127 FactData(theEnv)->LastModuleIndex = -1;
00128
00129
00130
00131
00132
00133
00134 InitializeFactHashTable(theEnv);
00135
00136
00137
00138
00139
00140
00141 EnvAddResetFunction(theEnv,"facts",ResetFacts,60);
00142 AddClearReadyFunction(theEnv,"facts",ClearFactsReady,0);
00143
00144
00145
00146
00147
00148
00149 AddCleanupFunction(theEnv,"facts",RemoveGarbageFacts,0);
00150
00151
00152
00153
00154
00155 InitializeFactPatterns(theEnv);
00156
00157
00158
00159
00160
00161
00162 #if DEBUGGING_FUNCTIONS
00163 AddWatchItem(theEnv,"facts",0,&FactData(theEnv)->WatchFacts,80,DeftemplateWatchAccess,DeftemplateWatchPrint);
00164 #endif
00165
00166
00167
00168
00169
00170 FactCommandDefinitions(theEnv);
00171 FactFunctionDefinitions(theEnv);
00172
00173
00174
00175
00176
00177 #if FACT_SET_QUERIES
00178 SetupFactQuery(theEnv);
00179 #endif
00180
00181
00182
00183
00184
00185
00186 #if (BLOAD || BLOAD_ONLY || BLOAD_AND_BSAVE) && (! RUN_TIME)
00187 FactBinarySetup(theEnv);
00188 #endif
00189
00190
00191
00192
00193
00194
00195 #if CONSTRUCT_COMPILER && (! RUN_TIME)
00196 FactPatternsCompilerSetup(theEnv);
00197 #endif
00198 }
00199
00200
00201
00202
00203
00204 static void DeallocateFactData(
00205 void *theEnv)
00206 {
00207 struct factHashEntry *tmpFHEPtr, *nextFHEPtr;
00208 struct fact *tmpFactPtr, *nextFactPtr;
00209 unsigned long i;
00210 struct patternMatch *theMatch, *tmpMatch;
00211
00212 for (i = 0; i < FactData(theEnv)->FactHashTableSize; i++)
00213 {
00214 tmpFHEPtr = FactData(theEnv)->FactHashTable[i];
00215
00216 while (tmpFHEPtr != NULL)
00217 {
00218 nextFHEPtr = tmpFHEPtr->next;
00219 rtn_struct(theEnv,factHashEntry,tmpFHEPtr);
00220 tmpFHEPtr = nextFHEPtr;
00221 }
00222 }
00223
00224 rm3(theEnv,FactData(theEnv)->FactHashTable,
00225 sizeof(struct factHashEntry *) * FactData(theEnv)->FactHashTableSize);
00226
00227 tmpFactPtr = FactData(theEnv)->FactList;
00228 while (tmpFactPtr != NULL)
00229 {
00230 nextFactPtr = tmpFactPtr->nextFact;
00231
00232 theMatch = (struct patternMatch *) tmpFactPtr->list;
00233 while (theMatch != NULL)
00234 {
00235 tmpMatch = theMatch->next;
00236 rtn_struct(theEnv,patternMatch,theMatch);
00237 theMatch = tmpMatch;
00238 }
00239
00240 ReturnEntityDependencies(theEnv,(struct patternEntity *) tmpFactPtr);
00241
00242 ReturnFact(theEnv,tmpFactPtr);
00243 tmpFactPtr = nextFactPtr;
00244 }
00245
00246 tmpFactPtr = FactData(theEnv)->GarbageFacts;
00247 while (tmpFactPtr != NULL)
00248 {
00249 nextFactPtr = tmpFactPtr->nextFact;
00250
00251 ReturnFact(theEnv,tmpFactPtr);
00252 tmpFactPtr = nextFactPtr;
00253 }
00254 }
00255
00256
00257
00258
00259
00260 globle void PrintFactWithIdentifier(
00261 void *theEnv,
00262 char *logicalName,
00263 struct fact *factPtr)
00264 {
00265 char printSpace[20];
00266
00267 gensprintf(printSpace,"f-%-5lld ",factPtr->factIndex);
00268 EnvPrintRouter(theEnv,logicalName,printSpace);
00269 PrintFact(theEnv,logicalName,factPtr,FALSE,FALSE);
00270 }
00271
00272
00273
00274
00275 globle void PrintFactIdentifier(
00276 void *theEnv,
00277 char *logicalName,
00278 void *factPtr)
00279 {
00280 char printSpace[20];
00281
00282 gensprintf(printSpace,"f-%lld",((struct fact *) factPtr)->factIndex);
00283 EnvPrintRouter(theEnv,logicalName,printSpace);
00284 }
00285
00286
00287
00288
00289
00290 globle void PrintFactIdentifierInLongForm(
00291 void *theEnv,
00292 char *logicalName,
00293 void *factPtr)
00294 {
00295 if (PrintUtilityData(theEnv)->AddressesToStrings) EnvPrintRouter(theEnv,logicalName,"\"");
00296 if (factPtr != (void *) &FactData(theEnv)->DummyFact)
00297 {
00298 EnvPrintRouter(theEnv,logicalName,"<Fact-");
00299 PrintLongInteger(theEnv,logicalName,((struct fact *) factPtr)->factIndex);
00300 EnvPrintRouter(theEnv,logicalName,">");
00301 }
00302 else
00303 { EnvPrintRouter(theEnv,logicalName,"<Dummy Fact>"); }
00304
00305 if (PrintUtilityData(theEnv)->AddressesToStrings) EnvPrintRouter(theEnv,logicalName,"\"");
00306 }
00307
00308
00309
00310
00311
00312 globle void DecrementFactBasisCount(
00313 void *theEnv,
00314 void *vFactPtr)
00315 {
00316 struct fact *factPtr = (struct fact *) vFactPtr;
00317 struct multifield *theSegment;
00318 int i;
00319
00320 EnvDecrementFactCount(theEnv,factPtr);
00321
00322 theSegment = &factPtr->theProposition;
00323
00324 for (i = 0 ; i < (int) theSegment->multifieldLength ; i++)
00325 {
00326 AtomDeinstall(theEnv,theSegment->theFields[i].type,theSegment->theFields[i].value);
00327 }
00328 }
00329
00330
00331
00332
00333
00334 globle void IncrementFactBasisCount(
00335 void *theEnv,
00336 void *vFactPtr)
00337 {
00338 struct fact *factPtr = (struct fact *) vFactPtr;
00339 struct multifield *theSegment;
00340 int i;
00341
00342 EnvIncrementFactCount(theEnv,factPtr);
00343
00344 theSegment = &factPtr->theProposition;
00345
00346 for (i = 0 ; i < (int) theSegment->multifieldLength ; i++)
00347 {
00348 AtomInstall(theEnv,theSegment->theFields[i].type,theSegment->theFields[i].value);
00349 }
00350 }
00351
00352
00353
00354
00355
00356
00357 globle void PrintFact(
00358 void *theEnv,
00359 char *logicalName,
00360 struct fact *factPtr,
00361 int seperateLines,
00362 int ignoreDefaults)
00363 {
00364 struct multifield *theMultifield;
00365
00366
00367
00368
00369
00370 if (factPtr->whichDeftemplate->implied == FALSE)
00371 {
00372 PrintTemplateFact(theEnv,logicalName,factPtr,seperateLines,ignoreDefaults);
00373 return;
00374 }
00375
00376
00377
00378
00379
00380
00381 EnvPrintRouter(theEnv,logicalName,"(");
00382
00383 EnvPrintRouter(theEnv,logicalName,factPtr->whichDeftemplate->header.name->contents);
00384
00385 theMultifield = (struct multifield *) factPtr->theProposition.theFields[0].value;
00386 if (theMultifield->multifieldLength != 0)
00387 {
00388 EnvPrintRouter(theEnv,logicalName," ");
00389 PrintMultifield(theEnv,logicalName,theMultifield,0,
00390 (long) (theMultifield->multifieldLength - 1),
00391 FALSE);
00392 }
00393
00394 EnvPrintRouter(theEnv,logicalName,")");
00395 }
00396
00397
00398
00399
00400
00401 globle void MatchFactFunction(
00402 void *theEnv,
00403 void *vTheFact)
00404 {
00405 struct fact *theFact = (struct fact *) vTheFact;
00406
00407 FactPatternMatch(theEnv,theFact,theFact->whichDeftemplate->patternNetwork,0,NULL,NULL);
00408 }
00409
00410
00411
00412
00413 globle intBool EnvRetract(
00414 void *theEnv,
00415 void *vTheFact)
00416 {
00417 struct fact *theFact = (struct fact *) vTheFact;
00418 struct deftemplate *theTemplate = theFact->whichDeftemplate;
00419
00420
00421
00422
00423
00424
00425 if (EngineData(theEnv)->JoinOperationInProgress)
00426 {
00427 PrintErrorID(theEnv,"FACTMNGR",1,TRUE);
00428 EnvPrintRouter(theEnv,WERROR,"Facts may not be retracted during pattern-matching\n");
00429 return(FALSE);
00430 }
00431
00432
00433
00434
00435
00436
00437 if (theFact == NULL)
00438 {
00439 RemoveAllFacts(theEnv);
00440 return(TRUE);
00441 }
00442
00443
00444
00445
00446
00447 if (theFact->garbage) return(FALSE);
00448
00449
00450
00451
00452
00453
00454 #if DEBUGGING_FUNCTIONS
00455 if (theFact->whichDeftemplate->watch)
00456 {
00457 EnvPrintRouter(theEnv,WTRACE,"<== ");
00458 PrintFactWithIdentifier(theEnv,WTRACE,theFact);
00459 EnvPrintRouter(theEnv,WTRACE,"\n");
00460 }
00461 #endif
00462
00463
00464
00465
00466
00467
00468 FactData(theEnv)->ChangeToFactList = TRUE;
00469
00470
00471
00472
00473
00474
00475
00476 RemoveEntityDependencies(theEnv,(struct patternEntity *) theFact);
00477
00478
00479
00480
00481
00482 RemoveHashedFact(theEnv,theFact);
00483
00484
00485
00486
00487
00488 if (theFact == theTemplate->lastFact)
00489 { theTemplate->lastFact = theFact->previousTemplateFact; }
00490
00491 if (theFact->previousTemplateFact == NULL)
00492 {
00493 theTemplate->factList = theTemplate->factList->nextTemplateFact;
00494 if (theTemplate->factList != NULL)
00495 { theTemplate->factList->previousTemplateFact = NULL; }
00496 }
00497 else
00498 {
00499 theFact->previousTemplateFact->nextTemplateFact = theFact->nextTemplateFact;
00500 if (theFact->nextTemplateFact != NULL)
00501 { theFact->nextTemplateFact->previousTemplateFact = theFact->previousTemplateFact; }
00502 }
00503
00504
00505
00506
00507
00508 if (theFact == FactData(theEnv)->LastFact)
00509 { FactData(theEnv)->LastFact = theFact->previousFact; }
00510
00511 if (theFact->previousFact == NULL)
00512 {
00513 FactData(theEnv)->FactList = FactData(theEnv)->FactList->nextFact;
00514 if (FactData(theEnv)->FactList != NULL)
00515 { FactData(theEnv)->FactList->previousFact = NULL; }
00516 }
00517 else
00518 {
00519 theFact->previousFact->nextFact = theFact->nextFact;
00520 if (theFact->nextFact != NULL)
00521 { theFact->nextFact->previousFact = theFact->previousFact; }
00522 }
00523
00524
00525
00526
00527
00528
00529 FactDeinstall(theEnv,theFact);
00530 UtilityData(theEnv)->EphemeralItemCount++;
00531 UtilityData(theEnv)->EphemeralItemSize += sizeof(struct fact) + (sizeof(struct field) * theFact->theProposition.multifieldLength);
00532
00533
00534
00535
00536
00537 theFact->nextFact = FactData(theEnv)->GarbageFacts;
00538 FactData(theEnv)->GarbageFacts = theFact;
00539 theFact->garbage = TRUE;
00540
00541
00542
00543
00544
00545
00546 SetEvaluationError(theEnv,FALSE);
00547
00548
00549
00550
00551
00552
00553
00554 EngineData(theEnv)->JoinOperationInProgress = TRUE;
00555 NetworkRetract(theEnv,(struct patternMatch *) theFact->list);
00556 EngineData(theEnv)->JoinOperationInProgress = FALSE;
00557
00558
00559
00560
00561
00562
00563 if (EngineData(theEnv)->ExecutingRule == NULL)
00564 { FlushGarbagePartialMatches(theEnv); }
00565
00566
00567
00568
00569
00570
00571 ForceLogicalRetractions(theEnv);
00572
00573
00574
00575
00576
00577
00578 if ((EvaluationData(theEnv)->CurrentEvaluationDepth == 0) && (! CommandLineData(theEnv)->EvaluatingTopLevelCommand) &&
00579 (EvaluationData(theEnv)->CurrentExpression == NULL))
00580 { PeriodicCleanup(theEnv,TRUE,FALSE); }
00581
00582
00583
00584
00585
00586
00587 return(TRUE);
00588 }
00589
00590
00591
00592
00593
00594
00595
00596
00597 static void RemoveGarbageFacts(
00598 void *theEnv)
00599 {
00600 struct fact *factPtr, *nextPtr, *lastPtr = NULL;
00601
00602 factPtr = FactData(theEnv)->GarbageFacts;
00603
00604 while (factPtr != NULL)
00605 {
00606 nextPtr = factPtr->nextFact;
00607 if ((factPtr->factHeader.busyCount == 0) &&
00608 (((int) factPtr->depth) > EvaluationData(theEnv)->CurrentEvaluationDepth))
00609 {
00610 UtilityData(theEnv)->EphemeralItemCount--;
00611 UtilityData(theEnv)->EphemeralItemSize -= sizeof(struct fact) + (sizeof(struct field) * factPtr->theProposition.multifieldLength);
00612 ReturnFact(theEnv,factPtr);
00613 if (lastPtr == NULL) FactData(theEnv)->GarbageFacts = nextPtr;
00614 else lastPtr->nextFact = nextPtr;
00615 }
00616 else
00617 { lastPtr = factPtr; }
00618
00619 factPtr = nextPtr;
00620 }
00621 }
00622
00623
00624
00625
00626 globle void *EnvAssert(
00627 void *theEnv,
00628 void *vTheFact)
00629 {
00630 unsigned long hashValue;
00631 unsigned long length, i;
00632 struct field *theField;
00633 struct fact *theFact = (struct fact *) vTheFact;
00634 intBool duplicate;
00635
00636
00637
00638
00639
00640
00641 if (EngineData(theEnv)->JoinOperationInProgress)
00642 {
00643 ReturnFact(theEnv,theFact);
00644 PrintErrorID(theEnv,"FACTMNGR",2,TRUE);
00645 EnvPrintRouter(theEnv,WERROR,"Facts may not be asserted during pattern-matching\n");
00646 return(NULL);
00647 }
00648
00649
00650
00651
00652
00653 length = theFact->theProposition.multifieldLength;
00654 theField = theFact->theProposition.theFields;
00655
00656 for (i = 0; i < length; i++)
00657 {
00658 if (theField[i].type == RVOID)
00659 {
00660 theField[i].type = SYMBOL;
00661 theField[i].value = (void *) EnvAddSymbol(theEnv,"nil");
00662 }
00663 }
00664
00665
00666
00667
00668
00669
00670 hashValue = HandleFactDuplication(theEnv,theFact,&duplicate);
00671 if (duplicate) return(NULL);
00672
00673
00674
00675
00676
00677
00678 if (AddLogicalDependencies(theEnv,(struct patternEntity *) theFact,FALSE) == FALSE)
00679 {
00680 ReturnFact(theEnv,theFact);
00681 return(NULL);
00682 }
00683
00684
00685
00686
00687
00688 AddHashedFact(theEnv,theFact,hashValue);
00689
00690
00691
00692
00693
00694 theFact->nextFact = NULL;
00695 theFact->list = NULL;
00696 theFact->previousFact = FactData(theEnv)->LastFact;
00697 if (FactData(theEnv)->LastFact == NULL)
00698 { FactData(theEnv)->FactList = theFact; }
00699 else
00700 { FactData(theEnv)->LastFact->nextFact = theFact; }
00701 FactData(theEnv)->LastFact = theFact;
00702
00703
00704
00705
00706
00707 theFact->previousTemplateFact = theFact->whichDeftemplate->lastFact;
00708 theFact->nextTemplateFact = NULL;
00709
00710 if (theFact->whichDeftemplate->lastFact == NULL)
00711 { theFact->whichDeftemplate->factList = theFact; }
00712 else
00713 { theFact->whichDeftemplate->lastFact->nextTemplateFact = theFact; }
00714
00715 theFact->whichDeftemplate->lastFact = theFact;
00716
00717
00718
00719
00720
00721 theFact->factIndex = FactData(theEnv)->NextFactIndex++;
00722 theFact->factHeader.timeTag = DefruleData(theEnv)->CurrentEntityTimeTag++;
00723
00724
00725
00726
00727
00728 FactInstall(theEnv,theFact);
00729
00730
00731
00732
00733
00734
00735 #if DEBUGGING_FUNCTIONS
00736 if (theFact->whichDeftemplate->watch)
00737 {
00738 EnvPrintRouter(theEnv,WTRACE,"==> ");
00739 PrintFactWithIdentifier(theEnv,WTRACE,theFact);
00740 EnvPrintRouter(theEnv,WTRACE,"\n");
00741 }
00742 #endif
00743
00744
00745
00746
00747
00748
00749 FactData(theEnv)->ChangeToFactList = TRUE;
00750
00751
00752
00753
00754
00755 CheckTemplateFact(theEnv,theFact);
00756
00757
00758
00759
00760
00761
00762 SetEvaluationError(theEnv,FALSE);
00763
00764
00765
00766
00767
00768
00769 EngineData(theEnv)->JoinOperationInProgress = TRUE;
00770 FactPatternMatch(theEnv,theFact,theFact->whichDeftemplate->patternNetwork,0,NULL,NULL);
00771 EngineData(theEnv)->JoinOperationInProgress = FALSE;
00772
00773
00774
00775
00776
00777
00778 ForceLogicalRetractions(theEnv);
00779
00780
00781
00782
00783
00784
00785 if (EngineData(theEnv)->ExecutingRule == NULL) FlushGarbagePartialMatches(theEnv);
00786
00787
00788
00789
00790
00791
00792 if ((EvaluationData(theEnv)->CurrentEvaluationDepth == 0) && (! CommandLineData(theEnv)->EvaluatingTopLevelCommand) &&
00793 (EvaluationData(theEnv)->CurrentExpression == NULL))
00794 { PeriodicCleanup(theEnv,TRUE,FALSE); }
00795
00796
00797
00798
00799
00800 return((void *) theFact);
00801 }
00802
00803
00804
00805
00806
00807 globle void RemoveAllFacts(
00808 void *theEnv)
00809 {
00810 while (FactData(theEnv)->FactList != NULL)
00811 { EnvRetract(theEnv,(void *) FactData(theEnv)->FactList); }
00812 }
00813
00814
00815
00816
00817
00818 globle struct fact *EnvCreateFact(
00819 void *theEnv,
00820 void *vTheDeftemplate)
00821 {
00822 struct deftemplate *theDeftemplate = (struct deftemplate *) vTheDeftemplate;
00823 struct fact *newFact;
00824 int i;
00825
00826
00827
00828
00829
00830
00831 if (theDeftemplate == NULL) return(NULL);
00832
00833
00834
00835
00836
00837 if (theDeftemplate->implied == FALSE)
00838 {
00839 newFact = CreateFactBySize(theEnv,theDeftemplate->numberOfSlots);
00840 for (i = 0;
00841 i < (int) theDeftemplate->numberOfSlots;
00842 i++)
00843 { newFact->theProposition.theFields[i].type = RVOID; }
00844 }
00845
00846
00847
00848
00849
00850 else
00851 {
00852 newFact = CreateFactBySize(theEnv,1);
00853 newFact->theProposition.theFields[0].type = MULTIFIELD;
00854 newFact->theProposition.theFields[0].value = CreateMultifield2(theEnv,0L);
00855 }
00856
00857
00858
00859
00860
00861 newFact->whichDeftemplate = theDeftemplate;
00862
00863 return(newFact);
00864 }
00865
00866
00867
00868
00869
00870 globle intBool EnvGetFactSlot(
00871 void *theEnv,
00872 void *vTheFact,
00873 char *slotName,
00874 DATA_OBJECT *theValue)
00875 {
00876 struct fact *theFact = (struct fact *) vTheFact;
00877 struct deftemplate *theDeftemplate;
00878 short whichSlot;
00879
00880
00881
00882
00883
00884 theDeftemplate = theFact->whichDeftemplate;
00885
00886
00887
00888
00889
00890
00891
00892 if (theDeftemplate->implied)
00893 {
00894 if (slotName != NULL) return(FALSE);
00895 theValue->type = theFact->theProposition.theFields[0].type;
00896 theValue->value = theFact->theProposition.theFields[0].value;
00897 SetpDOBegin(theValue,1);
00898 SetpDOEnd(theValue,((struct multifield *) theValue->value)->multifieldLength);
00899 return(TRUE);
00900 }
00901
00902
00903
00904
00905
00906
00907 if (FindSlot(theDeftemplate,(SYMBOL_HN *) EnvAddSymbol(theEnv,slotName),&whichSlot) == NULL)
00908 { return(FALSE); }
00909
00910
00911
00912
00913
00914
00915
00916 theValue->type = theFact->theProposition.theFields[whichSlot-1].type;
00917 theValue->value = theFact->theProposition.theFields[whichSlot-1].value;
00918 if (theValue->type == MULTIFIELD)
00919 {
00920 SetpDOBegin(theValue,1);
00921 SetpDOEnd(theValue,((struct multifield *) theValue->value)->multifieldLength);
00922 }
00923
00924 if (theValue->type == RVOID) return(FALSE);
00925
00926 return(TRUE);
00927 }
00928
00929
00930
00931
00932
00933 #if ALLOW_ENVIRONMENT_GLOBALS
00934 globle intBool GetFactSlot(
00935 void *vTheFact,
00936 char *slotName,
00937 DATA_OBJECT *theValue)
00938 {
00939 return(EnvGetFactSlot(GetCurrentEnvironment(),vTheFact,slotName,theValue));
00940 }
00941 #endif
00942
00943
00944
00945
00946
00947 globle intBool EnvPutFactSlot(
00948 void *theEnv,
00949 void *vTheFact,
00950 char *slotName,
00951 DATA_OBJECT *theValue)
00952 {
00953 struct fact *theFact = (struct fact *) vTheFact;
00954 struct deftemplate *theDeftemplate;
00955 struct templateSlot *theSlot;
00956 short whichSlot;
00957
00958
00959
00960
00961
00962 theDeftemplate = theFact->whichDeftemplate;
00963
00964
00965
00966
00967
00968
00969
00970 if (theDeftemplate->implied)
00971 {
00972 if ((slotName != NULL) || (theValue->type != MULTIFIELD))
00973 { return(FALSE); }
00974
00975 if (theFact->theProposition.theFields[0].type == MULTIFIELD)
00976 { ReturnMultifield(theEnv,(struct multifield *) theFact->theProposition.theFields[0].value); }
00977
00978 theFact->theProposition.theFields[0].type = theValue->type;
00979 theFact->theProposition.theFields[0].value = DOToMultifield(theEnv,theValue);
00980
00981 return(TRUE);
00982 }
00983
00984
00985
00986
00987
00988
00989 if ((theSlot = FindSlot(theDeftemplate,(SYMBOL_HN *) EnvAddSymbol(theEnv,slotName),&whichSlot)) == NULL)
00990 { return(FALSE); }
00991
00992
00993
00994
00995
00996
00997 if (((theSlot->multislot == 0) && (theValue->type == MULTIFIELD)) ||
00998 ((theSlot->multislot == 1) && (theValue->type != MULTIFIELD)))
00999 { return(FALSE); }
01000
01001
01002
01003
01004
01005 if (theFact->theProposition.theFields[whichSlot-1].type == MULTIFIELD)
01006 { ReturnMultifield(theEnv,(struct multifield *) theFact->theProposition.theFields[whichSlot-1].value); }
01007
01008 theFact->theProposition.theFields[whichSlot-1].type = theValue->type;
01009
01010 if (theValue->type == MULTIFIELD)
01011 { theFact->theProposition.theFields[whichSlot-1].value = DOToMultifield(theEnv,theValue); }
01012 else
01013 { theFact->theProposition.theFields[whichSlot-1].value = theValue->value; }
01014
01015 return(TRUE);
01016 }
01017
01018
01019
01020
01021
01022
01023 globle intBool EnvAssignFactSlotDefaults(
01024 void *theEnv,
01025 void *vTheFact)
01026 {
01027 struct fact *theFact = (struct fact *) vTheFact;
01028 struct deftemplate *theDeftemplate;
01029 struct templateSlot *slotPtr;
01030 int i;
01031 DATA_OBJECT theResult;
01032
01033
01034
01035
01036
01037 theDeftemplate = theFact->whichDeftemplate;
01038
01039
01040
01041
01042
01043
01044
01045 if (theDeftemplate->implied) return(TRUE);
01046
01047
01048
01049
01050
01051 for (i = 0, slotPtr = theDeftemplate->slotList;
01052 i < (int) theDeftemplate->numberOfSlots;
01053 i++, slotPtr = slotPtr->next)
01054 {
01055
01056
01057
01058
01059
01060 if (theFact->theProposition.theFields[i].type != RVOID) continue;
01061
01062
01063
01064
01065
01066 if (DeftemplateSlotDefault(theEnv,theDeftemplate,slotPtr,&theResult,FALSE))
01067 {
01068 theFact->theProposition.theFields[i].type = theResult.type;
01069 theFact->theProposition.theFields[i].value = theResult.value;
01070 }
01071 }
01072
01073
01074
01075
01076
01077
01078 return(TRUE);
01079 }
01080
01081
01082
01083
01084
01085 globle intBool DeftemplateSlotDefault(
01086 void *theEnv,
01087 struct deftemplate *theDeftemplate,
01088 struct templateSlot *slotPtr,
01089 DATA_OBJECT *theResult,
01090 int garbageMultifield)
01091 {
01092
01093
01094
01095
01096
01097 if (theDeftemplate->implied) return(FALSE);
01098
01099
01100
01101
01102
01103
01104
01105
01106
01107 if (slotPtr->noDefault) return(FALSE);
01108
01109
01110
01111
01112
01113
01114 else if (slotPtr->defaultPresent)
01115 {
01116 if (slotPtr->multislot)
01117 {
01118 StoreInMultifield(theEnv,theResult,slotPtr->defaultList,garbageMultifield);
01119 }
01120 else
01121 {
01122 theResult->type = slotPtr->defaultList->type;
01123 theResult->value = slotPtr->defaultList->value;
01124 }
01125 }
01126
01127
01128
01129
01130
01131
01132 else if (slotPtr->defaultDynamic)
01133 {
01134 if (! EvaluateAndStoreInDataObject(theEnv,(int) slotPtr->multislot,
01135 (EXPRESSION *) slotPtr->defaultList,
01136 theResult,garbageMultifield))
01137 { return(FALSE); }
01138 }
01139
01140
01141
01142
01143
01144
01145 else
01146 {
01147 DeriveDefaultFromConstraints(theEnv,slotPtr->constraints,theResult,
01148 (int) slotPtr->multislot,garbageMultifield);
01149 }
01150
01151
01152
01153
01154
01155
01156 return(TRUE);
01157 }
01158
01159
01160
01161
01162
01163 globle intBool CopyFactSlotValues(
01164 void *theEnv,
01165 void *vTheDestFact,
01166 void *vTheSourceFact)
01167 {
01168 struct fact *theDestFact = (struct fact *) vTheDestFact;
01169 struct fact *theSourceFact = (struct fact *) vTheSourceFact;
01170 struct deftemplate *theDeftemplate;
01171 struct templateSlot *slotPtr;
01172 int i;
01173
01174
01175
01176
01177
01178 theDeftemplate = theSourceFact->whichDeftemplate;
01179 if (theDestFact->whichDeftemplate != theDeftemplate)
01180 { return(FALSE); }
01181
01182
01183
01184
01185
01186
01187 for (i = 0, slotPtr = theDeftemplate->slotList;
01188 i < (int) theDeftemplate->numberOfSlots;
01189 i++, slotPtr = slotPtr->next)
01190 {
01191 theDestFact->theProposition.theFields[i].type =
01192 theSourceFact->theProposition.theFields[i].type;
01193 if (theSourceFact->theProposition.theFields[i].type != MULTIFIELD)
01194 {
01195 theDestFact->theProposition.theFields[i].value =
01196 theSourceFact->theProposition.theFields[i].value;
01197 }
01198 else
01199 {
01200 theDestFact->theProposition.theFields[i].value =
01201 CopyMultifield(theEnv,(struct multifield *) theSourceFact->theProposition.theFields[i].value);
01202 }
01203 }
01204
01205
01206
01207
01208
01209
01210 return(TRUE);
01211 }
01212
01213
01214
01215
01216
01217 globle struct fact *CreateFactBySize(
01218 void *theEnv,
01219 unsigned size)
01220 {
01221 struct fact *theFact;
01222 unsigned newSize;
01223
01224 if (size <= 0) newSize = 1;
01225 else newSize = size;
01226
01227 theFact = get_var_struct(theEnv,fact,sizeof(struct field) * (newSize - 1));
01228
01229 theFact->depth = (unsigned) EvaluationData(theEnv)->CurrentEvaluationDepth;
01230 theFact->garbage = FALSE;
01231 theFact->factIndex = 0LL;
01232 theFact->factHeader.busyCount = 0;
01233 theFact->factHeader.theInfo = &FactData(theEnv)->FactInfo;
01234 theFact->factHeader.dependents = NULL;
01235 theFact->whichDeftemplate = NULL;
01236 theFact->nextFact = NULL;
01237 theFact->previousFact = NULL;
01238 theFact->previousTemplateFact = NULL;
01239 theFact->nextTemplateFact = NULL;
01240 theFact->list = NULL;
01241
01242 theFact->theProposition.multifieldLength = size;
01243 theFact->theProposition.depth = (short) EvaluationData(theEnv)->CurrentEvaluationDepth;
01244 theFact->theProposition.busyCount = 0;
01245
01246 return(theFact);
01247 }
01248
01249
01250
01251
01252
01253 globle void ReturnFact(
01254 void *theEnv,
01255 struct fact *theFact)
01256 {
01257 struct multifield *theSegment, *subSegment;
01258 long newSize, i;
01259
01260 theSegment = &theFact->theProposition;
01261
01262 for (i = 0; i < theSegment->multifieldLength; i++)
01263 {
01264 if (theSegment->theFields[i].type == MULTIFIELD)
01265 {
01266 subSegment = (struct multifield *) theSegment->theFields[i].value;
01267 if (subSegment->busyCount == 0)
01268 { ReturnMultifield(theEnv,subSegment); }
01269 else
01270 { AddToMultifieldList(theEnv,subSegment); }
01271 }
01272 }
01273
01274 if (theFact->theProposition.multifieldLength == 0) newSize = 1;
01275 else newSize = theFact->theProposition.multifieldLength;
01276
01277 rtn_var_struct(theEnv,fact,sizeof(struct field) * (newSize - 1),theFact);
01278 }
01279
01280
01281
01282
01283
01284 globle void FactInstall(
01285 void *theEnv,
01286 struct fact *newFact)
01287 {
01288 struct multifield *theSegment;
01289 int i;
01290
01291 FactData(theEnv)->NumberOfFacts++;
01292 newFact->whichDeftemplate->busyCount++;
01293 theSegment = &newFact->theProposition;
01294
01295 for (i = 0 ; i < (int) theSegment->multifieldLength ; i++)
01296 {
01297 AtomInstall(theEnv,theSegment->theFields[i].type,theSegment->theFields[i].value);
01298 }
01299
01300 newFact->factHeader.busyCount++;
01301 }
01302
01303
01304
01305
01306
01307 globle void FactDeinstall(
01308 void *theEnv,
01309 struct fact *newFact)
01310 {
01311 struct multifield *theSegment;
01312 int i;
01313
01314 FactData(theEnv)->NumberOfFacts--;
01315 theSegment = &newFact->theProposition;
01316 newFact->whichDeftemplate->busyCount--;
01317
01318 for (i = 0 ; i < (int) theSegment->multifieldLength ; i++)
01319 {
01320 AtomDeinstall(theEnv,theSegment->theFields[i].type,theSegment->theFields[i].value);
01321 }
01322
01323 newFact->factHeader.busyCount--;
01324 }
01325
01326
01327
01328
01329
01330 #if WIN_BTC
01331 #pragma argsused
01332 #endif
01333 globle void EnvIncrementFactCount(
01334 void *theEnv,
01335 void *factPtr)
01336 {
01337 #if MAC_MCW || WIN_MCW || MAC_XCD
01338 #pragma unused(theEnv)
01339 #endif
01340
01341 ((struct fact *) factPtr)->factHeader.busyCount++;
01342 }
01343
01344
01345
01346
01347
01348 #if WIN_BTC
01349 #pragma argsused
01350 #endif
01351 globle void EnvDecrementFactCount(
01352 void *theEnv,
01353 void *factPtr)
01354 {
01355 #if MAC_MCW || WIN_MCW || MAC_XCD
01356 #pragma unused(theEnv)
01357 #endif
01358
01359 ((struct fact *) factPtr)->factHeader.busyCount--;
01360 }
01361
01362
01363
01364
01365
01366
01367 globle void *EnvGetNextFact(
01368 void *theEnv,
01369 void *factPtr)
01370 {
01371 if (factPtr == NULL)
01372 { return((void *) FactData(theEnv)->FactList); }
01373
01374 if (((struct fact *) factPtr)->garbage) return(NULL);
01375
01376 return((void *) ((struct fact *) factPtr)->nextFact);
01377 }
01378
01379
01380
01381
01382
01383
01384
01385 globle void *GetNextFactInScope(
01386 void *theEnv,
01387 void *vTheFact)
01388 {
01389 struct fact *theFact = (struct fact *) vTheFact;
01390
01391
01392
01393
01394
01395
01396
01397
01398
01399
01400 if (theFact == NULL)
01401 {
01402 theFact = FactData(theEnv)->FactList;
01403 if (FactData(theEnv)->LastModuleIndex != DefmoduleData(theEnv)->ModuleChangeIndex)
01404 {
01405 UpdateDeftemplateScope(theEnv);
01406 FactData(theEnv)->LastModuleIndex = DefmoduleData(theEnv)->ModuleChangeIndex;
01407 }
01408 }
01409
01410
01411
01412
01413
01414
01415
01416 else if (((struct fact *) theFact)->garbage)
01417 { return(NULL); }
01418
01419
01420
01421
01422
01423
01424
01425 else
01426 { theFact = theFact->nextFact; }
01427
01428
01429
01430
01431
01432
01433
01434 while (theFact != NULL)
01435 {
01436 if (theFact->whichDeftemplate->inScope) return((void *) theFact);
01437
01438 theFact = theFact->nextFact;
01439 }
01440
01441 return(NULL);
01442 }
01443
01444
01445
01446
01447
01448 globle void EnvGetFactPPForm(
01449 void *theEnv,
01450 char *buffer,
01451 unsigned bufferLength,
01452 void *theFact)
01453 {
01454 OpenStringDestination(theEnv,"FactPPForm",buffer,bufferLength);
01455 PrintFactWithIdentifier(theEnv,"FactPPForm",(struct fact *) theFact);
01456 CloseStringDestination(theEnv,"FactPPForm");
01457 }
01458
01459
01460
01461
01462
01463 #if WIN_BTC
01464 #pragma argsused
01465 #endif
01466 globle long long EnvFactIndex(
01467 void *theEnv,
01468 void *factPtr)
01469 {
01470 #if MAC_MCW || WIN_MCW || MAC_XCD
01471 #pragma unused(theEnv)
01472 #endif
01473
01474 return(((struct fact *) factPtr)->factIndex);
01475 }
01476
01477
01478
01479
01480
01481 #if ALLOW_ENVIRONMENT_GLOBALS
01482 globle long long FactIndex(
01483 void *factPtr)
01484 {
01485 return(EnvFactIndex(GetCurrentEnvironment(),factPtr));
01486 }
01487 #endif
01488
01489
01490
01491
01492
01493 globle void *EnvAssertString(
01494 void *theEnv,
01495 char *theString)
01496 {
01497 struct fact *theFact;
01498
01499 if ((theFact = StringToFact(theEnv,theString)) == NULL) return(NULL);
01500
01501 return((void *) EnvAssert(theEnv,(void *) theFact));
01502 }
01503
01504
01505
01506
01507
01508 globle int EnvGetFactListChanged(
01509 void *theEnv)
01510 {
01511 return(FactData(theEnv)->ChangeToFactList);
01512 }
01513
01514
01515
01516
01517
01518 globle void EnvSetFactListChanged(
01519 void *theEnv,
01520 int value)
01521 {
01522 FactData(theEnv)->ChangeToFactList = value;
01523 }
01524
01525
01526
01527
01528
01529 globle unsigned long GetNumberOfFacts(
01530 void *theEnv)
01531 {
01532 return(FactData(theEnv)->NumberOfFacts);
01533 }
01534
01535
01536
01537
01538
01539 static void ResetFacts(
01540 void *theEnv)
01541 {
01542
01543
01544
01545
01546 FactData(theEnv)->NextFactIndex = 0L;
01547
01548
01549
01550
01551
01552 RemoveAllFacts(theEnv);
01553 }
01554
01555
01556
01557
01558
01559
01560 static int ClearFactsReady(
01561 void *theEnv)
01562 {
01563
01564
01565
01566
01567 FactData(theEnv)->NextFactIndex = 0L;
01568
01569
01570
01571
01572
01573 RemoveAllFacts(theEnv);
01574
01575
01576
01577
01578
01579
01580 if (EnvGetNextFact(theEnv,NULL) != NULL) return(FALSE);
01581
01582
01583
01584
01585
01586
01587 return(TRUE);
01588 }
01589
01590
01591
01592
01593
01594 globle struct fact *FindIndexedFact(
01595 void *theEnv,
01596 long long factIndexSought)
01597 {
01598 struct fact *theFact;
01599
01600 for (theFact = (struct fact *) EnvGetNextFact(theEnv,NULL);
01601 theFact != NULL;
01602 theFact = (struct fact *) EnvGetNextFact(theEnv,theFact))
01603 {
01604 if (theFact->factIndex == factIndexSought)
01605 { return(theFact); }
01606 }
01607
01608 return(NULL);
01609 }
01610
01611 #endif
01612