1 23 24 package org.xquark.mediator.decomposer; 25 26 import java.util.ArrayList ; 27 import java.util.HashMap ; 28 import java.util.Stack ; 29 30 import org.xquark.mediator.algebra.*; 31 import org.xquark.mediator.runtime.MediatorException; 32 import org.xquark.xquery.parser.*; 33 import org.xquark.xquery.parser.primitivefunctions.fnfunctions.FunctionCOLLECTION; 34 import org.xquark.xquery.parser.primitivefunctions.fnfunctions.FunctionDOC; 35 import org.xquark.xquery.parser.util.Constants; 36 import org.xquark.xquery.typing.*; 37 38 43 44 48 49 public class AlgebraStructureVisitor extends DefaultParserVisitor { 50 private static final String RCSRevision = "$Revision: 1.10 $"; 54 private static final String RCSName = "Name"; 55 56 private final Integer IN_LOCATED = new Integer (1); 60 private final Integer IN_DEFINITION = new Integer (2); 61 private final Integer IN_FLWR = new Integer (3); 62 private final Integer IN_WHERE = new Integer (4); 63 private final Integer IN_FUNCTION = new Integer (5); 64 private final Integer IN_MAIN_DEFINITION = new Integer (6); 65 private final Integer IN_SEQUENCE = new Integer (7); 66 68 private AlgebraManager depmanager = null; 70 private HashMap parentvariable = null; 71 72 private Stack varStack = new Stack (); 74 private AlgFLWR currentDepFLWR = null; 75 private Algebra currentDepNode = null; 76 private TypeVisitor typevisitor = null; 77 private Stack stateStack = new Stack (); 78 79 private boolean ongoingLet = false; 80 private LocatedExpression ongoingLocatedExpression = null; 81 83 public AlgebraStructureVisitor(AlgebraManager depmanager) throws XQueryException { 87 this.depmanager = depmanager; 88 this.typevisitor = depmanager.getTypeVisitor(); 89 this.parentvariable = new HashMap (); 90 stateStack.push(IN_FLWR); 91 } 92 93 97 private void addLeaf(Algebra leaf) throws MediatorException { 98 if (currentDepNode != null) 99 currentDepNode.addLeaf(leaf); 100 else if (currentDepFLWR != null) 101 currentDepFLWR.addLeaf(leaf); 102 } 103 104 108 public void visit(AggregateFunctionCall arg) throws XQueryException { 109 Integer state = (Integer ) stateStack.peek(); 110 if (state == IN_DEFINITION 111 ) { 112 AlgFunction depfunction = depmanager.addFunction(arg, (state == IN_DEFINITION) ? (Variable) varStack.peek() : null, true); 113 addLeaf(depfunction); 114 ongoingLet = false; 115 ArrayList arguments = arg.getArguments(); 116 if (arguments != null) { 117 Algebra tmpDepNode = currentDepNode; 118 currentDepNode = depfunction; 119 stateStack.push(IN_FUNCTION); 120 for (int i = 0; i < arguments.size(); i++) 121 arg.getArgument(i).accept(this); 122 stateStack.pop(); 123 currentDepNode = tmpDepNode; 124 } 125 } 126 } 127 128 129 public void visit(ListOpArithExpression arg) throws XQueryException { 130 Integer state = (Integer ) stateStack.peek(); 131 if (state == IN_DEFINITION 132 ) { 133 AlgArithFunction deparithfunction = depmanager.addArithFunction(arg, (state == IN_DEFINITION) ? (Variable) varStack.peek() : null, true); 134 addLeaf(deparithfunction); 135 ongoingLet = false; 136 Algebra tmpDepNode = currentDepNode; 137 currentDepNode = deparithfunction; 138 stateStack.push(IN_FUNCTION); 139 arg.getExpression1().accept(this); 140 arg.getExpression2().accept(this); 141 stateStack.pop(); 142 currentDepNode = tmpDepNode; 143 } 144 } 145 146 public void visit(ListOpUNIONExpression arg) throws XQueryException { 147 Integer state = (Integer ) stateStack.peek(); 148 if (state == IN_DEFINITION 149 ) { 150 AlgUnion depunion = depmanager.addUnion(arg, (state == IN_DEFINITION) ? (Variable) varStack.peek() : null, true); 151 addLeaf(depunion); 152 ongoingLet = false; 153 Algebra tmpDepNode = currentDepNode; 154 currentDepNode = depunion; 155 stateStack.push(IN_FUNCTION); 156 arg.getExpression1().accept(this); 157 arg.getExpression2().accept(this); 158 stateStack.pop(); 159 currentDepNode = tmpDepNode; 160 } 161 } 162 163 public void visit(FLWRExpression arg) throws XQueryException { 164 165 ArrayList variables = arg.getVariables(); 166 Integer currentstate = (Integer ) stateStack.peek(); 167 169 Algebra tmpDepNode = currentDepNode; 170 if (currentstate == IN_DEFINITION) { 171 AlgFLWR depflwr = depmanager.addFlwr(arg, (Variable) varStack.peek(), ongoingLet); 172 addLeaf(depflwr); 173 currentDepNode = depflwr; 174 ongoingLet = false; 175 } else if (currentstate == IN_FUNCTION) { 176 AlgFLWR depflwr = depmanager.addFlwr(arg, null, ongoingLet); 177 addLeaf(depflwr); 178 currentDepNode = depflwr; 179 ongoingLet = false; 180 } 181 182 205 229 248 for (int i = 0; i < variables.size(); i++) { 249 Variable vari = (Variable) variables.get(i); 250 vari.setParentFLWRExpression(arg); 251 stateStack.push(IN_FLWR); 252 vari.accept(this); 253 stateStack.pop(); 254 } 255 256 298 XQueryExpression whereclause = arg.getWhereClause(); 299 if (whereclause != null) { 300 stateStack.push(IN_WHERE); 301 whereclause.accept(this); 302 stateStack.pop(); 303 } 304 305 if (currentstate == IN_DEFINITION) 307 currentDepNode = tmpDepNode; 308 309 } 310 311 public void visit(FunctionCOLLECTION arg) throws XQueryException { 312 313 Integer state = (Integer ) stateStack.peek(); 314 AlgSource newsource = null; 315 if (state == IN_DEFINITION) 316 newsource = depmanager.addSource(arg, (Variable) varStack.peek(), ongoingLet); 317 else if (state == IN_LOCATED) 318 newsource = depmanager.addSource(ongoingLocatedExpression, (Variable) null, ongoingLet); 319 else 320 newsource = depmanager.addSource(arg, (Variable) null, ongoingLet); 321 addLeaf(newsource); 323 ongoingLet = false; 324 } 325 326 public void visit(FunctionDOC arg) throws XQueryException { 327 328 Integer state = (Integer ) stateStack.peek(); 329 AlgSourceEval newsource = null; 330 if (state == IN_DEFINITION) 331 newsource = depmanager.addSourceEval(arg, (Variable) varStack.peek(), ongoingLet); 332 else if (state == IN_FUNCTION) 333 newsource = depmanager.addSourceEval(arg, (Variable) varStack.peek(), ongoingLet); 334 else if (state == IN_LOCATED) 335 newsource = depmanager.addSourceEval(ongoingLocatedExpression, (Variable) null, ongoingLet); 336 else 337 newsource = depmanager.addSourceEval(arg, (Variable) null, ongoingLet); 338 addLeaf(newsource); 340 ongoingLet = false; 341 } 342 343 public void visit(PrimitiveFunctionCall arg) throws XQueryException { 344 345 Integer state = (Integer ) stateStack.peek(); 346 if (state == IN_DEFINITION 347 ) { 348 AlgFunction depfunction = depmanager.addFunction(arg, (state == IN_DEFINITION) ? (Variable) varStack.peek() : null, ongoingLet); 349 addLeaf(depfunction); 350 ongoingLet = false; 351 ArrayList arguments = arg.getArguments(); 352 if (arguments != null) { 353 Algebra tmpDepNode = currentDepNode; 354 currentDepNode = depfunction; 355 stateStack.push(IN_FUNCTION); 356 for (int i = 0; i < arguments.size(); i++) 357 arg.getArgument(i).accept(this); 358 stateStack.pop(); 359 currentDepNode = tmpDepNode; 360 } 361 } 362 363 } 364 365 public void visit(LocatedExpression arg) throws XQueryException { 366 Integer state = (Integer ) stateStack.peek(); 367 ArrayList steps = arg.getSteps(); 368 if (state != IN_DEFINITION) 369 stateStack.push(IN_LOCATED); 370 ongoingLocatedExpression = arg; 371 XQueryExpression exp = ((Step) steps.get(0)).getExpression(); 372 exp.accept(this); 373 if (state != IN_DEFINITION) 374 stateStack.pop(); 375 ongoingLocatedExpression = null; 376 } 377 378 public void visit(Value arg) throws XQueryException { 379 Integer state = (Integer ) stateStack.peek(); 380 if (state == IN_DEFINITION 381 ) { 382 AlgEval depeval = depmanager.addEval(arg, (state == IN_DEFINITION) ? (Variable) varStack.peek() : null, true); 383 addLeaf(depeval); 384 ongoingLet = false; 385 } 386 } 387 388 private GetUsedVariablesVisitor guvvisitor = null; 389 390 public void visit(Variable arg) throws XQueryException { 391 Integer currentstate = (Integer ) stateStack.peek(); 393 if (currentstate == IN_FLWR && !this.depmanager.getPlan().getQAOnly()) { 394 } 421 449 if (currentstate == IN_DEFINITION) { 450 if (ongoingLocatedExpression == null) { 451 AlgVar depvar = depmanager.addVar(arg, (Variable) varStack.peek(), ongoingLet); 452 addLeaf(depvar); 454 ongoingLet = false; 455 } else { 456 AlgLocated deplocated = depmanager.addLocated(ongoingLocatedExpression, arg, (Variable) varStack.peek(), ongoingLet); 457 addLeaf(deplocated); 459 ongoingLet = false; 460 } 461 } else if (currentstate == IN_FLWR) { 462 if (arg.getBindingType() == Constants.LET_BINDINGTYPE) { 463 ongoingLet = true; 464 } 467 varStack.push(arg); 468 if (arg.getParentFLWRExpression() == null) { 469 FLWRExpression tmpflwr = (FLWRExpression) arg.getExpression(); 471 ArrayList variables = tmpflwr.getVariables(); 472 473 Variable lastremovedvar = null; 476 Variable lastparentvar = null; 477 for (int i = 0; i < variables.size(); i++) { 478 Variable vari = (Variable) variables.get(i); 479 Variable parentvari = (Variable) parentvariable.get(vari); 480 if (parentvari == null) 481 break; 482 lastremovedvar = vari; 483 if (parentvari != lastparentvar) { 484 lastparentvar = parentvari; 485 if (tmpflwr.getWhereClause() != null) { 486 XQueryExpression tmpwhere = ((FLWRExpression) depmanager.getMapNode(lastparentvar).getExpression()).getWhereClause(); 487 if (tmpflwr.getWhereClause() != null) 488 tmpflwr.setWhereClause(tmpflwr.getWhereClause().erasePart(tmpwhere)); 489 } 490 } 491 variables.remove(i); 496 i--; 497 } 498 499 517 currentDepFLWR = depmanager.addQDB(arg, ongoingLet); 518 522 if (lastremovedvar != null) 523 currentDepFLWR.setDependency((AlgFLWR) depmanager.getMapNode((Variable) parentvariable.get(lastremovedvar))); 524 if (currentDepFLWR.getVarsDependingOn() != null) 525 currentDepFLWR.updateVarDependencyList((ArrayList ) currentDepFLWR.getVarsDependingOn().clone()); 526 527 531 533 562 ongoingLet = false; 563 stateStack.push(IN_MAIN_DEFINITION); 564 } else { 566 parentvariable.put(arg, varStack.firstElement()); 567 stateStack.push(IN_DEFINITION); 568 } 569 arg.getExpression().accept(this); 570 stateStack.pop(); 571 varStack.pop(); 572 } 573 } 575 576 public void visit(UnOpMinusExpression arg) throws XQueryException { 577 578 Integer state = (Integer ) stateStack.peek(); 579 if (state == IN_DEFINITION 580 ) { 581 AlgFunction depfunction = depmanager.addFunction(arg, (state == IN_DEFINITION) ? (Variable) varStack.peek() : null, ongoingLet); 582 addLeaf(depfunction); 583 ongoingLet = false; 584 Algebra tmpDepNode = currentDepNode; 585 currentDepNode = depfunction; 586 stateStack.push(IN_FUNCTION); 587 arg.getExpression().accept(this); 588 stateStack.pop(); 589 currentDepNode = tmpDepNode; 590 } 591 } 592 593 public void visit(XQueryExpressionSequence arg) throws XQueryException { 594 595 Integer state = (Integer ) stateStack.peek(); 596 if (state == IN_DEFINITION 597 ) { 598 AlgSequence depsequence = depmanager.addSequence(arg, (state == IN_DEFINITION) ? (Variable) varStack.peek() : null, ongoingLet); 599 addLeaf(depsequence); 600 ongoingLet = false; 601 ArrayList expressions = arg.getSubExpressions(); 602 if (expressions != null) { 603 stateStack.push(IN_FUNCTION); 604 for (int i = 0; i < expressions.size(); i++) { 605 Algebra tmpDepNode = currentDepNode; 606 currentDepNode = depsequence; 607 ((XQueryExpression) expressions.get(i)).accept(this); 608 currentDepNode = tmpDepNode; 609 } 610 stateStack.pop(); 611 } 612 } 613 } 614 615 public void visit(Element arg) throws XQueryException { 617 618 Integer state = (Integer ) stateStack.peek(); 619 if (state == IN_DEFINITION) { 620 AlgEval depeval = null; 621 if (ongoingLocatedExpression == null) { 622 depeval = depmanager.addEval(arg, (Variable) varStack.peek(), ongoingLet); 623 } else { 624 depeval = depmanager.addEval(ongoingLocatedExpression, (Variable) varStack.peek(), ongoingLet); 625 } 626 addLeaf(depeval); 627 } 628 } 629 630 public void visit(AttributeValuePair arg) throws XQueryException { 631 632 Integer state = (Integer ) stateStack.peek(); 633 if (state == IN_DEFINITION) { 634 AlgEval depeval = depmanager.addEval(arg, (Variable) varStack.peek(), ongoingLet); 635 addLeaf(depeval); 636 } 637 } 638 639 public void visit(XQueryExpression arg) throws XQueryException { 640 } 642 643 } 644 | Popular Tags |