use of org.apache.jena.sparql.expr.ExprVar in project jena by apache.
the class AbstractQueryBuilderTest method testMakeVar.
@Test
public void testMakeVar() {
Var v = AbstractQueryBuilder.makeVar(null);
assertEquals(Var.ANON, v);
v = AbstractQueryBuilder.makeVar("a");
assertEquals(Var.alloc("a"), v);
v = AbstractQueryBuilder.makeVar("?a");
assertEquals(Var.alloc("a"), v);
Node n = NodeFactory.createVariable("foo");
v = AbstractQueryBuilder.makeVar(n);
assertEquals(Var.alloc("foo"), v);
NodeFront nf = new NodeFront(n);
v = AbstractQueryBuilder.makeVar(nf);
assertEquals(Var.alloc("foo"), v);
v = AbstractQueryBuilder.makeVar(Node_RuleVariable.WILD);
assertNull(v);
ExprVar ev = new ExprVar("bar");
v = AbstractQueryBuilder.makeVar(ev);
assertEquals(Var.alloc("bar"), v);
ev = new ExprVar(n);
v = AbstractQueryBuilder.makeVar(ev);
assertEquals(Var.alloc("foo"), v);
ev = new ExprVar(Var.ANON);
v = AbstractQueryBuilder.makeVar(ev);
assertEquals(Var.ANON, v);
}
use of org.apache.jena.sparql.expr.ExprVar in project jena by apache.
the class Query method addOrderBy.
public void addOrderBy(String varName, int direction) {
varName = Var.canonical(varName);
SortCondition sc = new SortCondition(new ExprVar(varName), direction);
addOrderBy(sc);
}
use of org.apache.jena.sparql.expr.ExprVar in project jena by apache.
the class QueryTransformOps method transformVarExprList.
private static VarExprList transformVarExprList(VarExprList varExprList, ExprTransform exprTransform) {
VarExprList varExprList2 = new VarExprList();
boolean changed = false;
for (Var v : varExprList.getVars()) {
Expr e = varExprList.getExpr(v);
// Transform variable.
ExprVar ev = new ExprVar(v);
Expr ev2 = exprTransform.transform(ev);
if (ev != ev2)
changed = true;
if (e == null) {
// Variable only.
if (ev2.isConstant()) {
// Skip or old var, assign so it become (?old AS substitute)
// Skip .
// Require transform to add back substitutions "for the record";
varExprList2.remove(v);
varExprList2.add(v, ev2);
} else if (ev2.isVariable()) {
varExprList2.add(ev2.asVar());
} else {
throw new ARQException("Can't substitute " + v + " because it's not a simple value: " + ev2);
}
continue;
}
// There was an expression.
Expr e2 = ExprTransformer.transform(exprTransform, e);
if (e2 != e)
changed = true;
if (!ev2.isVariable())
throw new ARQException("Can't substitute (" + v + ", " + e + ") as (" + ev2 + ", " + e2 + ")");
varExprList2.add(ev.asVar(), e2);
}
return varExprList2;
}
use of org.apache.jena.sparql.expr.ExprVar in project jena by apache.
the class QueryTransformOps method transform.
// Transform a variable node (for low-usage cases).
// Returns node object for "no transform"
private static Node transform(Node node, ExprTransform exprTransform) {
if (!Var.isVar(node))
return node;
Var v = Var.alloc(node);
ExprVar ev = new ExprVar(v);
Expr e2 = exprTransform.transform(ev);
if (e2 == null || e2 == ev)
return node;
if (!e2.isConstant())
return node;
return e2.getConstant().getNode();
}
use of org.apache.jena.sparql.expr.ExprVar 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);
}
Aggregations