Search in sources :

Example 6 with OpBGP

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());
}
Also used : Triple(org.apache.jena.graph.Triple) Op(org.apache.jena.sparql.algebra.Op) 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) Test(org.junit.Test)

Example 7 with OpBGP

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;
}
Also used : Triple(org.apache.jena.graph.Triple) HashMap(java.util.HashMap) OpBGP(org.apache.jena.sparql.algebra.op.OpBGP) ArrayList(java.util.ArrayList) BasicPattern(org.apache.jena.sparql.core.BasicPattern)

Example 8 with OpBGP

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);
}
Also used : OpBGP(org.apache.jena.sparql.algebra.op.OpBGP) BasicPattern(org.apache.jena.sparql.core.BasicPattern)

Example 9 with OpBGP

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;
}
Also used : Triple(org.apache.jena.graph.Triple) Op(org.apache.jena.sparql.algebra.Op) Var(org.apache.jena.sparql.core.Var) Node(org.apache.jena.graph.Node) OpBGP(org.apache.jena.sparql.algebra.op.OpBGP) BasicPattern(org.apache.jena.sparql.core.BasicPattern)

Example 10 with OpBGP

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;
}
Also used : Op(org.apache.jena.sparql.algebra.Op) OpSequence(org.apache.jena.sparql.algebra.op.OpSequence) OpBGP(org.apache.jena.sparql.algebra.op.OpBGP) BasicPattern(org.apache.jena.sparql.core.BasicPattern)

Aggregations

OpBGP (org.apache.jena.sparql.algebra.op.OpBGP)21 BasicPattern (org.apache.jena.sparql.core.BasicPattern)19 Op (org.apache.jena.sparql.algebra.Op)11 Node (org.apache.jena.graph.Node)10 Triple (org.apache.jena.graph.Triple)9 Test (org.junit.Test)9 SocketException (java.net.SocketException)5 OpService (org.apache.jena.sparql.algebra.op.OpService)5 QueryExceptionHTTP (org.apache.jena.sparql.engine.http.QueryExceptionHTTP)5 Context (org.apache.jena.sparql.util.Context)5 Var (org.apache.jena.sparql.core.Var)4 ArrayList (java.util.ArrayList)3 ConnectTimeoutException (org.apache.http.conn.ConnectTimeoutException)3 Query (org.apache.jena.query.Query)3 QueryIterator (org.apache.jena.sparql.engine.QueryIterator)3 Binding (org.apache.jena.sparql.engine.binding.Binding)3 Expr (org.apache.jena.sparql.expr.Expr)3 ExprVar (org.apache.jena.sparql.expr.ExprVar)3 UnknownHostException (java.net.UnknownHostException)2 HttpConnectTimeoutException (java.net.http.HttpConnectTimeoutException)2