/
factcom.c
1297 lines (1049 loc) · 42 KB
/
factcom.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*******************************************************/
/* "C" Language Integrated Production System */
/* */
/* CLIPS Version 6.24 06/05/06 */
/* */
/* FACT COMMANDS MODULE */
/*******************************************************/
/*************************************************************/
/* Purpose: Provides the facts, assert, retract, save-facts, */
/* load-facts, set-fact-duplication, get-fact-duplication, */
/* assert-string, and fact-index commands and functions. */
/* */
/* Principal Programmer(s): */
/* Gary D. Riley */
/* */
/* Contributing Programmer(s): */
/* Brian L. Dantes */
/* */
/* Revision History: */
/* 6.23: Correction for FalseSymbol/TrueSymbol. DR0859 */
/* */
/* 6.24: Added environment parameter to GenClose. */
/* Added environment parameter to GenOpen. */
/* */
/* Renamed BOOLEAN macro type to intBool. */
/* */
/*************************************************************/
#include <stdio.h>
#define _STDIO_INCLUDED_
#include <string.h>
#include "setup.h"
#if DEFTEMPLATE_CONSTRUCT
#define _FACTCOM_SOURCE_
#include "memalloc.h"
#include "envrnmnt.h"
#include "exprnpsr.h"
#include "factmngr.h"
#include "argacces.h"
#include "match.h"
#include "router.h"
#include "scanner.h"
#include "constant.h"
#include "factrhs.h"
#include "factmch.h"
#include "extnfunc.h"
#include "tmpltpsr.h"
#include "tmpltutl.h"
#include "facthsh.h"
#include "modulutl.h"
#include "strngrtr.h"
#include "tmpltdef.h"
#include "tmpltfun.h"
#include "sysdep.h"
#if BLOAD_AND_BSAVE || BLOAD || BLOAD_ONLY
#include "bload.h"
#endif
#include "factcom.h"
#define INVALID -2L
#define UNSPECIFIED -1L
/***************************************/
/* LOCAL INTERNAL FUNCTION DEFINITIONS */
/***************************************/
#if (! RUN_TIME)
static struct expr *AssertParse(void *,struct expr *,char *);
#endif
#if DEBUGGING_FUNCTIONS
static long long GetFactsArgument(void *,int,int);
#endif
static struct expr *StandardLoadFact(void *,char *,struct token *);
static DATA_OBJECT_PTR GetSaveFactsDeftemplateNames(void *,struct expr *,int,int *,int *);
/***************************************/
/* FactCommandDefinitions: Initializes */
/* fact commands and functions. */
/***************************************/
globle void FactCommandDefinitions(
void *theEnv)
{
#if ! RUN_TIME
#if DEBUGGING_FUNCTIONS
EnvDefineFunction2(theEnv,"facts", 'v', PTIEF FactsCommand, "FactsCommand", "*4iu");
#endif
EnvDefineFunction(theEnv,"assert", 'u', PTIEF AssertCommand, "AssertCommand");
EnvDefineFunction2(theEnv,"retract", 'v', PTIEF RetractCommand, "RetractCommand","1*z");
EnvDefineFunction2(theEnv,"assert-string", 'u', PTIEF AssertStringFunction, "AssertStringFunction", "11s");
EnvDefineFunction2(theEnv,"str-assert", 'u', PTIEF AssertStringFunction, "AssertStringFunction", "11s");
EnvDefineFunction2(theEnv,"get-fact-duplication",'b',
GetFactDuplicationCommand,"GetFactDuplicationCommand", "00");
EnvDefineFunction2(theEnv,"set-fact-duplication",'b',
SetFactDuplicationCommand,"SetFactDuplicationCommand", "11");
EnvDefineFunction2(theEnv,"save-facts", 'b', PTIEF SaveFactsCommand, "SaveFactsCommand", "1*wk");
EnvDefineFunction2(theEnv,"load-facts", 'b', PTIEF LoadFactsCommand, "LoadFactsCommand", "11k");
EnvDefineFunction2(theEnv,"fact-index", 'g', PTIEF FactIndexFunction,"FactIndexFunction", "11y");
AddFunctionParser(theEnv,"assert",AssertParse);
FuncSeqOvlFlags(theEnv,"assert",FALSE,FALSE);
#else
#endif
}
/***************************************/
/* AssertCommand: H/L access routine */
/* for the assert function. */
/***************************************/
globle void AssertCommand(
void *theEnv,
DATA_OBJECT_PTR rv)
{
struct deftemplate *theDeftemplate;
struct field *theField;
DATA_OBJECT theValue;
struct expr *theExpression;
struct templateSlot *slotPtr;
struct fact *newFact;
int error = FALSE;
int i;
struct fact *theFact;
/*===================================================*/
/* Set the default return value to the symbol FALSE. */
/*===================================================*/
SetpType(rv,SYMBOL);
SetpValue(rv,EnvFalseSymbol(theEnv));
/*================================*/
/* Get the deftemplate associated */
/* with the fact being asserted. */
/*================================*/
theExpression = GetFirstArgument();
theDeftemplate = (struct deftemplate *) theExpression->value;
/*=======================================*/
/* Create the fact and store the name of */
/* the deftemplate as the 1st field. */
/*=======================================*/
if (theDeftemplate->implied == FALSE)
{
newFact = CreateFactBySize(theEnv,theDeftemplate->numberOfSlots);
slotPtr = theDeftemplate->slotList;
}
else
{
newFact = CreateFactBySize(theEnv,1);
if (theExpression->nextArg == NULL)
{
newFact->theProposition.theFields[0].type = MULTIFIELD;
newFact->theProposition.theFields[0].value = CreateMultifield2(theEnv,0L);
}
slotPtr = NULL;
}
newFact->whichDeftemplate = theDeftemplate;
/*===================================================*/
/* Evaluate the expression associated with each slot */
/* and store the result in the appropriate slot of */
/* the newly created fact. */
/*===================================================*/
theField = newFact->theProposition.theFields;
for (theExpression = theExpression->nextArg, i = 0;
theExpression != NULL;
theExpression = theExpression->nextArg, i++)
{
/*===================================================*/
/* Evaluate the expression to be stored in the slot. */
/*===================================================*/
EvaluateExpression(theEnv,theExpression,&theValue);
/*============================================================*/
/* A multifield value can't be stored in a single field slot. */
/*============================================================*/
if ((slotPtr != NULL) ?
(slotPtr->multislot == FALSE) && (theValue.type == MULTIFIELD) :
FALSE)
{
MultiIntoSingleFieldSlotError(theEnv,slotPtr,theDeftemplate);
theValue.type = SYMBOL;
theValue.value = EnvFalseSymbol(theEnv);
error = TRUE;
}
/*==============================*/
/* Store the value in the slot. */
/*==============================*/
theField[i].type = theValue.type;
theField[i].value = theValue.value;
/*========================================*/
/* Get the information for the next slot. */
/*========================================*/
if (slotPtr != NULL) slotPtr = slotPtr->next;
}
/*============================================*/
/* If an error occured while generating the */
/* fact's slot values, then abort the assert. */
/*============================================*/
if (error)
{
ReturnFact(theEnv,newFact);
return;
}
/*================================*/
/* Add the fact to the fact-list. */
/*================================*/
theFact = (struct fact *) EnvAssert(theEnv,(void *) newFact);
/*========================================*/
/* The asserted fact is the return value. */
/*========================================*/
if (theFact != NULL)
{
SetpType(rv,FACT_ADDRESS);
SetpValue(rv,(void *) theFact);
}
return;
}
/****************************************/
/* RetractCommand: H/L access routine */
/* for the retract command. */
/****************************************/
globle void RetractCommand(
void *theEnv)
{
long long factIndex;
struct fact *ptr;
struct expr *theArgument;
DATA_OBJECT theResult;
int argNumber;
/*================================*/
/* Iterate through each argument. */
/*================================*/
for (theArgument = GetFirstArgument(), argNumber = 1;
theArgument != NULL;
theArgument = GetNextArgument(theArgument), argNumber++)
{
/*========================*/
/* Evaluate the argument. */
/*========================*/
EvaluateExpression(theEnv,theArgument,&theResult);
/*===============================================*/
/* If the argument evaluates to an integer, then */
/* it's assumed to be the fact index of the fact */
/* to be retracted. */
/*===============================================*/
if (theResult.type == INTEGER)
{
/*==========================================*/
/* A fact index must be a positive integer. */
/*==========================================*/
factIndex = ValueToLong(theResult.value);
if (factIndex < 0)
{
ExpectedTypeError1(theEnv,"retract",argNumber,"fact-address, fact-index, or the symbol *");
return;
}
/*================================================*/
/* See if a fact with the specified index exists. */
/*================================================*/
ptr = FindIndexedFact(theEnv,factIndex);
/*=====================================*/
/* If the fact exists then retract it, */
/* otherwise print an error message. */
/*=====================================*/
if (ptr != NULL)
{ EnvRetract(theEnv,(void *) ptr); }
else
{
char tempBuffer[20];
gensprintf(tempBuffer,"f-%lld",factIndex);
CantFindItemErrorMessage(theEnv,"fact",tempBuffer);
}
}
/*===============================================*/
/* Otherwise if the argument evaluates to a fact */
/* address, we can directly retract it. */
/*===============================================*/
else if (theResult.type == FACT_ADDRESS)
{ EnvRetract(theEnv,theResult.value); }
/*============================================*/
/* Otherwise if the argument evaluates to the */
/* symbol *, then all facts are retracted. */
/*============================================*/
else if ((theResult.type == SYMBOL) ?
(strcmp(ValueToString(theResult.value),"*") == 0) : FALSE)
{
RemoveAllFacts(theEnv);
return;
}
/*============================================*/
/* Otherwise the argument has evaluated to an */
/* illegal value for the retract command. */
/*============================================*/
else
{
ExpectedTypeError1(theEnv,"retract",argNumber,"fact-address, fact-index, or the symbol *");
SetEvaluationError(theEnv,TRUE);
}
}
}
/***************************************************/
/* SetFactDuplicationCommand: H/L access routine */
/* for the set-fact-duplication command. */
/***************************************************/
globle int SetFactDuplicationCommand(
void *theEnv)
{
int oldValue;
DATA_OBJECT theValue;
/*=====================================================*/
/* Get the old value of the fact duplication behavior. */
/*=====================================================*/
oldValue = EnvGetFactDuplication(theEnv);
/*============================================*/
/* Check for the correct number of arguments. */
/*============================================*/
if (EnvArgCountCheck(theEnv,"set-fact-duplication",EXACTLY,1) == -1)
{ return(oldValue); }
/*========================*/
/* Evaluate the argument. */
/*========================*/
EnvRtnUnknown(theEnv,1,&theValue);
/*===============================================================*/
/* If the argument evaluated to FALSE, then the fact duplication */
/* behavior is disabled, otherwise it is enabled. */
/*===============================================================*/
if ((theValue.value == EnvFalseSymbol(theEnv)) && (theValue.type == SYMBOL))
{ EnvSetFactDuplication(theEnv,FALSE); }
else
{ EnvSetFactDuplication(theEnv,TRUE); }
/*========================================================*/
/* Return the old value of the fact duplication behavior. */
/*========================================================*/
return(oldValue);
}
/***************************************************/
/* GetFactDuplicationCommand: H/L access routine */
/* for the get-fact-duplication command. */
/***************************************************/
globle int GetFactDuplicationCommand(
void *theEnv)
{
int currentValue;
/*=========================================================*/
/* Get the current value of the fact duplication behavior. */
/*=========================================================*/
currentValue = EnvGetFactDuplication(theEnv);
/*============================================*/
/* Check for the correct number of arguments. */
/*============================================*/
if (EnvArgCountCheck(theEnv,"get-fact-duplication",EXACTLY,0) == -1)
{ return(currentValue); }
/*============================================================*/
/* Return the current value of the fact duplication behavior. */
/*============================================================*/
return(currentValue);
}
/*******************************************/
/* FactIndexFunction: H/L access routine */
/* for the fact-index function. */
/*******************************************/
globle long long FactIndexFunction(
void *theEnv)
{
DATA_OBJECT item;
/*============================================*/
/* Check for the correct number of arguments. */
/*============================================*/
if (EnvArgCountCheck(theEnv,"fact-index",EXACTLY,1) == -1) return(-1LL);
/*========================*/
/* Evaluate the argument. */
/*========================*/
EnvRtnUnknown(theEnv,1,&item);
/*======================================*/
/* The argument must be a fact address. */
/*======================================*/
if (GetType(item) != FACT_ADDRESS)
{
ExpectedTypeError1(theEnv,"fact-index",1,"fact-address");
return(-1L);
}
/*================================================*/
/* Return the fact index associated with the fact */
/* address. If the fact has been retracted, then */
/* return -1 for the fact index. */
/*================================================*/
if (((struct fact *) GetValue(item))->garbage) return(-1LL);
return (EnvFactIndex(theEnv,GetValue(item)));
}
#if DEBUGGING_FUNCTIONS
/**************************************/
/* FactsCommand: H/L access routine */
/* for the facts command. */
/**************************************/
globle void FactsCommand(
void *theEnv)
{
int argumentCount;
long long start = UNSPECIFIED, end = UNSPECIFIED, max = UNSPECIFIED;
struct defmodule *theModule;
DATA_OBJECT theValue;
int argOffset;
/*=========================================================*/
/* Determine the number of arguments to the facts command. */
/*=========================================================*/
if ((argumentCount = EnvArgCountCheck(theEnv,"facts",NO_MORE_THAN,4)) == -1) return;
/*==================================*/
/* The default module for the facts */
/* command is the current module. */
/*==================================*/
theModule = ((struct defmodule *) EnvGetCurrentModule(theEnv));
/*==========================================*/
/* If no arguments were specified, then use */
/* the default values to list the facts. */
/*==========================================*/
if (argumentCount == 0)
{
EnvFacts(theEnv,WDISPLAY,theModule,start,end,max);
return;
}
/*========================================================*/
/* Since there are one or more arguments, see if a module */
/* or start index was specified as the first argument. */
/*========================================================*/
EnvRtnUnknown(theEnv,1,&theValue);
/*===============================================*/
/* If the first argument is a symbol, then check */
/* to see that a valid module was specified. */
/*===============================================*/
if (theValue.type == SYMBOL)
{
theModule = (struct defmodule *) EnvFindDefmodule(theEnv,ValueToString(theValue.value));
if ((theModule == NULL) && (strcmp(ValueToString(theValue.value),"*") != 0))
{
SetEvaluationError(theEnv,TRUE);
CantFindItemErrorMessage(theEnv,"defmodule",ValueToString(theValue.value));
return;
}
if ((start = GetFactsArgument(theEnv,2,argumentCount)) == INVALID) return;
argOffset = 1;
}
/*================================================*/
/* Otherwise if the first argument is an integer, */
/* check to see that a valid index was specified. */
/*================================================*/
else if (theValue.type == INTEGER)
{
start = DOToLong(theValue);
if (start < 0)
{
ExpectedTypeError1(theEnv,"facts",1,"symbol or positive number");
SetHaltExecution(theEnv,TRUE);
SetEvaluationError(theEnv,TRUE);
return;
}
argOffset = 0;
}
/*==========================================*/
/* Otherwise the first argument is invalid. */
/*==========================================*/
else
{
ExpectedTypeError1(theEnv,"facts",1,"symbol or positive number");
SetHaltExecution(theEnv,TRUE);
SetEvaluationError(theEnv,TRUE);
return;
}
/*==========================*/
/* Get the other arguments. */
/*==========================*/
if ((end = GetFactsArgument(theEnv,2 + argOffset,argumentCount)) == INVALID) return;
if ((max = GetFactsArgument(theEnv,3 + argOffset,argumentCount)) == INVALID) return;
/*=================*/
/* List the facts. */
/*=================*/
EnvFacts(theEnv,WDISPLAY,theModule,start,end,max);
}
/*****************************************************/
/* EnvFacts: C access routine for the facts command. */
/*****************************************************/
globle void EnvFacts(
void *theEnv,
char *logicalName,
void *vTheModule,
long long start,
long long end,
long long max)
{
struct fact *factPtr;
long count = 0;
struct defmodule *oldModule, *theModule = (struct defmodule *) vTheModule;
int allModules = FALSE;
/*==========================*/
/* Save the current module. */
/*==========================*/
oldModule = ((struct defmodule *) EnvGetCurrentModule(theEnv));
/*=========================================================*/
/* Determine if facts from all modules are to be displayed */
/* or just facts from the current module. */
/*=========================================================*/
if (theModule == NULL) allModules = TRUE;
else EnvSetCurrentModule(theEnv,(void *) theModule);
/*=====================================*/
/* Get the first fact to be displayed. */
/*=====================================*/
if (allModules) factPtr = (struct fact *) EnvGetNextFact(theEnv,NULL);
else factPtr = (struct fact *) GetNextFactInScope(theEnv,NULL);
/*===============================*/
/* Display facts until there are */
/* no more facts to display. */
/*===============================*/
while (factPtr != NULL)
{
/*==================================================*/
/* Abort the display of facts if the Halt Execution */
/* flag has been set (normally by user action). */
/*==================================================*/
if (GetHaltExecution(theEnv) == TRUE)
{
EnvSetCurrentModule(theEnv,(void *) oldModule);
return;
}
/*===============================================*/
/* If the maximum fact index of facts to display */
/* has been reached, then stop displaying facts. */
/*===============================================*/
if ((factPtr->factIndex > end) && (end != UNSPECIFIED))
{
PrintTally(theEnv,logicalName,count,"fact","facts");
EnvSetCurrentModule(theEnv,(void *) oldModule);
return;
}
/*================================================*/
/* If the maximum number of facts to be displayed */
/* has been reached, then stop displaying facts. */
/*================================================*/
if (max == 0)
{
PrintTally(theEnv,logicalName,count,"fact","facts");
EnvSetCurrentModule(theEnv,(void *) oldModule);
return;
}
/*======================================================*/
/* If the index of the fact is greater than the minimum */
/* starting fact index, then display the fact. */
/*======================================================*/
if (factPtr->factIndex >= start)
{
PrintFactWithIdentifier(theEnv,logicalName,factPtr);
EnvPrintRouter(theEnv,logicalName,"\n");
count++;
if (max > 0) max--;
}
/*========================================*/
/* Proceed to the next fact to be listed. */
/*========================================*/
if (allModules) factPtr = (struct fact *) EnvGetNextFact(theEnv,factPtr);
else factPtr = (struct fact *) GetNextFactInScope(theEnv,factPtr);
}
/*===================================================*/
/* Print the total of the number of facts displayed. */
/*===================================================*/
PrintTally(theEnv,logicalName,count,"fact","facts");
/*=============================*/
/* Restore the current module. */
/*=============================*/
EnvSetCurrentModule(theEnv,(void *) oldModule);
}
/****************************************************************/
/* GetFactsArgument: Returns an argument for the facts command. */
/* A return value of -1 indicates that no value was specified. */
/* A return value of -2 indicates that the value specified is */
/* invalid. */
/****************************************************************/
static long long GetFactsArgument(
void *theEnv,
int whichOne,
int argumentCount)
{
long long factIndex;
DATA_OBJECT theValue;
if (whichOne > argumentCount) return(UNSPECIFIED);
if (EnvArgTypeCheck(theEnv,"facts",whichOne,INTEGER,&theValue) == FALSE) return(INVALID);
factIndex = DOToLong(theValue);
if (factIndex < 0)
{
ExpectedTypeError1(theEnv,"facts",whichOne,"positive number");
SetHaltExecution(theEnv,TRUE);
SetEvaluationError(theEnv,TRUE);
return(INVALID);
}
return(factIndex);
}
#endif /* DEBUGGING_FUNCTIONS */
/**********************************************/
/* AssertStringFunction: H/L access routine */
/* for the assert-string function. */
/**********************************************/
globle void AssertStringFunction(
void *theEnv,
DATA_OBJECT_PTR returnValue)
{
DATA_OBJECT argPtr;
struct fact *theFact;
/*===================================================*/
/* Set the default return value to the symbol FALSE. */
/*===================================================*/
SetpType(returnValue,SYMBOL);
SetpValue(returnValue,EnvFalseSymbol(theEnv));
/*=====================================================*/
/* Check for the correct number and type of arguments. */
/*=====================================================*/
if (EnvArgCountCheck(theEnv,"assert-string",EXACTLY,1) == -1) return;
if (EnvArgTypeCheck(theEnv,"assert-string",1,STRING,&argPtr) == FALSE)
{ return; }
/*==========================================*/
/* Call the driver routine for converting a */
/* string to a fact and then assert it. */
/*==========================================*/
theFact = (struct fact *) EnvAssertString(theEnv,DOToString(argPtr));
if (theFact != NULL)
{
SetpType(returnValue,FACT_ADDRESS);
SetpValue(returnValue,(void *) theFact);
}
return;
}
/******************************************/
/* SaveFactsCommand: H/L access routine */
/* for the save-facts command. */
/******************************************/
globle int SaveFactsCommand(
void *theEnv)
{
char *fileName;
int numArgs, saveCode = LOCAL_SAVE;
char *argument;
DATA_OBJECT theValue;
struct expr *theList = NULL;
/*============================================*/
/* Check for the correct number of arguments. */
/*============================================*/
if ((numArgs = EnvArgCountCheck(theEnv,"save-facts",AT_LEAST,1)) == -1) return(FALSE);
/*=================================================*/
/* Get the file name to which facts will be saved. */
/*=================================================*/
if ((fileName = GetFileName(theEnv,"save-facts",1)) == NULL) return(FALSE);
/*=============================================================*/
/* If specified, the second argument to save-facts indicates */
/* whether just facts local to the current module or all facts */
/* visible to the current module will be saved. */
/*=============================================================*/
if (numArgs > 1)
{
if (EnvArgTypeCheck(theEnv,"save-facts",2,SYMBOL,&theValue) == FALSE) return(FALSE);
argument = DOToString(theValue);
if (strcmp(argument,"local") == 0)
{ saveCode = LOCAL_SAVE; }
else if (strcmp(argument,"visible") == 0)
{ saveCode = VISIBLE_SAVE; }
else
{
ExpectedTypeError1(theEnv,"save-facts",2,"symbol with value local or visible");
return(FALSE);
}
}
/*======================================================*/
/* Subsequent arguments indicate that only those facts */
/* associated with the specified deftemplates should be */
/* saved to the file. */
/*======================================================*/
if (numArgs > 2) theList = GetFirstArgument()->nextArg->nextArg;
/*====================================*/
/* Call the SaveFacts driver routine. */
/*====================================*/
if (EnvSaveFacts(theEnv,fileName,saveCode,theList) == FALSE)
{ return(FALSE); }
return(TRUE);
}
/******************************************/
/* LoadFactsCommand: H/L access routine */
/* for the load-facts command. */
/******************************************/
globle int LoadFactsCommand(
void *theEnv)
{
char *fileName;
/*============================================*/
/* Check for the correct number of arguments. */
/*============================================*/
if (EnvArgCountCheck(theEnv,"load-facts",EXACTLY,1) == -1) return(FALSE);
/*====================================================*/
/* Get the file name from which facts will be loaded. */
/*====================================================*/
if ((fileName = GetFileName(theEnv,"load-facts",1)) == NULL) return(FALSE);
/*====================================*/
/* Call the LoadFacts driver routine. */
/*====================================*/
if (EnvLoadFacts(theEnv,fileName) == FALSE) return(FALSE);
return(TRUE);
}
/**************************************************************/
/* EnvSaveFacts: C access routine for the save-facts command. */
/**************************************************************/
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,"w")) == NULL)
{
OpenErrorMessage(theEnv,"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,"\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);