Search in sources :

Example 11 with Var

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

Example 12 with Var

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);
    }
}
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) Var(org.apache.jena.sparql.core.Var)

Example 13 with Var

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);
}
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)

Example 14 with Var

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);
        }
    }
}
Also used : TextOutput(org.apache.jena.sparql.resultset.TextOutput) Op(org.apache.jena.sparql.algebra.Op) QueryEngineMain(org.apache.jena.sparql.engine.main.QueryEngineMain) QueryIterator(org.apache.jena.sparql.engine.QueryIterator) Var(org.apache.jena.sparql.core.Var) ArrayList(java.util.ArrayList) ResultSetStream(org.apache.jena.sparql.engine.ResultSetStream)

Example 15 with Var

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);
}
Also used : Var(org.apache.jena.sparql.core.Var) HashSet(java.util.HashSet)

Aggregations

Var (org.apache.jena.sparql.core.Var)264 Node (org.apache.jena.graph.Node)83 ArrayList (java.util.ArrayList)53 Test (org.junit.Test)47 Binding (org.apache.jena.sparql.engine.binding.Binding)33 VarExprList (org.apache.jena.sparql.core.VarExprList)30 Op (org.apache.jena.sparql.algebra.Op)29 Expr (org.apache.jena.sparql.expr.Expr)28 Triple (org.apache.jena.graph.Triple)17 HashMap (java.util.HashMap)15 ContractTest (org.xenei.junit.contract.ContractTest)13 BindingMap (org.apache.jena.sparql.engine.binding.BindingMap)12 Query (org.apache.jena.query.Query)11 ExprList (org.apache.jena.sparql.expr.ExprList)11 SortCondition (org.apache.jena.query.SortCondition)10 ExprVar (org.apache.jena.sparql.expr.ExprVar)10 HashSet (java.util.HashSet)9 Pair (org.apache.jena.atlas.lib.Pair)9 SqlColumn (org.apache.jena.sdb.core.sqlexpr.SqlColumn)9 QueryIterator (org.apache.jena.sparql.engine.QueryIterator)9