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));
}
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));
}
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")));
}
}
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);
}
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);
}
Aggregations