use of org.apache.jena.sparql.algebra.op.OpBGP in project jena by apache.
the class OpRewriterTest method testBGP.
@Test
public void testBGP() {
SecurityEvaluator securityEvaluator = new MockSecurityEvaluator(true, 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.OpBGP in project jena by apache.
the class PropertyFunctionGenerator method compilePattern.
private static Op compilePattern(PropertyFunctionRegistry registry, BasicPattern pattern, Context context) {
// Split into triples and property functions.
// 1/ Find property functions.
// Property functions may involve other triples (for list arguments)
// (but leave the property function triple in-place as a marker)
// 2/ Find arguments for property functions
// (but leave the property function triple in-place as a marker)
// 3/ For remaining triples, put into basic graph patterns,
// and string together the procedure calls and BGPs.
// Property functions seen
List<Triple> propertyFunctionTriples = new ArrayList<>();
// A copy of all triples (later, it is mutated)
BasicPattern triples = new BasicPattern(pattern);
// Find the triples invoking property functions, and those not.
findPropertyFunctions(context, pattern, registry, propertyFunctionTriples);
if (propertyFunctionTriples.size() == 0)
// No property functions.
return new OpBGP(pattern);
// Map triple => property function instance
Map<Triple, PropertyFunctionInstance> pfInvocations = new HashMap<>();
// Removes triples of list arguments. This mutates 'triples'
findPropertyFunctionArgs(context, triples, propertyFunctionTriples, pfInvocations);
// Now make the OpSequence structure.
Op op = makeStages(triples, pfInvocations);
return op;
}
use of org.apache.jena.sparql.algebra.op.OpBGP in project jena by apache.
the class TransformReorder method transform.
/**
* Transforms BGPs with the reordering
*/
@Override
public Op transform(OpBGP opBGP) {
BasicPattern pattern = opBGP.getPattern();
if (pattern.size() < 2)
return opBGP;
BasicPattern pattern2 = reorder.reorder(pattern);
return new OpBGP(pattern2);
}
use of org.apache.jena.sparql.algebra.op.OpBGP in project jena by apache.
the class GraphSPARQLService method graphBaseFind.
// @Override
// public Capabilities getCapabilities()
// {
// if (capabilities == null)
// capabilities = new AllCapabilities()
// { @Override public boolean handlesLiteralTyping() { return false; } };
// return capabilities;
// }
@Override
protected ExtendedIterator<Triple> graphBaseFind(Triple m) {
Node s = m.getMatchSubject();
Var sVar = null;
if (s == null) {
sVar = Var.alloc("s");
s = sVar;
}
Node p = m.getMatchPredicate();
Var pVar = null;
if (p == null) {
pVar = Var.alloc("p");
p = pVar;
}
Node o = m.getMatchObject();
Var oVar = null;
if (o == null) {
oVar = Var.alloc("o");
o = oVar;
}
Triple triple = new Triple(s, p, o);
// Evaluate as an algebra expression
BasicPattern pattern = new BasicPattern();
pattern.add(triple);
Op op = new OpBGP(pattern);
// return WrappedIterator.createNoRemove(triples.iterator()) ;
return null;
}
use of org.apache.jena.sparql.algebra.op.OpBGP in project jena by apache.
the class TransformMergeBGPs method transform.
@Override
public Op transform(OpSequence opSequence, List<Op> elts) {
// First check whether we need to do anything at all.
// Check for two BGPs.
boolean xform = false;
boolean previousBGP = false;
for (Op op1 : elts) {
BasicPattern p1 = asBGP(op1);
if (previousBGP && p1 != null) {
xform = true;
break;
}
previousBGP = (p1 != null);
}
if (!xform)
// Nothing to do here.
return super.transform(opSequence, elts);
OpSequence seq2 = OpSequence.create();
for (int i = 0; i < elts.size(); i++) {
Op op = elts.get(i);
BasicPattern p1 = asBGP(op);
if (p1 == null) {
// Do nothing
seq2.add(op);
// Outer loop.
continue;
}
// This is the op after the merge, if any.
BasicPattern pMerge = new BasicPattern();
seq2.add(new OpBGP(pMerge));
// Re-gets the BGP that trigegrs this all.
for (; i < elts.size(); i++) {
// Look at next element.
Op opNext = elts.get(i);
BasicPattern p2 = asBGP(opNext);
if (p2 == null) {
seq2.add(opNext);
break;
}
// Merge.
pMerge.addAll(p2);
}
}
if (seq2.size() == 1)
return seq2.get(0);
return seq2;
}
Aggregations