// 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 java.util.HashMap;
import java.util.Map;
import org.openrdf.query.algebra.Filter;
import org.openrdf.query.algebra.TupleExpr;
import org.openrdf.query.algebra.UnaryTupleOperator;
import org.openrdf.query.algebra.helpers.QueryModelVisitorBase;
import de.uni_koblenz.west.splendid.model.RemoteQuery;
/*
 * 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.estimation;




/**
 * @author Olaf Goerlitz
 */
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 {
// 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/estimation/AbstractCardinalityEstimator.java
import java.util.HashMap;
import java.util.Map;
import org.openrdf.query.algebra.Filter;
import org.openrdf.query.algebra.TupleExpr;
import org.openrdf.query.algebra.UnaryTupleOperator;
import org.openrdf.query.algebra.helpers.QueryModelVisitorBase;
import de.uni_koblenz.west.splendid.model.RemoteQuery;

/*
 * 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.estimation;




/**
 * @author Olaf Goerlitz
 */
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) {