00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #define _REORDER_SOURCE_
00033
00034 #include "setup.h"
00035
00036 #if (! RUN_TIME) && (! BLOAD_ONLY) && DEFRULE_CONSTRUCT
00037
00038 #include <stdio.h>
00039 #define _STDIO_INCLUDED_
00040
00041 #include "cstrnutl.h"
00042 #include "envrnmnt.h"
00043 #include "extnfunc.h"
00044 #include "memalloc.h"
00045 #include "pattern.h"
00046 #include "prntutil.h"
00047 #include "router.h"
00048 #include "rulelhs.h"
00049
00050 #include "reorder.h"
00051
00052 struct variableReference
00053 {
00054 struct symbolHashNode *name;
00055 int depth;
00056 struct variableReference *next;
00057 };
00058
00059 struct groupReference
00060 {
00061 struct lhsParseNode *theGroup;
00062 int depth;
00063 struct groupReference *next;
00064 };
00065
00066
00067
00068
00069
00070 static struct lhsParseNode *ReverseAndOr(void *,struct lhsParseNode *,struct lhsParseNode *,int);
00071 static struct lhsParseNode *PerformReorder1(void *,struct lhsParseNode *,int *);
00072 static struct lhsParseNode *PerformReorder2(void *,struct lhsParseNode *,int *);
00073 static struct lhsParseNode *CompressCEs(void *,struct lhsParseNode *,int *);
00074 static void IncrementNandDepth(void *,struct lhsParseNode *,int);
00075 static struct lhsParseNode *CreateInitialPattern(void *);
00076 static struct lhsParseNode *ReorderDriver(void *,struct lhsParseNode *,int *,int);
00077 static struct lhsParseNode *AddRemainingInitialPatterns(void *,struct lhsParseNode *);
00078 static void PrintNodes(void *,char *,struct lhsParseNode *);
00079 static struct lhsParseNode *AssignPatternIndices(struct lhsParseNode *,short,int);
00080 static void PropagateIndexSlotPatternValues(struct lhsParseNode *,
00081 short,short,
00082 struct symbolHashNode *,
00083 short);
00084 static void PropagateJoinDepth(struct lhsParseNode *,short);
00085 static void PropagateNandDepth(struct lhsParseNode *,int,int);
00086 static void PropagateNandDepth2( struct lhsParseNode *,int,int);
00087 static intBool AddNandPatterns(void *,int,struct lhsParseNode *,struct lhsParseNode *,
00088 struct lhsParseNode *,struct variableReference *,
00089 struct groupReference *);
00090 static int VariableDepth(void *,struct variableReference *);
00091 static void InsertNandPatterns(void *,struct lhsParseNode *,struct lhsParseNode *,
00092 struct lhsParseNode *,int);
00093 static void MarkExistsNands(struct lhsParseNode *);
00094 static intBool AddNandPatternsForTestCE(int,struct lhsParseNode *,struct variableReference *);
00095 static intBool AddNandPatternsForSubconstraints(int,struct lhsParseNode *,struct variableReference *);
00096
00097
00098
00099
00100
00101 globle struct lhsParseNode *ReorderPatterns(
00102 void *theEnv,
00103 struct lhsParseNode *theLHS,
00104 int *anyChange)
00105 {
00106 struct lhsParseNode *newLHS, *patternPtr, *tempLHS, *lastLHS;
00107 unsigned int whichCE;
00108
00109
00110
00111
00112
00113
00114
00115 if (theLHS == NULL) return(theLHS);
00116
00117
00118
00119
00120
00121 newLHS = GetLHSParseNode(theEnv);
00122 newLHS->type = AND_CE;
00123 newLHS->right = theLHS;
00124
00125
00126
00127
00128
00129 newLHS = ReorderDriver(theEnv,newLHS,anyChange,1);
00130 newLHS = ReorderDriver(theEnv,newLHS,anyChange,2);
00131
00132
00133
00134
00135
00136
00137 if (newLHS->type == OR_CE)
00138 {
00139 for (tempLHS = newLHS->right, lastLHS = NULL;
00140 tempLHS != NULL;
00141 lastLHS = tempLHS, tempLHS = tempLHS->bottom)
00142 {
00143 if (tempLHS->type != AND_CE)
00144 {
00145 theLHS = GetLHSParseNode(theEnv);
00146 theLHS->type = AND_CE;
00147 theLHS->right = tempLHS;
00148 theLHS->bottom = tempLHS->bottom;
00149 tempLHS->bottom = NULL;
00150 if (lastLHS == NULL)
00151 { newLHS->right = theLHS; }
00152 else
00153 { lastLHS->bottom = theLHS; }
00154 tempLHS = theLHS;
00155 }
00156 }
00157 }
00158 else if (newLHS->type != AND_CE)
00159 {
00160 theLHS = newLHS;
00161 newLHS = GetLHSParseNode(theEnv);
00162 newLHS->type = AND_CE;
00163 newLHS->right = theLHS;
00164 }
00165
00166
00167
00168
00169
00170 if (newLHS->type == OR_CE)
00171 {
00172 for (theLHS = newLHS->right;
00173 theLHS != NULL;
00174 theLHS = theLHS->bottom)
00175 {
00176 MarkExistsNands(theLHS->right);
00177 AddNandPatterns(theEnv,1,NULL,theLHS->right,theLHS->right,NULL,NULL);
00178 }
00179 }
00180 else
00181 {
00182 MarkExistsNands(newLHS->right);
00183 AddNandPatterns(theEnv,1,NULL,newLHS->right,newLHS->right,NULL,NULL);
00184 }
00185
00186
00187
00188
00189
00190
00191
00192 AddInitialPatterns(theEnv,newLHS);
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202 if (newLHS->type == OR_CE) theLHS = newLHS->right;
00203 else theLHS = newLHS;
00204
00205 for (;
00206 theLHS != NULL;
00207 theLHS = theLHS->bottom)
00208 {
00209 whichCE = 1;
00210
00211 for (patternPtr = theLHS->right;
00212 patternPtr != NULL;
00213 patternPtr = patternPtr->bottom)
00214 { if (patternPtr->userCE) patternPtr->whichCE = whichCE++; }
00215
00216 AssignPatternIndices(theLHS->right,1,1);
00217 }
00218
00219
00220
00221
00222
00223 return(newLHS);
00224 }
00225
00226
00227
00228
00229
00230 static struct lhsParseNode *ReorderDriver(
00231 void *theEnv,
00232 struct lhsParseNode *theLHS,
00233 int *anyChange,
00234 int pass)
00235 {
00236 struct lhsParseNode *argPtr;
00237 struct lhsParseNode *before, *save;
00238 int change, newChange;
00239 *anyChange = FALSE;
00240
00241
00242
00243
00244
00245
00246 change = TRUE;
00247 while (change)
00248 {
00249
00250
00251
00252
00253 change = FALSE;
00254
00255
00256
00257
00258
00259 if ((theLHS->type == AND_CE) ||
00260 (theLHS->type == NOT_CE) ||
00261 (theLHS->type == OR_CE))
00262 {
00263 if (pass == 1) theLHS = PerformReorder1(theEnv,theLHS,&newChange);
00264 else theLHS = PerformReorder2(theEnv,theLHS,&newChange);
00265
00266 if (newChange)
00267 {
00268 *anyChange = TRUE;
00269 change = TRUE;
00270 }
00271
00272 theLHS = CompressCEs(theEnv,theLHS,&newChange);
00273
00274 if (newChange)
00275 {
00276 *anyChange = TRUE;
00277 change = TRUE;
00278 }
00279 }
00280
00281
00282
00283
00284
00285 before = NULL;
00286 argPtr = theLHS->right;
00287
00288 while (argPtr != NULL)
00289 {
00290
00291
00292
00293
00294 save = argPtr->bottom;
00295
00296
00297
00298
00299
00300 if ((argPtr->type == AND_CE) ||
00301 (argPtr->type == NOT_CE) ||
00302 (argPtr->type == OR_CE))
00303 {
00304 if (before == NULL)
00305 {
00306 argPtr->bottom = NULL;
00307 theLHS->right = ReorderDriver(theEnv,argPtr,&newChange,pass);
00308 theLHS->right->bottom = save;
00309 before = theLHS->right;
00310 }
00311 else
00312 {
00313 argPtr->bottom = NULL;
00314 before->bottom = ReorderDriver(theEnv,argPtr,&newChange,pass);
00315 before->bottom->bottom = save;
00316 before = before->bottom;
00317 }
00318
00319 if (newChange)
00320 {
00321 *anyChange = TRUE;
00322 change = TRUE;
00323 }
00324 }
00325 else
00326 { before = argPtr; }
00327
00328
00329
00330
00331
00332 argPtr = save;
00333 }
00334 }
00335
00336
00337
00338
00339
00340 return(theLHS);
00341 }
00342
00343
00344
00345
00346 static void MarkExistsNands(
00347 struct lhsParseNode *theLHS)
00348 {
00349 int currentDepth = 1;
00350 struct lhsParseNode *tmpLHS;
00351
00352 while (theLHS != NULL)
00353 {
00354 if (IsExistsSubjoin(theLHS,currentDepth))
00355 {
00356 theLHS->existsNand = TRUE;
00357
00358 for (tmpLHS = theLHS;
00359 tmpLHS != NULL;
00360 tmpLHS = tmpLHS->bottom)
00361 {
00362 tmpLHS->beginNandDepth--;
00363 if (tmpLHS->endNandDepth <= currentDepth)
00364 { break; }
00365 else
00366 { tmpLHS->endNandDepth--; }
00367 }
00368 }
00369
00370 currentDepth = theLHS->endNandDepth;
00371 theLHS = theLHS->bottom;
00372 }
00373 }
00374
00375
00376
00377
00378
00379
00380 static intBool AddNandPatterns(
00381 void *theEnv,
00382 int depth,
00383 struct lhsParseNode *parent,
00384 struct lhsParseNode *firstPattern,
00385 struct lhsParseNode *theLHS,
00386 struct variableReference *variables,
00387 struct groupReference *groups)
00388 {
00389 struct lhsParseNode *nandStart, *theField, *newParent, *subField;
00390 struct variableReference *startVariables, *tmpVar;
00391 intBool addPatterns = FALSE, returnAddPatterns = FALSE;
00392 struct groupReference *tmpGroup;
00393 int vdepth;
00394
00395 nandStart = theLHS;
00396 startVariables = variables;
00397 newParent = parent;
00398
00399 while (theLHS != NULL)
00400 {
00401 if (theLHS->beginNandDepth > depth)
00402 {
00403
00404
00405
00406
00407 tmpGroup = get_struct(theEnv,groupReference);
00408 tmpGroup->depth = depth;
00409 tmpGroup->theGroup = theLHS;
00410 tmpGroup->next = groups;
00411 groups = tmpGroup;
00412
00413 if (AddNandPatterns(theEnv,depth+1,newParent,firstPattern,theLHS,variables,groups))
00414 { addPatterns = TRUE; }
00415
00416 groups = tmpGroup->next;
00417 rtn_struct(theEnv,variableReference,tmpGroup);
00418
00419
00420
00421
00422
00423 while (theLHS->endNandDepth > depth)
00424 { theLHS = theLHS->bottom; }
00425 }
00426 else
00427 {
00428
00429
00430
00431
00432 if (theLHS->type == PATTERN_CE)
00433 {
00434 for (theField = theLHS->right; theField != NULL; theField = theField->right)
00435 {
00436 if (theField->multifieldSlot)
00437 { subField = theField->bottom; }
00438 else
00439 { subField = theField; }
00440
00441 while (subField != NULL)
00442 {
00443 if ((subField->type == SF_VARIABLE) || (subField->type == MF_VARIABLE))
00444 {
00445 vdepth = VariableDepth(subField->value,variables);
00446
00447 if (vdepth != -1)
00448 {
00449 if (theLHS->negated)
00450 {
00451 if ((depth - vdepth) > 0)
00452 { addPatterns = TRUE; }
00453 }
00454 else
00455 {
00456 if ((depth - vdepth) > 1)
00457 { returnAddPatterns = TRUE; }
00458 }
00459 }
00460
00461 tmpVar = get_struct(theEnv,variableReference);
00462 tmpVar->depth = depth;
00463 tmpVar->name = (struct symbolHashNode *) subField->value;
00464 tmpVar->next = variables;
00465 variables = tmpVar;
00466 }
00467
00468 if (theLHS->negated)
00469 {
00470 if (AddNandPatternsForSubconstraints(depth+1,subField,variables))
00471 { addPatterns = TRUE; }
00472 }
00473 else
00474 {
00475 if (AddNandPatternsForSubconstraints(depth,subField,variables))
00476 { returnAddPatterns = TRUE; }
00477 }
00478
00479 if (theField->multifieldSlot)
00480 { subField = subField->right; }
00481 else
00482 { subField = NULL; }
00483 }
00484 }
00485 }
00486 else if (theLHS->type == TEST_CE)
00487 {
00488 if (AddNandPatternsForTestCE(depth,theLHS->expression,variables))
00489 { returnAddPatterns = TRUE; }
00490 }
00491
00492
00493
00494
00495
00496 if (theLHS->endNandDepth < depth)
00497 {
00498
00499
00500
00501
00502 while (variables != startVariables)
00503 {
00504 tmpVar = variables->next;
00505 rtn_struct(theEnv,variableReference,variables);
00506 variables = tmpVar;
00507 }
00508
00509 if (addPatterns)
00510 { InsertNandPatterns(theEnv,parent,firstPattern,nandStart,depth); }
00511
00512 return(addPatterns || returnAddPatterns);
00513 }
00514 }
00515
00516
00517
00518
00519
00520 newParent = theLHS;
00521 theLHS = theLHS->bottom;
00522 }
00523
00524 if (addPatterns)
00525 { InsertNandPatterns(theEnv,parent,firstPattern,nandStart,depth); }
00526
00527
00528
00529
00530
00531 while (variables != startVariables)
00532 {
00533 tmpVar = variables->next;
00534 rtn_struct(theEnv,variableReference,variables);
00535 variables = tmpVar;
00536 }
00537
00538 return(addPatterns || returnAddPatterns);
00539 }
00540
00541
00542
00543
00544 static intBool AddNandPatternsForTestCE(
00545 int depth,
00546 struct lhsParseNode *testCE,
00547 struct variableReference *variables)
00548 {
00549 int vdepth;
00550
00551 while (testCE != NULL)
00552 {
00553 if ((testCE->type == SF_VARIABLE) || (testCE->type == MF_VARIABLE))
00554 {
00555 vdepth = VariableDepth(testCE->value,variables);
00556
00557 if (vdepth != -1)
00558 {
00559 if ((depth - vdepth) > 1)
00560 { return(TRUE); }
00561 }
00562 }
00563
00564 if ((testCE->bottom != NULL) &&
00565 (AddNandPatternsForTestCE(depth,testCE->bottom,variables)))
00566 { return(TRUE); }
00567
00568 testCE = testCE->right;
00569 }
00570
00571 return(FALSE);
00572 }
00573
00574
00575
00576
00577 static intBool AddNandPatternsForSubconstraints(
00578 int depth,
00579 struct lhsParseNode *theField,
00580 struct variableReference *variables)
00581 {
00582 struct lhsParseNode *andField;
00583 struct lhsParseNode *orField;
00584 int vdepth;
00585
00586
00587
00588
00589
00590 orField = theField->bottom;
00591 for (;
00592 orField != NULL;
00593 orField = orField->bottom)
00594 {
00595
00596
00597
00598
00599 for (andField = orField;
00600 andField != NULL;
00601 andField = andField->right)
00602 {
00603
00604
00605
00606
00607
00608 if ((andField->type == SF_VARIABLE) || (andField->type == MF_VARIABLE))
00609 {
00610 vdepth = VariableDepth(andField->value,variables);
00611
00612 if (vdepth != -1)
00613 {
00614 if ((depth - vdepth) > 1)
00615 { return(TRUE); }
00616 }
00617 }
00618
00619
00620
00621
00622
00623
00624 else if ((andField->type == PREDICATE_CONSTRAINT) ||
00625 (andField->type == RETURN_VALUE_CONSTRAINT))
00626 {
00627 if (AddNandPatternsForTestCE(depth,andField->expression,variables))
00628 { return(TRUE); }
00629 }
00630 }
00631 }
00632
00633
00634
00635
00636
00637 return(FALSE);
00638 }
00639
00640
00641
00642
00643 static void InsertNandPatterns(
00644 void *theEnv,
00645 struct lhsParseNode *parent,
00646 struct lhsParseNode *firstPattern,
00647 struct lhsParseNode *endPattern,
00648 int depth)
00649 {
00650 struct lhsParseNode *theLHS, *newNode, *first = NULL, *last = NULL;
00651 struct lhsParseNode *theFinalGroup = NULL;
00652 int lastEnd = 1;
00653 intBool insideFinalGroup = FALSE;
00654 int minDepth, maxDepth;
00655
00656 if (parent == NULL) return;
00657
00658
00659
00660
00661
00662 for (theLHS = firstPattern; theLHS != endPattern; theLHS = theLHS->bottom)
00663 {
00664 if ((lastEnd == 1) && (theLHS->beginNandDepth > 1))
00665 { theFinalGroup = theLHS; }
00666
00667 lastEnd = theLHS->endNandDepth;
00668 }
00669
00670 if (lastEnd == 1)
00671 { theFinalGroup = endPattern; }
00672
00673 minDepth = theFinalGroup->endNandDepth;
00674 maxDepth = theFinalGroup->beginNandDepth;
00675
00676 for (theLHS = theFinalGroup; theLHS != endPattern; theLHS = theLHS->bottom)
00677 {
00678 if (theLHS->endNandDepth < minDepth)
00679 { minDepth = theLHS->endNandDepth; }
00680
00681 if (theLHS->beginNandDepth > maxDepth)
00682 { maxDepth = theLHS->beginNandDepth; }
00683 }
00684
00685
00686
00687 for (theLHS = firstPattern; theLHS != endPattern; theLHS = theLHS->bottom)
00688 {
00689 if (theLHS == theFinalGroup)
00690 { insideFinalGroup = TRUE; }
00691
00692 newNode = GetLHSParseNode(theEnv);
00693 CopyLHSParseNode(theEnv,newNode,theLHS,TRUE);
00694 newNode->right = CopyLHSParseNodes(theEnv,theLHS->right);
00695 newNode->value = NULL;
00696
00697 if (insideFinalGroup)
00698 {
00699 newNode->beginNandDepth = depth + (theLHS->beginNandDepth - minDepth);
00700 newNode->endNandDepth = depth + (theLHS->endNandDepth - minDepth);
00701 }
00702 else
00703 {
00704 newNode->beginNandDepth = depth + (theLHS->beginNandDepth - firstPattern->beginNandDepth);
00705 newNode->endNandDepth = depth + (theLHS->endNandDepth - firstPattern->beginNandDepth);
00706 }
00707
00708 if (first == NULL)
00709 { first = newNode; }
00710 else
00711 { last->bottom = newNode; }
00712
00713 last = newNode;
00714 }
00715
00716 first->existsNand = endPattern->existsNand;
00717 endPattern->existsNand = FALSE;
00718
00719 parent->bottom = first;
00720 last->bottom = endPattern;
00721 }
00722
00723
00724
00725
00726
00727 static int VariableDepth(
00728 void *theName,
00729 struct variableReference *variables)
00730 {
00731 while (variables != NULL)
00732 {
00733 if (variables->name == theName)
00734 { return(variables->depth); }
00735
00736 variables = variables->next;
00737 }
00738
00739 return -1;
00740 }
00741
00742
00743
00744
00745
00746
00747 globle void AddInitialPatterns(
00748 void *theEnv,
00749 struct lhsParseNode *theLHS)
00750 {
00751 struct lhsParseNode *thePattern;
00752
00753
00754
00755
00756
00757
00758 if (theLHS->type == OR_CE)
00759 {
00760 for (thePattern = theLHS->right;
00761 thePattern != NULL;
00762 thePattern = thePattern->bottom)
00763 { AddInitialPatterns(theEnv,thePattern); }
00764
00765 return;
00766 }
00767
00768
00769
00770
00771
00772 theLHS->right = AddRemainingInitialPatterns(theEnv,theLHS->right);
00773 }
00774
00775
00776
00777
00778
00779
00780 static struct lhsParseNode *PerformReorder1(
00781 void *theEnv,
00782 struct lhsParseNode *theLHS,
00783 int *newChange)
00784 {
00785 struct lhsParseNode *argPtr, *lastArg, *nextArg;
00786 struct lhsParseNode *tempArg, *newNode;
00787 int count;
00788 int change;
00789
00790
00791
00792
00793
00794 change = TRUE;
00795 *newChange = FALSE;
00796
00797 while (change)
00798 {
00799 change = FALSE;
00800 count = 1;
00801 lastArg = NULL;
00802
00803 for (argPtr = theLHS->right;
00804 argPtr != NULL;)
00805 {
00806
00807
00808
00809
00810 if ((theLHS->type == AND_CE) && (argPtr->type == OR_CE))
00811 {
00812 theLHS = ReverseAndOr(theEnv,theLHS,argPtr->right,count);
00813
00814 change = TRUE;
00815 *newChange = TRUE;
00816 break;
00817 }
00818
00819
00820
00821
00822
00823 else if ((theLHS->type == NOT_CE) && (argPtr->type == OR_CE))
00824 {
00825 change = TRUE;
00826 *newChange = TRUE;
00827
00828 tempArg = argPtr->right;
00829
00830 argPtr->right = NULL;
00831 argPtr->bottom = NULL;
00832 ReturnLHSParseNodes(theEnv,argPtr);
00833 theLHS->type = AND_CE;
00834 theLHS->right = tempArg;
00835
00836 while (tempArg != NULL)
00837 {
00838 newNode = GetLHSParseNode(theEnv);
00839 CopyLHSParseNode(theEnv,newNode,tempArg,FALSE);
00840 newNode->right = tempArg->right;
00841 newNode->bottom = NULL;
00842
00843 tempArg->type = NOT_CE;
00844 tempArg->negated = FALSE;
00845 tempArg->exists = FALSE;
00846 tempArg->existsNand = FALSE;
00847 tempArg->logical = FALSE;
00848 tempArg->value = NULL;
00849 tempArg->expression = NULL;
00850 tempArg->right = newNode;
00851
00852 tempArg = tempArg->bottom;
00853 }
00854
00855 break;
00856 }
00857
00858
00859
00860
00861
00862
00863 else if (((theLHS->type == OR_CE) && (argPtr->type == OR_CE)) ||
00864 ((theLHS->type == AND_CE) && (argPtr->type == AND_CE)))
00865 {
00866 if (argPtr->logical) theLHS->logical = TRUE;
00867
00868 change = TRUE;
00869 *newChange = TRUE;
00870 tempArg = argPtr->right;
00871 nextArg = argPtr->bottom;
00872 argPtr->right = NULL;
00873 argPtr->bottom = NULL;
00874 ReturnLHSParseNodes(theEnv,argPtr);
00875
00876 if (lastArg == NULL)
00877 { theLHS->right = tempArg; }
00878 else
00879 { lastArg->bottom = tempArg; }
00880
00881 argPtr = tempArg;
00882 while (tempArg->bottom != NULL) tempArg = tempArg->bottom;
00883 tempArg->bottom = nextArg;
00884 }
00885
00886
00887
00888
00889
00890 else
00891 {
00892 count++;
00893 lastArg = argPtr;
00894 argPtr = argPtr->bottom;
00895 }
00896 }
00897 }
00898
00899
00900
00901
00902
00903 return(theLHS);
00904 }
00905
00906
00907
00908
00909
00910
00911 static struct lhsParseNode *PerformReorder2(
00912 void *theEnv,
00913 struct lhsParseNode *theLHS,
00914 int *newChange)
00915 {
00916 struct lhsParseNode *argPtr;
00917 int change;
00918
00919
00920
00921
00922
00923 change = TRUE;
00924 *newChange = FALSE;
00925
00926 while (change)
00927 {
00928 change = FALSE;
00929
00930 for (argPtr = theLHS->right;
00931 argPtr != NULL;)
00932 {
00933
00934
00935
00936
00937
00938
00939 if ((theLHS->type == NOT_CE) &&
00940 (argPtr->type == NOT_CE) &&
00941 (argPtr->right != NULL) &&
00942 (argPtr->right->type == NOT_CE))
00943 {
00944 change = TRUE;
00945 *newChange = TRUE;
00946
00947 theLHS->right = argPtr->right->right;
00948
00949 argPtr->right->right = NULL;
00950 ReturnLHSParseNodes(theEnv,argPtr);
00951
00952 break;
00953 }
00954
00955
00956
00957
00958
00959
00960 else if ((theLHS->type == NOT_CE) &&
00961 (argPtr->type == NOT_CE) &&
00962 (argPtr->right != NULL) &&
00963 (argPtr->right->type == PATTERN_CE))
00964 {
00965 change = TRUE;
00966 *newChange = TRUE;
00967
00968 CopyLHSParseNode(theEnv,theLHS,argPtr->right,FALSE);
00969
00970 theLHS->negated = TRUE;
00971 theLHS->exists = TRUE;
00972 theLHS->existsNand = FALSE;
00973 theLHS->right = argPtr->right->right;
00974
00975 argPtr->right->networkTest = NULL;
00976 argPtr->right->externalNetworkTest = NULL;
00977 argPtr->right->secondaryNetworkTest = NULL;
00978 argPtr->right->secondaryExternalNetworkTest = NULL;
00979 argPtr->right->externalRightHash = NULL;
00980 argPtr->right->externalLeftHash = NULL;
00981 argPtr->right->leftHash = NULL;
00982 argPtr->right->rightHash = NULL;
00983 argPtr->right->betaHash = NULL;
00984 argPtr->right->expression = NULL;
00985 argPtr->right->userData = NULL;
00986 argPtr->right->right = NULL;
00987 argPtr->right->bottom = NULL;
00988 ReturnLHSParseNodes(theEnv,argPtr);
00989 break;
00990 }
00991
00992
00993
00994
00995
00996
00997 else if ((theLHS->type == NOT_CE) && (argPtr->type == PATTERN_CE))
00998 {
00999 change = TRUE;
01000 *newChange = TRUE;
01001
01002 CopyLHSParseNode(theEnv,theLHS,argPtr,FALSE);
01003
01004 theLHS->negated = TRUE;
01005 theLHS->exists = FALSE;
01006 theLHS->existsNand = FALSE;
01007 theLHS->right = argPtr->right;
01008
01009 argPtr->networkTest = NULL;
01010 argPtr->externalNetworkTest = NULL;
01011 argPtr->secondaryNetworkTest = NULL;
01012 argPtr->secondaryExternalNetworkTest = NULL;
01013 argPtr->externalRightHash = NULL;
01014 argPtr->externalLeftHash = NULL;
01015 argPtr->constantSelector = NULL;
01016 argPtr->constantValue = NULL;
01017 argPtr->leftHash = NULL;
01018 argPtr->rightHash = NULL;
01019 argPtr->betaHash = NULL;
01020 argPtr->expression = NULL;
01021 argPtr->userData = NULL;
01022 argPtr->right = NULL;
01023 argPtr->bottom = NULL;
01024 ReturnLHSParseNodes(theEnv,argPtr);
01025 break;
01026 }
01027
01028
01029
01030
01031
01032
01033
01034 else if ((theLHS->type == NOT_CE) &&
01035 ((argPtr->type == AND_CE) || (argPtr->type == NOT_CE)))
01036 {
01037 change = TRUE;
01038 *newChange = TRUE;
01039
01040 theLHS->type = argPtr->type;
01041
01042 theLHS->negated = argPtr->negated;
01043 theLHS->exists = argPtr->exists;
01044 theLHS->existsNand = argPtr->existsNand;
01045 theLHS->value = argPtr->value;
01046 theLHS->logical = argPtr->logical;
01047 theLHS->right = argPtr->right;
01048 argPtr->right = NULL;
01049 argPtr->bottom = NULL;
01050 ReturnLHSParseNodes(theEnv,argPtr);
01051
01052 IncrementNandDepth(theEnv,theLHS->right,TRUE);
01053 break;
01054 }
01055
01056
01057
01058
01059
01060 else
01061 {
01062 argPtr = argPtr->bottom;
01063 }
01064 }
01065 }
01066
01067
01068
01069
01070
01071 return(theLHS);
01072 }
01073
01074
01075
01076
01077
01078
01079
01080
01081
01082
01083
01084
01085
01086 static struct lhsParseNode *ReverseAndOr(
01087 void *theEnv,
01088 struct lhsParseNode *listOfCEs,
01089 struct lhsParseNode *orCE,
01090 int orPosition)
01091 {
01092 int count;
01093 struct lhsParseNode *listOfExpandedOrCEs = NULL;
01094 struct lhsParseNode *lastExpandedOrCE = NULL;
01095 struct lhsParseNode *copyOfCEs, *replaceCE;
01096
01097
01098
01099
01100
01101
01102 while (orCE != NULL)
01103 {
01104
01105
01106
01107
01108 copyOfCEs = CopyLHSParseNodes(theEnv,listOfCEs);
01109
01110
01111
01112
01113
01114
01115
01116 for (count = 1, replaceCE = copyOfCEs->right;
01117 count != orPosition;
01118 count++, replaceCE = replaceCE->bottom)
01119 { }
01120
01121
01122
01123
01124
01125
01126
01127
01128
01129 ReturnLHSParseNodes(theEnv,replaceCE->right);
01130
01131
01132
01133
01134
01135
01136
01137
01138
01139
01140 CopyLHSParseNode(theEnv,replaceCE,orCE,TRUE);
01141 replaceCE->right = CopyLHSParseNodes(theEnv,orCE->right);
01142
01143
01144
01145
01146
01147
01148 if (lastExpandedOrCE == NULL)
01149 {
01150 listOfExpandedOrCEs = copyOfCEs;
01151 copyOfCEs->bottom = NULL;
01152 lastExpandedOrCE = copyOfCEs;
01153 }
01154 else
01155 {
01156 lastExpandedOrCE->bottom = copyOfCEs;
01157 copyOfCEs->bottom = NULL;
01158 lastExpandedOrCE = copyOfCEs;
01159 }
01160
01161
01162
01163
01164
01165 orCE = orCE->bottom;
01166 }
01167
01168
01169
01170
01171
01172 ReturnLHSParseNodes(theEnv,listOfCEs);
01173
01174
01175
01176
01177
01178 copyOfCEs = GetLHSParseNode(theEnv);
01179 copyOfCEs->type = OR_CE;
01180 copyOfCEs->right = listOfExpandedOrCEs;
01181
01182
01183
01184
01185
01186 return(copyOfCEs);
01187 }
01188
01189
01190
01191
01192 static struct lhsParseNode *CompressCEs(
01193 void *theEnv,
01194 struct lhsParseNode *theLHS,
01195 int *newChange)
01196 {
01197 struct lhsParseNode *argPtr, *lastArg, *nextArg;
01198 struct lhsParseNode *tempArg;
01199 int change;
01200 struct expr *e1, *e2;
01201
01202
01203
01204
01205
01206 change = TRUE;
01207 *newChange = FALSE;
01208
01209 while (change)
01210 {
01211 change = FALSE;
01212 lastArg = NULL;
01213
01214 for (argPtr = theLHS->right;
01215 argPtr != NULL;)
01216 {
01217
01218
01219
01220
01221
01222 if (((theLHS->type == OR_CE) && (argPtr->type == OR_CE)) ||
01223 ((theLHS->type == AND_CE) && (argPtr->type == AND_CE)))
01224 {
01225 if (argPtr->logical) theLHS->logical = TRUE;
01226
01227 change = TRUE;
01228 *newChange = TRUE;
01229 tempArg = argPtr->right;
01230 nextArg = argPtr->bottom;
01231 argPtr->right = NULL;
01232 argPtr->bottom = NULL;
01233 ReturnLHSParseNodes(theEnv,argPtr);
01234
01235 if (lastArg == NULL)
01236 { theLHS->right = tempArg; }
01237 else
01238 { lastArg->bottom = tempArg; }
01239
01240 argPtr = tempArg;
01241 while (tempArg->bottom != NULL) tempArg = tempArg->bottom;
01242 tempArg->bottom = nextArg;
01243 }
01244
01245
01246
01247
01248
01249
01250 else if ((theLHS->type == NOT_CE) && (argPtr->type == TEST_CE))
01251 {
01252 change = TRUE;
01253 *newChange = TRUE;
01254
01255 e1 = GenConstant(theEnv,FCALL,ExpressionData(theEnv)->PTR_NOT);
01256 e2 = LHSParseNodesToExpression(theEnv,argPtr->expression);
01257 e1->arg_list = e2;
01258
01259 CopyLHSParseNode(theEnv,theLHS,argPtr,TRUE);
01260
01261 ReturnLHSParseNodes(theEnv,argPtr);
01262 ReturnLHSParseNodes(theEnv,theLHS->expression);
01263
01264 theLHS->expression = ExpressionToLHSParseNodes(theEnv,e1);
01265 theLHS->right = NULL;
01266 ReturnExpression(theEnv,e1);
01267
01268 break;
01269 }
01270
01271
01272
01273
01274
01275
01276 else if ((theLHS->type == AND_CE) && (argPtr->type == TEST_CE) &&
01277 ((argPtr->bottom != NULL) ? argPtr->bottom->type == TEST_CE :
01278 FALSE) &&
01279 (argPtr->beginNandDepth == argPtr->endNandDepth) &&
01280 (argPtr->endNandDepth == argPtr->bottom->beginNandDepth))
01281 {
01282 change = TRUE;
01283 *newChange = TRUE;
01284
01285 e1 = LHSParseNodesToExpression(theEnv,argPtr->expression);
01286 e2 = LHSParseNodesToExpression(theEnv,argPtr->bottom->expression);
01287 e1 = CombineExpressions(theEnv,e1,e2);
01288
01289 ReturnLHSParseNodes(theEnv,argPtr->expression);
01290 argPtr->expression = ExpressionToLHSParseNodes(theEnv,e1);
01291 ReturnExpression(theEnv,e1);
01292
01293 tempArg = argPtr->bottom;
01294 argPtr->bottom = tempArg->bottom;
01295 tempArg->bottom = NULL;
01296
01297 ReturnLHSParseNodes(theEnv,tempArg);
01298 }
01299
01300
01301
01302
01303
01304
01305 else if ((theLHS->type == AND_CE) && (argPtr->type == TEST_CE) &&
01306 (theLHS->right == argPtr) && (argPtr->bottom == NULL))
01307 {
01308 change = TRUE;
01309 *newChange = TRUE;
01310
01311 CopyLHSParseNode(theEnv,theLHS,argPtr,TRUE);
01312 theLHS->right = NULL;
01313 ReturnLHSParseNodes(theEnv,argPtr);
01314 break;
01315 }
01316
01317
01318
01319
01320
01321 else
01322 {
01323 lastArg = argPtr;
01324 argPtr = argPtr->bottom;
01325 }
01326 }
01327 }
01328
01329
01330
01331
01332
01333 return(theLHS);
01334 }
01335
01336
01337
01338
01339 globle struct lhsParseNode *CopyLHSParseNodes(
01340 void *theEnv,
01341 struct lhsParseNode *listOfCEs)
01342 {
01343 struct lhsParseNode *newList;
01344
01345 if (listOfCEs == NULL)
01346 { return(NULL); }
01347
01348 newList = get_struct(theEnv,lhsParseNode);
01349 CopyLHSParseNode(theEnv,newList,listOfCEs,TRUE);
01350
01351 newList->right = CopyLHSParseNodes(theEnv,listOfCEs->right);
01352 newList->bottom = CopyLHSParseNodes(theEnv,listOfCEs->bottom);
01353
01354 return(newList);
01355 }
01356
01357
01358
01359
01360 globle void CopyLHSParseNode(
01361 void *theEnv,
01362 struct lhsParseNode *dest,
01363 struct lhsParseNode *src,
01364 int duplicate)
01365 {
01366 dest->type = src->type;
01367 dest->value = src->value;
01368 dest->negated = src->negated;
01369 dest->exists = src->exists;
01370 dest->existsNand = src->existsNand;
01371 dest->bindingVariable = src->bindingVariable;
01372 dest->withinMultifieldSlot = src->withinMultifieldSlot;
01373 dest->multifieldSlot = src->multifieldSlot;
01374 dest->multiFieldsBefore = src->multiFieldsBefore;
01375 dest->multiFieldsAfter = src->multiFieldsAfter;
01376 dest->singleFieldsBefore = src->singleFieldsBefore;
01377 dest->singleFieldsAfter = src->singleFieldsAfter;
01378 dest->logical = src->logical;
01379 dest->userCE = src->userCE;
01380 dest->referringNode = src->referringNode;
01381 dest->patternType = src->patternType;
01382 dest->pattern = src->pattern;
01383 dest->index = src->index;
01384 dest->slot = src->slot;
01385 dest->slotNumber = src->slotNumber;
01386 dest->beginNandDepth = src->beginNandDepth;
01387 dest->endNandDepth = src->endNandDepth;
01388 dest->joinDepth = src->joinDepth;
01389
01390
01391
01392
01393
01394
01395
01396 if (duplicate)
01397 {
01398 dest->networkTest = CopyExpression(theEnv,src->networkTest);
01399 dest->externalNetworkTest = CopyExpression(theEnv,src->externalNetworkTest);
01400 dest->secondaryNetworkTest = CopyExpression(theEnv,src->secondaryNetworkTest);
01401 dest->secondaryExternalNetworkTest = CopyExpression(theEnv,src->secondaryExternalNetworkTest);
01402 dest->externalRightHash = CopyExpression(theEnv,src->externalRightHash);
01403 dest->externalLeftHash = CopyExpression(theEnv,src->externalLeftHash);
01404 dest->constantSelector = CopyExpression(theEnv,src->constantSelector);
01405 dest->constantValue = CopyExpression(theEnv,src->constantValue);
01406 dest->leftHash = CopyExpression(theEnv,src->leftHash);
01407 dest->betaHash = CopyExpression(theEnv,src->betaHash);
01408 dest->rightHash = CopyExpression(theEnv,src->rightHash);
01409 if (src->userData == NULL)
01410 { dest->userData = NULL; }
01411 else if (src->patternType->copyUserDataFunction == NULL)
01412 { dest->userData = src->userData; }
01413 else
01414 { dest->userData = (*src->patternType->copyUserDataFunction)(theEnv,src->userData); }
01415 dest->expression = CopyLHSParseNodes(theEnv,src->expression);
01416 dest->constraints = CopyConstraintRecord(theEnv,src->constraints);
01417 if (dest->constraints != NULL) dest->derivedConstraints = TRUE;
01418 else dest->derivedConstraints = FALSE;
01419 }
01420 else
01421 {
01422 dest->networkTest = src->networkTest;
01423 dest->externalNetworkTest = src->externalNetworkTest;
01424 dest->secondaryNetworkTest = src->secondaryNetworkTest;
01425 dest->secondaryExternalNetworkTest = src->secondaryExternalNetworkTest;
01426 dest->externalRightHash = src->externalRightHash;
01427 dest->externalLeftHash = src->externalLeftHash;
01428 dest->constantSelector = src->constantSelector;
01429 dest->constantValue = src->constantValue;
01430 dest->leftHash = src->leftHash;
01431 dest->betaHash = src->betaHash;
01432 dest->rightHash = src->rightHash;
01433 dest->userData = src->userData;
01434 dest->expression = src->expression;
01435 dest->derivedConstraints = FALSE;
01436 dest->constraints = src->constraints;
01437 }
01438 }
01439
01440
01441
01442
01443
01444 globle struct lhsParseNode *GetLHSParseNode(
01445 void *theEnv)
01446 {
01447 struct lhsParseNode *newNode;
01448
01449 newNode = get_struct(theEnv,lhsParseNode);
01450 newNode->type = UNKNOWN_VALUE;
01451 newNode->value = NULL;
01452 newNode->negated = FALSE;
01453 newNode->exists = FALSE;
01454 newNode->existsNand = FALSE;
01455 newNode->bindingVariable = FALSE;
01456 newNode->withinMultifieldSlot = FALSE;
01457 newNode->multifieldSlot = FALSE;
01458 newNode->multiFieldsBefore = 0;
01459 newNode->multiFieldsAfter = 0;
01460 newNode->singleFieldsBefore = 0;
01461 newNode->singleFieldsAfter = 0;
01462 newNode->logical = FALSE;
01463 newNode->derivedConstraints = FALSE;
01464 newNode->userCE = TRUE;
01465 newNode->constraints = NULL;
01466 newNode->referringNode = NULL;
01467 newNode->patternType = NULL;
01468 newNode->pattern = -1;
01469 newNode->index = -1;
01470 newNode->slot = NULL;
01471 newNode->slotNumber = -1;
01472 newNode->beginNandDepth = 1;
01473 newNode->endNandDepth = 1;
01474 newNode->joinDepth = 0;
01475 newNode->userData = NULL;
01476 newNode->networkTest = NULL;
01477 newNode->externalNetworkTest = NULL;
01478 newNode->secondaryNetworkTest = NULL;
01479 newNode->secondaryExternalNetworkTest = NULL;
01480 newNode->externalRightHash = NULL;
01481 newNode->externalLeftHash = NULL;
01482 newNode->constantSelector = NULL;
01483 newNode->constantValue = NULL;
01484 newNode->leftHash = NULL;
01485 newNode->betaHash = NULL;
01486 newNode->rightHash = NULL;
01487 newNode->expression = NULL;
01488 newNode->right = NULL;
01489 newNode->bottom = NULL;
01490
01491 return(newNode);
01492 }
01493
01494
01495
01496
01497
01498 globle void ReturnLHSParseNodes(
01499 void *theEnv,
01500 struct lhsParseNode *waste)
01501 {
01502 if (waste != NULL)
01503 {
01504 ReturnExpression(theEnv,waste->networkTest);
01505 ReturnExpression(theEnv,waste->externalNetworkTest);
01506 ReturnExpression(theEnv,waste->secondaryNetworkTest);
01507 ReturnExpression(theEnv,waste->secondaryExternalNetworkTest);
01508 ReturnExpression(theEnv,waste->externalRightHash);
01509 ReturnExpression(theEnv,waste->externalLeftHash);
01510 ReturnExpression(theEnv,waste->constantSelector);
01511 ReturnExpression(theEnv,waste->constantValue);
01512 ReturnExpression(theEnv,waste->leftHash);
01513 ReturnExpression(theEnv,waste->betaHash);
01514 ReturnExpression(theEnv,waste->rightHash);
01515 ReturnLHSParseNodes(theEnv,waste->right);
01516 ReturnLHSParseNodes(theEnv,waste->bottom);
01517 ReturnLHSParseNodes(theEnv,waste->expression);
01518 if (waste->derivedConstraints) RemoveConstraint(theEnv,waste->constraints);
01519 if ((waste->userData != NULL) &&
01520 (waste->patternType->returnUserDataFunction != NULL))
01521 { (*waste->patternType->returnUserDataFunction)(theEnv,waste->userData); }
01522 rtn_struct(theEnv,lhsParseNode,waste);
01523 }
01524 }
01525
01526
01527
01528
01529
01530 globle struct lhsParseNode *ExpressionToLHSParseNodes(
01531 void *theEnv,
01532 struct expr *expressionList)
01533 {
01534 struct lhsParseNode *newList, *theList;
01535 struct FunctionDefinition *theFunction;
01536 int i, theRestriction;
01537
01538
01539
01540
01541
01542 if (expressionList == NULL) return(NULL);
01543
01544
01545
01546
01547
01548
01549 newList = GetLHSParseNode(theEnv);
01550 newList->type = expressionList->type;
01551 newList->value = expressionList->value;
01552 newList->right = ExpressionToLHSParseNodes(theEnv,expressionList->nextArg);
01553 newList->bottom = ExpressionToLHSParseNodes(theEnv,expressionList->argList);
01554
01555
01556
01557
01558
01559
01560
01561 if (newList->type != FCALL) return(newList);
01562
01563 theFunction = (struct FunctionDefinition *) newList->value;
01564 for (theList = newList->bottom, i = 1;
01565 theList != NULL;
01566 theList = theList->right, i++)
01567 {
01568 if (theList->type == SF_VARIABLE)
01569 {
01570 theRestriction = GetNthRestriction(theFunction,i);
01571 theList->constraints = ArgumentTypeToConstraintRecord(theEnv,theRestriction);
01572 theList->derivedConstraints = TRUE;
01573 }
01574 }
01575
01576
01577
01578
01579
01580 return(newList);
01581 }
01582
01583
01584
01585
01586
01587 globle struct expr *LHSParseNodesToExpression(
01588 void *theEnv,
01589 struct lhsParseNode *nodeList)
01590 {
01591 struct expr *newList;
01592
01593 if (nodeList == NULL)
01594 { return(NULL); }
01595
01596 newList = get_struct(theEnv,expr);
01597 newList->type = nodeList->type;
01598 newList->value = nodeList->value;
01599 newList->nextArg = LHSParseNodesToExpression(theEnv,nodeList->right);
01600 newList->argList = LHSParseNodesToExpression(theEnv,nodeList->bottom);
01601
01602 return(newList);
01603 }
01604
01605
01606
01607
01608
01609
01610
01611
01612
01613
01614
01615
01616
01617
01618
01619
01620
01621
01622
01623 static void IncrementNandDepth(
01624 void *theEnv,
01625 struct lhsParseNode *theLHS,
01626 int lastCE)
01627 {
01628
01629
01630
01631
01632
01633 for (;
01634 theLHS != NULL;
01635 theLHS = theLHS->bottom)
01636 {
01637
01638
01639
01640
01641
01642
01643
01644
01645
01646
01647 if ((theLHS->type == PATTERN_CE) || (theLHS->type == TEST_CE))
01648 {
01649 theLHS->beginNandDepth++;
01650
01651 if (lastCE == FALSE) theLHS->endNandDepth++;
01652 else if (theLHS->bottom != NULL) theLHS->endNandDepth++;
01653 }
01654
01655
01656
01657
01658
01659
01660
01661 else if ((theLHS->type == AND_CE) || (theLHS->type == NOT_CE))
01662 {
01663 IncrementNandDepth(theEnv,theLHS->right,
01664 (lastCE ? (theLHS->bottom == NULL) : FALSE));
01665 }
01666
01667
01668
01669
01670
01671
01672 else if (theLHS->type == OR_CE)
01673 { SystemError(theEnv,"REORDER",1); }
01674 }
01675 }
01676
01677
01678
01679
01680
01681
01682
01683 static struct lhsParseNode *CreateInitialPattern(
01684 void *theEnv)
01685 {
01686 struct lhsParseNode *topNode;
01687
01688
01689
01690
01691
01692 topNode = GetLHSParseNode(theEnv);
01693 topNode->type = PATTERN_CE;
01694 topNode->userCE = FALSE;
01695 topNode->bottom = NULL;
01696
01697 return(topNode);
01698 }
01699
01700
01701
01702
01703
01704
01705
01706 static struct lhsParseNode *AddRemainingInitialPatterns(
01707 void *theEnv,
01708 struct lhsParseNode *theLHS)
01709 {
01710 struct lhsParseNode *lastNode = NULL, *thePattern, *rv = theLHS;
01711 int currentDepth = 1;
01712
01713 while (theLHS != NULL)
01714 {
01715 if ((theLHS->type == TEST_CE) &&
01716 (theLHS->beginNandDepth > currentDepth))
01717 {
01718 thePattern = CreateInitialPattern(theEnv);
01719 thePattern->beginNandDepth = theLHS->beginNandDepth;
01720 thePattern->endNandDepth = theLHS->beginNandDepth;
01721 thePattern->logical = theLHS->logical;
01722 thePattern->existsNand = theLHS->existsNand;
01723 theLHS->existsNand = FALSE;
01724
01725 thePattern->bottom = theLHS;
01726
01727 if (lastNode == NULL)
01728 { rv = thePattern; }
01729 else
01730 { lastNode->bottom = thePattern; }
01731 }
01732
01733 lastNode = theLHS;
01734 currentDepth = theLHS->endNandDepth;
01735 theLHS = theLHS->bottom;
01736 }
01737
01738 return(rv);
01739 }
01740
01741
01742
01743
01744
01745 static void PrintNodes(
01746 void *theEnv,
01747 char *fileid,
01748 struct lhsParseNode *theNode)
01749 {
01750 if (theNode == NULL) return;
01751
01752 while (theNode != NULL)
01753 {
01754 switch (theNode->type)
01755 {
01756 case PATTERN_CE:
01757 EnvPrintRouter(theEnv,fileid,"(");
01758 if (theNode->negated) EnvPrintRouter(theEnv,fileid,"n");
01759 if (theNode->exists) EnvPrintRouter(theEnv,fileid,"x");
01760 if (theNode->logical) EnvPrintRouter(theEnv,fileid,"l");
01761 PrintLongInteger(theEnv,fileid,(long long) theNode->beginNandDepth);
01762 EnvPrintRouter(theEnv,fileid,"-");
01763 PrintLongInteger(theEnv,fileid,(long long) theNode->endNandDepth);
01764 EnvPrintRouter(theEnv,fileid," ");
01765 EnvPrintRouter(theEnv,fileid,ValueToString(theNode->right->bottom->value));
01766 EnvPrintRouter(theEnv,fileid,")");
01767 break;
01768
01769 case TEST_CE:
01770 EnvPrintRouter(theEnv,fileid,"(test ");
01771 PrintLongInteger(theEnv,fileid,(long long) theNode->beginNandDepth);
01772 EnvPrintRouter(theEnv,fileid,"-");
01773 PrintLongInteger(theEnv,fileid,(long long) theNode->endNandDepth);
01774 EnvPrintRouter(theEnv,fileid,")");
01775 break;
01776
01777 case NOT_CE:
01778 if (theNode->logical) EnvPrintRouter(theEnv,fileid,"(lnot ");
01779 else EnvPrintRouter(theEnv,fileid,"(not ");;
01780 PrintNodes(theEnv,fileid,theNode->right);
01781 EnvPrintRouter(theEnv,fileid,")");
01782 break;
01783
01784 case OR_CE:
01785 if (theNode->logical) EnvPrintRouter(theEnv,fileid,"(lor ");
01786 else EnvPrintRouter(theEnv,fileid,"(or ");
01787 PrintNodes(theEnv,fileid,theNode->right);
01788 EnvPrintRouter(theEnv,fileid,")");
01789 break;
01790
01791 case AND_CE:
01792 if (theNode->logical) EnvPrintRouter(theEnv,fileid,"(land ");
01793 else EnvPrintRouter(theEnv,fileid,"(and ");
01794 PrintNodes(theEnv,fileid,theNode->right);
01795 EnvPrintRouter(theEnv,fileid,")");
01796 break;
01797
01798 default:
01799 EnvPrintRouter(theEnv,fileid,"(unknown)");
01800 break;
01801
01802 }
01803
01804 theNode = theNode->bottom;
01805 if (theNode != NULL) EnvPrintRouter(theEnv,fileid," ");
01806 }
01807
01808 return;
01809 }
01810
01811
01812
01813
01814
01815
01816
01817
01818
01819
01820
01821
01822
01823
01824 static struct lhsParseNode *AssignPatternIndices(
01825 struct lhsParseNode *theLHS,
01826 short startIndex,
01827 int depth)
01828 {
01829 struct lhsParseNode *theField;
01830 short joinDepth = 0;
01831
01832
01833
01834
01835
01836
01837 while (theLHS != NULL)
01838 {
01839
01840
01841
01842
01843
01844
01845
01846
01847
01848
01849 if (theLHS->beginNandDepth > depth)
01850 {
01851 theLHS = AssignPatternIndices(theLHS,startIndex,theLHS->beginNandDepth);
01852 if (theLHS->endNandDepth < depth) return(theLHS);
01853 startIndex++;
01854 joinDepth++;
01855 }
01856
01857
01858
01859
01860
01861
01862
01863 else if (theLHS->type == TEST_CE)
01864 {
01865 theLHS->joinDepth = joinDepth - 1;
01866 PropagateJoinDepth(theLHS->expression,(short) (joinDepth - 1));
01867 PropagateNandDepth2(theLHS->expression,theLHS->beginNandDepth,theLHS->endNandDepth);
01868 if (theLHS->endNandDepth < depth) return(theLHS);
01869 }
01870
01871
01872
01873
01874
01875
01876
01877
01878 else if (theLHS->type == PATTERN_CE)
01879 {
01880 theLHS->pattern = startIndex;
01881 theLHS->joinDepth = joinDepth;
01882 PropagateJoinDepth(theLHS->right,joinDepth);
01883 for (theField = theLHS->right; theField != NULL; theField = theField->right)
01884 {
01885 theField->pattern = startIndex;
01886 PropagateIndexSlotPatternValues(theField,theField->pattern,
01887 theField->index,theField->slot,
01888 theField->slotNumber);
01889 PropagateNandDepth(theField,theLHS->beginNandDepth,theLHS->endNandDepth);
01890 }
01891
01892 if (theLHS->endNandDepth < depth) return(theLHS);
01893 startIndex++;
01894 joinDepth++;
01895 }
01896
01897
01898
01899
01900
01901 theLHS = theLHS->bottom;
01902 }
01903
01904
01905
01906
01907
01908
01909 return(NULL);
01910 }
01911
01912
01913
01914
01915
01916 static void PropagateIndexSlotPatternValues(
01917 struct lhsParseNode *theField,
01918 short thePattern,
01919 short theIndex,
01920 struct symbolHashNode *theSlot,
01921 short theSlotNumber)
01922 {
01923 struct lhsParseNode *tmpNode, *andField;
01924
01925
01926
01927
01928
01929 if (theField == NULL) return;
01930
01931
01932
01933
01934
01935
01936 if (theField->multifieldSlot)
01937 {
01938 theField->pattern = thePattern;
01939 if (theIndex > 0) theField->index = theIndex;
01940 theField->slot = theSlot;
01941 theField->slotNumber = theSlotNumber;
01942
01943 for (tmpNode = theField->bottom;
01944 tmpNode != NULL;
01945 tmpNode = tmpNode->right)
01946 {
01947 tmpNode->pattern = thePattern;
01948 tmpNode->slot = theSlot;
01949 PropagateIndexSlotPatternValues(tmpNode,thePattern,tmpNode->index,
01950 theSlot,theSlotNumber);
01951 }
01952
01953 return;
01954 }
01955
01956
01957
01958
01959
01960
01961 for (theField = theField->bottom;
01962 theField != NULL;
01963 theField = theField->bottom)
01964 {
01965
01966
01967
01968
01969
01970
01971 for (andField = theField; andField != NULL; andField = andField->right)
01972 {
01973 andField->pattern = thePattern;
01974 if (theIndex > 0) andField->index = theIndex;
01975 andField->slot = theSlot;
01976 andField->slotNumber = theSlotNumber;
01977 }
01978 }
01979 }
01980
01981
01982
01983
01984 static void PropagateJoinDepth(
01985 struct lhsParseNode *theField,
01986 short joinDepth)
01987 {
01988 while (theField != NULL)
01989 {
01990 theField->joinDepth = joinDepth;
01991 if (theField->bottom != NULL)
01992 { PropagateJoinDepth(theField->bottom,joinDepth); }
01993 if (theField->expression != NULL)
01994 { PropagateJoinDepth(theField->expression,joinDepth); }
01995 theField = theField->right;
01996 }
01997 }
01998
01999
02000
02001
02002 static void PropagateNandDepth(
02003 struct lhsParseNode *theField,
02004 int beginDepth,
02005 int endDepth)
02006 {
02007 if (theField == NULL) return;
02008 theField->beginNandDepth = beginDepth;
02009 theField->endNandDepth = endDepth;
02010
02011 PropagateNandDepth(theField->expression,beginDepth,endDepth);
02012
02013 for (theField = theField->bottom; theField != NULL; theField = theField->right)
02014 { PropagateNandDepth(theField,beginDepth,endDepth); }
02015 }
02016
02017
02018
02019
02020 static void PropagateNandDepth2(
02021 struct lhsParseNode *theField,
02022 int beginDepth,
02023 int endDepth)
02024 {
02025 if (theField == NULL) return;
02026 for (; theField != NULL; theField = theField->right)
02027 {
02028 theField->beginNandDepth = beginDepth;
02029 theField->endNandDepth = endDepth;
02030 PropagateNandDepth2(theField->bottom,beginDepth,endDepth);
02031 }
02032 }
02033
02034
02035
02036
02037 globle int IsExistsSubjoin(
02038 struct lhsParseNode *theLHS,
02039 int parentDepth)
02040 {
02041 int startDepth = theLHS->beginNandDepth;
02042
02043 if ((startDepth - parentDepth) != 2)
02044 { return(FALSE); }
02045
02046 while (theLHS->endNandDepth >= startDepth)
02047 { theLHS = theLHS->bottom; }
02048
02049
02050
02051
02052
02053
02054 if (theLHS->endNandDepth <= parentDepth)
02055 { return(TRUE); }
02056
02057 return(FALSE);
02058 }
02059
02060 #endif
02061