Search in sources :

Example 1 with OpFilter

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);
}
Also used : Op(org.apache.jena.sparql.algebra.Op) Expr(org.apache.jena.sparql.expr.Expr) ExprList(org.apache.jena.sparql.expr.ExprList) OpFilter(org.apache.jena.sparql.algebra.op.OpFilter) HashSet(java.util.HashSet)

Example 2 with OpFilter

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());
}
Also used : Triple(org.apache.jena.graph.Triple) Op(org.apache.jena.sparql.algebra.Op) SecuredFunction(org.apache.jena.permissions.query.rewriter.SecuredFunction) ExprList(org.apache.jena.sparql.expr.ExprList) MockSecurityEvaluator(org.apache.jena.permissions.MockSecurityEvaluator) OpBGP(org.apache.jena.sparql.algebra.op.OpBGP) SecurityEvaluator(org.apache.jena.permissions.SecurityEvaluator) MockSecurityEvaluator(org.apache.jena.permissions.MockSecurityEvaluator) BasicPattern(org.apache.jena.sparql.core.BasicPattern) OpFilter(org.apache.jena.sparql.algebra.op.OpFilter) OpRewriter(org.apache.jena.permissions.query.rewriter.OpRewriter) Test(org.junit.Test)

Aggregations

Op (org.apache.jena.sparql.algebra.Op)2 OpFilter (org.apache.jena.sparql.algebra.op.OpFilter)2 ExprList (org.apache.jena.sparql.expr.ExprList)2 HashSet (java.util.HashSet)1 Triple (org.apache.jena.graph.Triple)1 MockSecurityEvaluator (org.apache.jena.permissions.MockSecurityEvaluator)1 SecurityEvaluator (org.apache.jena.permissions.SecurityEvaluator)1 OpRewriter (org.apache.jena.permissions.query.rewriter.OpRewriter)1 SecuredFunction (org.apache.jena.permissions.query.rewriter.SecuredFunction)1 OpBGP (org.apache.jena.sparql.algebra.op.OpBGP)1 BasicPattern (org.apache.jena.sparql.core.BasicPattern)1 Expr (org.apache.jena.sparql.expr.Expr)1 Test (org.junit.Test)1