/
rulebld.c
864 lines (744 loc) · 32.8 KB
/
rulebld.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
/*******************************************************/
/* "C" Language Integrated Production System */
/* */
/* CLIPS Version 6.30 10/19/06 */
/* */
/* 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: Added support for hashed alpha memories. */
/* */
/*************************************************************/
#define _RULEBLD_SOURCE_
#include "setup.h"
#if DEFRULE_CONSTRUCT && (! RUN_TIME) && (! BLOAD_ONLY)
#include <stdio.h>
#define _STDIO_INCLUDED_
#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 "reteutil.h"
#include "router.h"
#include "rulebld.h"
#include "watch.h"
/***************************************/
/* LOCAL INTERNAL FUNCTION DEFINITIONS */
/***************************************/
static struct joinNode *FindShareableJoin(struct joinLink *,struct joinNode *,intBool,void *,unsigned,unsigned,
unsigned,unsigned,struct expr *,struct expr *,
struct expr *,struct expr *);
static int TestJoinForReuse(struct joinNode *,unsigned,unsigned,
unsigned,unsigned,struct expr *,struct expr *,
struct expr *,struct expr *);
static struct joinNode *CreateNewJoin(void *,struct expr *,struct expr *,struct joinNode *,void *,
int,int,int,struct expr *,struct expr *);
static void AttachTestCEsToPatternCEs(void *,struct lhsParseNode *);
//add by xuchao
extern struct JoinNodeList *joinNodeListHead;
extern struct JoinNodeList *joinNodeListTail;
/****************************************************************/
/* ConstructJoins: Integrates a set of pattern and join tests */
/* associated with a rule into the pattern and join networks. */
/****************************************************************/
globle struct joinNode *ConstructJoins(
void *theEnv,
int logicalJoin,
struct lhsParseNode *theLHS,
int startDepth)
{
struct joinNode *lastJoin = NULL;
struct patternNodeHeader *lastPattern;
unsigned firstJoin = TRUE;
int tryToReuse = TRUE;
struct joinNode *listOfJoins = NULL;
struct joinNode *oldJoin;
int joinNumber = 1;
int isLogical, isExists;
struct joinNode *lastRightJoin;
int lastIteration = FALSE;
int rhsType;
struct expr *leftHash, *rightHash;
void *rhsStruct;
struct lhsParseNode *nextLHS;
struct expr *networkTest, *secondaryNetworkTest, *secondaryExternalTest;
int joinFromTheRight;
struct joinLink *theLinks;
intBool useLinks;
/*===================================================*/
/* Remove any test CEs from the LHS and attach their */
/* expression to the closest preceeding non-negated */
/* join at the same not/and depth. */
/*===================================================*/
if (startDepth == 1)
{ AttachTestCEsToPatternCEs(theEnv,theLHS); }
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/test 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 (nextLHS != NULL)
{ nextLHS = nextLHS->bottom; }
if ((nextLHS != NULL) && (nextLHS->type == TEST_CE))
{
secondaryExternalTest = nextLHS->networkTest;
nextLHS = nextLHS->bottom;
}
}
/*=======================================*/
/* Is this the last pattern to be added? */
/*=======================================*/
if (nextLHS == NULL)
{ lastIteration = TRUE; }
else if (theLHS->endNandDepth < startDepth)
{ lastIteration = TRUE; }
else if ((nextLHS->type == TEST_CE) &&
(theLHS->beginNandDepth > startDepth) &&
(nextLHS->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);
rhsStruct = lastRightJoin;
rhsType = 0;
lastPattern = NULL;
networkTest = theLHS->externalNetworkTest; /* TBD */
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 (firstJoin == TRUE)
{
if (theLHS->right == NULL)
{ theLinks = DefruleData(theEnv)->RightPrimeJoins; }
else if (lastPattern != NULL)
{
listOfJoins = lastPattern->entryJoin;
theLinks = NULL;
useLinks = FALSE;
}
else
{ theLinks = lastRightJoin->nextLinks; }
}
else
{ theLinks = lastJoin->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 ((EnvGetWatchItem(theEnv,"compilations") == TRUE) && GetPrintWhileLoading(theEnv))
{ EnvPrintRouter(theEnv,WDIALOG,"=j"); }
#endif
lastJoin = oldJoin;
}
else
{
tryToReuse = FALSE;
if (! joinFromTheRight)
{
lastJoin = CreateNewJoin(theEnv,networkTest,secondaryNetworkTest,lastJoin,
lastPattern,FALSE,(int) theLHS->negated, isExists,
leftHash,rightHash);
lastJoin->rhsType = rhsType;
}
else
{
lastJoin = CreateNewJoin(theEnv,networkTest,secondaryNetworkTest,lastJoin,
lastRightJoin,TRUE,(int) 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;
#if THREAD
//add by xuchao
lastJoin->nodeMaxSalience = max(lastJoin->nodeMaxSalience, PatternData(theEnv)->GlobalSalience);
lastJoin->nodeMinSalience = min(lastJoin->nodeMinSalience, PatternData(theEnv)->GlobalSalience);
#endif
}
/*=================================================*/
/* 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) &&
(EnvGetWatchItem(theEnv,"compilations") == TRUE) &&
GetPrintWhileLoading(theEnv))
{ EnvPrintRouter(theEnv,WDIALOG,"\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. */
/****************************************************************/
static void AttachTestCEsToPatternCEs(
void *theEnv,
struct lhsParseNode *theLHS)
{
struct lhsParseNode *lastNode, *tempNode;
if (theLHS == NULL) return;
/*=============================================================*/
/* Attach test CEs that can be attached directly to a pattern. */
/*=============================================================*/
lastNode = theLHS;
theLHS = lastNode->bottom;
while (theLHS != NULL)
{
if ((theLHS->type != TEST_CE) ||
(lastNode->beginNandDepth != lastNode->endNandDepth) ||
(lastNode->beginNandDepth != theLHS->beginNandDepth))
{
lastNode = theLHS;
theLHS = theLHS->bottom;
continue;
}
if (lastNode->negated)
{
lastNode->secondaryNetworkTest =
CombineExpressions(theEnv,lastNode->secondaryNetworkTest,theLHS->networkTest);
}
else
{
lastNode->networkTest =
CombineExpressions(theEnv,lastNode->networkTest,theLHS->networkTest);
}
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,
intBool useLinks,
void *rhsStruct,
unsigned int firstJoin,
unsigned int negatedRHS,
unsigned int existsRHS,
unsigned int 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 int TestJoinForReuse(
struct joinNode *testJoin,
unsigned firstJoin,
unsigned negatedRHS,
unsigned existsRHS,
unsigned int 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(
void *theEnv,
struct expr *joinTest,
struct expr *secondaryJoinTest,
struct joinNode *lhsEntryStruct,
void *rhsEntryStruct,
int joinFromTheRight,
int negatedRHSPattern,
int 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 ((EnvGetWatchItem(theEnv,"compilations") == TRUE) && GetPrintWhileLoading(theEnv))
{ EnvPrintRouter(theEnv,WDIALOG,"+j"); }
#endif
/*======================*/
/* Create the new join. */
/*======================*/
newJoin = get_struct(theEnv,joinNode);
#if THREAD
//add by xuchao
if (theEnv == GetEnvironmentByIndex(0)){
newJoin->nodeMaxSalience = newJoin->nodeMinSalience = PatternData(theEnv)->GlobalSalience;
newJoin->activeJoinNodeListHead = (struct activeJoinNode*) malloc(sizeof(struct activeJoinNode));
newJoin->activeJoinNodeListHead->next = NULL;
newJoin->activeJoinNodeListHead->pre = NULL;
newJoin->activeJoinNodeListTail = newJoin->activeJoinNodeListHead;
newJoin->numOfActiveNode = 0;
#if CSECTION
#if SPINCOUNT
InitializeCriticalSectionAndSpinCount(&(newJoin->nodeSection), 0x00000400);
#else if
InitializeCriticalSection(&(newJoin->nodeSection));
#endif
#endif
#if MUTILTHREAD
newJoin->threadTag = newJoin->nodeMaxSalience % 2;
#endif
#if SPEEDUP
#if !DATASTRUCT
struct JoinNodeList *oneNode = (struct JoinNodeList*)malloc(sizeof(struct JoinNodeList));
oneNode->join = newJoin;
oneNode->next = NULL;
if (joinNodeListHead->next == NULL){
joinNodeListHead->next = oneNode;
}
else{
joinNodeListTail->next = oneNode;
}
joinNodeListTail = oneNode;
#endif
#endif
}
#endif // THREAD
/*======================================================*/
/* 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;
#if SLIDING_WINDOW
//newJoin->leftMemory->beta_last = (struct partialMatch**) genalloc(theEnv, sizeof(struct partialMatch*));
//newJoin->leftMemory->beta_last[0] = NULL;
newJoin->leftMemory->beta_last = (struct partialMatch **) genalloc(theEnv, sizeof(struct partialMatch *) * INITIAL_BETA_HASH_SIZE);
memset(newJoin->leftMemory->beta_last, 0, sizeof(struct partialMatch *) * INITIAL_BETA_HASH_SIZE);
#endif
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);
#if SLIDING_WINDOW
newJoin->leftMemory->beta_last = (struct partialMatch **) genalloc(theEnv, sizeof(struct partialMatch *) * INITIAL_BETA_HASH_SIZE);
memset(newJoin->leftMemory->beta_last, 0, sizeof(struct partialMatch *) * INITIAL_BETA_HASH_SIZE);
#endif
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);
#if SLIDING_WINDOW
newJoin->leftMemory->beta[0]->rhsMemory = FALSE;
newJoin->leftMemory->beta_last[0] = newJoin->leftMemory->beta[0];
#endif
newJoin->leftMemory->beta[0]->owner = newJoin;
newJoin->leftMemory->count = 1;
}
#if £¡SLIDING_WINDOW
newJoin->leftMemory->beta_last = (struct partialMatch**) genalloc(theEnv, sizeof(struct partialMatch*) * newJoin->leftMemory->size);
newJoin->leftMemory->beta_last[0] = NULL;
memset(newJoin->leftMemory->beta_last, 0, sizeof(struct partialMatch *) * newJoin->leftMemory->size);
#endif
}
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 ((lhsEntryStruct == NULL) && (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 = EnvGetIncrementalReset(theEnv);
newJoin->logicalJoin = FALSE;
newJoin->ruleToActivate = NULL;
newJoin->memoryAdds = 0;
newJoin->memoryDeletes = 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;
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