Search in sources :

Example 76 with QueryIterator

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

the class TextQueryPF method resultsToQueryIterator.

private QueryIterator resultsToQueryIterator(Binding binding, Node s, Node score, Node literal, Collection<TextHit> results, ExecutionContext execCxt) {
    Var sVar = Var.isVar(s) ? Var.alloc(s) : null;
    Var scoreVar = (score == null) ? null : Var.alloc(score);
    Var literalVar = (literal == null) ? null : Var.alloc(literal);
    Function<TextHit, Binding> converter = (TextHit hit) -> {
        if (score == null && literal == null)
            return sVar != null ? BindingFactory.binding(binding, sVar, hit.getNode()) : BindingFactory.binding(binding);
        BindingMap bmap = BindingFactory.create(binding);
        if (sVar != null)
            bmap.add(sVar, hit.getNode());
        if (scoreVar != null)
            bmap.add(scoreVar, NodeFactoryExtra.floatToNode(hit.getScore()));
        if (literalVar != null)
            bmap.add(literalVar, hit.getLiteral());
        return bmap;
    };
    Iterator<Binding> bIter = Iter.map(results.iterator(), converter);
    QueryIterator qIter = new QueryIterPlainWrapper(bIter, execCxt);
    return qIter;
}
Also used : Binding(org.apache.jena.sparql.engine.binding.Binding) QueryIterPlainWrapper(org.apache.jena.sparql.engine.iterator.QueryIterPlainWrapper) QueryIterator(org.apache.jena.sparql.engine.QueryIterator) BindingMap(org.apache.jena.sparql.engine.binding.BindingMap)

Example 77 with QueryIterator

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

the class Eval method evalDS.

static Table evalDS(OpDatasetNames opDSN, Evaluator evaluator) {
    Node graphNode = opDSN.getGraphNode();
    if (graphNode.isURI()) {
        if (evaluator.getExecContext().getDataset().containsGraph(graphNode)) {
            return new TableUnit();
        } else // WRONG
        {
            return new TableEmpty();
        }
    }
    if (!Var.isVar(graphNode))
        throw new ARQInternalErrorException("OpDatasetNames: Not a URI or variable: " + graphNode);
    DatasetGraph dsg = evaluator.getExecContext().getDataset();
    Iterator<Node> iter = dsg.listGraphNodes();
    List<Binding> list = new ArrayList<>((int) dsg.size());
    for (; iter.hasNext(); ) {
        Node gn = iter.next();
        Binding b = BindingFactory.binding(Var.alloc(graphNode), gn);
        list.add(b);
    }
    QueryIterator qIter = new QueryIterPlainWrapper(list.iterator(), evaluator.getExecContext());
    return TableFactory.create(qIter);
}
Also used : Binding(org.apache.jena.sparql.engine.binding.Binding) QueryIterPlainWrapper(org.apache.jena.sparql.engine.iterator.QueryIterPlainWrapper) QueryIterator(org.apache.jena.sparql.engine.QueryIterator) Node(org.apache.jena.graph.Node) ARQInternalErrorException(org.apache.jena.sparql.ARQInternalErrorException) ArrayList(java.util.ArrayList) TableEmpty(org.apache.jena.sparql.algebra.table.TableEmpty) TableUnit(org.apache.jena.sparql.algebra.table.TableUnit) DatasetGraph(org.apache.jena.sparql.core.DatasetGraph)

Example 78 with QueryIterator

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

the class Eval method evalQuadPattern.

static Table evalQuadPattern(OpQuadPattern opQuad, Evaluator evaluator) {
    if (opQuad.isEmpty())
        return TableFactory.createUnit();
    ExecutionContext cxt = evaluator.getExecContext();
    DatasetGraph ds = cxt.getDataset();
    BasicPattern pattern = opQuad.getBasicPattern();
    if (!opQuad.getGraphNode().isVariable()) {
        if (!opQuad.getGraphNode().isURI()) {
            throw new ARQInternalErrorException("Not a URI or variable: " + opQuad.getGraphNode());
        }
        Graph g = null;
        if (opQuad.isDefaultGraph())
            g = ds.getDefaultGraph();
        else
            g = ds.getGraph(opQuad.getGraphNode());
        if (g == null)
            return new TableEmpty();
        ExecutionContext cxt2 = new ExecutionContext(cxt, g);
        QueryIterator qIter = executeBGP(pattern, QueryIterRoot.create(cxt2), cxt2);
        return TableFactory.create(qIter);
    } else {
        // Variable.
        Var gVar = Var.alloc(opQuad.getGraphNode());
        // Or just just devolve to OpGraph and get OpUnion chain of OpJoin
        QueryIterConcat concat = new QueryIterConcat(cxt);
        for (Iterator<Node> graphNodes = cxt.getDataset().listGraphNodes(); graphNodes.hasNext(); ) {
            Node gn = graphNodes.next();
            //Op tableVarURI = TableFactory.create(gn.getName(), Node.createURI(uri)) ;
            Graph g = cxt.getDataset().getGraph(gn);
            Binding b = BindingFactory.binding(BindingRoot.create(), gVar, gn);
            ExecutionContext cxt2 = new ExecutionContext(cxt, g);
            // Eval the pattern, eval the variable, join.
            // Pattern may be non-linear in the variable - do a pure execution.  
            Table t1 = TableFactory.create(gVar, gn);
            QueryIterator qIter = executeBGP(pattern, QueryIterRoot.create(cxt2), cxt2);
            Table t2 = TableFactory.create(qIter);
            Table t3 = evaluator.join(t1, t2);
            concat.add(t3.iterator(cxt2));
        }
        return TableFactory.create(concat);
    }
}
Also used : Binding(org.apache.jena.sparql.engine.binding.Binding) Table(org.apache.jena.sparql.algebra.Table) Var(org.apache.jena.sparql.core.Var) ARQInternalErrorException(org.apache.jena.sparql.ARQInternalErrorException) Node(org.apache.jena.graph.Node) BasicPattern(org.apache.jena.sparql.core.BasicPattern) TableEmpty(org.apache.jena.sparql.algebra.table.TableEmpty) DatasetGraph(org.apache.jena.sparql.core.DatasetGraph) QueryIterConcat(org.apache.jena.sparql.engine.iterator.QueryIterConcat) ExecutionContext(org.apache.jena.sparql.engine.ExecutionContext) DatasetGraph(org.apache.jena.sparql.core.DatasetGraph) OpGraph(org.apache.jena.sparql.algebra.op.OpGraph) Graph(org.apache.jena.graph.Graph) QueryIterator(org.apache.jena.sparql.engine.QueryIterator)

Example 79 with QueryIterator

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

the class QueryIterOptionalIndex method nextStage.

@Override
protected QueryIterator nextStage(Binding binding) {
    Op op2 = QC.substitute(op, binding);
    QueryIterator thisStep = QueryIterSingleton.create(binding, getExecContext());
    QueryIterator cIter = QC.execute(op2, thisStep, super.getExecContext());
    cIter = new QueryIterDefaulting(cIter, binding, getExecContext());
    return cIter;
}
Also used : Op(org.apache.jena.sparql.algebra.Op) QueryIterator(org.apache.jena.sparql.engine.QueryIterator) QueryIterDefaulting(org.apache.jena.sparql.engine.iterator.QueryIterDefaulting)

Example 80 with QueryIterator

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

the class ExprFunctionOp method eval.

// ---- Evaluation
@Override
public final NodeValue eval(Binding binding, FunctionEnv env) {
    // Substitute?
    // Apply optimize transforms after substitution?
    //        if ( opRun == null )
    //        {
    //            opRun = op ;
    //            if ( env.getContext().isTrueOrUndef(ARQ.propertyFunctions) )
    //                opRun = Optimize.apply("Property Functions", new TransformPropertyFunction(env.getContext()), opRun) ;
    //        }
    ExecutionContext execCxt = new ExecutionContext(env.getContext(), env.getActiveGraph(), env.getDataset(), QC.getFactory(env.getContext()));
    QueryIterator qIter1 = QueryIterSingleton.create(binding, execCxt);
    QueryIterator qIter = QC.execute(op, qIter1, execCxt);
    // Wrap with something to check for closed iterators.
    qIter = QueryIteratorCheck.check(qIter, execCxt);
    // Call the per-operation functionality.
    NodeValue v = eval(binding, qIter, env);
    qIter.close();
    return v;
}
Also used : ExecutionContext(org.apache.jena.sparql.engine.ExecutionContext) QueryIterator(org.apache.jena.sparql.engine.QueryIterator)

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