use of org.apache.jena.sparql.engine.QueryIterator in project jena by apache.
the class TableBase method equals.
@Override
public boolean equals(Object other) {
if (this == other)
return true;
if (!(other instanceof Table))
return false;
Table table = (Table) other;
if (table.size() != this.size())
return false;
QueryIterator qIter1 = iterator(null);
QueryIterator qIter2 = table.iterator(null);
try {
for (; qIter1.hasNext(); ) {
Binding bind1 = qIter1.nextBinding();
Binding bind2 = qIter2.nextBinding();
if (!BindingBase.equals(bind1, bind2))
return false;
}
return true;
} finally {
qIter1.close();
qIter2.close();
}
}
use of org.apache.jena.sparql.engine.QueryIterator in project jena by apache.
the class TableBase method hashCode.
@Override
public int hashCode() {
int hash = 0;
QueryIterator qIter = iterator(null);
try {
for (; qIter.hasNext(); ) {
Binding binding = qIter.nextBinding();
hash ^= binding.hashCode();
}
return hash;
} finally {
qIter.close();
}
}
use of org.apache.jena.sparql.engine.QueryIterator in project jena by apache.
the class TestSemanticEquivalence method testAsAlgebra.
/**
* Tests whether an algebra expression gives the same results when run both
* with and without a given optimizer
*
* @param algStr
* Algebra
* @param ds
* Dataset
* @param opt
* Optimizer
* @param expected
* Expected number of results
*/
public static void testAsAlgebra(String algStr, Dataset ds, Symbol opt, int expected) {
Op op = SSE.parseOp(algStr);
List<String> vars = new ArrayList<>();
for (Var v : OpVars.visibleVars(op)) {
vars.add(v.getName());
}
// Track current state
boolean isEnabled = ARQ.isTrue(opt);
boolean isDisabled = ARQ.isFalse(opt);
try {
// Run first without optimization
ARQ.set(opt, false);
QueryEngineMain engine = new QueryEngineMain(op, ds.asDatasetGraph(), BindingFactory.binding(), ARQ.getContext());
QueryIterator iter = engine.eval(op, ds.asDatasetGraph(), BindingFactory.binding(), ARQ.getContext());
ResultSetRewindable rs = ResultSetFactory.makeRewindable(new ResultSetStream(vars, ModelFactory.createDefaultModel(), iter));
if (expected != rs.size()) {
System.err.println("Non-optimized results not as expected");
TextOutput output = new TextOutput((SerializationContext) null);
output.format(System.out, rs);
rs.reset();
}
Assert.assertEquals(expected, rs.size());
iter.close();
// Run with optimization
ARQ.set(opt, true);
engine = new QueryEngineMain(op, ds.asDatasetGraph(), BindingFactory.binding(), ARQ.getContext());
QueryIterator iterOpt = engine.eval(op, ds.asDatasetGraph(), BindingFactory.binding(), ARQ.getContext());
ResultSetRewindable rsOpt = ResultSetFactory.makeRewindable(new ResultSetStream(vars, ModelFactory.createDefaultModel(), iterOpt));
if (expected != rsOpt.size()) {
System.err.println("Optimized results not as expected");
TextOutput output = new TextOutput((SerializationContext) null);
output.format(System.out, rsOpt);
rsOpt.reset();
}
Assert.assertEquals(expected, rsOpt.size());
iterOpt.close();
Assert.assertTrue(ResultSetCompare.isomorphic(rs, rsOpt));
} finally {
// Restore previous state
if (isEnabled) {
ARQ.set(opt, true);
} else if (isDisabled) {
ARQ.set(opt, false);
} else {
ARQ.unset(opt);
}
}
}
use of org.apache.jena.sparql.engine.QueryIterator in project jena by apache.
the class AbstractTestDistinctReduced method distinct.
private void distinct(List<String> data, List<String> results) {
// Distinct Iterators are not required to preserve order.
List<Binding> input = build(data);
List<Binding> output = build(results);
QueryIterator qIter = createQueryIter(input);
List<Binding> iterList = Iter.toList(qIter);
assertEquals(output + " :: " + iterList, output.size(), iterList.size());
// Assume results has no duplicates so same size, same members => order dependent same.
Set<Binding> testExpected = new HashSet<>(output);
Set<Binding> testResult = new HashSet<>(iterList);
assertEquals(testExpected, testResult);
}
use of org.apache.jena.sparql.engine.QueryIterator in project jena by apache.
the class TextQueryPF method exec.
@Override
public QueryIterator exec(Binding binding, PropFuncArg argSubject, Node predicate, PropFuncArg argObject, ExecutionContext execCxt) {
if (textIndex == null) {
if (!warningIssued) {
Log.warn(getClass(), "No text index - no text search performed");
warningIssued = true;
}
// Not a text dataset - no-op
return IterLib.result(binding, execCxt);
}
argSubject = Substitute.substitute(argSubject, binding);
argObject = Substitute.substitute(argObject, binding);
Node s = null;
Node score = null;
Node literal = null;
if (argSubject.isList()) {
// Length checked in build()
s = argSubject.getArg(0);
score = argSubject.getArg(1);
if (!score.isVariable())
throw new QueryExecException("Hit score is not a variable: " + argSubject);
if (argSubject.getArgListSize() > 2) {
literal = argSubject.getArg(2);
if (!literal.isVariable())
throw new QueryExecException("Hit literal is not a variable: " + argSubject);
}
} else {
s = argSubject.getArg();
}
if (s.isLiteral())
// Does not match
return IterLib.noResults(execCxt);
StrMatch match = objectToStruct(argObject, true);
if (match == null) {
// can't match
return IterLib.noResults(execCxt);
}
// ----
QueryIterator qIter = (Var.isVar(s)) ? variableSubject(binding, s, score, literal, match, execCxt) : concreteSubject(binding, s, score, literal, match, execCxt);
if (match.getLimit() >= 0)
qIter = new QueryIterSlice(qIter, 0, match.getLimit(), execCxt);
return qIter;
}
Aggregations