Search in sources :

Example 86 with Binding

use of org.apache.jena.sparql.engine.binding.Binding in project jena by apache.

the class LibSDB method findInQuads.

/** Match a quad pattern (not triples in the default graph) */
public static Iterator<Quad> findInQuads(DatasetGraph dsg, Node g, Node s, Node p, Node o) {
    // If null, create and remember a variable, else use the node.
    final Node vg = varOrConst(g, "g");
    final Node vs = varOrConst(s, "s");
    final Node vp = varOrConst(p, "p");
    final Node vo = varOrConst(o, "o");
    Triple triple = new Triple(vs, vp, vo);
    // Evaluate as an algebra expression
    BasicPattern pattern = new BasicPattern();
    pattern.add(triple);
    Op op = new OpQuadPattern(vg, pattern);
    Plan plan = QueryEngineSDB.getFactory().create(op, dsg, BindingRoot.create(), null);
    QueryIterator qIter = plan.iterator();
    Iterator<Binding> iter;
    if (SDB.getContext().isTrue(SDB.streamGraphAPI)) {
        iter = qIter;
    } else {
        // ---- Safe version: 
        List<Binding> x = Iter.toList(qIter);
        Iterator<Binding> qIter2 = x.iterator();
        qIter.close();
        iter = qIter2;
    }
    return Iter.map(iter, (b) -> bindingToQuad(vg, vs, vp, vo, b));
}
Also used : Triple(org.apache.jena.graph.Triple) Binding(org.apache.jena.sparql.engine.binding.Binding) Op(org.apache.jena.sparql.algebra.Op) OpQuadPattern(org.apache.jena.sparql.algebra.op.OpQuadPattern) QueryIterator(org.apache.jena.sparql.engine.QueryIterator) Node(org.apache.jena.graph.Node) BasicPattern(org.apache.jena.sparql.core.BasicPattern) Plan(org.apache.jena.sparql.engine.Plan)

Example 87 with Binding

use of org.apache.jena.sparql.engine.binding.Binding in project jena by apache.

the class BindingTDB method calcVars.

private List<Var> calcVars() {
    List<Var> vars = new ArrayList<>(4);
    // Only if not in parent.
    // A (var/value) binding may have been copied down to record it's NodeId.  
    Binding b = idBinding.getParentBinding();
    Iterator<Var> iter = idBinding.iterator();
    for (Var v : idBinding) {
        if (b == null || !b.contains(v))
            vars.add(v);
    }
    return vars;
}
Also used : Binding(org.apache.jena.sparql.engine.binding.Binding) Var(org.apache.jena.sparql.core.Var)

Example 88 with Binding

use of org.apache.jena.sparql.engine.binding.Binding 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 89 with Binding

use of org.apache.jena.sparql.engine.binding.Binding in project jena by apache.

the class RDFInput method buildRowsOrdered.

private void buildRowsOrdered(Resource root, int count) {
    Model m = root.getModel();
    // Assume one result set per file.
    for (int index = 1; ; index++) {
        Literal ind = m.createTypedLiteral(index);
        StmtIterator sIter = m.listStatements(null, ResultSetGraphVocab.index, ind);
        if (!sIter.hasNext())
            break;
        Statement s = sIter.nextStatement();
        if (sIter.hasNext())
            Log.warn(this, "More than one solution: index = " + index);
        Resource soln = s.getSubject();
        Binding rb = buildBinding(soln);
        rows.add(rb);
        sIter.close();
    }
    if (rows.size() != count)
        Log.warn(this, "Found " + rows.size() + ": expected " + count);
}
Also used : Binding(org.apache.jena.sparql.engine.binding.Binding)

Example 90 with Binding

use of org.apache.jena.sparql.engine.binding.Binding in project jena by apache.

the class RDFInput method buildRows.

private void buildRows(Resource root) {
    // Now the results themselves
    int count = 0;
    StmtIterator solnIter = root.listProperties(ResultSetGraphVocab.solution);
    for (; solnIter.hasNext(); ) {
        Resource soln = solnIter.nextStatement().getResource();
        count++;
        Binding rb = buildBinding(soln);
        rows.add(rb);
    }
    solnIter.close();
    if (root.hasProperty(ResultSetGraphVocab.size)) {
        try {
            int size = root.getRequiredProperty(ResultSetGraphVocab.size).getInt();
            if (size != count)
                Log.warn(this, "Warning: Declared size = " + size + " : Count = " + count);
        } catch (JenaException rdfEx) {
        }
    }
}
Also used : Binding(org.apache.jena.sparql.engine.binding.Binding) JenaException(org.apache.jena.shared.JenaException)

Aggregations

Binding (org.apache.jena.sparql.engine.binding.Binding)132 Node (org.apache.jena.graph.Node)39 ArrayList (java.util.ArrayList)36 Var (org.apache.jena.sparql.core.Var)33 QueryIterator (org.apache.jena.sparql.engine.QueryIterator)27 Test (org.junit.Test)21 QueryIterPlainWrapper (org.apache.jena.sparql.engine.iterator.QueryIterPlainWrapper)16 BindingMap (org.apache.jena.sparql.engine.binding.BindingMap)10 BindingComparator (org.apache.jena.sparql.engine.binding.BindingComparator)8 BuilderBinding (org.apache.jena.sparql.sse.builders.BuilderBinding)8 NoSuchElementException (java.util.NoSuchElementException)7 Triple (org.apache.jena.graph.Triple)7 ResultSetStream (org.apache.jena.sparql.engine.ResultSetStream)7 ARQInternalErrorException (org.apache.jena.sparql.ARQInternalErrorException)5 Op (org.apache.jena.sparql.algebra.Op)5 BasicPattern (org.apache.jena.sparql.core.BasicPattern)5 ResultSet (org.apache.jena.query.ResultSet)4 SortCondition (org.apache.jena.query.SortCondition)4 TableN (org.apache.jena.sparql.algebra.table.TableN)4 File (java.io.File)3