Search in sources :

Example 36 with Binding

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

the class TestDistinctDataNet method testDistinct2.

@Test
public void testDistinct2() {
    List<Binding> undistinct = new ArrayList<>();
    undistinct.add(b12);
    undistinct.add(b19);
    undistinct.add(b02);
    undistinct.add(b12);
    undistinct.add(b19);
    undistinct.add(b12);
    undistinct.add(b02);
    undistinct.add(x10);
    List<Binding> control = Iter.toList(Iter.distinct(undistinct.iterator()));
    List<Binding> distinct = new ArrayList<>();
    DistinctDataNet<Binding> db = new DistinctDataNet<>(new ThresholdPolicyCount<Binding>(2), SerializationFactoryFinder.bindingSerializationFactory(), new BindingComparator(new ArrayList<SortCondition>()));
    try {
        for (Binding b : undistinct) {
            if (db.netAdd(b)) {
                distinct.add(b);
            }
        }
        Iterator<Binding> iter = db.netIterator();
        while (iter.hasNext()) {
            distinct.add(iter.next());
        }
        Iter.close(iter);
    } finally {
        db.close();
    }
    assertEquals(control.size(), distinct.size());
    assertTrue(ResultSetCompare.equalsByTest(control, distinct, NodeUtils.sameTerm));
}
Also used : Binding(org.apache.jena.sparql.engine.binding.Binding) BuilderBinding(org.apache.jena.sparql.sse.builders.BuilderBinding) BindingComparator(org.apache.jena.sparql.engine.binding.BindingComparator) DistinctDataNet(org.apache.jena.atlas.data.DistinctDataNet) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 37 with Binding

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

the class TestDistinctDataNet method testDistinct.

@Test
public void testDistinct() {
    List<Binding> undistinct = new ArrayList<>();
    undistinct.add(b12);
    undistinct.add(b19);
    undistinct.add(b02);
    undistinct.add(b12);
    undistinct.add(b19);
    undistinct.add(b12);
    undistinct.add(b02);
    undistinct.add(x10);
    List<Binding> control = Iter.toList(Iter.distinct(undistinct.iterator()));
    List<Binding> distinct = new ArrayList<>();
    DistinctDataNet<Binding> db = new DistinctDataNet<>(new ThresholdPolicyCount<Binding>(2), SerializationFactoryFinder.bindingSerializationFactory(), new BindingComparator(new ArrayList<SortCondition>()));
    try {
        db.addAll(undistinct);
        Iterator<Binding> iter = db.iterator();
        while (iter.hasNext()) {
            distinct.add(iter.next());
        }
        Iter.close(iter);
    } finally {
        db.close();
    }
    assertEquals(control.size(), distinct.size());
    assertTrue(ResultSetCompare.equalsByTest(control, distinct, NodeUtils.sameTerm));
}
Also used : Binding(org.apache.jena.sparql.engine.binding.Binding) BuilderBinding(org.apache.jena.sparql.sse.builders.BuilderBinding) BindingComparator(org.apache.jena.sparql.engine.binding.BindingComparator) DistinctDataNet(org.apache.jena.atlas.data.DistinctDataNet) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 38 with Binding

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

the class TestStatisticsAggregates method testErr.

private void testErr(String qsAgg, DatasetGraph ds, Syntax syntax) {
    Query query = build(qsAgg, syntax);
    try (QueryExecution qExec = QueryExecutionFactory.create(query, DatasetFactory.wrap(ds))) {
        ResultSet rs = qExec.execSelect();
        assertTrue(rs.hasNext());
        assertTrue(rs.getResultVars().contains("X"));
        Binding b = rs.nextBinding();
        assertFalse(b.contains(Var.alloc("X")));
    }
}
Also used : Binding(org.apache.jena.sparql.engine.binding.Binding)

Example 39 with Binding

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

the class SolverLib method execute.

// The worker.  Callers choose the NodeTupleTable.  
//     graphNode may be Node.ANY, meaning we should make triples unique.
//     graphNode may be null, meaning default graph
private static QueryIterator execute(NodeTupleTable nodeTupleTable, Node graphNode, BasicPattern pattern, QueryIterator input, Predicate<Tuple<NodeId>> filter, ExecutionContext execCxt) {
    if (Quad.isUnionGraph(graphNode))
        graphNode = Node.ANY;
    if (Quad.isDefaultGraph(graphNode))
        graphNode = null;
    List<Triple> triples = pattern.getList();
    boolean anyGraph = (graphNode == null ? false : (Node.ANY.equals(graphNode)));
    int tupleLen = nodeTupleTable.getTupleTable().getTupleLen();
    if (graphNode == null) {
        if (3 != tupleLen)
            throw new TDBException("SolverLib: Null graph node but tuples are of length " + tupleLen);
    } else {
        if (4 != tupleLen)
            throw new TDBException("SolverLib: Graph node specified but tuples are of length " + tupleLen);
    }
    // Convert from a QueryIterator (Bindings of Var/Node) to BindingNodeId
    NodeTable nodeTable = nodeTupleTable.getNodeTable();
    Iterator<BindingNodeId> chain = Iter.map(input, SolverLib.convFromBinding(nodeTable));
    List<Abortable> killList = new ArrayList<>();
    for (Triple triple : triples) {
        Tuple<Node> tuple = null;
        if (graphNode == null)
            // 3-tuples
            tuple = tuple(triple.getSubject(), triple.getPredicate(), triple.getObject());
        else
            // 4-tuples.
            tuple = tuple(graphNode, triple.getSubject(), triple.getPredicate(), triple.getObject());
        chain = solve(nodeTupleTable, tuple, anyGraph, chain, filter, execCxt);
        chain = makeAbortable(chain, killList);
    }
    // DEBUG POINT
    if (false) {
        if (chain.hasNext())
            chain = Iter.debug(chain);
        else
            System.out.println("No results");
    }
    // Timeout wrapper ****
    // QueryIterTDB gets called async.
    // Iter.abortable?
    // Or each iterator has a place to test.
    // or pass in a thing to test?
    // Need to make sure the bindings here point to parent.
    Iterator<Binding> iterBinding = convertToNodes(chain, nodeTable);
    // "killList" will be aborted on timeout.
    return new QueryIterTDB(iterBinding, killList, input, execCxt);
}
Also used : Binding(org.apache.jena.sparql.engine.binding.Binding) TDBException(org.apache.jena.tdb.TDBException) Node(org.apache.jena.graph.Node) Triple(org.apache.jena.graph.Triple) NodeTable(org.apache.jena.tdb.store.nodetable.NodeTable)

Example 40 with Binding

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

the class SolverLib method graphNames.

/** Find all the graph names in the quads table. */
public static QueryIterator graphNames(DatasetGraphTDB ds, Node graphNode, QueryIterator input, Predicate<Tuple<NodeId>> filter, ExecutionContext execCxt) {
    List<Abortable> killList = new ArrayList<>();
    Iterator<Tuple<NodeId>> iter1 = ds.getQuadTable().getNodeTupleTable().find(NodeId.NodeIdAny, NodeId.NodeIdAny, NodeId.NodeIdAny, NodeId.NodeIdAny);
    if (filter != null)
        iter1 = Iter.filter(iter1, filter);
    Iterator<NodeId> iter2 = Iter.map(iter1, (t) -> t.get(0));
    iter2 = makeAbortable(iter2, killList);
    Iterator<NodeId> iter3 = Iter.distinct(iter2);
    iter3 = makeAbortable(iter3, killList);
    Iterator<Node> iter4 = NodeLib.nodes(ds.getQuadTable().getNodeTupleTable().getNodeTable(), iter3);
    final Var var = Var.alloc(graphNode);
    Iterator<Binding> iterBinding = Iter.map(iter4, node -> BindingFactory.binding(var, node));
    // Not abortable.
    return new QueryIterTDB(iterBinding, killList, input, execCxt);
}
Also used : Binding(org.apache.jena.sparql.engine.binding.Binding) Var(org.apache.jena.sparql.core.Var) Node(org.apache.jena.graph.Node) NodeId(org.apache.jena.tdb.store.NodeId) Tuple(org.apache.jena.atlas.lib.tuple.Tuple)

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