Search in sources :

Example 41 with QueryIterator

use of org.apache.jena.sparql.engine.QueryIterator 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 42 with QueryIterator

use of org.apache.jena.sparql.engine.QueryIterator in project jena by apache.

the class AlgebraEx method main.

public static void main(String[] args) {
    String s = "SELECT DISTINCT ?s { ?s ?p ?o }";
    // Parse
    Query query = QueryFactory.create(s);
    System.out.println(query);
    // Generate algebra
    Op op = Algebra.compile(query);
    op = Algebra.optimize(op);
    System.out.println(op);
    // Execute it.
    QueryIterator qIter = Algebra.exec(op, ExQuerySelect1.createModel());
    // Results
    for (; qIter.hasNext(); ) {
        Binding b = qIter.nextBinding();
        System.out.println(b);
    }
    qIter.close();
}
Also used : Binding(org.apache.jena.sparql.engine.binding.Binding) Op(org.apache.jena.sparql.algebra.Op) Query(org.apache.jena.query.Query) QueryIterator(org.apache.jena.sparql.engine.QueryIterator)

Example 43 with QueryIterator

use of org.apache.jena.sparql.engine.QueryIterator in project jena by apache.

the class Eval method executeBGP.

private static QueryIterator executeBGP(BasicPattern pattern, QueryIterator input, ExecutionContext execCxt) {
    if (pattern.isEmpty())
        return input;
    boolean hideBNodeVars = execCxt.getContext().isTrue(ARQ.hideNonDistiguishedVariables);
    StageGenerator gen = StageBuilder.executeInline;
    QueryIterator qIter = gen.execute(pattern, input, execCxt);
    // Project out only named variables.
    if (hideBNodeVars)
        qIter = new QueryIterDistinguishedVars(qIter, execCxt);
    return qIter;
}
Also used : QueryIterDistinguishedVars(org.apache.jena.sparql.engine.iterator.QueryIterDistinguishedVars) StageGenerator(org.apache.jena.sparql.engine.main.StageGenerator) QueryIterator(org.apache.jena.sparql.engine.QueryIterator)

Example 44 with QueryIterator

use of org.apache.jena.sparql.engine.QueryIterator in project jena by apache.

the class EvaluatorSimple method project.

@Override
public Table project(Table table, List<Var> projectVars) {
    QueryIterator qIter = table.iterator(getExecContext());
    qIter = new QueryIterProject(qIter, projectVars, getExecContext());
    return new TableN(qIter);
}
Also used : QueryIterator(org.apache.jena.sparql.engine.QueryIterator) TableN(org.apache.jena.sparql.algebra.table.TableN)

Example 45 with QueryIterator

use of org.apache.jena.sparql.engine.QueryIterator in project jena by apache.

the class EvaluatorSimple method assign.

@Override
public Table assign(Table table, VarExprList exprs) {
    QueryIterator qIter = table.iterator(getExecContext());
    qIter = new QueryIterAssign(qIter, exprs, getExecContext(), false);
    return new TableN(qIter);
}
Also used : QueryIterator(org.apache.jena.sparql.engine.QueryIterator) TableN(org.apache.jena.sparql.algebra.table.TableN)

Aggregations

QueryIterator (org.apache.jena.sparql.engine.QueryIterator)126 Binding (org.apache.jena.sparql.engine.binding.Binding)27 Op (org.apache.jena.sparql.algebra.Op)15 Node (org.apache.jena.graph.Node)13 TableN (org.apache.jena.sparql.algebra.table.TableN)13 ArrayList (java.util.ArrayList)12 ResultSetStream (org.apache.jena.sparql.engine.ResultSetStream)10 Var (org.apache.jena.sparql.core.Var)9 ResultSet (org.apache.jena.query.ResultSet)8 QueryIterPlainWrapper (org.apache.jena.sparql.engine.iterator.QueryIterPlainWrapper)8 Triple (org.apache.jena.graph.Triple)6 ExecutionContext (org.apache.jena.sparql.engine.ExecutionContext)6 BasicPattern (org.apache.jena.sparql.core.BasicPattern)5 QueryIterConcat (org.apache.jena.sparql.engine.iterator.QueryIterConcat)5 Table (org.apache.jena.sparql.algebra.Table)4 DatasetGraph (org.apache.jena.sparql.core.DatasetGraph)4 HashSet (java.util.HashSet)3 Plan (org.apache.jena.sparql.engine.Plan)3 BindingMap (org.apache.jena.sparql.engine.binding.BindingMap)3 Procedure (org.apache.jena.sparql.procedure.Procedure)3