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 #include <stdio.h>
00031 #define _STDIO_INCLUDED_
00032 #include <string.h>
00033
00034 #include "setup.h"
00035
00036 #if DEFTEMPLATE_CONSTRUCT
00037
00038 #define _FACTCOM_SOURCE_
00039
00040 #include "memalloc.h"
00041 #include "envrnmnt.h"
00042 #include "exprnpsr.h"
00043 #include "factmngr.h"
00044 #include "argacces.h"
00045 #include "match.h"
00046 #include "router.h"
00047 #include "scanner.h"
00048 #include "constant.h"
00049 #include "factrhs.h"
00050 #include "factmch.h"
00051 #include "extnfunc.h"
00052 #include "tmpltpsr.h"
00053 #include "tmpltutl.h"
00054 #include "facthsh.h"
00055 #include "modulutl.h"
00056 #include "strngrtr.h"
00057 #include "tmpltdef.h"
00058 #include "tmpltfun.h"
00059 #include "sysdep.h"
00060
00061 #if BLOAD_AND_BSAVE || BLOAD || BLOAD_ONLY
00062 #include "bload.h"
00063 #endif
00064
00065 #include "factcom.h"
00066
00067 #define INVALID -2L
00068 #define UNSPECIFIED -1L
00069
00070
00071
00072
00073
00074 #if (! RUN_TIME)
00075 static struct expr *AssertParse(void *,struct expr *,char *);
00076 #endif
00077 #if DEBUGGING_FUNCTIONS
00078 static long long GetFactsArgument(void *,int,int);
00079 #endif
00080 static struct expr *StandardLoadFact(void *,char *,struct token *);
00081 static DATA_OBJECT_PTR GetSaveFactsDeftemplateNames(void *,struct expr *,int,int *,int *);
00082
00083
00084
00085
00086
00087 globle void FactCommandDefinitions(
00088 void *theEnv)
00089 {
00090 #if ! RUN_TIME
00091 #if DEBUGGING_FUNCTIONS
00092 EnvDefineFunction2(theEnv,"facts", 'v', PTIEF FactsCommand, "FactsCommand", "*4iu");
00093 #endif
00094
00095 EnvDefineFunction(theEnv,"assert", 'u', PTIEF AssertCommand, "AssertCommand");
00096 EnvDefineFunction2(theEnv,"retract", 'v', PTIEF RetractCommand, "RetractCommand","1*z");
00097 EnvDefineFunction2(theEnv,"assert-string", 'u', PTIEF AssertStringFunction, "AssertStringFunction", "11s");
00098 EnvDefineFunction2(theEnv,"str-assert", 'u', PTIEF AssertStringFunction, "AssertStringFunction", "11s");
00099
00100 EnvDefineFunction2(theEnv,"get-fact-duplication",'b',
00101 GetFactDuplicationCommand,"GetFactDuplicationCommand", "00");
00102 EnvDefineFunction2(theEnv,"set-fact-duplication",'b',
00103 SetFactDuplicationCommand,"SetFactDuplicationCommand", "11");
00104
00105 EnvDefineFunction2(theEnv,"save-facts", 'b', PTIEF SaveFactsCommand, "SaveFactsCommand", "1*wk");
00106 EnvDefineFunction2(theEnv,"load-facts", 'b', PTIEF LoadFactsCommand, "LoadFactsCommand", "11k");
00107 EnvDefineFunction2(theEnv,"fact-index", 'g', PTIEF FactIndexFunction,"FactIndexFunction", "11y");
00108
00109 AddFunctionParser(theEnv,"assert",AssertParse);
00110 FuncSeqOvlFlags(theEnv,"assert",FALSE,FALSE);
00111 #else
00112 #if MAC_MCW || WIN_MCW || MAC_XCD
00113 #pragma unused(theEnv)
00114 #endif
00115 #endif
00116 }
00117
00118
00119
00120
00121
00122 globle void AssertCommand(
00123 void *theEnv,
00124 DATA_OBJECT_PTR rv)
00125 {
00126 struct deftemplate *theDeftemplate;
00127 struct field *theField;
00128 DATA_OBJECT theValue;
00129 struct expr *theExpression;
00130 struct templateSlot *slotPtr;
00131 struct fact *newFact;
00132 int error = FALSE;
00133 int i;
00134 struct fact *theFact;
00135
00136
00137
00138
00139
00140 SetpType(rv,SYMBOL);
00141 SetpValue(rv,EnvFalseSymbol(theEnv));
00142
00143
00144
00145
00146
00147
00148 theExpression = GetFirstArgument();
00149 theDeftemplate = (struct deftemplate *) theExpression->value;
00150
00151
00152
00153
00154
00155
00156 if (theDeftemplate->implied == FALSE)
00157 {
00158 newFact = CreateFactBySize(theEnv,theDeftemplate->numberOfSlots);
00159 slotPtr = theDeftemplate->slotList;
00160 }
00161 else
00162 {
00163 newFact = CreateFactBySize(theEnv,1);
00164 if (theExpression->nextArg == NULL)
00165 {
00166 newFact->theProposition.theFields[0].type = MULTIFIELD;
00167 newFact->theProposition.theFields[0].value = CreateMultifield2(theEnv,0L);
00168 }
00169 slotPtr = NULL;
00170 }
00171
00172 newFact->whichDeftemplate = theDeftemplate;
00173
00174
00175
00176
00177
00178
00179
00180 theField = newFact->theProposition.theFields;
00181
00182 for (theExpression = theExpression->nextArg, i = 0;
00183 theExpression != NULL;
00184 theExpression = theExpression->nextArg, i++)
00185 {
00186
00187
00188
00189
00190 EvaluateExpression(theEnv,theExpression,&theValue);
00191
00192
00193
00194
00195
00196 if ((slotPtr != NULL) ?
00197 (slotPtr->multislot == FALSE) && (theValue.type == MULTIFIELD) :
00198 FALSE)
00199 {
00200 MultiIntoSingleFieldSlotError(theEnv,slotPtr,theDeftemplate);
00201 theValue.type = SYMBOL;
00202 theValue.value = EnvFalseSymbol(theEnv);
00203 error = TRUE;
00204 }
00205
00206
00207
00208
00209
00210 theField[i].type = theValue.type;
00211 theField[i].value = theValue.value;
00212
00213
00214
00215
00216
00217 if (slotPtr != NULL) slotPtr = slotPtr->next;
00218 }
00219
00220
00221
00222
00223
00224
00225 if (error)
00226 {
00227 ReturnFact(theEnv,newFact);
00228 return;
00229 }
00230
00231
00232
00233
00234
00235 theFact = (struct fact *) EnvAssert(theEnv,(void *) newFact);
00236
00237
00238
00239
00240
00241 if (theFact != NULL)
00242 {
00243 SetpType(rv,FACT_ADDRESS);
00244 SetpValue(rv,(void *) theFact);
00245 }
00246
00247 return;
00248 }
00249
00250
00251
00252
00253
00254 globle void RetractCommand(
00255 void *theEnv)
00256 {
00257 long long factIndex;
00258 struct fact *ptr;
00259 struct expr *theArgument;
00260 DATA_OBJECT theResult;
00261 int argNumber;
00262
00263
00264
00265
00266
00267 for (theArgument = GetFirstArgument(), argNumber = 1;
00268 theArgument != NULL;
00269 theArgument = GetNextArgument(theArgument), argNumber++)
00270 {
00271
00272
00273
00274
00275 EvaluateExpression(theEnv,theArgument,&theResult);
00276
00277
00278
00279
00280
00281
00282
00283 if (theResult.type == INTEGER)
00284 {
00285
00286
00287
00288
00289 factIndex = ValueToLong(theResult.value);
00290 if (factIndex < 0)
00291 {
00292 ExpectedTypeError1(theEnv,"retract",argNumber,"fact-address, fact-index, or the symbol *");
00293 return;
00294 }
00295
00296
00297
00298
00299
00300 ptr = FindIndexedFact(theEnv,factIndex);
00301
00302
00303
00304
00305
00306
00307 if (ptr != NULL)
00308 { EnvRetract(theEnv,(void *) ptr); }
00309 else
00310 {
00311 char tempBuffer[20];
00312 gensprintf(tempBuffer,"f-%lld",factIndex);
00313 CantFindItemErrorMessage(theEnv,"fact",tempBuffer);
00314 }
00315 }
00316
00317
00318
00319
00320
00321
00322 else if (theResult.type == FACT_ADDRESS)
00323 { EnvRetract(theEnv,theResult.value); }
00324
00325
00326
00327
00328
00329
00330 else if ((theResult.type == SYMBOL) ?
00331 (strcmp(ValueToString(theResult.value),"*") == 0) : FALSE)
00332 {
00333 RemoveAllFacts(theEnv);
00334 return;
00335 }
00336
00337
00338
00339
00340
00341
00342 else
00343 {
00344 ExpectedTypeError1(theEnv,"retract",argNumber,"fact-address, fact-index, or the symbol *");
00345 SetEvaluationError(theEnv,TRUE);
00346 }
00347 }
00348 }
00349
00350
00351
00352
00353
00354 globle int SetFactDuplicationCommand(
00355 void *theEnv)
00356 {
00357 int oldValue;
00358 DATA_OBJECT theValue;
00359
00360
00361
00362
00363
00364 oldValue = EnvGetFactDuplication(theEnv);
00365
00366
00367
00368
00369
00370 if (EnvArgCountCheck(theEnv,"set-fact-duplication",EXACTLY,1) == -1)
00371 { return(oldValue); }
00372
00373
00374
00375
00376
00377 EnvRtnUnknown(theEnv,1,&theValue);
00378
00379
00380
00381
00382
00383
00384 if ((theValue.value == EnvFalseSymbol(theEnv)) && (theValue.type == SYMBOL))
00385 { EnvSetFactDuplication(theEnv,FALSE); }
00386 else
00387 { EnvSetFactDuplication(theEnv,TRUE); }
00388
00389
00390
00391
00392
00393 return(oldValue);
00394 }
00395
00396
00397
00398
00399
00400 globle int GetFactDuplicationCommand(
00401 void *theEnv)
00402 {
00403 int currentValue;
00404
00405
00406
00407
00408
00409 currentValue = EnvGetFactDuplication(theEnv);
00410
00411
00412
00413
00414
00415 if (EnvArgCountCheck(theEnv,"get-fact-duplication",EXACTLY,0) == -1)
00416 { return(currentValue); }
00417
00418
00419
00420
00421
00422 return(currentValue);
00423 }
00424
00425
00426
00427
00428
00429 globle long long FactIndexFunction(
00430 void *theEnv)
00431 {
00432 DATA_OBJECT item;
00433
00434
00435
00436
00437
00438 if (EnvArgCountCheck(theEnv,"fact-index",EXACTLY,1) == -1) return(-1LL);
00439
00440
00441
00442
00443
00444 EnvRtnUnknown(theEnv,1,&item);
00445
00446
00447
00448
00449
00450 if (GetType(item) != FACT_ADDRESS)
00451 {
00452 ExpectedTypeError1(theEnv,"fact-index",1,"fact-address");
00453 return(-1L);
00454 }
00455
00456
00457
00458
00459
00460
00461
00462 if (((struct fact *) GetValue(item))->garbage) return(-1LL);
00463
00464 return (EnvFactIndex(theEnv,GetValue(item)));
00465 }
00466
00467 #if DEBUGGING_FUNCTIONS
00468
00469
00470
00471
00472
00473 globle void FactsCommand(
00474 void *theEnv)
00475 {
00476 int argumentCount;
00477 long long start = UNSPECIFIED, end = UNSPECIFIED, max = UNSPECIFIED;
00478 struct defmodule *theModule;
00479 DATA_OBJECT theValue;
00480 int argOffset;
00481
00482
00483
00484
00485
00486 if ((argumentCount = EnvArgCountCheck(theEnv,"facts",NO_MORE_THAN,4)) == -1) return;
00487
00488
00489
00490
00491
00492
00493 theModule = ((struct defmodule *) EnvGetCurrentModule(theEnv));
00494
00495
00496
00497
00498
00499
00500 if (argumentCount == 0)
00501 {
00502 EnvFacts(theEnv,WDISPLAY,theModule,start,end,max);
00503 return;
00504 }
00505
00506
00507
00508
00509
00510
00511 EnvRtnUnknown(theEnv,1,&theValue);
00512
00513
00514
00515
00516
00517
00518 if (theValue.type == SYMBOL)
00519 {
00520 theModule = (struct defmodule *) EnvFindDefmodule(theEnv,ValueToString(theValue.value));
00521 if ((theModule == NULL) && (strcmp(ValueToString(theValue.value),"*") != 0))
00522 {
00523 SetEvaluationError(theEnv,TRUE);
00524 CantFindItemErrorMessage(theEnv,"defmodule",ValueToString(theValue.value));
00525 return;
00526 }
00527
00528 if ((start = GetFactsArgument(theEnv,2,argumentCount)) == INVALID) return;
00529
00530 argOffset = 1;
00531 }
00532
00533
00534
00535
00536
00537
00538 else if (theValue.type == INTEGER)
00539 {
00540 start = DOToLong(theValue);
00541 if (start < 0)
00542 {
00543 ExpectedTypeError1(theEnv,"facts",1,"symbol or positive number");
00544 SetHaltExecution(theEnv,TRUE);
00545 SetEvaluationError(theEnv,TRUE);
00546 return;
00547 }
00548 argOffset = 0;
00549 }
00550
00551
00552
00553
00554
00555 else
00556 {
00557 ExpectedTypeError1(theEnv,"facts",1,"symbol or positive number");
00558 SetHaltExecution(theEnv,TRUE);
00559 SetEvaluationError(theEnv,TRUE);
00560 return;
00561 }
00562
00563
00564
00565
00566
00567 if ((end = GetFactsArgument(theEnv,2 + argOffset,argumentCount)) == INVALID) return;
00568 if ((max = GetFactsArgument(theEnv,3 + argOffset,argumentCount)) == INVALID) return;
00569
00570
00571
00572
00573
00574 EnvFacts(theEnv,WDISPLAY,theModule,start,end,max);
00575 }
00576
00577
00578
00579
00580 globle void EnvFacts(
00581 void *theEnv,
00582 char *logicalName,
00583 void *vTheModule,
00584 long long start,
00585 long long end,
00586 long long max)
00587 {
00588 struct fact *factPtr;
00589 long count = 0;
00590 struct defmodule *oldModule, *theModule = (struct defmodule *) vTheModule;
00591 int allModules = FALSE;
00592
00593
00594
00595
00596
00597 oldModule = ((struct defmodule *) EnvGetCurrentModule(theEnv));
00598
00599
00600
00601
00602
00603
00604 if (theModule == NULL) allModules = TRUE;
00605 else EnvSetCurrentModule(theEnv,(void *) theModule);
00606
00607
00608
00609
00610
00611 if (allModules) factPtr = (struct fact *) EnvGetNextFact(theEnv,NULL);
00612 else factPtr = (struct fact *) GetNextFactInScope(theEnv,NULL);
00613
00614
00615
00616
00617
00618
00619 while (factPtr != NULL)
00620 {
00621
00622
00623
00624
00625
00626 if (GetHaltExecution(theEnv) == TRUE)
00627 {
00628 EnvSetCurrentModule(theEnv,(void *) oldModule);
00629 return;
00630 }
00631
00632
00633
00634
00635
00636
00637 if ((factPtr->factIndex > end) && (end != UNSPECIFIED))
00638 {
00639 PrintTally(theEnv,logicalName,count,"fact","facts");
00640 EnvSetCurrentModule(theEnv,(void *) oldModule);
00641 return;
00642 }
00643
00644
00645
00646
00647
00648
00649 if (max == 0)
00650 {
00651 PrintTally(theEnv,logicalName,count,"fact","facts");
00652 EnvSetCurrentModule(theEnv,(void *) oldModule);
00653 return;
00654 }
00655
00656
00657
00658
00659
00660
00661 if (factPtr->factIndex >= start)
00662 {
00663 PrintFactWithIdentifier(theEnv,logicalName,factPtr);
00664 EnvPrintRouter(theEnv,logicalName,"\n");
00665 count++;
00666 if (max > 0) max--;
00667 }
00668
00669
00670
00671
00672
00673 if (allModules) factPtr = (struct fact *) EnvGetNextFact(theEnv,factPtr);
00674 else factPtr = (struct fact *) GetNextFactInScope(theEnv,factPtr);
00675 }
00676
00677
00678
00679
00680
00681 PrintTally(theEnv,logicalName,count,"fact","facts");
00682
00683
00684
00685
00686
00687 EnvSetCurrentModule(theEnv,(void *) oldModule);
00688 }
00689
00690
00691
00692
00693
00694
00695
00696 static long long GetFactsArgument(
00697 void *theEnv,
00698 int whichOne,
00699 int argumentCount)
00700 {
00701 long long factIndex;
00702 DATA_OBJECT theValue;
00703
00704 if (whichOne > argumentCount) return(UNSPECIFIED);
00705
00706 if (EnvArgTypeCheck(theEnv,"facts",whichOne,INTEGER,&theValue) == FALSE) return(INVALID);
00707
00708 factIndex = DOToLong(theValue);
00709
00710 if (factIndex < 0)
00711 {
00712 ExpectedTypeError1(theEnv,"facts",whichOne,"positive number");
00713 SetHaltExecution(theEnv,TRUE);
00714 SetEvaluationError(theEnv,TRUE);
00715 return(INVALID);
00716 }
00717
00718 return(factIndex);
00719 }
00720
00721 #endif
00722
00723
00724
00725
00726
00727 globle void AssertStringFunction(
00728 void *theEnv,
00729 DATA_OBJECT_PTR returnValue)
00730 {
00731 DATA_OBJECT argPtr;
00732 struct fact *theFact;
00733
00734
00735
00736
00737
00738 SetpType(returnValue,SYMBOL);
00739 SetpValue(returnValue,EnvFalseSymbol(theEnv));
00740
00741
00742
00743
00744
00745 if (EnvArgCountCheck(theEnv,"assert-string",EXACTLY,1) == -1) return;
00746
00747 if (EnvArgTypeCheck(theEnv,"assert-string",1,STRING,&argPtr) == FALSE)
00748 { return; }
00749
00750
00751
00752
00753
00754
00755 theFact = (struct fact *) EnvAssertString(theEnv,DOToString(argPtr));
00756 if (theFact != NULL)
00757 {
00758 SetpType(returnValue,FACT_ADDRESS);
00759 SetpValue(returnValue,(void *) theFact);
00760 }
00761
00762 return;
00763 }
00764
00765
00766
00767
00768
00769 globle int SaveFactsCommand(
00770 void *theEnv)
00771 {
00772 char *fileName;
00773 int numArgs, saveCode = LOCAL_SAVE;
00774 char *argument;
00775 DATA_OBJECT theValue;
00776 struct expr *theList = NULL;
00777
00778
00779
00780
00781
00782 if ((numArgs = EnvArgCountCheck(theEnv,"save-facts",AT_LEAST,1)) == -1) return(FALSE);
00783
00784
00785
00786
00787
00788 if ((fileName = GetFileName(theEnv,"save-facts",1)) == NULL) return(FALSE);
00789
00790
00791
00792
00793
00794
00795
00796 if (numArgs > 1)
00797 {
00798 if (EnvArgTypeCheck(theEnv,"save-facts",2,SYMBOL,&theValue) == FALSE) return(FALSE);
00799
00800 argument = DOToString(theValue);
00801
00802 if (strcmp(argument,"local") == 0)
00803 { saveCode = LOCAL_SAVE; }
00804 else if (strcmp(argument,"visible") == 0)
00805 { saveCode = VISIBLE_SAVE; }
00806 else
00807 {
00808 ExpectedTypeError1(theEnv,"save-facts",2,"symbol with value local or visible");
00809 return(FALSE);
00810 }
00811 }
00812
00813
00814
00815
00816
00817
00818
00819 if (numArgs > 2) theList = GetFirstArgument()->nextArg->nextArg;
00820
00821
00822
00823
00824
00825 if (EnvSaveFacts(theEnv,fileName,saveCode,theList) == FALSE)
00826 { return(FALSE); }
00827
00828 return(TRUE);
00829 }
00830
00831
00832
00833
00834
00835 globle int LoadFactsCommand(
00836 void *theEnv)
00837 {
00838 char *fileName;
00839
00840
00841
00842
00843
00844 if (EnvArgCountCheck(theEnv,"load-facts",EXACTLY,1) == -1) return(FALSE);
00845
00846
00847
00848
00849
00850 if ((fileName = GetFileName(theEnv,"load-facts",1)) == NULL) return(FALSE);
00851
00852
00853
00854
00855
00856 if (EnvLoadFacts(theEnv,fileName) == FALSE) return(FALSE);
00857
00858 return(TRUE);
00859 }
00860
00861
00862
00863
00864 globle intBool EnvSaveFacts(
00865 void *theEnv,
00866 char *fileName,
00867 int saveCode,
00868 struct expr *theList)
00869 {
00870 int tempValue1, tempValue2, tempValue3;
00871 struct fact *theFact;
00872 FILE *filePtr;
00873 struct defmodule *theModule;
00874 DATA_OBJECT_PTR theDOArray;
00875 int count, i, printFact, error;
00876
00877
00878
00879
00880
00881 if ((filePtr = GenOpen(theEnv,fileName,"w")) == NULL)
00882 {
00883 OpenErrorMessage(theEnv,"save-facts",fileName);
00884 return(FALSE);
00885 }
00886
00887 SetFastSave(theEnv,filePtr);
00888
00889
00890
00891
00892
00893
00894 tempValue1 = PrintUtilityData(theEnv)->PreserveEscapedCharacters;
00895 PrintUtilityData(theEnv)->PreserveEscapedCharacters = TRUE;
00896 tempValue2 = PrintUtilityData(theEnv)->AddressesToStrings;
00897 PrintUtilityData(theEnv)->AddressesToStrings = TRUE;
00898 tempValue3 = PrintUtilityData(theEnv)->InstanceAddressesToNames;
00899 PrintUtilityData(theEnv)->InstanceAddressesToNames = TRUE;
00900
00901
00902
00903
00904
00905 theDOArray = GetSaveFactsDeftemplateNames(theEnv,theList,saveCode,&count,&error);
00906
00907 if (error)
00908 {
00909 PrintUtilityData(theEnv)->PreserveEscapedCharacters = tempValue1;
00910 PrintUtilityData(theEnv)->AddressesToStrings = tempValue2;
00911 PrintUtilityData(theEnv)->InstanceAddressesToNames = tempValue3;
00912 GenClose(theEnv,filePtr);
00913 SetFastSave(theEnv,NULL);
00914 return(FALSE);
00915 }
00916
00917
00918
00919
00920
00921 theModule = ((struct defmodule *) EnvGetCurrentModule(theEnv));
00922
00923 for (theFact = (struct fact *) GetNextFactInScope(theEnv,NULL);
00924 theFact != NULL;
00925 theFact = (struct fact *) GetNextFactInScope(theEnv,theFact))
00926 {
00927
00928
00929
00930
00931
00932
00933 if ((saveCode == LOCAL_SAVE) &&
00934 (theFact->whichDeftemplate->header.whichModule->theModule != theModule))
00935 { printFact = FALSE; }
00936
00937
00938
00939
00940
00941
00942 else if (theList == NULL)
00943 { printFact = TRUE; }
00944
00945
00946
00947
00948
00949
00950
00951
00952 else
00953 {
00954 printFact = FALSE;
00955 for (i = 0; i < count; i++)
00956 {
00957 if (theDOArray[i].value == (void *) theFact->whichDeftemplate)
00958 {
00959 printFact = TRUE;
00960 break;
00961 }
00962 }
00963 }
00964
00965
00966
00967
00968
00969
00970 if (printFact)
00971 {
00972 PrintFact(theEnv,(char *) filePtr,theFact,FALSE,FALSE);
00973 EnvPrintRouter(theEnv,(char *) filePtr,"\n");
00974 }
00975 }
00976
00977
00978
00979
00980
00981 PrintUtilityData(theEnv)->PreserveEscapedCharacters = tempValue1;
00982 PrintUtilityData(theEnv)->AddressesToStrings = tempValue2;
00983 PrintUtilityData(theEnv)->InstanceAddressesToNames = tempValue3;
00984
00985
00986
00987
00988
00989 GenClose(theEnv,filePtr);
00990 SetFastSave(theEnv,NULL);
00991
00992
00993
00994
00995
00996 if (theList != NULL) rm3(theEnv,theDOArray,(long) sizeof(DATA_OBJECT) * count);
00997
00998
00999
01000
01001
01002
01003 return(TRUE);
01004 }
01005
01006
01007
01008
01009
01010 static DATA_OBJECT_PTR GetSaveFactsDeftemplateNames(
01011 void *theEnv,
01012 struct expr *theList,
01013 int saveCode,
01014 int *count,
01015 int *error)
01016 {
01017 struct expr *tempList;
01018 DATA_OBJECT_PTR theDOArray;
01019 int i, tempCount;
01020 struct deftemplate *theDeftemplate = NULL;
01021
01022
01023
01024
01025
01026 *error = FALSE;
01027
01028
01029
01030
01031
01032
01033 if (theList == NULL)
01034 {
01035 *count = 0;
01036 return(NULL);
01037 }
01038
01039
01040
01041
01042
01043
01044 for (tempList = theList, *count = 0;
01045 tempList != NULL;
01046 tempList = tempList->nextArg, (*count)++)
01047 { }
01048
01049
01050
01051
01052
01053 theDOArray = (DATA_OBJECT_PTR) gm3(theEnv,(long) sizeof(DATA_OBJECT) * *count);
01054
01055
01056
01057
01058
01059 for (tempList = theList, i = 0;
01060 i < *count;
01061 tempList = tempList->nextArg, i++)
01062 {
01063
01064
01065
01066
01067 EvaluateExpression(theEnv,tempList,&theDOArray[i]);
01068
01069 if (EvaluationData(theEnv)->EvaluationError)
01070 {
01071 *error = TRUE;
01072 rm3(theEnv,theDOArray,(long) sizeof(DATA_OBJECT) * *count);
01073 return(NULL);
01074 }
01075
01076
01077
01078
01079
01080 if (theDOArray[i].type != SYMBOL)
01081 {
01082 *error = TRUE;
01083 ExpectedTypeError1(theEnv,"save-facts",3+i,"symbol");
01084 rm3(theEnv,theDOArray,(long) sizeof(DATA_OBJECT) * *count);
01085 return(NULL);
01086 }
01087
01088
01089
01090
01091
01092
01093
01094 if (saveCode == LOCAL_SAVE)
01095 {
01096 theDeftemplate = (struct deftemplate *)
01097 EnvFindDeftemplate(theEnv,ValueToString(theDOArray[i].value));
01098 if (theDeftemplate == NULL)
01099 {
01100 *error = TRUE;
01101 ExpectedTypeError1(theEnv,"save-facts",3+i,"local deftemplate name");
01102 rm3(theEnv,theDOArray,(long) sizeof(DATA_OBJECT) * *count);
01103 return(NULL);
01104 }
01105 }
01106 else if (saveCode == VISIBLE_SAVE)
01107 {
01108 theDeftemplate = (struct deftemplate *)
01109 FindImportedConstruct(theEnv,"deftemplate",NULL,
01110 ValueToString(theDOArray[i].value),
01111 &tempCount,TRUE,NULL);
01112 if (theDeftemplate == NULL)
01113 {
01114 *error = TRUE;
01115 ExpectedTypeError1(theEnv,"save-facts",3+i,"visible deftemplate name");
01116 rm3(theEnv,theDOArray,(long) sizeof(DATA_OBJECT) * *count);
01117 return(NULL);
01118 }
01119 }
01120
01121
01122
01123
01124
01125
01126 theDOArray[i].type = DEFTEMPLATE_PTR;
01127 theDOArray[i].value = (void *) theDeftemplate;
01128 }
01129
01130
01131
01132
01133
01134 return(theDOArray);
01135 }
01136
01137
01138
01139
01140 globle intBool EnvLoadFacts(
01141 void *theEnv,
01142 char *fileName)
01143 {
01144 FILE *filePtr;
01145 struct token theToken;
01146 struct expr *testPtr;
01147 DATA_OBJECT rv;
01148
01149
01150
01151
01152
01153 if ((filePtr = GenOpen(theEnv,fileName,"r")) == NULL)
01154 {
01155 OpenErrorMessage(theEnv,"load-facts",fileName);
01156 return(FALSE);
01157 }
01158
01159 SetFastLoad(theEnv,filePtr);
01160
01161
01162
01163
01164
01165 theToken.type = LPAREN;
01166 while (theToken.type != STOP)
01167 {
01168 testPtr = StandardLoadFact(theEnv,(char *) filePtr,&theToken);
01169 if (testPtr == NULL) theToken.type = STOP;
01170 else EvaluateExpression(theEnv,testPtr,&rv);
01171 ReturnExpression(theEnv,testPtr);
01172 }
01173
01174
01175
01176
01177
01178 SetFastLoad(theEnv,NULL);
01179 GenClose(theEnv,filePtr);
01180
01181
01182
01183
01184
01185
01186 if (EvaluationData(theEnv)->EvaluationError) return(FALSE);
01187 return(TRUE);
01188 }
01189
01190
01191
01192
01193 globle intBool EnvLoadFactsFromString(
01194 void *theEnv,
01195 char *theString,
01196 int theMax)
01197 {
01198 char * theStrRouter = "*** load-facts-from-string ***";
01199 struct token theToken;
01200 struct expr *testPtr;
01201 DATA_OBJECT rv;
01202
01203
01204
01205
01206
01207 if ((theMax == -1) ? (!OpenStringSource(theEnv,theStrRouter,theString,0)) :
01208 (!OpenTextSource(theEnv,theStrRouter,theString,0,(unsigned) theMax)))
01209 return(FALSE);
01210
01211
01212
01213
01214
01215 theToken.type = LPAREN;
01216 while (theToken.type != STOP)
01217 {
01218 testPtr = StandardLoadFact(theEnv,theStrRouter,&theToken);
01219 if (testPtr == NULL) theToken.type = STOP;
01220 else EvaluateExpression(theEnv,testPtr,&rv);
01221 ReturnExpression(theEnv,testPtr);
01222 }
01223
01224
01225
01226
01227
01228 CloseStringSource(theEnv,theStrRouter);
01229
01230
01231
01232
01233
01234
01235 if (EvaluationData(theEnv)->EvaluationError) return(FALSE);
01236 return(TRUE);
01237 }
01238
01239
01240
01241
01242 static struct expr *StandardLoadFact(
01243 void *theEnv,
01244 char *logicalName,
01245 struct token *theToken)
01246 {
01247 int error = FALSE;
01248 struct expr *temp;
01249
01250 GetToken(theEnv,logicalName,theToken);
01251 if (theToken->type != LPAREN) return(NULL);
01252
01253 temp = GenConstant(theEnv,FCALL,FindFunction(theEnv,"assert"));
01254 temp->argList = GetRHSPattern(theEnv,logicalName,theToken,&error,
01255 TRUE,FALSE,TRUE,RPAREN);
01256
01257 if (error == TRUE)
01258 {
01259 EnvPrintRouter(theEnv,WERROR,"Function load-facts encountered an error\n");
01260 SetEvaluationError(theEnv,TRUE);
01261 ReturnExpression(theEnv,temp);
01262 return(NULL);
01263 }
01264
01265 if (ExpressionContainsVariables(temp,TRUE))
01266 {
01267 ReturnExpression(theEnv,temp);
01268 return(NULL);
01269 }
01270
01271 return(temp);
01272 }
01273
01274 #if (! RUN_TIME)
01275
01276
01277
01278
01279 static struct expr *AssertParse(
01280 void *theEnv,
01281 struct expr *top,
01282 char *logicalName)
01283 {
01284 int error;
01285 struct expr *rv;
01286 struct token theToken;
01287
01288 ReturnExpression(theEnv,top);
01289 SavePPBuffer(theEnv," ");
01290 IncrementIndentDepth(theEnv,8);
01291 rv = BuildRHSAssert(theEnv,logicalName,&theToken,&error,TRUE,TRUE,"assert command");
01292 DecrementIndentDepth(theEnv,8);
01293 return(rv);
01294 }
01295
01296 #endif
01297
01298 #endif
01299
01300