Search in sources :

Example 1 with ExprVar

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);
}
Also used : ExprVar(org.apache.jena.sparql.expr.ExprVar) ExprVar(org.apache.jena.sparql.expr.ExprVar) Var(org.apache.jena.sparql.core.Var) FrontsNode(org.apache.jena.graph.FrontsNode) Node(org.apache.jena.graph.Node) Test(org.junit.Test)

Example 2 with ExprVar

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);
}
Also used : ExprVar(org.apache.jena.sparql.expr.ExprVar)

Example 3 with ExprVar

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;
}
Also used : ExprVar(org.apache.jena.sparql.expr.ExprVar) Expr(org.apache.jena.sparql.expr.Expr) ARQException(org.apache.jena.sparql.ARQException) ExprVar(org.apache.jena.sparql.expr.ExprVar)

Example 4 with ExprVar

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();
}
Also used : ExprVar(org.apache.jena.sparql.expr.ExprVar) Expr(org.apache.jena.sparql.expr.Expr) ExprVar(org.apache.jena.sparql.expr.ExprVar)

Example 5 with ExprVar

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);
}
Also used : ExprVar(org.apache.jena.sparql.expr.ExprVar) NodeValue(org.apache.jena.sparql.expr.NodeValue) Expr(org.apache.jena.sparql.expr.Expr) ExprVar(org.apache.jena.sparql.expr.ExprVar) Var(org.apache.jena.sparql.core.Var) InternalErrorException(org.apache.jena.atlas.lib.InternalErrorException)

Aggregations

ExprVar (org.apache.jena.sparql.expr.ExprVar)50 Expr (org.apache.jena.sparql.expr.Expr)33 Test (org.junit.Test)17 ContractTest (org.xenei.junit.contract.ContractTest)15 SortCondition (org.apache.jena.query.SortCondition)12 Var (org.apache.jena.sparql.core.Var)11 Node (org.apache.jena.graph.Node)9 Query (org.apache.jena.query.Query)6 Op (org.apache.jena.sparql.algebra.Op)6 Triple (org.apache.jena.graph.Triple)5 E_LessThan (org.apache.jena.sparql.expr.E_LessThan)5 E_Random (org.apache.jena.sparql.expr.E_Random)5 NodeValueInteger (org.apache.jena.sparql.expr.nodevalue.NodeValueInteger)5 E_Multiply (org.apache.jena.sparql.expr.E_Multiply)4 ElementFilter (org.apache.jena.sparql.syntax.ElementFilter)4 ArrayList (java.util.ArrayList)3 OpBGP (org.apache.jena.sparql.algebra.op.OpBGP)3 BasicPattern (org.apache.jena.sparql.core.BasicPattern)3 E_Regex (org.apache.jena.sparql.expr.E_Regex)3 WhereValidator (org.apache.jena.arq.querybuilder.WhereValidator)2