001: /*
002: * Copyright Aduna (http://www.aduna-software.com/) (c) 1997-2006.
003: *
004: * Licensed under the Aduna BSD-style license.
005: */
006: package org.openrdf.query.algebra.helpers;
007:
008: import org.openrdf.query.algebra.BinaryTupleOperator;
009: import org.openrdf.query.algebra.BinaryValueOperator;
010: import org.openrdf.query.algebra.QueryModelNode;
011: import org.openrdf.query.algebra.Filter;
012: import org.openrdf.query.algebra.TupleExpr;
013: import org.openrdf.query.algebra.UnaryTupleOperator;
014: import org.openrdf.query.algebra.UnaryValueOperator;
015: import org.openrdf.query.algebra.ValueExpr;
016:
017: public class QueryModelNodeReplacer extends
018: QueryModelVisitorBase<RuntimeException> {
019:
020: private QueryModelNode former;
021:
022: private QueryModelNode replacement;
023:
024: public void replaceChildNode(QueryModelNode parent,
025: QueryModelNode former, QueryModelNode replacement) {
026: this .former = former;
027: this .replacement = replacement;
028: parent.visit(this );
029: }
030:
031: public void replaceNode(QueryModelNode former,
032: QueryModelNode replacement) {
033: replaceChildNode(former.getParentNode(), former, replacement);
034: }
035:
036: public void removeChildNode(QueryModelNode parent,
037: QueryModelNode former) {
038: replaceChildNode(parent, former, null);
039: }
040:
041: public void removeNode(QueryModelNode former) {
042: replaceChildNode(former.getParentNode(), former, null);
043: }
044:
045: @Override
046: public void meet(Filter node) {
047: if (replacement == null) {
048: replaceNode(node, node.getArg());
049: } else if (replacement instanceof ValueExpr) {
050: assert former == node.getCondition();
051: node.setCondition((ValueExpr) replacement);
052: } else {
053: assert former == node.getArg();
054: node.setArg((TupleExpr) replacement);
055: }
056: }
057:
058: @Override
059: protected void meetBinaryTupleOperator(BinaryTupleOperator node) {
060: if (node.getLeftArg() == former) {
061: if (replacement == null) {
062: replaceNode(node, node.getRightArg());
063: } else {
064: node.setLeftArg((TupleExpr) replacement);
065: }
066: } else {
067: assert former == node.getRightArg();
068: if (replacement == null) {
069: replaceNode(node, node.getLeftArg());
070: } else {
071: node.setRightArg((TupleExpr) replacement);
072: }
073: }
074: }
075:
076: @Override
077: protected void meetBinaryValueOperator(BinaryValueOperator node) {
078: if (former == node.getLeftArg()) {
079: if (replacement == null) {
080: replaceNode(node, node.getRightArg());
081: } else {
082: node.setLeftArg((ValueExpr) replacement);
083: }
084: } else {
085: assert former == node.getRightArg();
086: if (replacement == null) {
087: replaceNode(node, node.getLeftArg());
088: } else {
089: node.setRightArg((ValueExpr) replacement);
090: }
091: }
092: }
093:
094: @Override
095: protected void meetUnaryTupleOperator(UnaryTupleOperator node) {
096: assert former == node.getArg();
097: if (replacement == null) {
098: removeNode(node);
099: } else {
100: node.setArg((TupleExpr) replacement);
101: }
102: }
103:
104: @Override
105: protected void meetUnaryValueOperator(UnaryValueOperator node) {
106: assert former == node.getArg();
107: if (replacement == null) {
108: removeNode(node);
109: } else {
110: node.setArg((ValueExpr) replacement);
111: }
112: }
113:
114: @Override
115: protected void meetNode(QueryModelNode node) {
116: throw new IllegalArgumentException("Unhandled Node: " + node);
117: }
118: }
|