/
generate.c
1060 lines (896 loc) · 42.7 KB
/
generate.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.30 10/19/06 */
/* */
/* GENERATE MODULE */
/*******************************************************/
/*************************************************************/
/* Purpose: Provides routines for converting field */
/* constraints to expressions which can be used */
/* in the pattern and join networks. */
/* */
/* Principal Programmer(s): */
/* Gary D. Riley */
/* */
/* Contributing Programmer(s): */
/* */
/* Revision History: */
/* */
/* 6.30: Added support for hashed alpha memories. */
/* */
/*************************************************************/
#define _GENERATE_SOURCE_
#include <stdio.h>
#define _STDIO_INCLUDED_
#include <stdlib.h>
#include "setup.h"
#if (! RUN_TIME) && (! BLOAD_ONLY) && DEFRULE_CONSTRUCT
#include "constant.h"
#include "envrnmnt.h"
#include "memalloc.h"
#include "symbol.h"
#include "exprnpsr.h"
#include "argacces.h"
#include "extnfunc.h"
#include "router.h"
#include "ruledef.h"
#include "pattern.h"
#include "generate.h"
#if DEFGLOBAL_CONSTRUCT
#include "globlpsr.h"
#endif
/***************************************/
/* LOCAL INTERNAL FUNCTION DEFINITIONS */
/***************************************/
static void ExtractAnds(void *,struct lhsParseNode *,int,
struct expr **,struct expr **,struct expr **,
struct expr **,struct nandFrame *);
static void ExtractFieldTest(void *,struct lhsParseNode *,int,
struct expr **,struct expr **,struct expr **,
struct expr **,struct nandFrame *);
static struct expr *GetfieldReplace(void *,struct lhsParseNode *);
static struct expr *GenPNConstant(void *,struct lhsParseNode *);
static struct expr *GenJNConstant(void *,struct lhsParseNode *,int);
static struct expr *GenJNColon(void *,struct lhsParseNode *,int,struct nandFrame *);
static struct expr *GenPNColon(void *,struct lhsParseNode *);
static struct expr *GenJNEq(void *,struct lhsParseNode *,int,struct nandFrame *);
static struct expr *GenPNEq(void *,struct lhsParseNode *);
static struct expr *GenJNVariableComparison(void *,struct lhsParseNode *,
struct lhsParseNode *,int);
static struct expr *GenPNVariableComparison(void *,struct lhsParseNode *,
struct lhsParseNode *);
static int AllVariablesInPattern(struct lhsParseNode *,
int);
static int AllVariablesInExpression(struct lhsParseNode *,
int);
/*******************************************************/
/* FieldConversion: Generates join and pattern network */
/* expressions for a field constraint. */
/*******************************************************/
globle void FieldConversion(
void *theEnv,
struct lhsParseNode *theField,
struct lhsParseNode *thePattern,
struct nandFrame *theNandFrames)
{
int testInPatternNetwork = TRUE;
struct lhsParseNode *patternPtr;
struct expr *headOfPNExpression, *headOfJNExpression;
struct expr *lastPNExpression, *lastJNExpression;
struct expr *tempExpression;
struct expr *patternNetTest = NULL;
struct expr *joinNetTest = NULL;
struct expr *constantSelector = NULL;
struct expr *constantValue = NULL;
/*==================================================*/
/* Consider a NULL pointer to be an internal error. */
/*==================================================*/
if (theField == NULL)
{
SystemError(theEnv,"ANALYSIS",3);
EnvExitRouter(theEnv,EXIT_FAILURE);
}
/*========================================================*/
/* Determine if constant testing must be performed in the */
/* join network. Only possible when a field contains an */
/* or ('|') and references are made to variables outside */
/* the pattern. */
/*========================================================*/
if (theField->bottom != NULL)
{
if (theField->bottom->bottom != NULL)
{ testInPatternNetwork = AllVariablesInPattern(theField->bottom,theField->pattern); }
}
/*=============================================================*/
/* Extract pattern and join network expressions. Loop through */
/* the or'ed constraints of the field, extracting pattern and */
/* join network expressions and adding them to a running list. */
/*=============================================================*/
headOfPNExpression = lastPNExpression = NULL;
headOfJNExpression = lastJNExpression = NULL;
for (patternPtr = theField->bottom;
patternPtr != NULL;
patternPtr = patternPtr->bottom)
{
/*=============================================*/
/* Extract pattern and join network tests from */
/* the or'ed constraint being examined. */
/*=============================================*/
ExtractAnds(theEnv,patternPtr,testInPatternNetwork,&patternNetTest,&joinNetTest,
&constantSelector,&constantValue,theNandFrames);
/*=============================================================*/
/* Constant hashing is only used in the pattern network if the */
/* field doesn't contain an or'ed constraint. For example, the */
/* constaint "red | blue" can not use hashing. */
/*=============================================================*/
if (constantSelector != NULL)
{
if ((patternPtr == theField->bottom) &&
(patternPtr->bottom == NULL))
{
theField->constantSelector = constantSelector;
theField->constantValue = constantValue;
}
else
{
ReturnExpression(theEnv,constantSelector);
ReturnExpression(theEnv,constantValue);
ReturnExpression(theEnv,theField->constantSelector);
ReturnExpression(theEnv,theField->constantValue);
theField->constantSelector = NULL;
theField->constantValue = NULL;
}
}
/*=====================================================*/
/* Add the new pattern network expressions to the list */
/* of pattern network expressions being constructed. */
/*=====================================================*/
if (patternNetTest != NULL)
{
if (lastPNExpression == NULL)
{ headOfPNExpression = patternNetTest; }
else
{ lastPNExpression->nextArg = patternNetTest; }
lastPNExpression = patternNetTest;
}
/*==================================================*/
/* Add the new join network expressions to the list */
/* of join network expressions being constructed. */
/*==================================================*/
if (joinNetTest != NULL)
{
if (lastJNExpression == NULL)
{ headOfJNExpression = joinNetTest; }
else
{ lastJNExpression->nextArg = joinNetTest; }
lastJNExpression = joinNetTest;
}
}
/*==========================================================*/
/* If there was more than one expression generated from the */
/* or'ed field constraints for the pattern network, then */
/* enclose the expressions within an "or" function call. */
/*==========================================================*/
if ((headOfPNExpression != NULL) ? (headOfPNExpression->nextArg != NULL) : FALSE)
{
tempExpression = GenConstant(theEnv,FCALL,ExpressionData(theEnv)->PTR_OR);
tempExpression->argList = headOfPNExpression;
headOfPNExpression = tempExpression;
}
/*==========================================================*/
/* If there was more than one expression generated from the */
/* or'ed field constraints for the join network, then */
/* enclose the expressions within an "or" function call. */
/*==========================================================*/
if ((headOfJNExpression != NULL) ? (headOfJNExpression->nextArg != NULL) : FALSE)
{
tempExpression = GenConstant(theEnv,FCALL,ExpressionData(theEnv)->PTR_OR);
tempExpression->argList = headOfJNExpression;
headOfJNExpression = tempExpression;
}
/*===============================================================*/
/* If the field constraint binds a variable that was previously */
/* bound somewhere in the LHS of the rule, then generate an */
/* expression to compare this binding occurrence of the variable */
/* to the previous binding occurrence. */
/*===============================================================*/
if (((theField->type == MF_VARIABLE) || (theField->type == SF_VARIABLE)) &&
(theField->referringNode != NULL))
{
/*================================================================*/
/* If the previous variable reference is within the same pattern, */
/* then the variable comparison can occur in the pattern network. */
/*================================================================*/
if (theField->referringNode->pattern == theField->pattern)
{
tempExpression = GenPNVariableComparison(theEnv,theField,theField->referringNode);
headOfPNExpression = CombineExpressions(theEnv,tempExpression,headOfPNExpression);
}
/*====================================*/
/* Otherwise, the variable comparison */
/* must occur in the join network. */
/*====================================*/
else if (theField->referringNode->pattern > 0)
{
AddNandUnification(theEnv,theField,theNandFrames);
/*====================================*/
/* Generate an expression to test the */
/* variable in a non-nand join. */
/*====================================*/
tempExpression = GenJNVariableComparison(theEnv,theField,theField->referringNode,FALSE);
headOfJNExpression = CombineExpressions(theEnv,tempExpression,headOfJNExpression);
/*==========================*/
/* Generate the hash index. */
/*==========================*/
if (theField->patternType->genGetPNValueFunction != NULL)
{
tempExpression = (*theField->patternType->genGetPNValueFunction)(theEnv,theField);
thePattern->rightHash = AppendExpressions(tempExpression,thePattern->rightHash);
}
if (theField->referringNode->patternType->genGetJNValueFunction)
{
tempExpression = (*theField->referringNode->patternType->genGetJNValueFunction)(theEnv,theField->referringNode,LHS);
thePattern->leftHash = AppendExpressions(tempExpression,thePattern->leftHash);
}
}
}
/*======================================================*/
/* Attach the pattern network expressions to the field. */
/*======================================================*/
theField->networkTest = headOfPNExpression;
/*=====================================================*/
/* Attach the join network expressions to the pattern. */
/*=====================================================*/
thePattern->networkTest = CombineExpressions(theEnv,thePattern->networkTest,headOfJNExpression);
}
/****************************************************************************/
/* ExtractAnds: Loops through a single set of subfields bound together by */
/* an & connective constraint in a field and generates expressions needed */
/* for testing conditions in the pattern and join network. */
/****************************************************************************/
static void ExtractAnds(
void *theEnv,
struct lhsParseNode *andField,
int testInPatternNetwork,
struct expr **patternNetTest,
struct expr **joinNetTest,
struct expr **constantSelector,
struct expr **constantValue,
struct nandFrame *theNandFrames)
{
struct expr *newPNTest, *newJNTest, *newConstantSelector, *newConstantValue;
/*=================================================*/
/* Before starting, the subfield has no pattern or */
/* join network expressions associated with it. */
/*=================================================*/
*patternNetTest = NULL;
*joinNetTest = NULL;
*constantSelector = NULL;
*constantValue = NULL;
/*=========================================*/
/* Loop through each of the subfields tied */
/* together by the & constraint. */
/*=========================================*/
for (;
andField != NULL;
andField = andField->right)
{
/*======================================*/
/* Extract the pattern and join network */
/* expressions from the subfield. */
/*======================================*/
ExtractFieldTest(theEnv,andField,testInPatternNetwork,&newPNTest,&newJNTest,
&newConstantSelector,&newConstantValue,theNandFrames);
/*=================================================*/
/* Add the new expressions to the list of pattern */
/* and join network expressions being constructed. */
/*=================================================*/
*patternNetTest = CombineExpressions(theEnv,*patternNetTest,newPNTest);
*joinNetTest = CombineExpressions(theEnv,*joinNetTest,newJNTest);
*constantSelector = CombineExpressions(theEnv,*constantSelector,newConstantSelector);
*constantValue = CombineExpressions(theEnv,*constantValue,newConstantValue);
}
}
/************************************************************************/
/* ExtractFieldTest: Generates the pattern or join network expression */
/* associated with the basic field constraints: constants, predicate, */
/* return value, and variable constraints. Based on the context in */
/* which a constraint is used, some constraints may be tested in the */
/* pattern network while other constraints must be tested in the join */
/* network. Constraints which refer to variables in other patterns */
/* must be tested in the join network. The predicate constraint */
/* associated with a test CE is tested in the join network (even if */
/* all the variables it refers to are contained in the previous */
/* pattern CE). If one of the or'ed constraints in a field refers to */
/* a binding occurrence of a variable in another pattern, then the */
/* other constraints in the field must be tested in the join network */
/* (this is how some constant constraint tests must occasionally be */
/* performed in the join network). */
/************************************************************************/
static void ExtractFieldTest(
void *theEnv,
struct lhsParseNode *theField,
int testInPatternNetwork,
struct expr **patternNetTest,
struct expr **joinNetTest,
struct expr **constantSelector,
struct expr **constantValue,
struct nandFrame *theNandFrames)
{
*patternNetTest = NULL;
*joinNetTest = NULL;
*constantSelector = NULL;
*constantValue = NULL;
/*==========================================================*/
/* Generate a network expression for a constant constraint. */
/*==========================================================*/
if ((theField->type == STRING) || (theField->type == SYMBOL) ||
#if OBJECT_SYSTEM
(theField->type == INSTANCE_NAME) ||
#endif
(theField->type == FLOAT) || (theField->type == INTEGER))
{
if (testInPatternNetwork == TRUE)
{
*patternNetTest = GenPNConstant(theEnv,theField);
if (! theField->negated)
{
*constantSelector = (*theField->patternType->genGetPNValueFunction)(theEnv,theField);
*constantValue = GenConstant(theEnv,theField->type,theField->value);
}
}
else
{ *joinNetTest = GenJNConstant(theEnv,theField,FALSE); } // TBD Remove FALSE
}
/*===========================================================*/
/* Generate a network expression for a predicate constraint. */
/*===========================================================*/
else if (theField->type == PREDICATE_CONSTRAINT)
{
if ((testInPatternNetwork == TRUE) &&
(AllVariablesInExpression(theField->expression,theField->pattern) == TRUE))
{ *patternNetTest = GenPNColon(theEnv,theField); }
else
{ *joinNetTest = GenJNColon(theEnv,theField,FALSE,theNandFrames); } // TBD Remove FALSE
}
/*==============================================================*/
/* Generate a network expression for a return value constraint. */
/*==============================================================*/
else if (theField->type == RETURN_VALUE_CONSTRAINT)
{
if ((testInPatternNetwork == TRUE) &&
(AllVariablesInExpression(theField->expression,theField->pattern) == TRUE))
{ *patternNetTest = GenPNEq(theEnv,theField); }
else
{ *joinNetTest = GenJNEq(theEnv,theField,FALSE,theNandFrames); } // TBD Remove FALSE
}
/*=====================================================================*/
/* Generate a network expression for a variable comparison constraint. */
/*=====================================================================*/
else if ((theField->type == SF_VARIABLE) || (theField->type == MF_VARIABLE))
{
if ((testInPatternNetwork == TRUE) &&
((theField->referringNode != NULL) ?
(theField->referringNode->pattern == theField->pattern) :
FALSE))
{ *patternNetTest = GenPNVariableComparison(theEnv,theField,theField->referringNode); }
else
{
*joinNetTest = GenJNVariableComparison(theEnv,theField,theField->referringNode,FALSE);
AddNandUnification(theEnv,theField,theNandFrames);
}
}
}
/*********************************************************/
/* GenPNConstant: Generates an expression for use in the */
/* pattern network of a data entity (such as a fact or */
/* instance). The expression generated is for comparing */
/* a constant value against a specified slot/field in */
/* the data entity for equality or inequality. */
/*********************************************************/
static struct expr *GenPNConstant(
void *theEnv,
struct lhsParseNode *theField)
{
struct expr *top;
/*===============================================*/
/* If the pattern parser is capable of creating */
/* a specialized test, then call the function to */
/* generate the pattern network test and return */
/* the expression generated. */
/*===============================================*/
if (theField->patternType->genPNConstantFunction != NULL)
{ return (*theField->patternType->genPNConstantFunction)(theEnv,theField); }
/*===================================================*/
/* Otherwise, generate a test which uses the eq/neq */
/* function to compare the pattern field/slot to the */
/* constant and then return the expression. */
/*===================================================*/
if (theField->negated)
{ top = GenConstant(theEnv,FCALL,ExpressionData(theEnv)->PTR_NEQ); }
else
{ top = GenConstant(theEnv,FCALL,ExpressionData(theEnv)->PTR_EQ); }
top->argList = (*theField->patternType->genGetPNValueFunction)(theEnv,theField);
top->argList->nextArg = GenConstant(theEnv,theField->type,theField->value);
return(top);
}
/************************************************************/
/* GenJNConstant: Generates an expression for use in the */
/* join network. The expression generated is for comparing */
/* a constant value against a specified slot/field in the */
/* data entity for equality or inequality. */
/************************************************************/
static struct expr *GenJNConstant(
void *theEnv,
struct lhsParseNode *theField,
int isNand)
{
struct expr *top;
/*===============================================*/
/* If the pattern parser is capable of creating */
/* a specialized test, then call the function to */
/* generate the join network test and return the */
/* expression generated. */
/*===============================================*/
if (theField->patternType->genJNConstantFunction != NULL)
{
if (isNand)
{ return (*theField->patternType->genJNConstantFunction)(theEnv,theField,NESTED_RHS); }
else
{ return (*theField->patternType->genJNConstantFunction)(theEnv,theField,RHS); }
}
/*===================================================*/
/* Otherwise, generate a test which uses the eq/neq */
/* function to compare the pattern field/slot to the */
/* constant and then return the expression. */
/*===================================================*/
if (theField->negated)
{ top = GenConstant(theEnv,FCALL,ExpressionData(theEnv)->PTR_NEQ); }
else
{ top = GenConstant(theEnv,FCALL,ExpressionData(theEnv)->PTR_EQ); }
if (isNand)
{ top->argList = (*theField->patternType->genGetJNValueFunction)(theEnv,theField,NESTED_RHS); }
else
{ top->argList = (*theField->patternType->genGetJNValueFunction)(theEnv,theField,RHS); }
top->argList->nextArg = GenConstant(theEnv,theField->type,theField->value);
return(top);
}
/******************************************************/
/* GenJNColon: Generates an expression for use in the */
/* join network. The expression generated is for a */
/* predicate field constraint (the : constraint). */
/******************************************************/
static struct expr *GenJNColon(
void *theEnv,
struct lhsParseNode *theField,
int isNand,
struct nandFrame *theNandFrames)
{
struct expr *top, *conversion;
/*==================================================*/
/* Replace variables with function calls to extract */
/* the appropriate value from the data entity. */
/*==================================================*/
if (isNand)
{ conversion = GetvarReplace(theEnv,theField->expression,TRUE,theNandFrames); }
else
{ conversion = GetvarReplace(theEnv,theField->expression,FALSE,theNandFrames); }
/*================================================*/
/* If the predicate constraint is negated by a ~, */
/* then wrap a "not" function call around the */
/* expression before returning it. Otherwise, */
/* just return the expression. */
/*================================================*/
if (theField->negated)
{
top = GenConstant(theEnv,FCALL,ExpressionData(theEnv)->PTR_NOT);
top->argList = conversion;
}
else
{ top = conversion; }
return(top);
}
/******************************************************/
/* GenPNColon: Generates an expression for use in the */
/* pattern network. The expression generated is for */
/* a predicate field constraint (the : constraint). */
/******************************************************/
static struct expr *GenPNColon(
void *theEnv,
struct lhsParseNode *theField)
{
struct expr *top, *conversion;
/*==================================================*/
/* Replace variables with function calls to extract */
/* the appropriate value from the data entity. */
/*==================================================*/
conversion = GetfieldReplace(theEnv,theField->expression);
/*================================================*/
/* If the predicate constraint is negated by a ~, */
/* then wrap a "not" function call around the */
/* expression before returning it. Otherwise, */
/* just return the expression. */
/*================================================*/
if (theField->negated)
{
top = GenConstant(theEnv,FCALL,ExpressionData(theEnv)->PTR_NOT);
top->argList = conversion;
}
else
{ top = conversion; }
return(top);
}
/******************************************************/
/* GenJNEq: Generates an expression for use in the */
/* join network. The expression generated is for a */
/* return value field constraint (the = constraint). */
/******************************************************/
static struct expr *GenJNEq(
void *theEnv,
struct lhsParseNode *theField,
int isNand,
struct nandFrame *theNandFrames)
{
struct expr *top, *conversion;
/*==================================================*/
/* Replace variables with function calls to extract */
/* the appropriate value from the data entity. */
/*==================================================*/
if (isNand)
{ conversion = GetvarReplace(theEnv,theField->expression,TRUE,theNandFrames); }
else
{ conversion = GetvarReplace(theEnv,theField->expression,FALSE,theNandFrames); }
/*============================================================*/
/* If the return value constraint is negated by a ~, then use */
/* the neq function to compare the value of the field to the */
/* value returned by the function call. Otherwise, use eq to */
/* compare the two values. */
/*============================================================*/
if (theField->negated)
{ top = GenConstant(theEnv,FCALL,ExpressionData(theEnv)->PTR_NEQ); }
else
{ top = GenConstant(theEnv,FCALL,ExpressionData(theEnv)->PTR_EQ); }
if (isNand)
{ top->argList = (*theField->patternType->genGetJNValueFunction)(theEnv,theField,NESTED_RHS); }
else
{ top->argList = (*theField->patternType->genGetJNValueFunction)(theEnv,theField,RHS); }
top->argList->nextArg = conversion;
return(top);
}
/*******************************************************/
/* GenPNEq: Generates an expression for use in the */
/* pattern network. The expression generated is for a */
/* return value field constraint (the = constraint). */
/*******************************************************/
static struct expr *GenPNEq(
void *theEnv,
struct lhsParseNode *theField)
{
struct expr *top, *conversion;
/*==================================================*/
/* Replace variables with function calls to extract */
/* the appropriate value from the data entity. */
/*==================================================*/
conversion = GetfieldReplace(theEnv,theField->expression);
/*============================================================*/
/* If the return value constraint is negated by a ~, then use */
/* the neq function to compare the value of the field to the */
/* value returned by the function call. Otherwise, use eq to */
/* compare the two values. */
/*============================================================*/
if (theField->negated)
{ top = GenConstant(theEnv,FCALL,ExpressionData(theEnv)->PTR_NEQ); }
else
{ top = GenConstant(theEnv,FCALL,ExpressionData(theEnv)->PTR_EQ); }
top->argList = (*theField->patternType->genGetPNValueFunction)(theEnv,theField);
top->argList->nextArg = conversion;
return(top);
}
/************************************************************************/
/* AddNandUnification: Adds expressions to the nand joins to unify the */
/* variable bindings that need to match from the left and right paths */
/* taken through the join network for not/and CE group. */
/************************************************************************/
globle void AddNandUnification(
void *theEnv,
struct lhsParseNode *nodeList,
struct nandFrame *theNandFrames)
{
struct nandFrame *theFrame;
struct expr *tempExpression;
/*====================================================*/
/* If the reference is to a prior variable within the */
/* same nand group, then there's no need to create an */
/* external network test. */
/*====================================================*/
if (nodeList->beginNandDepth == nodeList->referringNode->beginNandDepth)
{ return; }
/*=========================================*/
/* Don't generate an external network test */
/* if one has already been generated. */
/*=========================================*/
if (nodeList->referringNode->marked)
{ return; }
/*======================================================*/
/* Find the frame to which the test should be attached. */
/*======================================================*/
for (theFrame = theNandFrames;
theFrame != NULL;
theFrame = theFrame->next)
{
if (theFrame->depth >= nodeList->referringNode->beginNandDepth)
{
nodeList->referringNode->marked = TRUE;
tempExpression = GenJNVariableComparison(theEnv,nodeList->referringNode,nodeList->referringNode,TRUE);
theFrame->nandCE->externalNetworkTest = CombineExpressions(theEnv,theFrame->nandCE->externalNetworkTest,tempExpression);
tempExpression = (*nodeList->referringNode->patternType->genGetJNValueFunction)(theEnv,nodeList->referringNode,LHS);
theFrame->nandCE->externalRightHash = AppendExpressions(theFrame->nandCE->externalRightHash,tempExpression);
tempExpression = (*nodeList->referringNode->patternType->genGetJNValueFunction)(theEnv,nodeList->referringNode,LHS);
theFrame->nandCE->externalLeftHash = AppendExpressions(theFrame->nandCE->externalLeftHash,tempExpression);
}
}
}
/*******************************************************************/
/* GetvarReplace: Replaces occurences of variables in expressions */
/* with function calls that will extract the variable's value */
/* from a partial match (i.e. from information stored in the */
/* join network or the activation of the rule). */
/*******************************************************************/
globle struct expr *GetvarReplace(
void *theEnv,
struct lhsParseNode *nodeList,
int isNand,
struct nandFrame *theNandFrames)
{
struct expr *newList;
/*====================================*/
/* Return NULL for a NULL pointer */
/* (i.e. nothing has to be replaced). */
/*====================================*/
if (nodeList == NULL) return(NULL);
/*=====================================================*/
/* Create an expression data structure and recursively */
/* replace variables in its argument list and next */
/* argument links. */
/*=====================================================*/
newList = get_struct(theEnv,expr);
newList->type = nodeList->type;
newList->value = nodeList->value;
newList->nextArg = GetvarReplace(theEnv,nodeList->right,isNand,theNandFrames);
newList->argList = GetvarReplace(theEnv,nodeList->bottom,isNand,theNandFrames);
/*=========================================================*/
/* If the present node being examined is either a local or */
/* global variable, then replace it with a function call */
/* that will return the variable's value. */
/*=========================================================*/
if ((nodeList->type == SF_VARIABLE) || (nodeList->type == MF_VARIABLE))
{
AddNandUnification(theEnv,nodeList,theNandFrames);
/*=============================================================*/
/* Referencing a variable outside the scope of the immediately */
/* enclosing not/and CE requires that the test be performed in */
/* the "join from the right" join. */
/*=============================================================*/
if (isNand)
{
if (nodeList->beginNandDepth > nodeList->referringNode->beginNandDepth)
{
(*nodeList->referringNode->patternType->replaceGetJNValueFunction)
(theEnv,newList,nodeList->referringNode,LHS);
}
else
{
(*nodeList->referringNode->patternType->replaceGetJNValueFunction)
(theEnv,newList,nodeList->referringNode,NESTED_RHS);
}
}
else
{
if (nodeList->joinDepth != nodeList->referringNode->joinDepth)
{
(*nodeList->referringNode->patternType->replaceGetJNValueFunction)
(theEnv,newList,nodeList->referringNode,LHS);
}
else
{
(*nodeList->referringNode->patternType->replaceGetJNValueFunction)
(theEnv,newList,nodeList->referringNode,RHS);
}
}
}
#if DEFGLOBAL_CONSTRUCT
else if (newList->type == GBL_VARIABLE)
{ ReplaceGlobalVariable(theEnv,newList); }
#endif
/*====================================================*/
/* Return the expression with its variables replaced. */
/*====================================================*/
return(newList);
}
/**********************************************************************/
/* GetfieldReplace: Replaces occurences of variables in expressions */
/* with function calls that will extract the variable's value */
/* given a pointer to the data entity that contains the value (i.e. */
/* from information stored in the pattern network). */
/**********************************************************************/
static struct expr *GetfieldReplace(
void *theEnv,
struct lhsParseNode *nodeList)
{
struct expr *newList;
/*====================================*/
/* Return NULL for a NULL pointer */
/* (i.e. nothing has to be replaced). */
/*====================================*/
if (nodeList == NULL) return(NULL);
/*=====================================================*/
/* Create an expression data structure and recursively */
/* replace variables in its argument list and next */
/* argument links. */
/*=====================================================*/
newList = get_struct(theEnv,expr);
newList->type = nodeList->type;
newList->value = nodeList->value;
newList->nextArg = GetfieldReplace(theEnv,nodeList->right);
newList->argList = GetfieldReplace(theEnv,nodeList->bottom);
/*=========================================================*/
/* If the present node being examined is either a local or */
/* global variable, then replace it with a function call */
/* that will return the variable's value. */
/*=========================================================*/
if ((nodeList->type == SF_VARIABLE) || (nodeList->type == MF_VARIABLE))
{
(*nodeList->referringNode->patternType->replaceGetPNValueFunction)
(theEnv,newList,nodeList->referringNode);
}
#if DEFGLOBAL_CONSTRUCT
else if (newList->type == GBL_VARIABLE)
{ ReplaceGlobalVariable(theEnv,newList); }
#endif
/*====================================================*/
/* Return the expression with its variables replaced. */
/*====================================================*/
return(newList);
}
/**************************************************************/
/* GenJNVariableComparison: Generates a join network test for */
/* comparing two variables found in different patterns. */
/**************************************************************/
static struct expr *GenJNVariableComparison(
void *theEnv,
struct lhsParseNode *selfNode,
struct lhsParseNode *referringNode,
int isNand)
{
struct expr *top;
/*========================================================*/
/* If either pattern is missing a function for generating */
/* the appropriate test, then no test is generated. */
/*========================================================*/
if ((selfNode->patternType->genCompareJNValuesFunction == NULL) ||
(referringNode->patternType->genCompareJNValuesFunction == NULL))
{ return(NULL); }
/*=====================================================*/
/* If both patterns are of the same type, then use the */
/* special function for generating the join test. */
/*=====================================================*/
if (selfNode->patternType->genCompareJNValuesFunction ==
referringNode->patternType->genCompareJNValuesFunction)
{
return (*selfNode->patternType->genCompareJNValuesFunction)(theEnv,selfNode,
referringNode,isNand);
}
/*===========================================================*/
/* If the patterns are of different types, then generate a */
/* join test by using the eq/neq function with its arguments */
/* being function calls to retrieve the appropriate values */
/* from the patterns. */
/*===========================================================*/
if (selfNode->negated) top = GenConstant(theEnv,FCALL,ExpressionData(theEnv)->PTR_NEQ);
else top = GenConstant(theEnv,FCALL,ExpressionData(theEnv)->PTR_EQ);
top->argList = (*selfNode->patternType->genGetJNValueFunction)(theEnv,selfNode,RHS);
top->argList->nextArg = (*referringNode->patternType->genGetJNValueFunction)(theEnv,referringNode,LHS);
return(top);
}
/*************************************************************/
/* GenPNVariableComparison: Generates a pattern network test */
/* for comparing two variables found in the same pattern. */
/*************************************************************/
static struct expr *GenPNVariableComparison(
void *theEnv,
struct lhsParseNode *selfNode,
struct lhsParseNode *referringNode)
{
if (selfNode->patternType->genComparePNValuesFunction != NULL)
{
return (*selfNode->patternType->genComparePNValuesFunction)(theEnv,selfNode,referringNode);
}
return(NULL);
}
/************************************************************/
/* AllVariablesInPattern: Determines if all of the variable */
/* references in a field constraint can be referenced */
/* within thepattern in which the field is contained. */
/************************************************************/
static int AllVariablesInPattern(
struct lhsParseNode *orField,
int pattern)
{
struct lhsParseNode *andField;
/*=========================================*/
/* Loop through each of the | constraints. */
/*=========================================*/
for (;
orField != NULL;
orField = orField->bottom)
{
/*=========================================*/
/* Loop through each of the & constraints. */
/*=========================================*/
for (andField = orField;
andField != NULL;
andField = andField->right)
{
/*========================================================*/
/* If a variable is found, make sure the pattern in which */
/* the variable was previously bound is the same as the */
/* pattern being checked. */
/*========================================================*/
if ((andField->type == SF_VARIABLE) || (andField->type == MF_VARIABLE))
{ if (andField->referringNode->pattern != pattern) return(FALSE); }
/*========================================================*/
/* Check predicate and return value constraints to see */
/* that all variables can be referenced from the pattern. */
/*========================================================*/
else if ((andField->type == PREDICATE_CONSTRAINT) ||
(andField->type == RETURN_VALUE_CONSTRAINT))
{