-
Notifications
You must be signed in to change notification settings - Fork 3
/
drive.c
executable file
·1118 lines (938 loc) · 40.3 KB
/
drive.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 */
/* */
/* DRIVE MODULE */
/*******************************************************/
/*************************************************************/
/* Purpose: Handles join network activity associated with */
/* with the addition of a data entity such as a fact or */
/* instance. */
/* */
/* Principal Programmer(s): */
/* Gary D. Riley */
/* */
/* Contributing Programmer(s): */
/* */
/* Revision History: */
/* 6.23: Correction for FalseSymbol/TrueSymbol. DR0859 */
/* */
/* 6.24: Removed INCREMENTAL_RESET and */
/* LOGICAL_DEPENDENCIES compilation flags. */
/* */
/* Renamed BOOLEAN macro type to intBool. */
/* */
/* Rule with exists CE has incorrect activation. */
/* DR0867 */
/* */
/* 6.30: Added support for hashed alpha memories. */
/* */
/* Added additional developer statistics to help */
/* analyze join network performance. */
/* */
/* Removed pseudo-facts used in not CE. */
/* */
/*************************************************************/
#define _DRIVE_SOURCE_
#include <stdio.h>
#define _STDIO_INCLUDED_
#include <stdlib.h>
#include "setup.h"
#if DEFRULE_CONSTRUCT
#include "agenda.h"
#include "constant.h"
#include "engine.h"
#include "envrnmnt.h"
#include "memalloc.h"
#include "prntutil.h"
#include "reteutil.h"
#include "retract.h"
#include "router.h"
#include "lgcldpnd.h"
#include "incrrset.h"
#include "drive.h"
/***************************************/
/* LOCAL INTERNAL FUNCTION DEFINITIONS */
/***************************************/
static void EmptyDrive(void *,struct joinNode *,struct partialMatch *);
static void JoinNetErrorMessage(void *,struct joinNode *);
/************************************************/
/* NetworkAssert: Primary routine for filtering */
/* a partial match through the join network. */
/************************************************/
globle void NetworkAssert(
void *theEnv,
struct partialMatch *binds,
struct joinNode *join)
{
/*=========================================================*/
/* If an incremental reset is being performed and the join */
/* is not part of the network to be reset, then return. */
/*=========================================================*/
#if (! BLOAD_ONLY) && (! RUN_TIME)
if (EngineData(theEnv)->IncrementalResetInProgress && (join->initialize == FALSE)) return;
#endif
/*==================================================*/
/* Use a special routine if this is the first join. */
/*==================================================*/
if (join->firstJoin)
{
EmptyDrive(theEnv,join,binds);
return;
}
/*================================*/
/* Enter the join from the right. */
/*================================*/
NetworkAssertRight(theEnv,binds,join);
return;
}
/*****************************************************/
/* NetworkAssertRight: Primary routine for filtering */
/* a partial match through the join network from */
/* the RHS of a join. */
/*****************************************************/
globle void NetworkAssertRight(
void *theEnv,
struct partialMatch *rhsBinds,
struct joinNode *join)
{
struct partialMatch *lhsBinds, *nextBind;
int exprResult, restore = FALSE;
struct partialMatch *oldLHSBinds = NULL;
struct partialMatch *oldRHSBinds = NULL;
struct joinNode *oldJoin = NULL;
/*=========================================================*/
/* If an incremental reset is being performed and the join */
/* is not part of the network to be reset, then return. */
/*=========================================================*/
#if (! BLOAD_ONLY) && (! RUN_TIME)
if (EngineData(theEnv)->IncrementalResetInProgress && (join->initialize == FALSE)) return;
#endif
if (join->firstJoin)
{
EmptyDrive(theEnv,join,rhsBinds);
return;
}
/*=====================================================*/
/* The partial matches entering from the LHS of a join */
/* are stored in the left beta memory of the join. */
/*=====================================================*/
lhsBinds = GetLeftBetaMemory(join,rhsBinds->hashValue);
#if DEVELOPER
if (lhsBinds != NULL)
{ EngineData(theEnv)->rightToLeftLoops++; }
#endif
/*====================================*/
/* Set up the evaluation environment. */
/*====================================*/
if (lhsBinds != NULL)
{
oldLHSBinds = EngineData(theEnv)->GlobalLHSBinds;
oldRHSBinds = EngineData(theEnv)->GlobalRHSBinds;
oldJoin = EngineData(theEnv)->GlobalJoin;
EngineData(theEnv)->GlobalRHSBinds = rhsBinds;
EngineData(theEnv)->GlobalJoin = join;
restore = TRUE;
}
/*===================================================*/
/* Compare each set of binds on the opposite side of */
/* the join with the set of binds that entered this */
/* join. If the binds don't mismatch, then perform */
/* the appropriate action for the logic of the join. */
/*===================================================*/
while (lhsBinds != NULL)
{
nextBind = lhsBinds->nextInMemory;
join->memoryCompares++;
/*===========================================================*/
/* Initialize some variables pointing to the partial matches */
/* in the LHS and RHS of the join. */
/*===========================================================*/
if (lhsBinds->hashValue != rhsBinds->hashValue)
{
#if DEVELOPER
if (join->leftMemory->size == 1)
{ EngineData(theEnv)->betaHashListSkips++; }
else
{ EngineData(theEnv)->betaHashHTSkips++; }
if (lhsBinds->marker != NULL)
{ EngineData(theEnv)->unneededMarkerCompare++; }
#endif
lhsBinds = nextBind;
continue;
}
/*===============================================================*/
/* If there already is an associated RHS partial match stored in */
/* the LHS partial match from the beta memory of this join, then */
/* the exists/nand CE has already been satisfied and we can move */
/* on to the next partial match found in the beta memory. */
/*===============================================================*/
if (lhsBinds->marker != NULL)
{
#if DEVELOPER
EngineData(theEnv)->unneededMarkerCompare++;
#endif
lhsBinds = nextBind;
continue;
}
/*===================================================*/
/* If the join has no expression associated with it, */
/* then the new partial match derived from the LHS */
/* and RHS partial matches is valid. */
/*===================================================*/
if (join->networkTest == NULL)
{ exprResult = TRUE; }
/*=========================================================*/
/* If the join has an expression associated with it, then */
/* evaluate the expression to determine if the new partial */
/* match derived from the LHS and RHS partial matches is */
/* valid (i.e. variable bindings are consistent and */
/* predicate expressions evaluate to TRUE). */
/*=========================================================*/
else
{
#if DEVELOPER
EngineData(theEnv)->rightToLeftComparisons++;
#endif
EngineData(theEnv)->GlobalLHSBinds = lhsBinds;
exprResult = EvaluateJoinExpression(theEnv,join->networkTest,join);
if (EvaluationData(theEnv)->EvaluationError)
{
if (join->patternIsNegated) exprResult = TRUE;
SetEvaluationError(theEnv,FALSE);
}
#if DEVELOPER
if (exprResult)
{ EngineData(theEnv)->rightToLeftSucceeds++; }
#endif
}
if ((join->secondaryNetworkTest != NULL) && exprResult)
{
EngineData(theEnv)->GlobalLHSBinds = lhsBinds;
exprResult = EvaluateJoinExpression(theEnv,join->secondaryNetworkTest,join);
if (EvaluationData(theEnv)->EvaluationError)
{ SetEvaluationError(theEnv,FALSE); }
}
/*====================================================*/
/* If the join expression evaluated to TRUE (i.e. */
/* there were no conflicts between variable bindings, */
/* all tests were satisfied, etc.), then perform the */
/* appropriate action given the logic of this join. */
/*====================================================*/
if (exprResult != FALSE)
{
if (join->patternIsExists)
{
AddBlockedLink(lhsBinds,rhsBinds);
PPDrive(theEnv,lhsBinds,NULL,join);
}
else if (join->patternIsNegated || join->joinFromTheRight)
{
AddBlockedLink(lhsBinds,rhsBinds);
if (lhsBinds->children != NULL)
{ PosEntryRetractBeta(theEnv,lhsBinds,lhsBinds->children); }
/*
if (lhsBinds->dependents != NULL)
{ RemoveLogicalSupport(theEnv,lhsBinds); }
*/
}
else
{ PPDrive(theEnv,lhsBinds,rhsBinds,join); }
}
/*====================================*/
/* Move on to the next partial match. */
/*====================================*/
lhsBinds = nextBind;
}
/*=========================================*/
/* Restore the old evaluation environment. */
/*=========================================*/
if (restore)
{
EngineData(theEnv)->GlobalLHSBinds = oldLHSBinds;
EngineData(theEnv)->GlobalRHSBinds = oldRHSBinds;
EngineData(theEnv)->GlobalJoin = oldJoin;
}
return;
}
/****************************************************/
/* NetworkAssertLeft: Primary routine for filtering */
/* a partial match through the join network when */
/* entering through the left side of a join. */
/****************************************************/
globle void NetworkAssertLeft(
void *theEnv,
struct partialMatch *lhsBinds,
struct joinNode *join)
{
struct partialMatch *rhsBinds;
int exprResult, restore = FALSE;
unsigned long entryHashValue;
struct partialMatch *oldLHSBinds = NULL;
struct partialMatch *oldRHSBinds = NULL;
struct joinNode *oldJoin = NULL;
/*=========================================================*/
/* If an incremental reset is being performed and the join */
/* is not part of the network to be reset, then return. */
/*=========================================================*/
#if (! BLOAD_ONLY) && (! RUN_TIME)
if (EngineData(theEnv)->IncrementalResetInProgress && (join->initialize == FALSE)) return;
#endif
/*===================================*/
/* The only action for the last join */
/* of a rule is to activate it. */
/*===================================*/
if (join->ruleToActivate != NULL)
{
AddActivation(theEnv,join->ruleToActivate,lhsBinds);
return;
}
/*==================================================*/
/* Initialize some variables used to indicate which */
/* side is being compared to the new partial match. */
/*==================================================*/
entryHashValue = lhsBinds->hashValue;
if (join->joinFromTheRight)
{ rhsBinds = GetRightBetaMemory(join,entryHashValue); }
else
{ rhsBinds = GetAlphaMemory(theEnv,(struct patternNodeHeader *) join->rightSideEntryStructure,entryHashValue); }
#if DEVELOPER
if (rhsBinds != NULL)
{ EngineData(theEnv)->leftToRightLoops++; }
#endif
/*====================================*/
/* Set up the evaluation environment. */
/*====================================*/
if ((rhsBinds != NULL) || (join->secondaryNetworkTest != NULL))
{
oldLHSBinds = EngineData(theEnv)->GlobalLHSBinds;
oldRHSBinds = EngineData(theEnv)->GlobalRHSBinds;
oldJoin = EngineData(theEnv)->GlobalJoin;
EngineData(theEnv)->GlobalLHSBinds = lhsBinds;
EngineData(theEnv)->GlobalJoin = join;
restore = TRUE;
}
/*===================================================*/
/* Compare each set of binds on the opposite side of */
/* the join with the set of binds that entered this */
/* join. If the binds don't mismatch, then perform */
/* the appropriate action for the logic of the join. */
/*===================================================*/
while (rhsBinds != NULL)
{
join->memoryCompares++;
/*===================================================*/
/* If the join has no expression associated with it, */
/* then the new partial match derived from the LHS */
/* and RHS partial matches is valid. */
/*===================================================*/
if (join->networkTest == NULL)
{ exprResult = TRUE; }
/*=========================================================*/
/* If the join has an expression associated with it, then */
/* evaluate the expression to determine if the new partial */
/* match derived from the LHS and RHS partial matches is */
/* valid (i.e. variable bindings are consistent and */
/* predicate expressions evaluate to TRUE). */
/*=========================================================*/
else
{
#if DEVELOPER
EngineData(theEnv)->leftToRightComparisons++;
#endif
EngineData(theEnv)->GlobalRHSBinds = rhsBinds;
exprResult = EvaluateJoinExpression(theEnv,join->networkTest,join);
if (EvaluationData(theEnv)->EvaluationError)
{
if (join->patternIsNegated) exprResult = TRUE;
SetEvaluationError(theEnv,FALSE);
}
#if DEVELOPER
if (exprResult)
{ EngineData(theEnv)->leftToRightSucceeds++; }
#endif
}
// Bug Fix - Need to evaluate secondary network test for exists CE 0881
if ((join->secondaryNetworkTest != NULL) && exprResult && join->patternIsExists)
{
EngineData(theEnv)->GlobalRHSBinds = rhsBinds;
exprResult = EvaluateJoinExpression(theEnv,join->secondaryNetworkTest,join);
if (EvaluationData(theEnv)->EvaluationError)
{ SetEvaluationError(theEnv,FALSE); }
}
/*====================================================*/
/* If the join expression evaluated to TRUE (i.e. */
/* there were no conflicts between variable bindings, */
/* all tests were satisfied, etc.), then perform the */
/* appropriate action given the logic of this join. */
/*====================================================*/
if (exprResult != FALSE)
{
/*==============================================*/
/* Use the PPDrive routine when the join isn't */
/* associated with a not CE and it doesn't have */
/* a join from the right. */
/*==============================================*/
if ((join->patternIsNegated == FALSE) &&
(join->patternIsExists == FALSE) &&
(join->joinFromTheRight == FALSE))
{ PPDrive(theEnv,lhsBinds,rhsBinds,join); }
/*==================================================*/
/* At most, one partial match will be generated for */
/* a match from the right memory of an exists CE. */
/*==================================================*/
else if (join->patternIsExists)
{
AddBlockedLink(lhsBinds,rhsBinds);
PPDrive(theEnv,lhsBinds,NULL,join);
EngineData(theEnv)->GlobalLHSBinds = oldLHSBinds;
EngineData(theEnv)->GlobalRHSBinds = oldRHSBinds;
EngineData(theEnv)->GlobalJoin = oldJoin;
return;
}
/*===========================================================*/
/* If the new partial match entered from the LHS of the join */
/* and the join is either associated with a not CE or the */
/* join has a join from the right, then mark the LHS partial */
/* match indicating that there is a RHS partial match */
/* preventing this join from being satisfied. Once this has */
/* happened, the other RHS partial matches don't have to be */
/* tested since it only takes one partial match to prevent */
/* the LHS from being satisfied. */
/*===========================================================*/
else
{
AddBlockedLink(lhsBinds,rhsBinds);
break;
}
}
/*====================================*/
/* Move on to the next partial match. */
/*====================================*/
rhsBinds = rhsBinds->nextInMemory;
}
/*==================================================================*/
/* If a join with an associated not CE or join from the right was */
/* entered from the LHS side of the join, and the join expression */
/* failed for all sets of matches for the new bindings on the LHS */
/* side (there was no RHS partial match preventing the LHS partial */
/* match from being satisfied), then the LHS partial match appended */
/* with an pseudo-fact that represents the instance of the not */
/* pattern or join from the right that was satisfied should be sent */
/* to the joins below this join. */
/*==================================================================*/
if ((join->patternIsNegated || join->joinFromTheRight) &&
(! join->patternIsExists) &&
(lhsBinds->marker == NULL))
{
if (join->secondaryNetworkTest != NULL)
{
EngineData(theEnv)->GlobalRHSBinds = NULL;
exprResult = EvaluateJoinExpression(theEnv,join->secondaryNetworkTest,join);
if (EvaluationData(theEnv)->EvaluationError)
{ SetEvaluationError(theEnv,FALSE); }
if (exprResult)
{ PPDrive(theEnv,lhsBinds,NULL,join); }
}
else
{ PPDrive(theEnv,lhsBinds,NULL,join); }
}
/*=========================================*/
/* Restore the old evaluation environment. */
/*=========================================*/
if (restore)
{
EngineData(theEnv)->GlobalLHSBinds = oldLHSBinds;
EngineData(theEnv)->GlobalRHSBinds = oldRHSBinds;
EngineData(theEnv)->GlobalJoin = oldJoin;
}
return;
}
/*******************************************************/
/* EvaluateJoinExpression: Evaluates join expressions. */
/* Performs a faster evaluation for join expressions */
/* than if EvaluateExpression was used directly. */
/*******************************************************/
globle intBool EvaluateJoinExpression(
void *theEnv,
struct expr *joinExpr,
struct joinNode *joinPtr)
{
DATA_OBJECT theResult;
int andLogic, result = TRUE;
/*======================================*/
/* A NULL expression evaluates to TRUE. */
/*======================================*/
if (joinExpr == NULL) return(TRUE);
/*====================================================*/
/* Initialize some variables which allow this routine */
/* to avoid calling the "and" and "or" functions if */
/* they are the first part of the expression to be */
/* evaluated. Most of the join expressions do not use */
/* deeply nested and/or functions so this technique */
/* speeds up evaluation. */
/*====================================================*/
if (joinExpr->value == ExpressionData(theEnv)->PTR_AND)
{
andLogic = TRUE;
joinExpr = joinExpr->argList;
}
else if (joinExpr->value == ExpressionData(theEnv)->PTR_OR)
{
andLogic = FALSE;
joinExpr = joinExpr->argList;
}
else
{ andLogic = TRUE; }
/*=========================================*/
/* Evaluate each of the expressions linked */
/* together in the join expression. */
/*=========================================*/
while (joinExpr != NULL)
{
/*================================*/
/* Evaluate a primitive function. */
/*================================*/
if ((EvaluationData(theEnv)->PrimitivesArray[joinExpr->type] == NULL) ?
FALSE :
EvaluationData(theEnv)->PrimitivesArray[joinExpr->type]->evaluateFunction != NULL)
{
struct expr *oldArgument;
oldArgument = EvaluationData(theEnv)->CurrentExpression;
EvaluationData(theEnv)->CurrentExpression = joinExpr;
result = (*EvaluationData(theEnv)->PrimitivesArray[joinExpr->type]->evaluateFunction)(theEnv,joinExpr->value,&theResult);
EvaluationData(theEnv)->CurrentExpression = oldArgument;
}
/*=============================*/
/* Evaluate the "or" function. */
/*=============================*/
else if (joinExpr->value == ExpressionData(theEnv)->PTR_OR)
{
result = FALSE;
if (EvaluateJoinExpression(theEnv,joinExpr,joinPtr) == TRUE)
{
if (EvaluationData(theEnv)->EvaluationError)
{ return(FALSE); }
result = TRUE;
}
else if (EvaluationData(theEnv)->EvaluationError)
{ return(FALSE); }
}
/*==============================*/
/* Evaluate the "and" function. */
/*==============================*/
else if (joinExpr->value == ExpressionData(theEnv)->PTR_AND)
{
result = TRUE;
if (EvaluateJoinExpression(theEnv,joinExpr,joinPtr) == FALSE)
{
if (EvaluationData(theEnv)->EvaluationError)
{ return(FALSE); }
result = FALSE;
}
else if (EvaluationData(theEnv)->EvaluationError)
{ return(FALSE); }
}
/*==========================================================*/
/* Evaluate all other expressions using EvaluateExpression. */
/*==========================================================*/
else
{
EvaluateExpression(theEnv,joinExpr,&theResult);
if (EvaluationData(theEnv)->EvaluationError)
{
JoinNetErrorMessage(theEnv,joinPtr);
return(FALSE);
}
if ((theResult.value == EnvFalseSymbol(theEnv)) && (theResult.type == SYMBOL))
{ result = FALSE; }
else
{ result = TRUE; }
}
/*====================================*/
/* Handle the short cut evaluation of */
/* the "and" and "or" functions. */
/*====================================*/
if ((andLogic == TRUE) && (result == FALSE))
{ return(FALSE); }
else if ((andLogic == FALSE) && (result == TRUE))
{ return(TRUE); }
/*==============================================*/
/* Move to the next expression to be evaluated. */
/*==============================================*/
joinExpr = joinExpr->nextArg;
}
/*=================================================*/
/* Return the result of evaluating the expression. */
/*=================================================*/
return(result);
}
/*******************************************************/
/* EvaluateSecondaryNetworkTest: */
/*******************************************************/
globle intBool EvaluateSecondaryNetworkTest(
void *theEnv,
struct partialMatch *leftMatch,
struct joinNode *joinPtr)
{
int joinExpr;
struct partialMatch *oldLHSBinds;
struct partialMatch *oldRHSBinds;
struct joinNode *oldJoin;
if (joinPtr->secondaryNetworkTest == NULL)
{ return(TRUE); }
#if DEVELOPER
EngineData(theEnv)->rightToLeftComparisons++;
#endif
oldLHSBinds = EngineData(theEnv)->GlobalLHSBinds;
oldRHSBinds = EngineData(theEnv)->GlobalRHSBinds;
oldJoin = EngineData(theEnv)->GlobalJoin;
EngineData(theEnv)->GlobalLHSBinds = leftMatch;
EngineData(theEnv)->GlobalRHSBinds = NULL;
EngineData(theEnv)->GlobalJoin = joinPtr;
joinExpr = EvaluateJoinExpression(theEnv,joinPtr->secondaryNetworkTest,joinPtr);
EvaluationData(theEnv)->EvaluationError = FALSE;
EngineData(theEnv)->GlobalLHSBinds = oldLHSBinds;
EngineData(theEnv)->GlobalRHSBinds = oldRHSBinds;
EngineData(theEnv)->GlobalJoin = oldJoin;
return(joinExpr);
}
/*******************************************************/
/* BetaMemoryHashValue: */
/*******************************************************/
globle unsigned long BetaMemoryHashValue(
void *theEnv,
struct expr *hashExpr,
struct partialMatch *lbinds,
struct partialMatch *rbinds,
struct joinNode *joinPtr)
{
DATA_OBJECT theResult;
struct partialMatch *oldLHSBinds;
struct partialMatch *oldRHSBinds;
struct joinNode *oldJoin;
unsigned long hashValue = 0;
unsigned long multiplier = 1;
union
{
void *vv;
unsigned long liv;
} fis;
/*======================================*/
/* A NULL expression evaluates to zero. */
/*======================================*/
if (hashExpr == NULL) return(0);
/*=========================================*/
/* Initialize some of the global variables */
/* used when evaluating expressions. */
/*=========================================*/
oldLHSBinds = EngineData(theEnv)->GlobalLHSBinds;
oldRHSBinds = EngineData(theEnv)->GlobalRHSBinds;
oldJoin = EngineData(theEnv)->GlobalJoin;
EngineData(theEnv)->GlobalLHSBinds = lbinds;
EngineData(theEnv)->GlobalRHSBinds = rbinds;
EngineData(theEnv)->GlobalJoin = joinPtr;
/*=========================================*/
/* Evaluate each of the expressions linked */
/* together in the join expression. */
/*=========================================*/
while (hashExpr != NULL)
{
/*================================*/
/* Evaluate a primitive function. */
/*================================*/
if ((EvaluationData(theEnv)->PrimitivesArray[hashExpr->type] == NULL) ?
FALSE :
EvaluationData(theEnv)->PrimitivesArray[hashExpr->type]->evaluateFunction != NULL)
{
struct expr *oldArgument;
oldArgument = EvaluationData(theEnv)->CurrentExpression;
EvaluationData(theEnv)->CurrentExpression = hashExpr;
(*EvaluationData(theEnv)->PrimitivesArray[hashExpr->type]->evaluateFunction)(theEnv,hashExpr->value,&theResult);
EvaluationData(theEnv)->CurrentExpression = oldArgument;
}
/*==========================================================*/
/* Evaluate all other expressions using EvaluateExpression. */
/*==========================================================*/
else
{ EvaluateExpression(theEnv,hashExpr,&theResult); }
switch (theResult.type)
{
case STRING:
case SYMBOL:
case INSTANCE_NAME:
hashValue += (((SYMBOL_HN *) theResult.value)->bucket * multiplier);
break;
case INTEGER:
hashValue += (((INTEGER_HN *) theResult.value)->bucket * multiplier);
break;
case FLOAT:
hashValue += (((FLOAT_HN *) theResult.value)->bucket * multiplier);
break;
case FACT_ADDRESS:
#if OBJECT_SYSTEM
case INSTANCE_ADDRESS:
#endif
fis.liv = 0;
fis.vv = theResult.value;
hashValue += (unsigned long) (fis.liv * multiplier);
break;
case EXTERNAL_ADDRESS:
fis.liv = 0;
fis.vv = ValueToExternalAddress(theResult.value);
hashValue += (unsigned long) (fis.liv * multiplier);
break;
}
/*==============================================*/
/* Move to the next expression to be evaluated. */
/*==============================================*/
hashExpr = hashExpr->nextArg;
multiplier = multiplier * 509;
}
/*=======================================*/
/* Restore some of the global variables. */
/*=======================================*/
EngineData(theEnv)->GlobalLHSBinds = oldLHSBinds;
EngineData(theEnv)->GlobalRHSBinds = oldRHSBinds;
EngineData(theEnv)->GlobalJoin = oldJoin;
/*=================================================*/
/* Return the result of evaluating the expression. */
/*=================================================*/
return(hashValue);
}
/*******************************************************************/
/* PPDrive: Handles the merging of an alpha memory partial match */
/* with a beta memory partial match for a join that has positive */
/* LHS entry and positive RHS entry. The partial matches being */
/* merged have previously been checked to determine that they */
/* satisify the constraints for the join. Once merged, the new */
/* partial match is sent to each child join of the join from */
/* which the merge took place. */
/*******************************************************************/
globle void PPDrive(
void *theEnv,
struct partialMatch *lhsBinds,
struct partialMatch *rhsBinds,
struct joinNode *join)
{
struct partialMatch *linker;
struct joinLink *listOfJoins;
unsigned long hashValue;
/*================================================*/
/* Send the new partial match to all child joins. */
/*================================================*/
listOfJoins = join->nextLinks;
if (listOfJoins == NULL) return;
/*===============================================================*/
/* In the current implementation, all children of this join must */
/* be entered from the same side (either all left or all right). */
/*===============================================================*/
while (listOfJoins != NULL)
{
/*==================================================*/
/* Merge the alpha and beta memory partial matches. */
/*==================================================*/
linker = MergePartialMatches(theEnv,lhsBinds,rhsBinds);
/*================================================*/
/* Determine the hash value of the partial match. */
/*================================================*/
if (listOfJoins->enterDirection == LHS)
{
if (listOfJoins->join->leftHash != NULL)
{ hashValue = BetaMemoryHashValue(theEnv,listOfJoins->join->leftHash,linker,NULL,listOfJoins->join); }
else
{ hashValue = 0; }
}
else
{
if (listOfJoins->join->rightHash != NULL)
{ hashValue = BetaMemoryHashValue(theEnv,listOfJoins->join->rightHash,linker,NULL,listOfJoins->join); }
else
{ hashValue = 0; }
}
/*=======================================================*/
/* Add the partial match to the beta memory of the join. */
/*=======================================================*/
UpdateBetaPMLinks(theEnv,linker,lhsBinds,rhsBinds,listOfJoins->join,hashValue,listOfJoins->enterDirection);
if (listOfJoins->enterDirection == LHS)
{ NetworkAssertLeft(theEnv,linker,listOfJoins->join); }
else
{ NetworkAssertRight(theEnv,linker,listOfJoins->join); }
listOfJoins = listOfJoins->next;
}
return;
}
/***********************************************************************/
/* EPMDrive: Drives an empty partial match to the next level of joins. */
/* An empty partial match is usually associated with a negated CE */
/* that is the first CE of a rule. */
/***********************************************************************/
globle void EPMDrive(
void *theEnv,
struct partialMatch *parent,
struct joinNode *join)
{
struct partialMatch *linker;
struct joinLink *listOfJoins;
listOfJoins = join->nextLinks;
if (listOfJoins == NULL) return;
while (listOfJoins != NULL)
{
linker = CreateEmptyPartialMatch(theEnv);
UpdateBetaPMLinks(theEnv,linker,parent,NULL,listOfJoins->join,0,listOfJoins->enterDirection);
if (listOfJoins->enterDirection == LHS)
{ NetworkAssertLeft(theEnv,linker,listOfJoins->join); }
else
{ NetworkAssertRight(theEnv,linker,listOfJoins->join); }
listOfJoins = listOfJoins->next;
}
}
/***************************************************************/
/* EmptyDrive: Handles the entry of a alpha memory partial */
/* match from the RHS of a join that is the first join of */
/* a rule (i.e. a join that cannot be entered from the LHS). */
/***************************************************************/
static void EmptyDrive(
void *theEnv,
struct joinNode *join,
struct partialMatch *rhsBinds)
{
struct partialMatch *linker, *existsParent = NULL, *notParent;
struct joinLink *listOfJoins;
int joinExpr;
unsigned long hashValue;
struct partialMatch *oldLHSBinds;
struct partialMatch *oldRHSBinds;
struct joinNode *oldJoin;
/*======================================================*/
/* Determine if the alpha memory partial match satifies */
/* the join expression. If it doesn't then no further */
/* action is taken. */
/*======================================================*/
join->memoryCompares++;
if (join->networkTest != NULL)
{
#if DEVELOPER
if (join->networkTest)
{ EngineData(theEnv)->rightToLeftComparisons++; }
#endif
oldLHSBinds = EngineData(theEnv)->GlobalLHSBinds;
oldRHSBinds = EngineData(theEnv)->GlobalRHSBinds;
oldJoin = EngineData(theEnv)->GlobalJoin;
EngineData(theEnv)->GlobalLHSBinds = NULL;
EngineData(theEnv)->GlobalRHSBinds = rhsBinds;
EngineData(theEnv)->GlobalJoin = join;
joinExpr = EvaluateJoinExpression(theEnv,join->networkTest,join);
EvaluationData(theEnv)->EvaluationError = FALSE;
EngineData(theEnv)->GlobalLHSBinds = oldLHSBinds;
EngineData(theEnv)->GlobalRHSBinds = oldRHSBinds;
EngineData(theEnv)->GlobalJoin = oldJoin;
if (joinExpr == FALSE) return;
}
/*========================================================*/
/* Handle a negated first pattern or join from the right. */
/*========================================================*/
if (join->patternIsNegated || (join->joinFromTheRight && (! join->patternIsExists))) /* reorder to remove patternIsExists test */
{
notParent = join->leftMemory->beta[0];
if (notParent->marker != NULL)