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