Search in sources :

Example 6 with BasicPattern

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

Example 7 with BasicPattern

use of org.apache.jena.sparql.core.BasicPattern in project jena by apache.

the class PropertyFunctionGenerator method makeStages.

private static Op makeStages(BasicPattern triples, Map<Triple, PropertyFunctionInstance> pfInvocations) {
    // Step 3 : Make the operation expression.
    //   For each property function, insert the implementation 
    //   For each block of non-property function triples, make a BGP.
    Op op = null;
    BasicPattern pattern = null;
    for (Triple t : triples) {
        if (pfInvocations.containsKey(t)) {
            op = flush(pattern, op);
            pattern = null;
            PropertyFunctionInstance pfi = pfInvocations.get(t);
            OpPropFunc opPF = new OpPropFunc(t.getPredicate(), pfi.getSubjectArgList(), pfi.getObjectArgList(), op);
            op = opPF;
            continue;
        }
        // Regular triples - make sure there is a basic pattern in progress. 
        if (pattern == null)
            pattern = new BasicPattern();
        pattern.add(t);
    }
    op = flush(pattern, op);
    return op;
}
Also used : Triple(org.apache.jena.graph.Triple) BasicPattern(org.apache.jena.sparql.core.BasicPattern) OpPropFunc(org.apache.jena.sparql.algebra.op.OpPropFunc)

Example 8 with BasicPattern

use of org.apache.jena.sparql.core.BasicPattern in project jena by apache.

the class OpQuad method asQuadPattern.

public OpQuadPattern asQuadPattern() {
    if (opQuadPattern == null) {
        BasicPattern bp = new BasicPattern();
        bp.add(getQuad().asTriple());
        opQuadPattern = new OpQuadPattern(quad.getGraph(), bp);
    }
    return opQuadPattern;
}
Also used : BasicPattern(org.apache.jena.sparql.core.BasicPattern)

Example 9 with BasicPattern

use of org.apache.jena.sparql.core.BasicPattern in project jena by apache.

the class OpQuadBlock method convertOp.

/** Convenience - convert to OpQuadPatterns which are more widely used (currently?) */
public Op convertOp() {
    if (quads.size() == 0)
        return OpTable.empty();
    if (quads.size() == 1) {
        Quad q = quads.get(0);
        BasicPattern bgp = new BasicPattern();
        bgp.add(q.asTriple());
        return new OpQuadPattern(q.getGraph(), bgp);
    }
    List<OpQuadPattern> x = convert();
    OpSequence ops = OpSequence.create();
    for (OpQuadPattern oqp : x) ops.add(oqp);
    return ops;
}
Also used : Quad(org.apache.jena.sparql.core.Quad) BasicPattern(org.apache.jena.sparql.core.BasicPattern)

Example 10 with BasicPattern

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

Aggregations

BasicPattern (org.apache.jena.sparql.core.BasicPattern)44 Node (org.apache.jena.graph.Node)15 Triple (org.apache.jena.graph.Triple)13 OpBGP (org.apache.jena.sparql.algebra.op.OpBGP)13 Op (org.apache.jena.sparql.algebra.Op)11 Test (org.junit.Test)11 Var (org.apache.jena.sparql.core.Var)5 QueryIterator (org.apache.jena.sparql.engine.QueryIterator)5 Binding (org.apache.jena.sparql.engine.binding.Binding)5 ArrayList (java.util.ArrayList)4 BaseTest (org.apache.jena.atlas.junit.BaseTest)4 OpQuadPattern (org.apache.jena.sparql.algebra.op.OpQuadPattern)4 Quad (org.apache.jena.sparql.core.Quad)4 SocketException (java.net.SocketException)3 HashMap (java.util.HashMap)3 ConnectTimeoutException (org.apache.http.conn.ConnectTimeoutException)3 OpService (org.apache.jena.sparql.algebra.op.OpService)3 QueryExceptionHTTP (org.apache.jena.sparql.engine.http.QueryExceptionHTTP)3 Context (org.apache.jena.sparql.util.Context)3 IndentedLineBuffer (org.apache.jena.atlas.io.IndentedLineBuffer)2