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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
/*******************************************************/
/* "C" Language Integrated Production System */
/* */
/* CLIPS Version 6.40 06/28/17 */
/* */
/* RULE BUILD MODULE */
/*******************************************************/
/*************************************************************/
/* Purpose: Provides routines to ntegrates a set of pattern */
/* and join tests associated with a rule into the pattern */
/* and join networks. The joins are integrated into the */
/* join network by routines in this module. The pattern */
/* is integrated by calling the external routine */
/* associated with the pattern parser that originally */
/* parsed the pattern. */
/* */
/* Principal Programmer(s): */
/* Gary D. Riley */
/* */
/* Contributing Programmer(s): */
/* */
/* Revision History: */
/* */
/* 6.24: Removed INCREMENTAL_RESET compilation flag. */
/* */
/* Corrected code to remove compiler warnings. */
/* */
/* 6.30: Changes to constructing join network. */
/* */
/* Added support for hashed memories. */
/* */
/* 6.31: Fix for nand crash */
/* */
/* DR#882 Logical retraction not working if */
/* logical CE starts with test CE. */
/* */
/* 6.40: Pragma once and other inclusion changes. */
/* */
/* Added support for booleans with <stdbool.h>. */
/* */
/* Removed use of void pointers for specific */
/* data structures. */
/* */
/* Incremental reset is always enabled. */
/* */
/*************************************************************/
#include "setup.h"
#if DEFRULE_CONSTRUCT && (! RUN_TIME) && (! BLOAD_ONLY)
#include <stdio.h>
#include <stdlib.h>
#include "constant.h"
#include "envrnmnt.h"
#include "constrct.h"
#include "drive.h"
#include "incrrset.h"
#include "memalloc.h"
#include "pattern.h"
#include "prntutil.h"
#include "reteutil.h"
#include "router.h"
#include "rulebld.h"
#include "rulepsr.h"
#include "watch.h"
/***************************************/
/* LOCAL INTERNAL FUNCTION DEFINITIONS */
/***************************************/
static struct joinNode *FindShareableJoin(struct joinLink *,struct joinNode *,bool,void *,bool,bool,
bool,bool,struct expr *,struct expr *,
struct expr *,struct expr *);
static bool TestJoinForReuse(struct joinNode *,bool,bool,
bool,bool,struct expr *,struct expr *,
struct expr *,struct expr *);
static struct joinNode *CreateNewJoin(Environment *,struct expr *,struct expr *,struct joinNode *,void *,
bool,bool,bool,struct expr *,struct expr *);
/****************************************************************/
/* ConstructJoins: Integrates a set of pattern and join tests */
/* associated with a rule into the pattern and join networks. */
/****************************************************************/
struct joinNode *ConstructJoins(
Environment *theEnv,
int logicalJoin,
struct lhsParseNode *theLHS,
int startDepth,
struct joinNode *lastJoin,
bool tryToReuse,
bool firstJoin)
{
struct patternNodeHeader *lastPattern;
struct joinNode *listOfJoins = NULL;
struct joinNode *oldJoin;
int joinNumber = 1;
bool isLogical, isExists;
struct joinNode *lastRightJoin;
bool lastIteration = false;
unsigned short rhsType;
struct expr *leftHash, *rightHash;
void *rhsStruct;
struct lhsParseNode *nextLHS;
struct expr *networkTest, *secondaryNetworkTest, *secondaryExternalTest;
bool joinFromTheRight;
struct joinLink *theLinks;
bool useLinks;
if (theLHS == NULL)
{
lastJoin = FindShareableJoin(DefruleData(theEnv)->RightPrimeJoins,NULL,true,NULL,true,
false,false,false,NULL,NULL,NULL,NULL);
if (lastJoin == NULL)
{ lastJoin = CreateNewJoin(theEnv,NULL,NULL,NULL,NULL,false,false,false,NULL,NULL); }
}
/*=====================================================*/
/* Process each pattern CE in the rule. At this point, */
/* there should be no and/or/not CEs in the LHS. */
/*=====================================================*/
while (theLHS != NULL)
{
/*======================================================*/
/* Find the beginning of the next group of patterns. If */
/* the current pattern is not the beginning of a "join */
/* from the right" group of patterns, then the next */
/* pattern is the next pattern. Otherwise skip over all */
/* the patterns that belong to the group of subjoins. */
/*======================================================*/
nextLHS = theLHS->bottom;
secondaryExternalTest = NULL;
if (theLHS->endNandDepth > startDepth)
{
while ((nextLHS != NULL) &&
(nextLHS->endNandDepth > startDepth))
{ nextLHS = nextLHS->bottom; }
/*====================================================*/
/* Variable nextLHS is now pointing to the end of the */
/* not/and group beginning with variable theLHS. If */
/* the end depth of the group is less than the depth */
/* of the current enclosing not/and group, then this */
/* is the last iteration for the enclosing group. */
/*====================================================*/
if (nextLHS != NULL)
{
if (nextLHS->endNandDepth < startDepth)
{ lastIteration = true; }
}
if (! lastIteration)
{
if (nextLHS != NULL)
{ nextLHS = nextLHS->bottom; }
if ((nextLHS != NULL) &&
(nextLHS->pnType == TEST_CE_NODE) &&
(nextLHS->beginNandDepth >= startDepth))
{
if (nextLHS->endNandDepth < startDepth)
{ lastIteration = true; }
secondaryExternalTest = nextLHS->networkTest;
nextLHS = nextLHS->bottom;
}
}
}
else if (theLHS->endNandDepth == startDepth)
{
if (nextLHS == NULL)
{ lastIteration = true; }
else if ((nextLHS->pnType == TEST_CE_NODE) &&
(nextLHS->endNandDepth < startDepth))
{ lastIteration = true; }
}
else // theLHS->endNandDepth < startDepth
{
lastIteration = true;
}
/*===============================================*/
/* If the pattern is a join from the right, then */
/* construct the subgroup of patterns and use */
/* that as the RHS of the join to be added. */
/*===============================================*/
if (theLHS->beginNandDepth > startDepth)
{
joinFromTheRight = true;
isExists = theLHS->existsNand;
lastRightJoin = ConstructJoins(theEnv,logicalJoin,theLHS,startDepth+1,lastJoin,tryToReuse,firstJoin);
rhsStruct = lastRightJoin;
rhsType = 0;
lastPattern = NULL;
networkTest = theLHS->externalNetworkTest;
secondaryNetworkTest = secondaryExternalTest;
leftHash = theLHS->externalLeftHash;
rightHash = theLHS->externalRightHash;
}
/*=======================================================*/
/* Otherwise, add the pattern to the appropriate pattern */
/* network and use the pattern node containing the alpha */
/* memory as the RHS of the join to be added. */
/*=======================================================*/
else if (theLHS->right == NULL)
{
joinFromTheRight = false;
rhsType = 0;
lastPattern = NULL;
rhsStruct = NULL;
lastRightJoin = NULL;
isExists = theLHS->exists;
networkTest = theLHS->networkTest;
secondaryNetworkTest = theLHS->secondaryNetworkTest;
leftHash = NULL;
rightHash = NULL;
}
else
{
joinFromTheRight = false;
rhsType = theLHS->patternType->positionInArray;
lastPattern = (*theLHS->patternType->addPatternFunction)(theEnv,theLHS);
rhsStruct = lastPattern;
lastRightJoin = NULL;
isExists = theLHS->exists;
networkTest = theLHS->networkTest;
secondaryNetworkTest = theLHS->secondaryNetworkTest;
leftHash = theLHS->leftHash;
rightHash = theLHS->rightHash;
}
/*======================================================*/
/* Determine if the join being added is a logical join. */
/*======================================================*/
if ((startDepth == 1) && (joinNumber == logicalJoin)) isLogical = true;
else isLogical = false;
/*===============================================*/
/* Get the list of joins which could potentially */
/* be reused in place of the join being added. */
/*===============================================*/
useLinks = true;
if (lastJoin != NULL)
{ theLinks = lastJoin->nextLinks; }
else if (theLHS->right == NULL)
{ theLinks = DefruleData(theEnv)->RightPrimeJoins; }
else if (lastPattern != NULL)
{
listOfJoins = lastPattern->entryJoin;
theLinks = NULL;
useLinks = false;
}
else
{ theLinks = lastRightJoin->nextLinks; }
/*=======================================================*/
/* Determine if the next join to be added can be shared. */
/*=======================================================*/
if ((tryToReuse == true) &&
((oldJoin = FindShareableJoin(theLinks,listOfJoins,useLinks,rhsStruct,firstJoin,
theLHS->negated,isExists,isLogical,
networkTest,secondaryNetworkTest,
leftHash,rightHash)) != NULL) )
{
#if DEBUGGING_FUNCTIONS
if ((GetWatchItem(theEnv,"compilations") == 1) && GetPrintWhileLoading(theEnv))
{ WriteString(theEnv,STDOUT,"=j"); }
#endif
lastJoin = oldJoin;
}
else
{
tryToReuse = false;
if (! joinFromTheRight)
{
lastJoin = CreateNewJoin(theEnv,networkTest,secondaryNetworkTest,lastJoin,
lastPattern,false,theLHS->negated, isExists,
leftHash,rightHash);
lastJoin->rhsType = rhsType;
}
else
{
lastJoin = CreateNewJoin(theEnv,networkTest,secondaryNetworkTest,lastJoin,
lastRightJoin,true,theLHS->negated, isExists,
leftHash,rightHash);
lastJoin->rhsType = rhsType;
}
}
/*============================================*/
/* If we've reached the end of the subgroup, */
/* then return the last join of the subgroup. */
/*============================================*/
if (lastIteration)
{ break; }
/*=======================================*/
/* Move on to the next join to be added. */
/*=======================================*/
theLHS = nextLHS;
joinNumber++;
firstJoin = false;
}
/*=================================================*/
/* Add the final join which stores the activations */
/* of the rule. This join is never shared. */
/*=================================================*/
if (startDepth == 1)
{
lastJoin = CreateNewJoin(theEnv,NULL,NULL,lastJoin,NULL,
false,false,false,NULL,NULL);
}
/*===================================================*/
/* If compilations are being watched, put a carriage */
/* return after all of the =j's and +j's */
/*===================================================*/
#if DEBUGGING_FUNCTIONS
if ((startDepth == 1) &&
(GetWatchItem(theEnv,"compilations") == 1) &&
GetPrintWhileLoading(theEnv))
{ WriteString(theEnv,STDOUT,"\n"); }
#endif
/*=============================*/
/* Return the last join added. */
/*=============================*/
return(lastJoin);
}
/****************************************************************/
/* AttachTestCEsToPatternCEs: Attaches the expressions found in */
/* test CEs to the closest preceeding pattern CE that is not */
/* negated and is at the same not/and depth. */
/****************************************************************/
void AttachTestCEsToPatternCEs(
Environment *theEnv,
struct lhsParseNode *theLHS)
{
struct lhsParseNode *lastNode, *tempNode, *lastLastNode;
if (theLHS == NULL) return;
/*=============================================================*/
/* Attach test CEs that can be attached directly to a pattern. */
/*=============================================================*/
lastLastNode = NULL;
lastNode = theLHS;
theLHS = lastNode->bottom;
while (theLHS != NULL)
{
/*================================================================*/
/* Skip over any CE that's not a TEST CE as we're only interested */
/* in attaching a TEST CE to a preceding pattern CE. Update the */
/* variables that track the preceding pattern. */
/*================================================================*/
if (theLHS->pnType != TEST_CE_NODE)
{
lastLastNode = lastNode;
lastNode = theLHS;
theLHS = theLHS->bottom;
continue;
}
/*=====================================================*/
/* If this is the beginning of a new NOT/AND CE group, */
/* then we can't attach this TEST CE to a preceding */
/* pattern CE and should skip over it. */
/*=====================================================*/
/*================================================*/
/* Case #2 */
/* Pattern CE */
/* Depth Begin: N */
/* Depth End: N */
/* Test CE */
/* Depth Begin: M where M > N */
/* Depth End: - */
/* */
/* (defrule example */
/* (a) */
/* (not (and (test (> 1 0)) */
/* (b))) */
/* =>) */
/* */
/* Case #8 */
/* Pattern CE */
/* Depth Begin: N */
/* Depth End: M where M < N */
/* Test CE */
/* Depth Begin: R where R > M */
/* Depth End: - */
/* */
/* (defrule example */
/* (not (and (a) */
/* (c))) */
/* (not (and (test (> 1 0)) */
/* (b))) */
/* =>) */
/* */
/* This situation will not occur with the current */
/* implementation. The initial pattern will be */
/* added before the test CE so that there is a */
/* pattern CE beginning the not/and or exists/and */
/* pattern group. */
/*================================================*/
if (theLHS->beginNandDepth > lastNode->endNandDepth)
{
lastLastNode = lastNode;
lastNode = theLHS;
theLHS = theLHS->bottom;
continue;
}
/*==============================================================*/
/* If the preceding pattern was the end of a NOT/AND CE group, */
/* then we can't attach this TEST CE to a preceding pattern and */
/* should skip over it. The logic for handling the test CE will */
/* be triggered when the joins are constructed. Note that the */
/* endNandDepth will never be greater than the beginNandDepth. */
/*==============================================================*/
if (lastNode->beginNandDepth > lastNode->endNandDepth)
{
lastLastNode = lastNode;
lastNode = theLHS;
theLHS = theLHS->bottom;
continue;
}
/*===================================================*/
/* If the TEST CE does not close the preceding CE... */
/*===================================================*/
/*===================================================*/
/* Case #1 */
/* Pattern CE */
/* Depth Begin: N */
/* Depth End: N */
/* Test CE */
/* Depth Begin: N */
/* Depth End: N */
/* */
/* (defrule example */
/* (a ?x) */
/* (test (> ?x 0)) */
/* =>) */
/* */
/* The test expression can be directly attached to */
/* the network expression for the preceding pattern. */
/*===================================================*/
if (theLHS->beginNandDepth == theLHS->endNandDepth)
{
/*==============================================================*/
/* If the preceding pattern was a NOT or EXISTS CE containing */
/* a single pattern, then attached the TEST CE to the secondary */
/* test, otherwise combine it with the primary network test. */
/*==============================================================*/
if (lastNode->negated)
{
lastNode->secondaryNetworkTest =
CombineExpressions(theEnv,lastNode->secondaryNetworkTest,theLHS->networkTest);
}
else
{
lastNode->networkTest =
CombineExpressions(theEnv,lastNode->networkTest,theLHS->networkTest);
}
}
/*=================================================================*/
/* Otherwise the TEST CE closes a prior NOT/AND or EXISTS/AND CE. */
/*=================================================================*/
/*==================================================*/
/* If these are the first two patterns in the rule. */
/*==================================================*/
/*=========*/
/* Case #3 */
/*=========*/
else if (lastLastNode == NULL)
{
/*=========================================================*/
/* The prior pattern is a single pattern within a not/and. */
/* */
/* (defrule example */
/* (not (and (b) */
/* (test (= 1 1)))) */
/* =>) */
/* */
/* Collapse the nand pattern. */
/*=========================================================*/
if ((lastNode->negated == false) &&
(lastNode->existsNand == false))
{
lastNode->beginNandDepth = theLHS->endNandDepth;
lastNode->negated = true;
lastNode->networkTest =
CombineExpressions(theEnv,lastNode->networkTest,lastNode->externalNetworkTest);
lastNode->networkTest =
CombineExpressions(theEnv,lastNode->networkTest,theLHS->networkTest);
lastNode->externalNetworkTest = NULL;
}
/*=================================================================*/
/* The prior pattern is a single negated pattern within a not/and. */
/* */
/* (defrule example */
/* (not (and (not (b)) */
/* (test (= 1 1)))) */
/* =>) */
/* */
/*=================================================================*/
else if ((lastNode->negated == true) &&
(lastNode->exists == false) &&
(lastNode->existsNand == false))
{
lastNode->secondaryNetworkTest =
CombineExpressions(theEnv,lastNode->secondaryNetworkTest,theLHS->networkTest);
}
/*================================================================*/
/* The prior pattern is a single exists pattern within a not/and. */
/* */
/* (defrule example */
/* (not (and (exists (b)) */
/* (test (= 1 1)))) */
/* =>) */
/* */
/*================================================================*/
else if ((lastNode->negated == true) &&
(lastNode->exists == true) &&
(lastNode->existsNand == false))
{
lastNode->secondaryNetworkTest =
CombineExpressions(theEnv,lastNode->secondaryNetworkTest,theLHS->networkTest);
}
/*=============================================================*/
/* The prior pattern is a single pattern within an exists/and. */
/* */
/* (defrule example */
/* (exists (and (b) */
/* (test (= 1 1)))) */
/* =>) */
/* */
/* Collapse the exists pattern. */
/*=============================================================*/
else if ((lastNode->negated == false) &&
(lastNode->exists == false) &&
(lastNode->existsNand == true))
{
lastNode->beginNandDepth = theLHS->endNandDepth;
lastNode->existsNand = false;
lastNode->exists = true;
lastNode->negated = true;
/*===================================================*/
/* For the first two patterns, there shouldn't be an */
/* externalNetwork test, but this code is included */
/* to match the other cases where patterns are */
/* collapsed. */
/*===================================================*/
lastNode->networkTest =
CombineExpressions(theEnv,lastNode->networkTest,lastNode->externalNetworkTest);
lastNode->networkTest =
CombineExpressions(theEnv,lastNode->networkTest,theLHS->networkTest);
lastNode->externalNetworkTest = NULL;
}
/*=======================================*/
/* The prior pattern is a single negated */
/* pattern within an exists/and. */
/* */
/* (defrule example */
/* (exists (and (not (b)) */
/* (test (= 1 1)))) */
/* =>) */
/* */
/*=======================================*/
else if ((lastNode->negated == true) &&
(lastNode->exists == false) &&
(lastNode->existsNand == true))
{
lastNode->secondaryNetworkTest =
CombineExpressions(theEnv,lastNode->secondaryNetworkTest,theLHS->networkTest);
}
/*======================================*/
/* The prior pattern is a single exists */
/* pattern within an exists/and. */
/* */
/* (defrule example */
/* (exists (and (exists (b)) */
/* (test (= 1 1)))) */
/* =>) */
/* */
/* Collapse the exists pattern. */
/*======================================*/
else if ((lastNode->negated == true) &&
(lastNode->exists == true) &&
(lastNode->existsNand == true))
{
lastNode->beginNandDepth = theLHS->endNandDepth;
lastNode->existsNand = false;
/*===================================================*/
/* For the first two patterns, there shouldn't be an */
/* externalNetwork test, but this code is included */
/* to match the other cases where patterns are */
/* collapsed. */
/*===================================================*/
lastNode->secondaryNetworkTest =
CombineExpressions(theEnv,lastNode->secondaryNetworkTest,lastNode->externalNetworkTest);
lastNode->secondaryNetworkTest =
CombineExpressions(theEnv,lastNode->secondaryNetworkTest,theLHS->networkTest);
lastNode->externalNetworkTest = NULL;
}
/*==============================================*/
/* Unhandled case which should not be possible. */
/*==============================================*/
else
{
SystemError(theEnv,"RULEBLD",1);
ExitRouter(theEnv,EXIT_FAILURE);
}
}
/*==============================================*/
/* Otherwise, there are two preceding patterns. */
/*==============================================*/
/*====================================*/
/* Case #4 */
/* Pattern CE */
/* Depth Begin: - */
/* Depth End: N */
/* Pattern CE */
/* Depth Begin: N */
/* Depth End: N */
/* Test CE */
/* Depth Begin: N */
/* Depth End: M where M < N */
/*====================================*/
else if (lastLastNode->endNandDepth == theLHS->beginNandDepth)
{
/*==============================================================*/
/* If the preceding pattern was a NOT or EXISTS CE containing */
/* a single pattern, then attached the TEST CE to the secondary */
/* test, otherwise combine it with the primary network test. */
/*==============================================================*/
if (lastNode->negated)
{
lastNode->secondaryNetworkTest =
CombineExpressions(theEnv,lastNode->secondaryNetworkTest,theLHS->networkTest);
}
else
{
lastNode->networkTest =
CombineExpressions(theEnv,lastNode->networkTest,theLHS->networkTest);
}
}
/*====================================*/
/* Case #5 */
/* Pattern CE */
/* Depth Begin: - */
/* Depth End: R where R < N */
/* Pattern CE */
/* Depth Begin: N */
/* Depth End: N */
/* Test CE */
/* Depth Begin: N */
/* Depth End: M where M < N */
/*====================================*/
else if (lastLastNode->endNandDepth < theLHS->beginNandDepth)
{
/*=========================================================*/
/* The prior pattern is a single pattern within a not/and: */
/* */
/* (defrule example */
/* (a) */
/* (not (and (b) */
/* (test (= 1 1)))) */
/* =>) */
/* */
/* The test expression can be directly attached to the */
/* network expression for the pattern and the pattern */
/* group can be collapsed into a single negated pattern. */
/*=========================================================*/
if ((lastNode->negated == false) &&
(lastNode->existsNand == false))
{
/*====================*/
/* Use max of R and M */
/*====================*/
if (lastLastNode->endNandDepth > theLHS->endNandDepth)
{ lastNode->beginNandDepth = lastLastNode->endNandDepth; }
else
{ lastNode->beginNandDepth = theLHS->endNandDepth; }
lastNode->negated = true;
lastNode->networkTest =
CombineExpressions(theEnv,lastNode->networkTest,lastNode->externalNetworkTest);
lastNode->networkTest =
CombineExpressions(theEnv,lastNode->networkTest,theLHS->networkTest);
lastNode->externalNetworkTest = NULL;
}
/*=================================================================*/
/* The prior pattern is a single negated pattern within a not/and. */
/* */
/* (defrule example */
/* (a) */
/* (not (and (not (b)) */
/* (test (= 1 1)))) */
/* =>) */
/*=================================================================*/
else if ((lastNode->negated == true) &&
(lastNode->exists == false) &&
(lastNode->existsNand == false))
{
lastNode->secondaryNetworkTest =
CombineExpressions(theEnv,lastNode->secondaryNetworkTest,theLHS->networkTest);
}
/*================================================================*/
/* The prior pattern is a single exists pattern within a not/and. */
/* */
/* (defrule example */
/* (a) */
/* (not (and (exists (b)) */
/* (test (= 1 1)))) */
/* =>) */
/*================================================================*/
else if ((lastNode->negated == true) &&
(lastNode->exists == true) &&
(lastNode->existsNand == false))
{
lastNode->secondaryNetworkTest =
CombineExpressions(theEnv,lastNode->secondaryNetworkTest,theLHS->networkTest);
}
/*=============================================================*/
/* The prior pattern is a single pattern within an exists/and. */
/* */
/* (defrule example */
/* (a) */
/* (exists (and (b) */
/* (test (= 1 1)))) */
/* =>) */
/* */
/* The test expression can be directly attached to the */
/* network expression for the pattern and the pattern */
/* group can be collapsed into a single exists pattern. */
/*=============================================================*/
else if ((lastNode->negated == false) &&
(lastNode->exists == false) &&
(lastNode->existsNand == true))
{
if (lastLastNode->endNandDepth > theLHS->endNandDepth)
{ lastNode->beginNandDepth = lastLastNode->endNandDepth; }
else
{ lastNode->beginNandDepth = theLHS->endNandDepth; }
lastNode->existsNand = false;
lastNode->exists = true;
lastNode->negated = true;
lastNode->networkTest =
CombineExpressions(theEnv,lastNode->networkTest,lastNode->externalNetworkTest);
lastNode->networkTest =
CombineExpressions(theEnv,lastNode->networkTest,theLHS->networkTest);
lastNode->externalNetworkTest = NULL;
}
/*=======================================*/
/* The prior pattern is a single negated */
/* pattern within an exists/and. */
/* */
/* (defrule example */
/* (a) */
/* (exists (and (not (b)) */
/* (test (= 1 1)))) */
/* =>) */
/* */
/*=======================================*/
else if ((lastNode->negated == true) &&
(lastNode->exists == false) &&
(lastNode->existsNand == true))
{
lastNode->secondaryNetworkTest =
CombineExpressions(theEnv,lastNode->secondaryNetworkTest,theLHS->networkTest);
}
/*======================================*/
/* The prior pattern is a single exists */
/* pattern within an exists/and. */
/* */
/* (defrule example */
/* (a) */
/* (exists (and (exists (b)) */
/* (test (= 1 1)))) */
/* =>) */
/* */
/*======================================*/
else if ((lastNode->negated == true) &&
(lastNode->exists == true) &&
(lastNode->existsNand == true))
{
lastNode->secondaryNetworkTest =
CombineExpressions(theEnv,lastNode->secondaryNetworkTest,theLHS->networkTest);
}
/*==============================================*/
/* Unhandled case which should not be possible. */
/*==============================================*/
else
{
SystemError(theEnv,"RULEBLD",2);
ExitRouter(theEnv,EXIT_FAILURE);
}
}
/*==============================================*/
/* Unhandled case which should not be possible. */
/*==============================================*/
else
{
SystemError(theEnv,"RULEBLD",3);
ExitRouter(theEnv,EXIT_FAILURE);
}
/*=================================================*/
/* Remove the TEST CE and continue to the next CE. */
/*=================================================*/
theLHS->networkTest = NULL;
tempNode = theLHS->bottom;
theLHS->bottom = NULL;
lastNode->bottom = tempNode;
lastNode->endNandDepth = theLHS->endNandDepth;
ReturnLHSParseNodes(theEnv,theLHS);
theLHS = tempNode;
}
}
/********************************************************************/
/* FindShareableJoin: Determines whether a join exists that can be */
/* reused for the join currently being added to the join network. */
/* Returns a pointer to the join to be shared if one if found, */
/* otherwise returns a NULL pointer. */
/********************************************************************/
static struct joinNode *FindShareableJoin(
struct joinLink *theLinks,
struct joinNode *listOfJoins,
bool useLinks,
void *rhsStruct,
bool firstJoin,
bool negatedRHS,
bool existsRHS,
bool isLogical,
struct expr *joinTest,
struct expr *secondaryJoinTest,
struct expr *leftHash,
struct expr *rightHash)
{
/*========================================*/
/* Loop through all of the joins in the */
/* list of potential candiates for reuse. */
/*========================================*/
if (useLinks)
{
if (theLinks != NULL)
{ listOfJoins = theLinks->join; }
else
{ listOfJoins = NULL; }
}
while (listOfJoins != NULL)
{
/*=========================================================*/
/* If the join being tested for reuse is connected on the */
/* RHS to the end node of the pattern node associated with */
/* the join to be added, then determine if the join can */
/* be reused. If so, return the join. */
/*=========================================================*/
if (listOfJoins->rightSideEntryStructure == rhsStruct)
{
if (TestJoinForReuse(listOfJoins,firstJoin,negatedRHS,existsRHS,
isLogical,joinTest,secondaryJoinTest,
leftHash,rightHash))
{ return(listOfJoins); }
}
/*====================================================*/
/* Move on to the next potential candidate. Note that */
/* the rightMatchNode link is used for traversing */
/* through the candidates for the first join of a */
/* rule and that rightDriveNode link is used for */
/* traversing through the candidates for subsequent */
/* joins of a rule. */
/*====================================================*/
if (useLinks)
{
theLinks = theLinks->next;
if (theLinks != NULL)
{ listOfJoins = theLinks->join; }
else
{ listOfJoins = NULL; }
}
else
{ listOfJoins = listOfJoins->rightMatchNode; }
}
/*================================*/
/* Return a NULL pointer, since a */
/* reusable join was not found. */
/*================================*/
return NULL;
}
/**************************************************************/
/* TestJoinForReuse: Determines if the specified join can be */
/* shared with a join being added for a rule being defined. */
/* Returns true if the join can be shared, otherwise false. */
/**************************************************************/
static bool TestJoinForReuse(
struct joinNode *testJoin,
bool firstJoin,
bool negatedRHS,
bool existsRHS,
bool isLogical,
struct expr *joinTest,
struct expr *secondaryJoinTest,
struct expr *leftHash,
struct expr *rightHash)
{
/*==================================================*/
/* The first join of a rule may only be shared with */
/* a join that has its firstJoin field set to true. */
/*==================================================*/
if (testJoin->firstJoin != firstJoin) return false;
/*========================================================*/
/* A join connected to a not CE may only be shared with a */
/* join that has its patternIsNegated field set to true. */
/*========================================================*/
if ((testJoin->patternIsNegated != negatedRHS) && (! existsRHS)) return false;
/*==========================================================*/
/* A join connected to an exists CE may only be shared with */
/* a join that has its patternIsExists field set to true. */
/*==========================================================*/
if (testJoin->patternIsExists != existsRHS) return false;
/*==========================================================*/
/* If the join added is associated with a logical CE, then */
/* either the join to be shared must be associated with a */
/* logical CE or the beta memory must be empty (since */
/* joins associate an extra field with each partial match). */
/*==========================================================*/
if ((isLogical == true) &&
(testJoin->logicalJoin == false) &&
BetaMemoryNotEmpty(testJoin))
{ return false; }
/*===============================================================*/
/* The expression associated with the join must be identical to */
/* the networkTest expression stored with the join to be shared. */
/*===============================================================*/
if (IdenticalExpression(testJoin->networkTest,joinTest) != true)
{ return false; }
if (IdenticalExpression(testJoin->secondaryNetworkTest,secondaryJoinTest) != true)
{ return false; }
/*====================================================================*/
/* The alpha memory hashing values associated with the join must be */
/* identical to the hashing values stored with the join to be shared. */
/*====================================================================*/
if (IdenticalExpression(testJoin->leftHash,leftHash) != true)
{ return false; }
if (IdenticalExpression(testJoin->rightHash,rightHash) != true)
{ return false; }
/*=============================================*/
/* The join can be shared since all conditions */
/* for sharing have been satisfied. */
/*=============================================*/
return true;
}
/*************************************************************************/
/* CreateNewJoin: Creates a new join and links it into the join network. */
/*************************************************************************/
static struct joinNode *CreateNewJoin(
Environment *theEnv,
struct expr *joinTest,
struct expr *secondaryJoinTest,
struct joinNode *lhsEntryStruct,
void *rhsEntryStruct,
bool joinFromTheRight,
bool negatedRHSPattern,
bool existsRHSPattern,
struct expr *leftHash,
struct expr *rightHash)
{
struct joinNode *newJoin;
struct joinLink *theLink;
/*===============================================*/
/* If compilations are being watch, print +j to */
/* indicate that a new join has been created for */
/* this pattern of the rule (i.e. a join could */
/* not be shared with another rule. */
/*===============================================*/
#if DEBUGGING_FUNCTIONS
if ((GetWatchItem(theEnv,"compilations") == 1) && GetPrintWhileLoading(theEnv))
{ WriteString(theEnv,STDOUT,"+j"); }
#endif
/*======================*/
/* Create the new join. */
/*======================*/
newJoin = get_struct(theEnv,joinNode);
/*======================================================*/
/* The first join of a rule does not have a beta memory */
/* unless the RHS pattern is an exists or not CE. */
/*======================================================*/
if ((lhsEntryStruct != NULL) || existsRHSPattern || negatedRHSPattern || joinFromTheRight)
{
if (leftHash == NULL)
{
newJoin->leftMemory = get_struct(theEnv,betaMemory);
newJoin->leftMemory->beta = (struct partialMatch **) genalloc(theEnv,sizeof(struct partialMatch *));
newJoin->leftMemory->beta[0] = NULL;
newJoin->leftMemory->last = NULL;
newJoin->leftMemory->size = 1;
newJoin->leftMemory->count = 0;
}
else
{
newJoin->leftMemory = get_struct(theEnv,betaMemory);
newJoin->leftMemory->beta = (struct partialMatch **) genalloc(theEnv,sizeof(struct partialMatch *) * INITIAL_BETA_HASH_SIZE);
memset(newJoin->leftMemory->beta,0,sizeof(struct partialMatch *) * INITIAL_BETA_HASH_SIZE);
newJoin->leftMemory->last = NULL;
newJoin->leftMemory->size = INITIAL_BETA_HASH_SIZE;
newJoin->leftMemory->count = 0;
}
/*===========================================================*/
/* If the first join of a rule connects to an exists or not */
/* CE, then we create an empty partial match for the usually */
/* empty left beta memory so that we can track the current */
/* current right memory partial match satisfying the CE. */
/*===========================================================*/
if ((lhsEntryStruct == NULL) && (existsRHSPattern || negatedRHSPattern || joinFromTheRight))
{
newJoin->leftMemory->beta[0] = CreateEmptyPartialMatch(theEnv);
newJoin->leftMemory->beta[0]->owner = newJoin;
newJoin->leftMemory->count = 1;
}
}
else
{ newJoin->leftMemory = NULL; }
if (joinFromTheRight)
{
if (leftHash == NULL)
{
newJoin->rightMemory = get_struct(theEnv,betaMemory);
newJoin->rightMemory->beta = (struct partialMatch **) genalloc(theEnv,sizeof(struct partialMatch *));
newJoin->rightMemory->last = (struct partialMatch **) genalloc(theEnv,sizeof(struct partialMatch *));
newJoin->rightMemory->beta[0] = NULL;
newJoin->rightMemory->last[0] = NULL;
newJoin->rightMemory->size = 1;
newJoin->rightMemory->count = 0;
}
else
{
newJoin->rightMemory = get_struct(theEnv,betaMemory);
newJoin->rightMemory->beta = (struct partialMatch **) genalloc(theEnv,sizeof(struct partialMatch *) * INITIAL_BETA_HASH_SIZE);
newJoin->rightMemory->last = (struct partialMatch **) genalloc(theEnv,sizeof(struct partialMatch *) * INITIAL_BETA_HASH_SIZE);
memset(newJoin->rightMemory->beta,0,sizeof(struct partialMatch *) * INITIAL_BETA_HASH_SIZE);
memset(newJoin->rightMemory->last,0,sizeof(struct partialMatch *) * INITIAL_BETA_HASH_SIZE);
newJoin->rightMemory->size = INITIAL_BETA_HASH_SIZE;
newJoin->rightMemory->count = 0;
}
}
else if (rhsEntryStruct == NULL)
{
newJoin->rightMemory = get_struct(theEnv,betaMemory);
newJoin->rightMemory->beta = (struct partialMatch **) genalloc(theEnv,sizeof(struct partialMatch *));
newJoin->rightMemory->last = (struct partialMatch **) genalloc(theEnv,sizeof(struct partialMatch *));
newJoin->rightMemory->beta[0] = CreateEmptyPartialMatch(theEnv);
newJoin->rightMemory->beta[0]->owner = newJoin;
newJoin->rightMemory->beta[0]->rhsMemory = true;
newJoin->rightMemory->last[0] = newJoin->rightMemory->beta[0];
newJoin->rightMemory->size = 1;
newJoin->rightMemory->count = 1;
}
else
{ newJoin->rightMemory = NULL; }
newJoin->nextLinks = NULL;
newJoin->joinFromTheRight = joinFromTheRight;
if (existsRHSPattern)
{ newJoin->patternIsNegated = false; }
else
{ newJoin->patternIsNegated = negatedRHSPattern; }
newJoin->patternIsExists = existsRHSPattern;
newJoin->marked = false;
newJoin->initialize = true;
newJoin->logicalJoin = false;
newJoin->ruleToActivate = NULL;
newJoin->memoryLeftAdds = 0;
newJoin->memoryRightAdds = 0;
newJoin->memoryLeftDeletes = 0;
newJoin->memoryRightDeletes = 0;
newJoin->memoryCompares = 0;
/*==============================================*/
/* Install the expressions used to determine */
/* if a partial match satisfies the constraints */
/* associated with this join. */
/*==============================================*/
newJoin->networkTest = AddHashedExpression(theEnv,joinTest);
newJoin->secondaryNetworkTest = AddHashedExpression(theEnv,secondaryJoinTest);
/*=====================================================*/
/* Install the expression used to hash the beta memory */
/* partial match to determine the location to search */
/* in the alpha memory. */
/*=====================================================*/
newJoin->leftHash = AddHashedExpression(theEnv,leftHash);
newJoin->rightHash = AddHashedExpression(theEnv,rightHash);
/*============================================================*/
/* Initialize the values associated with the LHS of the join. */
/*============================================================*/
newJoin->lastLevel = lhsEntryStruct;
if (lhsEntryStruct == NULL)
{
newJoin->firstJoin = true;
newJoin->depth = 1;
}
else
{
newJoin->firstJoin = false;
newJoin->depth = lhsEntryStruct->depth;
newJoin->depth++; /* To work around Sparcworks C compiler bug */
theLink = get_struct(theEnv,joinLink);
theLink->join = newJoin;
theLink->enterDirection = LHS;
/*==============================================================*/
/* If this is a join from the right, then there should already */
/* be a link from the previous join to the other join in the */
/* the bifurcated path through the join network. If so, we want */
/* add the next link to the join from the right so that it is */
/* visited after the other path. Doing this will reduce the */
/* number of activations added and then removed if the other */
/* path were followed first. The other path generates the */
/* partial matches which are negated by this path, so if that */
/* path is processed first, the partial matches from that path */
/* will prevent partial matches on this path. */
/*==============================================================*/
if ((joinFromTheRight) && (lhsEntryStruct->nextLinks != NULL))
{
theLink->next = lhsEntryStruct->nextLinks->next;
lhsEntryStruct->nextLinks->next = theLink;
}
else
{
theLink->next = lhsEntryStruct->nextLinks;
lhsEntryStruct->nextLinks = theLink;
}
}
/*=======================================================*/
/* Initialize the pointer values associated with the RHS */
/* of the join (both for the new join and the join or */
/* pattern which enters this join from the right. */
/*=======================================================*/
newJoin->rightSideEntryStructure = rhsEntryStruct;
if (rhsEntryStruct == NULL)
{
if (newJoin->firstJoin)
{
theLink = get_struct(theEnv,joinLink);
theLink->join = newJoin;
theLink->enterDirection = RHS;
theLink->next = DefruleData(theEnv)->RightPrimeJoins;
DefruleData(theEnv)->RightPrimeJoins = theLink;
}
newJoin->rightMatchNode = NULL;
return(newJoin);
}
/*===========================================================*/
/* If the first join of a rule is a not CE, then it needs to */
/* be "primed" under certain circumstances. This used to be */
/* handled by adding the (initial-fact) pattern to a rule */
/* with the not CE as its first pattern, but this alternate */
/* mechanism is now used so patterns don't have to be added. */
/*===========================================================*/
if (newJoin->firstJoin && (newJoin->patternIsNegated || newJoin->joinFromTheRight) && (! newJoin->patternIsExists))
{
theLink = get_struct(theEnv,joinLink);
theLink->join = newJoin;
theLink->enterDirection = LHS;
theLink->next = DefruleData(theEnv)->LeftPrimeJoins;
DefruleData(theEnv)->LeftPrimeJoins = theLink;
}
if (joinFromTheRight)
{
theLink = get_struct(theEnv,joinLink);
theLink->join = newJoin;
theLink->enterDirection = RHS;
theLink->next = ((struct joinNode *) rhsEntryStruct)->nextLinks;
((struct joinNode *) rhsEntryStruct)->nextLinks = theLink;
newJoin->rightMatchNode = NULL;
}
else
{
newJoin->rightMatchNode = ((struct patternNodeHeader *) rhsEntryStruct)->entryJoin;
((struct patternNodeHeader *) rhsEntryStruct)->entryJoin = newJoin;
}
/*================================*/
/* Return the newly created join. */
/*================================*/
return(newJoin);
}
#endif