// Path: src/main/java/org/deri/tarql/functions/ExpandPrefixFunction.java
// public class ExpandPrefixFunction implements Function {
// 
// 	public static String IRI = tarql.NS + "expandPrefix";
// 	public static String NAME = "tarql:expandPrefix";
// 	
// 	public static final Symbol PREFIX_MAPPING = Symbol.create("prefixMapping");
// 	
// 	static {
// 		TarqlQuery.registerFunctions();
// 	}
// 	
// 	public ExpandPrefixFunction() {
// 		super();
// 	}
// 
// 	public NodeValue exec(NodeValue prefix, Context context) {
// 		if (prefix == null) {
// 			return null;
// 		}
// 		if (!prefix.isString()) {
// 			throw new ExprEvalException(NAME + ": not a string: " + prefix);
// 		}
// 		PrefixMapping prefixes = context.get(PREFIX_MAPPING);
// 		if (prefixes == null) {
// 			throw new ExprEvalException(NAME + ": no prefix mapping registered");
// 		}
// 		String iri = prefixes.getNsPrefixURI(prefix.asString());
// 		if (iri == null) {
// 			throw new ExprEvalException(NAME + ": prefix not defined: " + prefix);
// 		}
// 		return NodeValue.makeString(iri);
// 	}
// 
// 	@Override
// 	public NodeValue exec(Binding binding, ExprList args, String uri, FunctionEnv env) {
// 		if (args == null) {
// 			throw new ARQInternalErrorException("ExpandPrefixFunction: Null args list");
// 		}
// 		if (args.size() != 1) {
// 			throw new ExprEvalException("ExpandPrefixFunction: Wrong number of arguments: Wanted 1, got " + args.size());
// 		}
//         return exec(args.get(0).eval(binding, env), env.getContext());
// 	}
// 
// 	@Override
// 	public void build(String uri, ExprList args) {
// 		if (args.size() != 1) {
// 			throw new QueryBuildException("Function '" + Lib.className(this) + "' takes one argument");
// 		}
// 	}
// }


import java.io.IOException;
import java.util.Iterator;
import org.apache.jena.graph.Triple;
import org.apache.jena.query.Query;
import org.apache.jena.query.QueryExecution;
import org.apache.jena.query.QueryExecutionFactory;
import org.apache.jena.query.ResultSet;
import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.ModelFactory;
import org.apache.jena.shared.impl.PrefixMappingImpl;
import org.apache.jena.sparql.algebra.Table;
import org.apache.jena.sparql.core.Var;
import org.apache.jena.sparql.syntax.Element;
import org.apache.jena.sparql.syntax.ElementData;
import org.apache.jena.sparql.syntax.ElementGroup;
import org.apache.jena.util.iterator.ExtendedIterator;
import org.apache.jena.util.iterator.NullIterator;
import org.deri.tarql.functions.ExpandPrefixFunction;
	/**
	 * Modifies a query so that it operates onto a table. This is achieved
	 * by appending the table as a VALUES block to the end of the main
	 * query pattern.
	 * 
	 * @param query Original query; will be modified in place
	 * @param table Data table to be added into the query
	 */
	private void modifyQuery(Query query, final Table table) {
		ElementData tableElement = new ElementData() {
			@Override
			public Table getTable() {
				return table;
			}
		};
		for (Var var: table.getVars()) {
			// Skip ?ROWNUM for "SELECT *" queries -- see further below
			if (query.isSelectType() && query.isQueryResultStar() 
					&& var.equals(TarqlQuery.ROWNUM)) continue;
			tableElement.add(var);
		}
		ElementGroup groupElement = new ElementGroup();
		groupElement.addElement(tableElement);
		if (query.getQueryPattern() instanceof ElementGroup) {
			for (Element element: ((ElementGroup) query.getQueryPattern()).getElements()) {
				groupElement.addElement(element);
			}
		} else {
			groupElement.addElement(query.getQueryPattern());
		}
// Path: src/main/java/org/deri/tarql/functions/ExpandPrefixFunction.java
// public class ExpandPrefixFunction implements Function {
// 
// 	public static String IRI = tarql.NS + "expandPrefix";
// 	public static String NAME = "tarql:expandPrefix";
// 	
// 	public static final Symbol PREFIX_MAPPING = Symbol.create("prefixMapping");
// 	
// 	static {
// 		TarqlQuery.registerFunctions();
// 	}
// 	
// 	public ExpandPrefixFunction() {
// 		super();
// 	}
// 
// 	public NodeValue exec(NodeValue prefix, Context context) {
// 		if (prefix == null) {
// 			return null;
// 		}
// 		if (!prefix.isString()) {
// 			throw new ExprEvalException(NAME + ": not a string: " + prefix);
// 		}
// 		PrefixMapping prefixes = context.get(PREFIX_MAPPING);
// 		if (prefixes == null) {
// 			throw new ExprEvalException(NAME + ": no prefix mapping registered");
// 		}
// 		String iri = prefixes.getNsPrefixURI(prefix.asString());
// 		if (iri == null) {
// 			throw new ExprEvalException(NAME + ": prefix not defined: " + prefix);
// 		}
// 		return NodeValue.makeString(iri);
// 	}
// 
// 	@Override
// 	public NodeValue exec(Binding binding, ExprList args, String uri, FunctionEnv env) {
// 		if (args == null) {
// 			throw new ARQInternalErrorException("ExpandPrefixFunction: Null args list");
// 		}
// 		if (args.size() != 1) {
// 			throw new ExprEvalException("ExpandPrefixFunction: Wrong number of arguments: Wanted 1, got " + args.size());
// 		}
//         return exec(args.get(0).eval(binding, env), env.getContext());
// 	}
// 
// 	@Override
// 	public void build(String uri, ExprList args) {
// 		if (args.size() != 1) {
// 			throw new QueryBuildException("Function '" + Lib.className(this) + "' takes one argument");
// 		}
// 	}
// }


// Path: src/main/java/org/deri/tarql/TarqlQueryExecution.java
import java.io.IOException;
import java.util.Iterator;
import org.apache.jena.graph.Triple;
import org.apache.jena.query.Query;
import org.apache.jena.query.QueryExecution;
import org.apache.jena.query.QueryExecutionFactory;
import org.apache.jena.query.ResultSet;
import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.ModelFactory;
import org.apache.jena.shared.impl.PrefixMappingImpl;
import org.apache.jena.sparql.algebra.Table;
import org.apache.jena.sparql.core.Var;
import org.apache.jena.sparql.syntax.Element;
import org.apache.jena.sparql.syntax.ElementData;
import org.apache.jena.sparql.syntax.ElementGroup;
import org.apache.jena.util.iterator.ExtendedIterator;
import org.apache.jena.util.iterator.NullIterator;
import org.deri.tarql.functions.ExpandPrefixFunction;

	/**
	 * Modifies a query so that it operates onto a table. This is achieved
	 * by appending the table as a VALUES block to the end of the main
	 * query pattern.
	 * 
	 * @param query Original query; will be modified in place
	 * @param table Data table to be added into the query
	 */
	private void modifyQuery(Query query, final Table table) {
		ElementData tableElement = new ElementData() {
			@Override
			public Table getTable() {
				return table;
			}
		};
		for (Var var: table.getVars()) {
			// Skip ?ROWNUM for "SELECT *" queries -- see further below
			if (query.isSelectType() && query.isQueryResultStar() 
					&& var.equals(TarqlQuery.ROWNUM)) continue;
			tableElement.add(var);
		}
		ElementGroup groupElement = new ElementGroup();
		groupElement.addElement(tableElement);
		if (query.getQueryPattern() instanceof ElementGroup) {
			for (Element element: ((ElementGroup) query.getQueryPattern()).getElements()) {
				groupElement.addElement(element);
			}
		} else {
			groupElement.addElement(query.getQueryPattern());
		}
		query.setQueryPattern(groupElement);