Search in sources :

Example 71 with Op

use of org.apache.jena.sparql.algebra.Op 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)

Example 72 with Op

use of org.apache.jena.sparql.algebra.Op in project jena by apache.

the class OpRewriter method rewriteOp2.

/**
	 * rewrites the left and right parts of the op2 the left part is returned
	 * the right part is placed in the rewriter
	 * 
	 * @param op2
	 * @param rewriter
	 * @return the rewritten op.
	 */
private Op rewriteOp2(final Op2 op2, final OpRewriter rewriter) {
    op2.getLeft().visit(rewriter.reset());
    final Op left = rewriter.getResult();
    op2.getRight().visit(rewriter.reset());
    return left;
}
Also used : Op(org.apache.jena.sparql.algebra.Op)

Example 73 with Op

use of org.apache.jena.sparql.algebra.Op in project jena by apache.

the class OpRewriter method visit.

@Override
public void visit(final OpBGP opBGP) throws ReadDeniedException, AuthenticationRequiredException {
    if (LOG.isDebugEnabled()) {
        LOG.debug("Starting visiting OpBGP");
    }
    Object principal = securityEvaluator.getPrincipal();
    if (!securityEvaluator.evaluate(principal, Action.Read, graphIRI)) {
        if (silentFail) {
            return;
        } else {
            throw new ReadDeniedException(SecuredItem.Util.modelPermissionMsg(graphIRI));
        }
    }
    // if the user can read any triple just add the opBGP
    if (securityEvaluator.evaluate(principal, Action.Read, graphIRI, Triple.ANY)) {
        addOp(opBGP);
    } else {
        // add security filtering to the resulting triples
        final List<Triple> newBGP = new ArrayList<>();
        final List<Node> variables = new ArrayList<>();
        // register all variables
        for (final Triple t : opBGP.getPattern().getList()) {
            newBGP.add(registerBGPTriple(t, variables));
        }
        // create the security function.
        final SecuredFunction secFunc = new SecuredFunction(graphIRI, securityEvaluator, variables, newBGP);
        // create the filter
        Op filter = OpFilter.filter(secFunc, new OpBGP(BasicPattern.wrap(newBGP)));
        // add the filter
        addOp(filter);
    }
}
Also used : Triple(org.apache.jena.graph.Triple) Op(org.apache.jena.sparql.algebra.Op) ReadDeniedException(org.apache.jena.shared.ReadDeniedException) Node(org.apache.jena.graph.Node) ArrayList(java.util.ArrayList)

Example 74 with Op

use of org.apache.jena.sparql.algebra.Op in project jena by apache.

the class OpExecutorTDB1 method optimizeExecuteQuads.

/** Execute, with optimization, a quad pattern */
private static QueryIterator optimizeExecuteQuads(DatasetGraphTDB ds, QueryIterator input, Node gn, BasicPattern bgp, ExprList exprs, ExecutionContext execCxt) {
    if (!input.hasNext())
        return input;
    // ---- Graph names with special meaning. 
    gn = decideGraphNode(gn, execCxt);
    if (gn == null)
        return optimizeExecuteTriples(ds, input, bgp, exprs, execCxt);
    // ---- Execute quads+filters
    if (bgp.size() >= 2) {
        ReorderTransformation transform = ds.getReorderTransform();
        if (transform != null) {
            QueryIterPeek peek = QueryIterPeek.create(input, execCxt);
            // Original input now invalid.
            input = peek;
            bgp = reorder(bgp, peek, transform);
        }
    }
    // -- Filter placement
    Op op = null;
    if (exprs != null)
        op = TransformFilterPlacement.transform(exprs, gn, bgp);
    else
        op = new OpQuadPattern(gn, bgp);
    return plainExecute(op, input, execCxt);
}
Also used : QueryIterPeek(org.apache.jena.sparql.engine.iterator.QueryIterPeek) Op(org.apache.jena.sparql.algebra.Op) ReorderTransformation(org.apache.jena.sparql.engine.optimizer.reorder.ReorderTransformation)

Example 75 with Op

use of org.apache.jena.sparql.algebra.Op in project jena by apache.

the class QueryIterService method nextStage.

@Override
protected QueryIterator nextStage(Binding outerBinding) {
    Op op = QC.substitute(opService, outerBinding);
    boolean silent = opService.getSilent();
    QueryIterator qIter;
    try {
        qIter = Service.exec((OpService) op, getExecContext().getContext());
        // This iterator is materialized already otherwise we may end up
        // not servicing the HTTP connection as needed.
        // In extremis, can cause a deadlock when SERVICE loops back to this server.
        // Add tracking.
        qIter = QueryIter.makeTracked(qIter, getExecContext());
    } catch (RuntimeException ex) {
        if (silent) {
            Log.warn(this, "SERVICE <" + opService.getService().toString() + ">: " + ex.getMessage());
            // Return the input
            return QueryIterSingleton.create(outerBinding, getExecContext());
        }
        throw ex;
    }
    // Need to put the outerBinding as parent to every binding of the service call.
    // There should be no variables in common because of the OpSubstitute.substitute 
    QueryIterator qIter2 = new QueryIterCommonParent(qIter, outerBinding, getExecContext());
    return qIter2;
}
Also used : Op(org.apache.jena.sparql.algebra.Op) QueryIterator(org.apache.jena.sparql.engine.QueryIterator) QueryIterCommonParent(org.apache.jena.sparql.engine.iterator.QueryIterCommonParent) OpService(org.apache.jena.sparql.algebra.op.OpService)

Aggregations

Op (org.apache.jena.sparql.algebra.Op)166 Var (org.apache.jena.sparql.core.Var)29 VarExprList (org.apache.jena.sparql.core.VarExprList)18 Query (org.apache.jena.query.Query)17 ExprList (org.apache.jena.sparql.expr.ExprList)17 QueryIterator (org.apache.jena.sparql.engine.QueryIterator)15 Test (org.junit.Test)14 Triple (org.apache.jena.graph.Triple)13 Expr (org.apache.jena.sparql.expr.Expr)13 BasicPattern (org.apache.jena.sparql.core.BasicPattern)11 ArrayList (java.util.ArrayList)9 Node (org.apache.jena.graph.Node)8 OpBGP (org.apache.jena.sparql.algebra.op.OpBGP)7 NodeValueInteger (org.apache.jena.sparql.expr.nodevalue.NodeValueInteger)7 Binding (org.apache.jena.sparql.engine.binding.Binding)5 ExprVar (org.apache.jena.sparql.expr.ExprVar)5 BaseTest (org.apache.jena.atlas.junit.BaseTest)3 Pair (org.apache.jena.atlas.lib.Pair)3 Table (org.apache.jena.sparql.algebra.Table)3 TransformCopy (org.apache.jena.sparql.algebra.TransformCopy)3