Search in sources :

Example 36 with QueryIterator

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

the class TableBase method equals.

@Override
public boolean equals(Object other) {
    if (this == other)
        return true;
    if (!(other instanceof Table))
        return false;
    Table table = (Table) other;
    if (table.size() != this.size())
        return false;
    QueryIterator qIter1 = iterator(null);
    QueryIterator qIter2 = table.iterator(null);
    try {
        for (; qIter1.hasNext(); ) {
            Binding bind1 = qIter1.nextBinding();
            Binding bind2 = qIter2.nextBinding();
            if (!BindingBase.equals(bind1, bind2))
                return false;
        }
        return true;
    } finally {
        qIter1.close();
        qIter2.close();
    }
}
Also used : Binding(org.apache.jena.sparql.engine.binding.Binding) Table(org.apache.jena.sparql.algebra.Table) QueryIterator(org.apache.jena.sparql.engine.QueryIterator)

Example 37 with QueryIterator

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

the class TableBase method hashCode.

@Override
public int hashCode() {
    int hash = 0;
    QueryIterator qIter = iterator(null);
    try {
        for (; qIter.hasNext(); ) {
            Binding binding = qIter.nextBinding();
            hash ^= binding.hashCode();
        }
        return hash;
    } finally {
        qIter.close();
    }
}
Also used : Binding(org.apache.jena.sparql.engine.binding.Binding) QueryIterator(org.apache.jena.sparql.engine.QueryIterator)

Example 38 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 39 with QueryIterator

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

the class AbstractTestDistinctReduced method distinct.

private void distinct(List<String> data, List<String> results) {
    // Distinct Iterators are not required to preserve order.
    List<Binding> input = build(data);
    List<Binding> output = build(results);
    QueryIterator qIter = createQueryIter(input);
    List<Binding> iterList = Iter.toList(qIter);
    assertEquals(output + " :: " + iterList, output.size(), iterList.size());
    // Assume results has no duplicates so same size, same members => order dependent same.
    Set<Binding> testExpected = new HashSet<>(output);
    Set<Binding> testResult = new HashSet<>(iterList);
    assertEquals(testExpected, testResult);
}
Also used : Binding(org.apache.jena.sparql.engine.binding.Binding) QueryIterator(org.apache.jena.sparql.engine.QueryIterator) HashSet(java.util.HashSet)

Example 40 with QueryIterator

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

the class TextQueryPF method exec.

@Override
public QueryIterator exec(Binding binding, PropFuncArg argSubject, Node predicate, PropFuncArg argObject, ExecutionContext execCxt) {
    if (textIndex == null) {
        if (!warningIssued) {
            Log.warn(getClass(), "No text index - no text search performed");
            warningIssued = true;
        }
        // Not a text dataset - no-op
        return IterLib.result(binding, execCxt);
    }
    argSubject = Substitute.substitute(argSubject, binding);
    argObject = Substitute.substitute(argObject, binding);
    Node s = null;
    Node score = null;
    Node literal = null;
    if (argSubject.isList()) {
        // Length checked in build()
        s = argSubject.getArg(0);
        score = argSubject.getArg(1);
        if (!score.isVariable())
            throw new QueryExecException("Hit score is not a variable: " + argSubject);
        if (argSubject.getArgListSize() > 2) {
            literal = argSubject.getArg(2);
            if (!literal.isVariable())
                throw new QueryExecException("Hit literal is not a variable: " + argSubject);
        }
    } else {
        s = argSubject.getArg();
    }
    if (s.isLiteral())
        // Does not match
        return IterLib.noResults(execCxt);
    StrMatch match = objectToStruct(argObject, true);
    if (match == null) {
        // can't match
        return IterLib.noResults(execCxt);
    }
    // ----
    QueryIterator qIter = (Var.isVar(s)) ? variableSubject(binding, s, score, literal, match, execCxt) : concreteSubject(binding, s, score, literal, match, execCxt);
    if (match.getLimit() >= 0)
        qIter = new QueryIterSlice(qIter, 0, match.getLimit(), execCxt);
    return qIter;
}
Also used : QueryIterSlice(org.apache.jena.sparql.engine.iterator.QueryIterSlice) QueryIterator(org.apache.jena.sparql.engine.QueryIterator) Node(org.apache.jena.graph.Node) QueryExecException(org.apache.jena.query.QueryExecException)

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