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