Search in sources :

Example 1 with OpSequence

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

Op (org.apache.jena.sparql.algebra.Op)1 OpBGP (org.apache.jena.sparql.algebra.op.OpBGP)1 OpSequence (org.apache.jena.sparql.algebra.op.OpSequence)1 BasicPattern (org.apache.jena.sparql.core.BasicPattern)1