use of org.apache.jena.sparql.algebra.op.OpFilter in project jena by apache.
the class TransformFilterDisjunction method transform.
@Override
public Op transform(OpFilter opFilter, final Op subOp) {
ExprList exprList = opFilter.getExprs();
// First pass - any disjunctions at all?
boolean processDisjunction = false;
for (Expr expr : exprList) {
if (isDisjunction(expr)) {
processDisjunction = true;
break;
}
}
// Still may be a disjunction in a form we don't optimize.
if (!processDisjunction)
return super.transform(opFilter, subOp);
ExprList exprList2 = new ExprList();
Op newOp = subOp;
// remember what's been seen so that FILTER(?x = <x> || ?x = <x> ) does not result in two transforms.
Set<Expr> doneSoFar = new HashSet<>();
for (Expr expr : exprList) {
if (!isDisjunction(expr)) {
// Assignment there?
exprList2.add(expr);
continue;
}
// // Relies on expression equality.
// if ( doneSoFar.contains(expr) )
// continue ;
// // Must be canonical: ?x = <x> is the same as <x> = ?x
// doneSoFar.add(expr) ;
Op op2 = expandDisjunction(expr, newOp);
if (op2 != null)
newOp = op2;
}
if (exprList2.isEmpty())
return newOp;
// There should have been at least on disjunction.
if (newOp == subOp) {
Log.warn(this, "FilterDisjunction assumption failure: didn't find a disjunction after all");
return super.transform(opFilter, subOp);
}
// Failed. These a was one or more expressions we couldn't handle.
// So the full pattern is going to be executed anyway.
//return super.transform(super.transform(opFilter, subOp)) ;
// Put the non-disjunctions outside the disjunction and the pattern rewrite.
Op opOther = OpFilter.filterBy(exprList2, newOp);
if (opOther instanceof OpFilter) {
return opOther;
}
// opOther is not a filter any more - should not happen but to isolate from future changes ...
Log.warn(this, "FilterDisjunction assumption failure: not a filter after processing disjunction/other mix");
return super.transform(opFilter, subOp);
}
use of org.apache.jena.sparql.algebra.op.OpFilter in project jena by apache.
the class OpRewriterTest method testBGP.
@Test
public void testBGP() {
SecurityEvaluator securityEvaluator = new MockSecurityEvaluator(true, true, true, true, true, true);
rewriter = new OpRewriter(securityEvaluator, "http://example.com/dummy");
rewriter.visit(new OpBGP(BasicPattern.wrap(Arrays.asList(triples))));
Op op = rewriter.getResult();
Assert.assertTrue("Should have been an OpFilter", op instanceof OpFilter);
OpFilter filter = (OpFilter) op;
ExprList eLst = filter.getExprs();
Assert.assertEquals(1, eLst.size());
Assert.assertTrue("Should have been a SecuredFunction", eLst.get(0) instanceof SecuredFunction);
op = filter.getSubOp();
Assert.assertTrue("Should have been a OpBGP", op instanceof OpBGP);
BasicPattern basicPattern = ((OpBGP) op).getPattern();
Assert.assertEquals(3, basicPattern.size());
Triple t = basicPattern.get(0);
Assert.assertEquals(NodeFactory.createVariable("foo"), t.getSubject());
Assert.assertEquals(RDF.type.asNode(), t.getPredicate());
Assert.assertEquals(NodeFactory.createURI("http://example.com/class"), t.getObject());
t = basicPattern.get(1);
Assert.assertEquals(NodeFactory.createVariable("foo"), t.getSubject());
Assert.assertTrue("Should have been blank", t.getPredicate().isBlank());
Assert.assertEquals(NodeFactory.createVariable("bar"), t.getObject());
t = basicPattern.get(2);
Assert.assertEquals(NodeFactory.createVariable("bar"), t.getSubject());
Assert.assertTrue("Should have been blank", t.getPredicate().isBlank());
Assert.assertEquals(NodeFactory.createVariable("baz"), t.getObject());
}
Aggregations