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);
}
}
}
use of org.apache.jena.sparql.core.Var in project jena by apache.
the class TestVarFinder method check.
private static void check(String[] varsExpected, Set<Var> varsFound) {
Var[] vars = new Var[varsExpected.length];
for (int i = 0; i < varsExpected.length; i++) {
Var v = Var.alloc(varsExpected[i]);
vars[i] = v;
}
List<Var> varList = Arrays.asList(vars);
HashSet<Var> varSet = new HashSet<>();
varSet.addAll(varList);
assertEquals(varSet, varsFound);
}
Aggregations