use of org.apache.jena.sparql.core.Var in project jena by apache.
the class ExprTransformExpand method transform.
@Override
public Expr transform(ExprFunctionN func, ExprList args) {
ExprFunction f = func.getFunction();
if (this.shouldExpand(f)) {
UserDefinedFunctionDefinition def = this.definitions.get(f.getFunction().getFunctionIRI());
UserDefinedFunction uFunc = (UserDefinedFunction) def.newFunctionInstance();
//Need to watch out for the case where the arguments supplied to the invoked
//function are in a different order to the arguments supplied to the defined
//function
//Thus we will build the list of arguments used to expand the inner function
//manually
List<Var> defArgs = def.getArgList();
ExprList subArgs = new ExprList();
for (int i = 0; i < args.size(); i++) {
Expr arg = args.get(i);
String var = arg.getVarName();
if (var == null) {
//Non-variable args may be passed as-is
subArgs.add(arg);
} else {
//We then use the arg as-is to substitute
if (i > defArgs.size())
throw new ExprBuildException("Unable to expand function dependency, the function <" + def.getUri() + "> is called but uses an argument ?" + var + " which is not an argument to the outer function");
subArgs.add(arg);
}
}
//Expand the function
uFunc.build(def.getUri(), subArgs);
return uFunc.getActualExpr();
} else {
return super.transform(func, args);
}
}
use of org.apache.jena.sparql.core.Var in project jena by apache.
the class SyntaxVarScope method fmtExprList.
private static String fmtExprList(VarExprList exprList) {
StringBuilder sb = new StringBuilder();
boolean first = true;
for (Var v : exprList.getVars()) {
Expr e = exprList.getExpr(v);
if (!first) {
sb.append(" ");
}
first = false;
sb.append("(").append(e).append(" AS ").append(v).append(")");
}
return sb.toString();
}
use of org.apache.jena.sparql.core.Var in project jena by apache.
the class QueryTransformOps method transformVarExprList.
// ** Mutates the VarExprList
private static void transformVarExprList(VarExprList varExprList, ExprTransform exprTransform) {
Map<Var, Expr> map = varExprList.getExprs();
for (Var v : varExprList.getVars()) {
Expr e = varExprList.getExpr(v);
ExprVar ev = new ExprVar(v);
Expr ev2 = exprTransform.transform(ev);
if (ev != ev2) {
if (e != null)
throw new ARQException("Can't substitute " + v + " because it's used as an AS variable");
if (ev2.isConstant() || ev2.isVariable()) {
// Convert to (substitute value AS ?var)
map.put(v, ev2);
continue;
} else
throw new ARQException("Can't substitute " + v + " because it's not a simple value: " + ev2);
}
if (e == null)
continue;
// Didn't change the variable.
Expr e2 = ExprTransformer.transform(exprTransform, e);
if (e != e2)
// replace
map.put(v, e2);
}
}
use of org.apache.jena.sparql.core.Var in project jena by apache.
the class TransformElementLib method apply.
public static Node apply(Node n, ExprTransform exprTransform) {
if (exprTransform == null)
return n;
Expr e = null;
if (Var.isVar(n)) {
Var v = Var.alloc(n);
ExprVar expr = new ExprVar(v);
e = exprTransform.transform(expr);
} else {
NodeValue nv = NodeValue.makeNode(n);
e = exprTransform.transform(nv);
}
if (e instanceof ExprVar)
return ((ExprVar) e).asVar();
if (e instanceof NodeValue)
return ((NodeValue) e).asNode();
throw new InternalErrorException("Managed to turn a node " + n + " into " + e);
}
use of org.apache.jena.sparql.core.Var in project jena by apache.
the class TestSemanticEquivalence method testAsAlgebra.
/**
* Tests whether an algebra expression gives the same results when run both
* with and without a given optimizer
*
* @param algStr
* Algebra
* @param ds
* Dataset
* @param opt
* Optimizer
* @param expected
* Expected number of results
*/
public static void testAsAlgebra(String algStr, Dataset ds, Symbol opt, int expected) {
Op op = SSE.parseOp(algStr);
List<String> vars = new ArrayList<>();
for (Var v : OpVars.visibleVars(op)) {
vars.add(v.getName());
}
// Track current state
boolean isEnabled = ARQ.isTrue(opt);
boolean isDisabled = ARQ.isFalse(opt);
try {
// Run first without optimization
ARQ.set(opt, false);
QueryEngineMain engine = new QueryEngineMain(op, ds.asDatasetGraph(), BindingFactory.binding(), ARQ.getContext());
QueryIterator iter = engine.eval(op, ds.asDatasetGraph(), BindingFactory.binding(), ARQ.getContext());
ResultSetRewindable rs = ResultSetFactory.makeRewindable(new ResultSetStream(vars, ModelFactory.createDefaultModel(), iter));
if (expected != rs.size()) {
System.err.println("Non-optimized results not as expected");
TextOutput output = new TextOutput((SerializationContext) null);
output.format(System.out, rs);
rs.reset();
}
Assert.assertEquals(expected, rs.size());
iter.close();
// Run with optimization
ARQ.set(opt, true);
engine = new QueryEngineMain(op, ds.asDatasetGraph(), BindingFactory.binding(), ARQ.getContext());
QueryIterator iterOpt = engine.eval(op, ds.asDatasetGraph(), BindingFactory.binding(), ARQ.getContext());
ResultSetRewindable rsOpt = ResultSetFactory.makeRewindable(new ResultSetStream(vars, ModelFactory.createDefaultModel(), iterOpt));
if (expected != rsOpt.size()) {
System.err.println("Optimized results not as expected");
TextOutput output = new TextOutput((SerializationContext) null);
output.format(System.out, rsOpt);
rsOpt.reset();
}
Assert.assertEquals(expected, rsOpt.size());
iterOpt.close();
Assert.assertTrue(ResultSetCompare.isomorphic(rs, rsOpt));
} finally {
// Restore previous state
if (isEnabled) {
ARQ.set(opt, true);
} else if (isDisabled) {
ARQ.set(opt, false);
} else {
ARQ.unset(opt);
}
}
}
Aggregations