// Path: src/de/uni_koblenz/west/splendid/estimation/AbstractCardinalityEstimator.java
// public abstract class AbstractCardinalityEstimator extends QueryModelVisitorBase<RuntimeException> implements ModelEvaluator {
// 
// 	protected Map<TupleExpr, Double> cardIndex = new HashMap<TupleExpr, Double>();
// 	
// 	@Override
// 	public Double process(TupleExpr expr) {
// 		synchronized (this) {
// 			expr.visit(this);
// 			return cardIndex.get(expr);
// 		}
// 	}
// 	
// 	@Override
// 	public void meet(Filter filter) {
// 		
// 		// check cardinality index first
// 		if (getIndexCard(filter) != null)
// 			return;
// 		
// 		// TODO: include condition in estimation
// 		// for now use same card as sub expression
// 		filter.getArg().visit(this);
// 		
// 		// add same cardinality as filter argument
// 		setIndexCard(filter, getIndexCard(filter.getArg()));
// 	}
// 	
// 	@Override
// 	protected void meetUnaryTupleOperator(UnaryTupleOperator node)
// 			throws RuntimeException {
// 		if (node instanceof RemoteQuery) {
// 			meet((RemoteQuery) node);
// 		} else {
// 			super.meetUnaryTupleOperator(node);
// 		}
// 	}
// 	
// 	protected void meet(RemoteQuery node) {
// 		if (getIndexCard(node) != null)
// 			return;
// 		node.getArg().visit(this);
// 		setIndexCard(node, getIndexCard(node.getArg()));
// 	}
// 	
// 	protected Double getIndexCard(TupleExpr expr) {
// 		return this.cardIndex.get(expr);
// 	}
// 	
// 	protected void setIndexCard(TupleExpr expr, Double value) {
// 		this.cardIndex.put(expr, value);
// 	}
// 
// }
// 
// Path: src/de/uni_koblenz/west/splendid/estimation/AbstractCostEstimator.java
// public abstract class AbstractCostEstimator extends QueryModelVisitorBase<RuntimeException> implements ModelEvaluator {
// 	
// 	protected double cost;
// 	
// 	protected AbstractCardinalityEstimator cardEst;
// 	
// 	public AbstractCardinalityEstimator getCardinalityEstimator() {
// 		return cardEst;
// 	}
// 
// 	public void setCardinalityEstimator(AbstractCardinalityEstimator cardEst) {
// 		this.cardEst = cardEst;
// 	}
// 
// 	public Double getCost(TupleExpr expr) {
// 		cost = 0;
// 		expr.visit(this);
// 		return cost;
// 	}
// 	
// 	@Override
// 	public Double process(TupleExpr expr) {
// 		synchronized (this) {
// 			return getCost(expr);
// 		}
// 	}
// 	
// }
// 
// Path: src/de/uni_koblenz/west/splendid/estimation/ModelEvaluator.java
// public interface ModelEvaluator {
// 	
// 	public Double process(TupleExpr expr);
// 	
// 	public String getName();
// 
// }
// 
// Path: src/de/uni_koblenz/west/splendid/model/RemoteQuery.java
// public class RemoteQuery extends UnaryTupleOperator {
// 	
// 	public RemoteQuery(TupleExpr expr) {
// 		super(expr);
// 	}
// 
// 	@Override
// 	public <X extends Exception> void visit(QueryModelVisitor<X> visitor) throws X {
// 		visitor.meetOther(this);
// 	}
// 	
// 	public Set<Graph> getSources() {
// 		StatementPattern p = StatementPatternCollector.process(this).get(0);
// 		if (p instanceof MappedStatementPattern)
// 			return ((MappedStatementPattern) p).getSources();
// 		else
// 			return null;
// 	}
// 	
// }


import org.openrdf.query.algebra.helpers.QueryModelVisitorBase;
import org.openrdf.query.algebra.helpers.StatementPatternCollector;
import de.uni_koblenz.west.splendid.estimation.AbstractCardinalityEstimator;
import de.uni_koblenz.west.splendid.estimation.AbstractCostEstimator;
import de.uni_koblenz.west.splendid.estimation.ModelEvaluator;
import de.uni_koblenz.west.splendid.model.RemoteQuery;
import org.openrdf.model.URI;
import org.openrdf.model.Value;
import org.openrdf.query.algebra.BinaryTupleOperator;
import org.openrdf.query.algebra.Compare;
import org.openrdf.query.algebra.Filter;
import org.openrdf.query.algebra.QueryModelNode;
import org.openrdf.query.algebra.StatementPattern;
import org.openrdf.query.algebra.UnaryTupleOperator;
import org.openrdf.query.algebra.ValueConstant;
import org.openrdf.query.algebra.Var;
/*
 * This file is part of RDF Federator.
 * Copyright 2011 Olaf Goerlitz
 * 
 * RDF Federator is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * RDF Federator is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with RDF Federator.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * RDF Federator uses libraries from the OpenRDF Sesame Project licensed 
 * under the Aduna BSD-style license. 
 */
package de.uni_koblenz.west.splendid.helpers;



/**
 * Prints the tree structure of a query plan.
 * Can annotate individual nodes with the result of a model evaluator.
 * Highlights the sub tree marked as a remote query.
 * 
 * @author Olaf Goerlitz
 */
public class AnnotatingTreePrinter extends QueryModelVisitorBase<RuntimeException> {
	
	private static final String LINE_SEPARATOR = System.getProperty("line.separator");
	
	private static final String indentString = "  ";

	private StringBuilder buffer;

	private int indentLevel = 0;
	
// Path: src/de/uni_koblenz/west/splendid/estimation/AbstractCardinalityEstimator.java
// public abstract class AbstractCardinalityEstimator extends QueryModelVisitorBase<RuntimeException> implements ModelEvaluator {
// 
// 	protected Map<TupleExpr, Double> cardIndex = new HashMap<TupleExpr, Double>();
// 	
// 	@Override
// 	public Double process(TupleExpr expr) {
// 		synchronized (this) {
// 			expr.visit(this);
// 			return cardIndex.get(expr);
// 		}
// 	}
// 	
// 	@Override
// 	public void meet(Filter filter) {
// 		
// 		// check cardinality index first
// 		if (getIndexCard(filter) != null)
// 			return;
// 		
// 		// TODO: include condition in estimation
// 		// for now use same card as sub expression
// 		filter.getArg().visit(this);
// 		
// 		// add same cardinality as filter argument
// 		setIndexCard(filter, getIndexCard(filter.getArg()));
// 	}
// 	
// 	@Override
// 	protected void meetUnaryTupleOperator(UnaryTupleOperator node)
// 			throws RuntimeException {
// 		if (node instanceof RemoteQuery) {
// 			meet((RemoteQuery) node);
// 		} else {
// 			super.meetUnaryTupleOperator(node);
// 		}
// 	}
// 	
// 	protected void meet(RemoteQuery node) {
// 		if (getIndexCard(node) != null)
// 			return;
// 		node.getArg().visit(this);
// 		setIndexCard(node, getIndexCard(node.getArg()));
// 	}
// 	
// 	protected Double getIndexCard(TupleExpr expr) {
// 		return this.cardIndex.get(expr);
// 	}
// 	
// 	protected void setIndexCard(TupleExpr expr, Double value) {
// 		this.cardIndex.put(expr, value);
// 	}
// 
// }
// 
// Path: src/de/uni_koblenz/west/splendid/estimation/AbstractCostEstimator.java
// public abstract class AbstractCostEstimator extends QueryModelVisitorBase<RuntimeException> implements ModelEvaluator {
// 	
// 	protected double cost;
// 	
// 	protected AbstractCardinalityEstimator cardEst;
// 	
// 	public AbstractCardinalityEstimator getCardinalityEstimator() {
// 		return cardEst;
// 	}
// 
// 	public void setCardinalityEstimator(AbstractCardinalityEstimator cardEst) {
// 		this.cardEst = cardEst;
// 	}
// 
// 	public Double getCost(TupleExpr expr) {
// 		cost = 0;
// 		expr.visit(this);
// 		return cost;
// 	}
// 	
// 	@Override
// 	public Double process(TupleExpr expr) {
// 		synchronized (this) {
// 			return getCost(expr);
// 		}
// 	}
// 	
// }
// 
// Path: src/de/uni_koblenz/west/splendid/estimation/ModelEvaluator.java
// public interface ModelEvaluator {
// 	
// 	public Double process(TupleExpr expr);
// 	
// 	public String getName();
// 
// }
// 
// Path: src/de/uni_koblenz/west/splendid/model/RemoteQuery.java
// public class RemoteQuery extends UnaryTupleOperator {
// 	
// 	public RemoteQuery(TupleExpr expr) {
// 		super(expr);
// 	}
// 
// 	@Override
// 	public <X extends Exception> void visit(QueryModelVisitor<X> visitor) throws X {
// 		visitor.meetOther(this);
// 	}
// 	
// 	public Set<Graph> getSources() {
// 		StatementPattern p = StatementPatternCollector.process(this).get(0);
// 		if (p instanceof MappedStatementPattern)
// 			return ((MappedStatementPattern) p).getSources();
// 		else
// 			return null;
// 	}
// 	
// }


// Path: src/de/uni_koblenz/west/splendid/helpers/AnnotatingTreePrinter.java
import org.openrdf.query.algebra.helpers.QueryModelVisitorBase;
import org.openrdf.query.algebra.helpers.StatementPatternCollector;
import de.uni_koblenz.west.splendid.estimation.AbstractCardinalityEstimator;
import de.uni_koblenz.west.splendid.estimation.AbstractCostEstimator;
import de.uni_koblenz.west.splendid.estimation.ModelEvaluator;
import de.uni_koblenz.west.splendid.model.RemoteQuery;
import org.openrdf.model.URI;
import org.openrdf.model.Value;
import org.openrdf.query.algebra.BinaryTupleOperator;
import org.openrdf.query.algebra.Compare;
import org.openrdf.query.algebra.Filter;
import org.openrdf.query.algebra.QueryModelNode;
import org.openrdf.query.algebra.StatementPattern;
import org.openrdf.query.algebra.UnaryTupleOperator;
import org.openrdf.query.algebra.ValueConstant;
import org.openrdf.query.algebra.Var;

/*
 * This file is part of RDF Federator.
 * Copyright 2011 Olaf Goerlitz
 * 
 * RDF Federator is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * RDF Federator is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with RDF Federator.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * RDF Federator uses libraries from the OpenRDF Sesame Project licensed 
 * under the Aduna BSD-style license. 
 */
package de.uni_koblenz.west.splendid.helpers;



/**
 * Prints the tree structure of a query plan.
 * Can annotate individual nodes with the result of a model evaluator.
 * Highlights the sub tree marked as a remote query.
 * 
 * @author Olaf Goerlitz
 */
public class AnnotatingTreePrinter extends QueryModelVisitorBase<RuntimeException> {
	
	private static final String LINE_SEPARATOR = System.getProperty("line.separator");
	
	private static final String indentString = "  ";

	private StringBuilder buffer;

	private int indentLevel = 0;
	
	private ModelEvaluator eval;