Search in sources :

Example 51 with Binding

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

the class ValuesHandlerTest method twoVarTwoBlocksWithVarReplacement.

@Test
public void twoVarTwoBlocksWithVarReplacement() {
    Node n = NodeFactory.createLiteral("hello");
    Node nn = NodeFactory.createLiteral("hola");
    Node n2 = NodeFactory.createLiteral("there");
    Node nn2 = NodeFactory.createLiteral("aqui");
    Var v = Var.alloc("x");
    Var v2 = Var.alloc("y");
    handler.addValueVar(v, null);
    handler.addValueVar(v2, null);
    handler.addValueRow(Arrays.asList(n, n2));
    handler.addValueRow(Arrays.asList(nn, nn2));
    Map<Var, Node> replaceVars = new HashMap<Var, Node>();
    replaceVars.put(v2, NodeFactory.createBlankNode());
    handler.setVars(replaceVars);
    handler.build();
    List<Var> vars = query.getValuesVariables();
    assertEquals(1, vars.size());
    assertEquals(v, vars.get(0));
    assertNotNull(query.getValuesData());
    List<Binding> lb = query.getValuesData();
    assertEquals(2, lb.size());
    List<Node> ln = new ArrayList<Node>();
    ln.add(n);
    ln.add(nn);
    for (Binding b : lb) {
        assertTrue(b.contains(v));
        assertFalse(b.contains(v2));
        assertTrue(ln.contains(b.get(v)));
        ln.remove(b.get(v));
    }
}
Also used : Binding(org.apache.jena.sparql.engine.binding.Binding) HashMap(java.util.HashMap) Var(org.apache.jena.sparql.core.Var) Node(org.apache.jena.graph.Node) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 52 with Binding

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

the class QueryIterExtendByVar method moveToNextBinding.

@Override
protected Binding moveToNextBinding() {
    Node n = members.next();
    Binding b = BindingFactory.binding(binding, var, n);
    return b;
}
Also used : Binding(org.apache.jena.sparql.engine.binding.Binding) Node(org.apache.jena.graph.Node)

Example 53 with Binding

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

the class QueryIterGroup method calc.

private static Iterator<Binding> calc(final QueryIterator iter, final VarExprList groupVarExpr, final List<ExprAggregator> aggregators, final ExecutionContext execCxt) {
    return new IteratorDelayedInitialization<Binding>() {

        @Override
        protected Iterator<Binding> initializeIterator() {
            boolean noAggregators = (aggregators == null || aggregators.isEmpty());
            // Phase 1 : assign bindings to buckets by key and pump through the aggregators.
            Multimap<Binding, Pair<Var, Accumulator>> accumulators = HashMultimap.create();
            while (iter.hasNext()) {
                Binding b = iter.nextBinding();
                Binding key = genKey(groupVarExpr, b, execCxt);
                if (noAggregators) {
                    // Put in a dummy to remember the input.
                    accumulators.put(key, placeholder);
                    continue;
                }
                // Create if does not exist.
                if (!accumulators.containsKey(key)) {
                    for (ExprAggregator agg : aggregators) {
                        Accumulator x = agg.getAggregator().createAccumulator();
                        Var v = agg.getVar();
                        accumulators.put(key, Pair.create(v, x));
                    }
                }
                // Do the per-accumulator calculation.
                for (Pair<Var, Accumulator> pair : accumulators.get(key)) pair.getRight().accumulate(b, execCxt);
            }
            if (accumulators.isEmpty()) {
                if (noAggregators) {
                    // ==> No result rows.
                    return Iter.nullIterator();
                }
                BindingMap binding = BindingFactory.create();
                for (ExprAggregator agg : aggregators) {
                    Var v = agg.getVar();
                    Node value = agg.getAggregator().getValueEmpty();
                    if (value != null) {
                        binding.add(v, value);
                    }
                }
                if (binding == null)
                    // This does not happen if there are any aggregators. 
                    return Iter.nullIterator();
                // cast to get the static type inference to work.
                return Iter.singletonIter((Binding) binding);
            }
            if (noAggregators)
                // We used placeholder so there are always the key. 
                return accumulators.keySet().iterator();
            List<Binding> results = new ArrayList<>();
            for (Binding k : accumulators.keySet()) {
                Collection<Pair<Var, Accumulator>> accs = accumulators.get(k);
                BindingMap b = BindingFactory.create(k);
                for (Pair<Var, Accumulator> pair : accs) {
                    Var v = pair.getLeft();
                    NodeValue value = pair.getRight().getValue();
                    Node n = (value == null) ? null : value.asNode();
                    if (v == null || n == null) {
                    } else
                        b.add(v, n);
                }
                results.add(b);
            }
            return results.iterator();
        }
    };
}
Also used : Binding(org.apache.jena.sparql.engine.binding.Binding) Accumulator(org.apache.jena.sparql.expr.aggregate.Accumulator) NodeValue(org.apache.jena.sparql.expr.NodeValue) Var(org.apache.jena.sparql.core.Var) Node(org.apache.jena.graph.Node) ArrayList(java.util.ArrayList) BindingMap(org.apache.jena.sparql.engine.binding.BindingMap) IteratorDelayedInitialization(org.apache.jena.atlas.iterator.IteratorDelayedInitialization) ExprAggregator(org.apache.jena.sparql.expr.ExprAggregator) Pair(org.apache.jena.atlas.lib.Pair)

Example 54 with Binding

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

the class QueryIterMinus method hasNextBinding.

@Override
protected final boolean hasNextBinding() {
    if (slot != null)
        return true;
    while (getLeft().hasNext()) {
        Binding bindingLeft = getLeft().nextBinding();
        slot = getNextSlot(bindingLeft);
        if (slot != null) {
            slot = bindingLeft;
            return true;
        }
    }
    getLeft().close();
    return false;
}
Also used : Binding(org.apache.jena.sparql.engine.binding.Binding)

Example 55 with Binding

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

the class QueryIter2LoopOnLeft method moveToNextBinding.

@Override
protected final Binding moveToNextBinding() {
    if (!hasNextBinding())
        return null;
    Binding x = slot;
    slot = null;
    return x;
}
Also used : Binding(org.apache.jena.sparql.engine.binding.Binding)

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