Minor modifications to the - still broken - test

Change-Id: I6cf1c785e21fe8b037e7a779d6fd79125c89a537
diff --git a/src/main/java/de/ids_mannheim/korap/query/spans/WithinSpans.java b/src/main/java/de/ids_mannheim/korap/query/spans/WithinSpans.java
index 2afcc42..539c331 100644
--- a/src/main/java/de/ids_mannheim/korap/query/spans/WithinSpans.java
+++ b/src/main/java/de/ids_mannheim/korap/query/spans/WithinSpans.java
@@ -24,6 +24,8 @@
  *
  * TODO: Support exclusivity
  * TODO: Use the term "queue" and implement it similar to SpanOrQuery
+ * TODO: Implement a incrStartPos() method to forward an embedded span
+ *       until the start position is higher than the current start position.
  */
 
 /**
@@ -282,13 +284,14 @@
                     this.more = true;
                     this.inSameDoc = true;
                     this.tryMatch = true;
-
+                    
                     this.nextSpanB();
                     continue;
                 }
 
                 // Fetch from second store?
                 else {
+
                     /** TODO: Change this to a single embedded object! */
                     this.embeddedStart = current.start;
                     this.embeddedEnd = current.end;
@@ -658,6 +661,8 @@
         this.wrapEnd = -1;
 
         // Shortcut to prevent lazyloading of .end()
+        //   [---
+        // [---
         if (this.wrapStart > this.embeddedStart) {
             // Can't match for in, rin, ew, sw, and m
             // and will always lead to next_b
@@ -669,6 +674,8 @@
             };
         }
 
+        // [---
+        //   [---
         else if (this.wrapStart < this.embeddedStart) {
             // Can't match for sw and m and will always
             // lead to next_a
@@ -973,6 +980,7 @@
 
             // Load wrapEnd
             this.wrapEnd = this.wrapSpans.end();
+            // this.embeddedEnd = this.embeddedSpans.end();
 
             // Case 6
             // |---|
@@ -1138,7 +1146,6 @@
 
         public short elementRef = -1;
 
-
         public void clear () {
             this.start = -1;
             this.end = -1;
diff --git a/src/main/java/de/ids_mannheim/korap/query/wrap/SpanAlterQueryWrapper.java b/src/main/java/de/ids_mannheim/korap/query/wrap/SpanAlterQueryWrapper.java
index 6503b3f..a7b55cf 100644
--- a/src/main/java/de/ids_mannheim/korap/query/wrap/SpanAlterQueryWrapper.java
+++ b/src/main/java/de/ids_mannheim/korap/query/wrap/SpanAlterQueryWrapper.java
@@ -103,22 +103,39 @@
     };
 
 
+    /*
+     * The query is extended to right in case one alternative is extended to the right.
+     */
     @Override
-    public SpanQuery toQuery () throws QueryException {
+    public boolean isExtendedToTheRight () {
+        if (this.alternatives.size() == 0)
+            return this.alternatives.get(0).isExtendedToTheRight();
+        Iterator<SpanQueryWrapper> clause = this.alternatives.iterator();
+        while (clause.hasNext()) {
+            if (clause.next().isExtendedToTheRight()) {
+                return true;
+            };
+        };
+        return false;
+    };
+
+
+    @Override
+    public SpanQuery toFragmentQuery () throws QueryException {
         if (this.isNull || this.alternatives.size() == 0)
             return (SpanQuery) null;
 
         if (this.alternatives.size() == 1) {
             return (SpanQuery) this.alternatives.get(0)
-                    .retrieveNode(this.retrieveNode).toQuery();
+                    .retrieveNode(this.retrieveNode).toFragmentQuery();
         };
 
         Iterator<SpanQueryWrapper> clause = this.alternatives.iterator();
         SpanOrQuery soquery = new SpanOrQuery(clause.next()
-                .retrieveNode(this.retrieveNode).toQuery());
+                .retrieveNode(this.retrieveNode).toFragmentQuery());
         while (clause.hasNext()) {
             soquery.addClause(clause.next().retrieveNode(this.retrieveNode)
-                    .toQuery());
+                    .toFragmentQuery());
         };
         return (SpanQuery) soquery;
     };
diff --git a/src/main/java/de/ids_mannheim/korap/query/wrap/SpanAttributeQueryWrapper.java b/src/main/java/de/ids_mannheim/korap/query/wrap/SpanAttributeQueryWrapper.java
index 968d544..5f11c02 100644
--- a/src/main/java/de/ids_mannheim/korap/query/wrap/SpanAttributeQueryWrapper.java
+++ b/src/main/java/de/ids_mannheim/korap/query/wrap/SpanAttributeQueryWrapper.java
@@ -37,11 +37,11 @@
 
 
     @Override
-    public SpanQuery toQuery () throws QueryException {
+    public SpanQuery toFragmentQuery () throws QueryException {
         if (isNull || isEmpty)
             return null;
 
-        SpanQuery sq = subquery.retrieveNode(this.retrieveNode).toQuery();
+        SpanQuery sq = subquery.retrieveNode(this.retrieveNode).toFragmentQuery();
         if (sq == null) {
             isNull = true;
             return null;
diff --git a/src/main/java/de/ids_mannheim/korap/query/wrap/SpanClassQueryWrapper.java b/src/main/java/de/ids_mannheim/korap/query/wrap/SpanClassQueryWrapper.java
index 05edf76..51d4ab7 100644
--- a/src/main/java/de/ids_mannheim/korap/query/wrap/SpanClassQueryWrapper.java
+++ b/src/main/java/de/ids_mannheim/korap/query/wrap/SpanClassQueryWrapper.java
@@ -66,12 +66,12 @@
     };
 
 
-    public SpanQuery toQuery () throws QueryException {
+    public SpanQuery toFragmentQuery () throws QueryException {
         if (this.subquery.isNull())
             return (SpanQuery) null;
 
         SpanQuery sq = (SpanQuery) this.subquery
-                .retrieveNode(this.retrieveNode).toQuery();
+                .retrieveNode(this.retrieveNode).toFragmentQuery();
 
         if (sq == null)
             return (SpanQuery) null;
diff --git a/src/main/java/de/ids_mannheim/korap/query/wrap/SpanElementQueryWrapper.java b/src/main/java/de/ids_mannheim/korap/query/wrap/SpanElementQueryWrapper.java
index bb9ddc2..3112dc2 100644
--- a/src/main/java/de/ids_mannheim/korap/query/wrap/SpanElementQueryWrapper.java
+++ b/src/main/java/de/ids_mannheim/korap/query/wrap/SpanElementQueryWrapper.java
@@ -26,7 +26,7 @@
 
 
     @Override
-    public SpanQuery toQuery () throws QueryException {
+    public SpanQuery toFragmentQuery () throws QueryException {
         // Todo: Respect request for retrieving node data (i.e. depth information)
         return (SpanQuery) new SpanElementQuery(this.field, this.element);
     };
diff --git a/src/main/java/de/ids_mannheim/korap/query/wrap/SpanFocusQueryWrapper.java b/src/main/java/de/ids_mannheim/korap/query/wrap/SpanFocusQueryWrapper.java
index adcce08..8fadb3e 100644
--- a/src/main/java/de/ids_mannheim/korap/query/wrap/SpanFocusQueryWrapper.java
+++ b/src/main/java/de/ids_mannheim/korap/query/wrap/SpanFocusQueryWrapper.java
@@ -49,11 +49,11 @@
     };
 
 
-    public SpanQuery toQuery () throws QueryException {
+    public SpanQuery toFragmentQuery () throws QueryException {
         if (this.subquery.isNull())
             return (SpanQuery) null;
         return new SpanFocusQuery(this.subquery.retrieveNode(this.retrieveNode)
-                .toQuery(), this.number);
+                .toFragmentQuery(), this.number);
     };
 
 
diff --git a/src/main/java/de/ids_mannheim/korap/query/wrap/SpanQueryWrapper.java b/src/main/java/de/ids_mannheim/korap/query/wrap/SpanQueryWrapper.java
index 987d21a..2cb72fc 100644
--- a/src/main/java/de/ids_mannheim/korap/query/wrap/SpanQueryWrapper.java
+++ b/src/main/java/de/ids_mannheim/korap/query/wrap/SpanQueryWrapper.java
@@ -1,8 +1,10 @@
 package de.ids_mannheim.korap.query.wrap;
 
 import org.apache.lucene.search.spans.SpanQuery;
-
 import de.ids_mannheim.korap.util.QueryException;
+import de.ids_mannheim.korap.query.SpanFocusQuery;
+import de.ids_mannheim.korap.query.SpanClassQuery;
+import de.ids_mannheim.korap.query.SpanWithinQuery;
 
 // TODO: Add warnings and errors - using KrillQuery
 
@@ -43,11 +45,37 @@
      * @return A {@link SpanQuery} object.
      * @throws QueryException
      */
-    public SpanQuery toQuery () throws QueryException {
+    public SpanQuery toFragmentQuery () throws QueryException {
+        System.err.println("||||||||||||||||||||||||||");
         return (SpanQuery) null;
     };
 
 
+
+    /**
+     * Serialize the wrapped query and return a SpanQuery.
+     * This will be the final query and may be rewritten.
+     * 
+     * @return A {@link SpanQuery} object.
+     * @throws QueryException
+     */
+    public SpanQuery toQuery () throws QueryException {
+
+        if (this.isNull() || this.isEmpty())
+            return null;
+
+        // Wrap the query in a <base/s=t>, if it's extended to the right
+        if (this.isExtendedToTheRight()) {
+            return new SpanFocusQuery(
+                new SpanWithinQuery(
+                    "base/s:t",
+                    new SpanClassQuery(this.toFragmentQuery(), (byte) 254)), (byte) 254
+            );
+        };
+
+        return this.toFragmentQuery();
+    };
+
     /**
      * Boolean value indicating that the wrapped query
      * is optional.
@@ -149,6 +177,13 @@
     };
 
 
+    public SpanQueryWrapper isExtended (boolean extended) {
+        this.isExtended = extended;
+        return this;
+    };
+
+
+
     /**
      * Boolean value indicating that the wrapped query
      * is extended by a subquery to the right.
@@ -172,6 +207,12 @@
     };
 
 
+    public SpanQueryWrapper isExtendedToTheRight (boolean extended) {
+        this.isExtendedToTheRight = extended;
+        return this;
+    };
+
+
     /**
      * Check, if the wrapped query can be used as an
      * anchor query in a sequence, i.e. a query that
diff --git a/src/main/java/de/ids_mannheim/korap/query/wrap/SpanReferenceQueryWrapper.java b/src/main/java/de/ids_mannheim/korap/query/wrap/SpanReferenceQueryWrapper.java
index 8b4c54d..48ba7c2 100644
--- a/src/main/java/de/ids_mannheim/korap/query/wrap/SpanReferenceQueryWrapper.java
+++ b/src/main/java/de/ids_mannheim/korap/query/wrap/SpanReferenceQueryWrapper.java
@@ -34,13 +34,13 @@
 
 
     @Override
-    public SpanQuery toQuery () throws QueryException {
+    public SpanQuery toFragmentQuery () throws QueryException {
 
         if (this.isNull() || this.isEmpty()) {
             return null;
         }
 
-        SpanQuery sq = subQuery.retrieveNode(this.retrieveNode).toQuery();
+        SpanQuery sq = subQuery.retrieveNode(this.retrieveNode).toFragmentQuery();
         if (sq == null)
             return null;
 
diff --git a/src/main/java/de/ids_mannheim/korap/query/wrap/SpanRegexQueryWrapper.java b/src/main/java/de/ids_mannheim/korap/query/wrap/SpanRegexQueryWrapper.java
index 24317ad..30ea49b 100644
--- a/src/main/java/de/ids_mannheim/korap/query/wrap/SpanRegexQueryWrapper.java
+++ b/src/main/java/de/ids_mannheim/korap/query/wrap/SpanRegexQueryWrapper.java
@@ -47,7 +47,7 @@
     };
 
 
-    public SpanQuery toQuery () {
+    public SpanQuery toFragmentQuery () {
         return this.query;
     };
 
diff --git a/src/main/java/de/ids_mannheim/korap/query/wrap/SpanRelationWrapper.java b/src/main/java/de/ids_mannheim/korap/query/wrap/SpanRelationWrapper.java
index 992787c..3eea9c8 100644
--- a/src/main/java/de/ids_mannheim/korap/query/wrap/SpanRelationWrapper.java
+++ b/src/main/java/de/ids_mannheim/korap/query/wrap/SpanRelationWrapper.java
@@ -37,14 +37,14 @@
 
 
     @Override
-    public SpanQuery toQuery () throws QueryException {
+    public SpanQuery toFragmentQuery () throws QueryException {
 
         if (this.isNull() || this.isEmpty()) {
             return null;
         }
 
         SpanTermQuery relationTermQuery = (SpanTermQuery) relationQuery
-                .retrieveNode(this.retrieveNode).toQuery();
+                .retrieveNode(this.retrieveNode).toFragmentQuery();
         if (relationTermQuery == null)
             return null;
 
@@ -54,7 +54,7 @@
         if (subQuery1.isEmpty) {
             if (!subQuery2.isEmpty) {
                 // match target
-                subq2 = subQuery2.retrieveNode(this.retrieveNode).toQuery();
+                subq2 = subQuery2.retrieveNode(this.retrieveNode).toFragmentQuery();
                 if (subQuery1.hasClass) {
                     rq.setSourceClass(subQuery1.getClassNumber());
                 }
@@ -65,7 +65,7 @@
         else if (subQuery2.isEmpty) {
             if (!subQuery1.isEmpty) {
                 // match source
-                subq1 = subQuery1.retrieveNode(this.retrieveNode).toQuery();
+                subq1 = subQuery1.retrieveNode(this.retrieveNode).toFragmentQuery();
                 if (subQuery2.hasClass) {
                     rq.setTargetClass(subQuery2.getClassNumber());
                 }
@@ -74,8 +74,8 @@
         }
         else {
             // match both
-            subq1 = subQuery1.retrieveNode(this.retrieveNode).toQuery();
-            subq2 = subQuery2.retrieveNode(this.retrieveNode).toQuery();
+            subq1 = subQuery1.retrieveNode(this.retrieveNode).toFragmentQuery();
+            subq2 = subQuery2.retrieveNode(this.retrieveNode).toFragmentQuery();
             return new SpanRelationMatchQuery(rq, subq1, subq2, true);
         }
 
diff --git a/src/main/java/de/ids_mannheim/korap/query/wrap/SpanRepetitionQueryWrapper.java b/src/main/java/de/ids_mannheim/korap/query/wrap/SpanRepetitionQueryWrapper.java
index 3f78fab..dbbdd67 100644
--- a/src/main/java/de/ids_mannheim/korap/query/wrap/SpanRepetitionQueryWrapper.java
+++ b/src/main/java/de/ids_mannheim/korap/query/wrap/SpanRepetitionQueryWrapper.java
@@ -83,7 +83,8 @@
 
 
     // Serialize to Lucene SpanQuery
-    public SpanQuery toQuery () throws QueryException {
+    @Override
+    public SpanQuery toFragmentQuery () throws QueryException {
 
         // The query is null
         if (this.isNull)
@@ -96,11 +97,11 @@
 
         // The query is not a repetition query at all, but may be optional
         if (this.min == 1 && this.max == 1)
-            return this.subquery.retrieveNode(this.retrieveNode).toQuery();
+            return this.subquery.retrieveNode(this.retrieveNode).toFragmentQuery();
 
         // That's a fine repetition query
         return new SpanRepetitionQuery(this.subquery.retrieveNode(
-                this.retrieveNode).toQuery(), this.min, this.max, true);
+                this.retrieveNode).toFragmentQuery(), this.min, this.max, true);
     };
 
 
diff --git a/src/main/java/de/ids_mannheim/korap/query/wrap/SpanSegmentQueryWrapper.java b/src/main/java/de/ids_mannheim/korap/query/wrap/SpanSegmentQueryWrapper.java
index f89c5b7..1b1929b 100644
--- a/src/main/java/de/ids_mannheim/korap/query/wrap/SpanSegmentQueryWrapper.java
+++ b/src/main/java/de/ids_mannheim/korap/query/wrap/SpanSegmentQueryWrapper.java
@@ -195,7 +195,7 @@
     };
 
 
-    public SpanQuery toQuery () throws QueryException {
+    public SpanQuery toFragmentQuery () throws QueryException {
         if (this.isNull || (this.inclusive.size() + this.exclusive.size() == 0)) {
             return (SpanQuery) null;
         }
@@ -219,10 +219,10 @@
 
     private SpanQuery _listToQuery (ArrayList<SpanQueryWrapper> list)
             throws QueryException {
-        SpanQuery query = list.get(0).toQuery();
+        SpanQuery query = list.get(0).toFragmentQuery();
 
         for (int i = 1; i < list.size(); i++) {
-            query = new SpanSegmentQuery(query, list.get(i).toQuery());
+            query = new SpanSegmentQuery(query, list.get(i).toFragmentQuery());
         };
 
         return (SpanQuery) query;
@@ -232,13 +232,13 @@
     private SpanQuery _listToOrQuery (ArrayList<SpanQueryWrapper> list)
             throws QueryException {
         if (list.size() == 1) {
-            return (SpanQuery) list.get(0).toQuery();
+            return (SpanQuery) list.get(0).toFragmentQuery();
         };
 
         Iterator<SpanQueryWrapper> clause = list.iterator();
-        SpanOrQuery soquery = new SpanOrQuery(clause.next().toQuery());
+        SpanOrQuery soquery = new SpanOrQuery(clause.next().toFragmentQuery());
         while (clause.hasNext()) {
-            soquery.addClause(clause.next().toQuery());
+            soquery.addClause(clause.next().toFragmentQuery());
         };
         return (SpanQuery) soquery;
     };
diff --git a/src/main/java/de/ids_mannheim/korap/query/wrap/SpanSequenceQueryWrapper.java b/src/main/java/de/ids_mannheim/korap/query/wrap/SpanSequenceQueryWrapper.java
index 2ef20f0..eadba22 100644
--- a/src/main/java/de/ids_mannheim/korap/query/wrap/SpanSequenceQueryWrapper.java
+++ b/src/main/java/de/ids_mannheim/korap/query/wrap/SpanSequenceQueryWrapper.java
@@ -19,7 +19,6 @@
 /*
   TODO: Make isNegative work!
   TODO: Make isEmpty work!
-  TODO: Make isExtendedToTheRight work!
   TODO: Evaluate if spanNext(spanNext(a,b),spanNext(c,d)) is faster
         than spanNext(spanNext(spanNext(a,b),c),d)
   TODO: Improve support for SpanElementQueryWrapper in constraints!
@@ -135,7 +134,7 @@
         if (DEBUG) {
             if (!sswq.isEmpty()) {
                 try {
-                    log.trace("New span sequence {}", sswq.toQuery().toString());
+                    log.trace("New span sequence {}", sswq.toFragmentQuery().toString());
                 }
                 catch (QueryException qe) {
                     log.trace("Unable to serialize query {}", qe.getMessage());
@@ -547,7 +546,7 @@
         // Todo: This should possibly be evaluated to a query later on!
         try {
             this.constraints.add(new DistanceConstraint((SpanElementQuery) unit
-                    .retrieveNode(this.retrieveNode).toQuery(), min, max,
+                    .retrieveNode(this.retrieveNode).toFragmentQuery(), min, max,
                     isInOrder, exclusion));
         }
         catch (QueryException qe) {
@@ -638,11 +637,18 @@
         return super.isNegative();
     };
 
-
     public boolean isExtendedToTheRight () {
         if (!this.isSolved)
             _solveProblematicSequence();
-        return this.isExtendedToTheRight;
+
+        int size = this.segments.size();
+
+        // Nothing to do
+        if (size == 0 || this.isNull())
+            return false;
+
+        // Return the value of the last segment
+        return this.segments.get(size - 1).isExtendedToTheRight();
     };
 
 
@@ -652,7 +658,7 @@
      * @return A {@link SpanQuery} object.
      * @throws QueryException
      */
-    public SpanQuery toQuery () throws QueryException {
+    public SpanQuery toFragmentQuery () throws QueryException {
 
         // There was a serialization failure not yet reported
         if (this.constraintException != null)
@@ -676,7 +682,7 @@
             // Unproblematic single query
             if (this.segments.get(0).maybeAnchor())
                 return (SpanQuery) this.segments.get(0)
-                        .retrieveNode(this.retrieveNode).toQuery();
+                        .retrieveNode(this.retrieveNode).toFragmentQuery();
 
             if (this.segments.get(0).isEmpty())
                 throw new QueryException(613,
@@ -708,11 +714,11 @@
         };
 
         // Create the initial query
-        SpanQuery query = null;// = this.segments.get(0).toQuery();
+        SpanQuery query = null;
         int i = 0;
         while (query == null && i < this.segments.size()) {
             query = this.segments.get(i).retrieveNode(this.retrieveNode)
-                    .toQuery();
+                    .toFragmentQuery();
             i++;
         };
 
@@ -724,7 +730,7 @@
             for (; i < this.segments.size(); i++) {
 
                 SpanQuery second = this.segments.get(i)
-                        .retrieveNode(this.retrieveNode).toQuery();
+                        .retrieveNode(this.retrieveNode).toFragmentQuery();
                 if (second == null)
                     continue;
 
@@ -746,7 +752,7 @@
                         throw new QueryException(613, limitationError);
 
                     SpanQuery sq = (SpanQuery) this.segments.get(i)
-                            .retrieveNode(this.retrieveNode).toQuery();
+                            .retrieveNode(this.retrieveNode).toFragmentQuery();
                     if (sq == null)
                         continue;
 
@@ -765,7 +771,7 @@
                         throw new QueryException(613, limitationError);
 
                     SpanQuery sq = (SpanQuery) this.segments.get(i)
-                            .retrieveNode(this.retrieveNode).toQuery();
+                            .retrieveNode(this.retrieveNode).toFragmentQuery();
                     if (sq == null)
                         continue;
 
@@ -786,7 +792,7 @@
                 throw new QueryException(613, limitationError);
 
             SpanQuery sq = (SpanQuery) this.segments.get(i)
-                    .retrieveNode(this.retrieveNode).toQuery();
+                    .retrieveNode(this.retrieveNode).toFragmentQuery();
             if (sq == null)
                 continue;
 
@@ -797,7 +803,6 @@
     };
 
 
-
     /*
       Check if there are problematic segments in the sequence
       (either negative, optional or empty) and deal with them
@@ -938,11 +943,17 @@
                         problem.getClassNumber());
 
             query = new SpanExpansionQuery(anchor.retrieveNode(
-                                                               this.retrieveNode).toQuery(), problem.isOptional() ? 0 : problem.getMin(),
+                                                               this.retrieveNode).toFragmentQuery(), problem.isOptional() ? 0 : problem.getMin(),
                     problem.getMax(), direction,
                     problem.hasClass() ? problem.getClassNumber() : (byte) 0,
                     true);
-            return new SpanSimpleQueryWrapper(query).isExtended(true);
+            SpanQueryWrapper sqw = new SpanSimpleQueryWrapper(query).isExtended(true);
+
+            // Set right extension
+            if (direction >= 0)
+                sqw.isExtendedToTheRight(true);
+
+            return sqw;
         }
 
         // make negative extension to anchor
@@ -955,12 +966,19 @@
                         problem.getClassNumber());
 
             query = new SpanExpansionQuery(anchor.retrieveNode(
-                    this.retrieveNode).toQuery(), problem.retrieveNode(
-                    this.retrieveNode).toQuery(), problem.getMin(),
+                    this.retrieveNode).toFragmentQuery(), problem.retrieveNode(
+                    this.retrieveNode).toFragmentQuery(), problem.getMin(),
                     problem.getMax(), direction,
                     problem.hasClass() ? problem.getClassNumber() : (byte) 0,
                     true);
-            return new SpanSimpleQueryWrapper(query).isExtended(true);
+
+            SpanQueryWrapper sqw = new SpanSimpleQueryWrapper(query).isExtended(true);
+
+            // Set right extension
+            if (direction >= 0)
+                sqw.isExtendedToTheRight(true);
+
+            return sqw;
         };
 
         if (DEBUG)
@@ -979,11 +997,11 @@
         // [base=der][base=baum]
         if (mergeLeft)
             ssqw.append(new SpanSimpleQueryWrapper(problem.retrieveNode(
-                    this.retrieveNode).toQuery()));
+                    this.retrieveNode).toFragmentQuery()));
         // [base=baum][base=der]
         else
             ssqw.prepend(new SpanSimpleQueryWrapper(problem.retrieveNode(
-                    this.retrieveNode).toQuery()));
+                    this.retrieveNode).toFragmentQuery()));
 
         saqw.or(ssqw);
 
diff --git a/src/main/java/de/ids_mannheim/korap/query/wrap/SpanSimpleQueryWrapper.java b/src/main/java/de/ids_mannheim/korap/query/wrap/SpanSimpleQueryWrapper.java
index 28edbf6..61608e5 100644
--- a/src/main/java/de/ids_mannheim/korap/query/wrap/SpanSimpleQueryWrapper.java
+++ b/src/main/java/de/ids_mannheim/korap/query/wrap/SpanSimpleQueryWrapper.java
@@ -26,13 +26,8 @@
     };
 
 
-    public SpanQuery toQuery () {
+    @Override
+    public SpanQuery toFragmentQuery () {
         return this.query;
     };
-
-
-    public SpanSimpleQueryWrapper isExtended (boolean extended) {
-        this.isExtended = true;
-        return this;
-    };
 };
diff --git a/src/main/java/de/ids_mannheim/korap/query/wrap/SpanSubspanQueryWrapper.java b/src/main/java/de/ids_mannheim/korap/query/wrap/SpanSubspanQueryWrapper.java
index ec61e70..8c809c5 100644
--- a/src/main/java/de/ids_mannheim/korap/query/wrap/SpanSubspanQueryWrapper.java
+++ b/src/main/java/de/ids_mannheim/korap/query/wrap/SpanSubspanQueryWrapper.java
@@ -105,12 +105,12 @@
 
 
     @Override
-    public SpanQuery toQuery () throws QueryException {
+    public SpanQuery toFragmentQuery () throws QueryException {
         if (this.isNull()) {
             return null;
         }
 
-        SpanQuery sq = subquery.retrieveNode(this.retrieveNode).toQuery();
+        SpanQuery sq = subquery.retrieveNode(this.retrieveNode).toFragmentQuery();
         if (sq == null)
             return null;
         if (sq instanceof SpanTermQuery) {
diff --git a/src/main/java/de/ids_mannheim/korap/query/wrap/SpanWildcardQueryWrapper.java b/src/main/java/de/ids_mannheim/korap/query/wrap/SpanWildcardQueryWrapper.java
index 6eb6bdf..5ac346e 100644
--- a/src/main/java/de/ids_mannheim/korap/query/wrap/SpanWildcardQueryWrapper.java
+++ b/src/main/java/de/ids_mannheim/korap/query/wrap/SpanWildcardQueryWrapper.java
@@ -30,7 +30,7 @@
     };
 
 
-    public SpanQuery toQuery () {
+    public SpanQuery toFragmentQuery () {
         return this.query;
     };
 
diff --git a/src/main/java/de/ids_mannheim/korap/query/wrap/SpanWithAttributeQueryWrapper.java b/src/main/java/de/ids_mannheim/korap/query/wrap/SpanWithAttributeQueryWrapper.java
index b51557d..435b3ee 100644
--- a/src/main/java/de/ids_mannheim/korap/query/wrap/SpanWithAttributeQueryWrapper.java
+++ b/src/main/java/de/ids_mannheim/korap/query/wrap/SpanWithAttributeQueryWrapper.java
@@ -118,7 +118,7 @@
 
 
     @Override
-    public SpanQuery toQuery () throws QueryException {
+    public SpanQuery toFragmentQuery () throws QueryException {
 
         if (isNull || isEmpty)
             return null;
@@ -134,7 +134,7 @@
     private SpanQuery createSpecificSpanWithAttributeQuery ()
             throws QueryException {
         SimpleSpanQuery withIdQuery = (SimpleSpanQuery) withIdQueryWrapper
-                .toQuery();
+                .toFragmentQuery();
         if (withIdQuery == null) {
             isNull = true;
             return null;
@@ -169,7 +169,7 @@
 
     private SpanAttributeQuery createSpanAttributeQuery (
             SpanQueryWrapper attrQueryWrapper) throws QueryException {
-        SpanQuery sq = attrQueryWrapper.toQuery();
+        SpanQuery sq = attrQueryWrapper.toFragmentQuery();
         if (sq != null) {
             if (sq instanceof SpanAttributeQuery)
                 return (SpanAttributeQuery) sq;
diff --git a/src/main/java/de/ids_mannheim/korap/query/wrap/SpanWithinQueryWrapper.java b/src/main/java/de/ids_mannheim/korap/query/wrap/SpanWithinQueryWrapper.java
index 904d771..89d1fc5 100644
--- a/src/main/java/de/ids_mannheim/korap/query/wrap/SpanWithinQueryWrapper.java
+++ b/src/main/java/de/ids_mannheim/korap/query/wrap/SpanWithinQueryWrapper.java
@@ -88,16 +88,16 @@
             this.isNull = false;
     };
 
-
-    public SpanQuery toQuery () throws QueryException {
+    @Override
+    public SpanQuery toFragmentQuery () throws QueryException {
         if (this.isNull)
             return (SpanQuery) null;
 
         // TODO: if (wrap.isNegative())
 
         return new SpanWithinQuery(this.element.retrieveNode(this.retrieveNode)
-                .toQuery(),
-                this.wrap.retrieveNode(this.retrieveNode).toQuery(), this.flag);
+                .toFragmentQuery(),
+                this.wrap.retrieveNode(this.retrieveNode).toFragmentQuery(), this.flag);
     };