You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@uima.apache.org by re...@apache.org on 2020/10/10 22:32:02 UTC

[uima-uimaj] 01/01: [UIMA-6151] Containment/overlap predicates for AnnotationFS

This is an automated email from the ASF dual-hosted git repository.

rec pushed a commit to branch UIMA-6151-Containment-and-overlap-predicates-for-AnnotationFS
in repository https://gitbox.apache.org/repos/asf/uima-uimaj.git

commit 0797fa50de4c15eefd37c1b52cb6b2251c368e45
Author: Richard Eckart de Castilho <re...@apache.org>
AuthorDate: Sun Oct 11 00:32:29 2020 +0200

    [UIMA-6151] Containment/overlap predicates for AnnotationFS
    
    - Added utility class with predicates for overlap/containment/etc of annotations
    - Added convenience access to these methods to the AnnotationFS interface
    - Added unit tests
---
 .../org/apache/uima/cas/text/AnnotationFS.java     | 115 +++++-
 .../apache/uima/cas/text/AnnotationPredicates.java | 215 +++++++++++
 .../uima/cas/text/AnnotationPredicatesTest.java    | 410 +++++++++++++++++++++
 3 files changed, 737 insertions(+), 3 deletions(-)

diff --git a/uimaj-core/src/main/java/org/apache/uima/cas/text/AnnotationFS.java b/uimaj-core/src/main/java/org/apache/uima/cas/text/AnnotationFS.java
index 3e49c89..6623c05 100644
--- a/uimaj-core/src/main/java/org/apache/uima/cas/text/AnnotationFS.java
+++ b/uimaj-core/src/main/java/org/apache/uima/cas/text/AnnotationFS.java
@@ -16,7 +16,6 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-
 package org.apache.uima.cas.text;
 
 import java.util.function.IntPredicate;
@@ -25,8 +24,6 @@ import org.apache.uima.cas.AnnotationBaseFS;
 
 /**
  * Interface for Annotation Feature Structures.
- * 
- * 
  */
 public interface AnnotationFS extends AnnotationBaseFS {
 
@@ -91,4 +88,116 @@ public interface AnnotationFS extends AnnotationBaseFS {
    * @param aPredicate the predicate used to identify  whitespace
    */
   void trim(IntPredicate aPredicate);
+  
+  /**
+   * @see AnnotationPredicates#covers(AnnotationFS, AnnotationFS)
+   */
+  default boolean covers(int aBegin, int aEnd)
+  {
+    return AnnotationPredicates.covers(this, aBegin, aEnd);
+  }
+  
+  /**
+   * @see AnnotationPredicates#covers(AnnotationFS, AnnotationFS)
+   */
+  default boolean covers(AnnotationFS aOther)
+  {
+    return AnnotationPredicates.covers(this, aOther);
+  }
+  
+  /**
+   * @see AnnotationPredicates#coveredBy(AnnotationFS, AnnotationFS)
+   */
+  default boolean coveredBy(int aBegin, int aEnd)
+  {
+    return AnnotationPredicates.coveredBy(this, aBegin, aEnd);
+  }
+  
+  /**
+   * @see AnnotationPredicates#coveredBy(AnnotationFS, AnnotationFS)
+   */
+  default boolean coveredBy(AnnotationFS aOther)
+  {
+    return AnnotationPredicates.coveredBy(this, aOther);
+  }
+  
+  /**
+   * @see AnnotationPredicates#overlaps(AnnotationFS, AnnotationFS)
+   */
+  default boolean overlaps(int aBegin, int aEnd)
+  {
+    return AnnotationPredicates.overlaps(this, aBegin, aEnd);
+  }
+
+  /**
+   * @see AnnotationPredicates#overlaps(AnnotationFS, AnnotationFS)
+   */
+  default boolean overlaps(AnnotationFS aOther)
+  {
+    return AnnotationPredicates.overlaps(this, aOther);
+  }
+
+  /**
+   * @see AnnotationPredicates#overlapsLeft(AnnotationFS, AnnotationFS)
+   */
+  default boolean overlapsLeft(int aBegin, int aEnd)
+  {
+    return AnnotationPredicates.overlapsLeft(this, aBegin, aEnd);
+  }
+
+  /**
+   * @see AnnotationPredicates#overlapsLeft(AnnotationFS, AnnotationFS)
+   */
+  default boolean overlapsLeft(AnnotationFS aOther)
+  {
+    return AnnotationPredicates.overlapsLeft(this, aOther);
+  }
+
+  /**
+   * @see AnnotationPredicates#overlapsRight(AnnotationFS, AnnotationFS)
+   */
+  default boolean overlapsRight(int aBegin, int aEnd)
+  {
+    return AnnotationPredicates.overlapsRight(this, aBegin, aEnd);
+  }
+
+  /**
+   * @see AnnotationPredicates#overlapsRight(AnnotationFS, AnnotationFS)
+   */
+  default boolean overlapsRight(AnnotationFS aOther)
+  {
+    return AnnotationPredicates.overlapsRight(this, aOther);
+  }
+
+  /**
+   * @see AnnotationPredicates#rightOf(AnnotationFS, AnnotationFS)
+   */
+  default boolean rightOf(int aBegin, int aEnd)
+  {
+    return AnnotationPredicates.rightOf(this, aBegin, aEnd);
+  }
+
+  /**
+   * @see AnnotationPredicates#rightOf(AnnotationFS, AnnotationFS)
+   */
+  default boolean rightOf(AnnotationFS aOther)
+  {
+    return AnnotationPredicates.rightOf(this, aOther);
+  }
+
+  /**
+   * @see AnnotationPredicates#leftOf(AnnotationFS, AnnotationFS)
+   */
+  default boolean leftOf(int aBegin, int aEnd)
+  {
+    return AnnotationPredicates.leftOf(this, aBegin, aEnd);
+  }
+
+  /**
+   * @see AnnotationPredicates#leftOf(AnnotationFS, AnnotationFS)
+   */
+  default boolean leftOf(AnnotationFS aOther)
+  {
+    return AnnotationPredicates.leftOf(this, aOther);
+  }
 }
diff --git a/uimaj-core/src/main/java/org/apache/uima/cas/text/AnnotationPredicates.java b/uimaj-core/src/main/java/org/apache/uima/cas/text/AnnotationPredicates.java
new file mode 100644
index 0000000..ae630e5
--- /dev/null
+++ b/uimaj-core/src/main/java/org/apache/uima/cas/text/AnnotationPredicates.java
@@ -0,0 +1,215 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ * 
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.uima.cas.text;
+
+public final class AnnotationPredicates {
+  private AnnotationPredicates() {
+    // No instances
+  }
+  
+  public static boolean coveredBy(int aXBegin, int aXEnd, int aYBegin, int aYEnd) {
+    return aYBegin <= aXBegin && aXEnd <= aYEnd && (aXBegin == aYBegin || aXBegin != aYEnd);
+  }
+
+  public static boolean coveredBy(AnnotationFS aX, int aYBegin, int aYEnd) {
+    int xBegin = aX.getBegin();
+    return aYBegin <= xBegin && (xBegin == aYBegin || xBegin != aYEnd) && aX.getEnd() <= aYEnd;
+  }
+
+  /**
+   * Y is starting before or at the same position as A and ends after or at the same position as X.
+   * 
+   * @param aX
+   *          X
+   * @param aY
+   *          Y
+   * @return whether X is covered by Y.
+   */
+  public static boolean coveredBy(AnnotationFS aX, AnnotationFS aY) {
+    int xBegin = aX.getBegin();
+    int yBegin = aY.getBegin();
+    int yEnd = aY.getEnd();
+    return yBegin <= xBegin && (xBegin == yBegin || xBegin != yEnd) && aX.getEnd() <= yEnd;
+  }
+
+  public static boolean covers(int aXBegin, int aXEnd, int aYBegin, int aYEnd) {
+    return aXBegin <= aYBegin && aYEnd <= aXEnd && (aYBegin == aXBegin || aYBegin != aXEnd);
+  }
+
+  public static boolean covers(AnnotationFS aX, int aYBegin, int aYEnd) {
+    int xBegin = aX.getBegin();
+    int xEnd = aX.getEnd();
+    return xBegin <= aYBegin && aYEnd <= xEnd && (aYBegin == xBegin || aYBegin != xEnd);
+  }
+
+  /**
+   * X is starting before or at the same position as Y and ends after or at the same position as Y.
+   * 
+   * @param aX
+   *          X
+   * @param aY
+   *          Y
+   * @return whether X is covering Y.
+   */
+  public static boolean covers(AnnotationFS aX, AnnotationFS aY) {
+    int xBegin = aX.getBegin();
+    int xEnd = aX.getEnd();
+    int yBegin = aY.getBegin();
+    return xBegin <= yBegin && (yBegin == xBegin || yBegin != xEnd) && aY.getEnd() <= xEnd;
+  }
+
+  public static boolean colocated(int aXBegin, int aXEnd, int aYBegin, int aYEnd) {
+    return aXBegin == aYBegin && aXEnd == aYEnd;
+  }
+
+  public static boolean colocated(AnnotationFS aX, int aYBegin, int aYEnd) {
+    return aX.getBegin() == aYBegin && aX.getEnd() == aYEnd;
+  }
+
+  /**
+   * X starts and ends at the same position as Y.
+   * 
+   * @param aX
+   *          X
+   * @param aY
+   *          Y
+   * @return whether X is at the same location as Y.
+   */
+  public static boolean colocated(AnnotationFS aX, AnnotationFS aY) {
+    return aX.getBegin() == aY.getBegin() && aX.getEnd() == aY.getEnd();
+  }
+
+  public static boolean overlaps(int aXBegin, int aXEnd, int aYBegin, int aYEnd) {
+    return aYBegin == aXBegin || (aXBegin < aYEnd && aYBegin < aXEnd);
+  }
+
+  public static boolean overlaps(AnnotationFS aX, int aYBegin, int aYEnd) {
+    int xBegin = aX.getBegin();
+    return aYBegin == xBegin || (xBegin < aYEnd && aYBegin < aX.getEnd());
+  }
+
+  /**
+   * The intersection of the spans X and Y is non-empty. If either X or Y have a zero-width, then
+   * the intersection is considered to be non-empty if the begin of X is either within Y or the same
+   * as the begin of Y - and vice versa.
+   * 
+   * @param aX
+   *          X
+   * @param aY
+   *          Y
+   * @return whether X overlaps with Y in any way.
+   */
+  public static boolean overlaps(AnnotationFS aX, AnnotationFS aY) {
+    int xBegin = aX.getBegin();
+    int yBegin = aY.getBegin();
+    return yBegin == xBegin || (xBegin < aY.getEnd() && yBegin < aX.getEnd());
+  }
+
+  public static boolean overlapsLeft(int aXBegin, int aXEnd, int aYBegin, int aYEnd) {
+    return aXBegin < aYBegin && aYBegin < aXEnd && aXEnd < aYEnd;
+  }
+
+  public static boolean overlapsLeft(AnnotationFS aX, int aYBegin, int aYEnd) {
+    int xEnd = aX.getEnd();
+    return aYBegin < xEnd && xEnd < aYEnd && aX.getBegin() < aYBegin;
+  }
+
+  /**
+   * X is starting before or at the same position as Y and ends before Y ends.
+   * 
+   * @param aX
+   *          X
+   * @param aY
+   *          Y
+   * @return whether X overlaps Y on the left.
+   */
+  public static boolean overlapsLeft(AnnotationFS aX, AnnotationFS aY) {
+    int xEnd = aX.getEnd();
+    int yBegin = aY.getBegin();
+    return yBegin < xEnd && xEnd < aY.getEnd() && aX.getBegin() < yBegin;
+  }
+
+  public static boolean overlapsRight(int aXBegin, int aXEnd, int aYBegin, int aYEnd) {
+    return aYBegin < aXBegin && aXBegin < aYEnd && aYEnd < aXEnd;
+  }
+
+  public static boolean overlapsRight(AnnotationFS aX, int aYBegin, int aYEnd) {
+    int xBegin = aX.getBegin();
+    return aYBegin < xBegin && xBegin < aYEnd && aYEnd < aX.getEnd();
+  }
+
+  /**
+   * X is starting after Y starts and ends after or at the same position as Y.
+   * 
+   * @param aX
+   *          X
+   * @param aY
+   *          Y
+   * @return whether X overlaps Y on the right.
+   */
+  public static boolean overlapsRight(AnnotationFS aX, AnnotationFS aY) {
+    int xBegin = aX.getBegin();
+    int yEnd = aY.getEnd();
+    return xBegin < yEnd && aY.getBegin() < xBegin && yEnd < aX.getEnd();
+  }
+
+  public static boolean rightOf(int aXBegin, int aXEnd, int aYBegin, int aYEnd) {
+    return aXBegin >= aYEnd && aXBegin != aYBegin;
+  }
+
+  public static boolean rightOf(AnnotationFS aX, int aYBegin, int aYEnd) {
+    int xBegin = aX.getBegin();
+    return xBegin >= aYEnd && xBegin != aYBegin;
+  }
+
+  /**
+   * X starts at or after the position that Y ends.
+   * 
+   * @param aX
+   *          X
+   * @param aY
+   *          Y
+   * @return whether X is right of Y.
+   */
+  public static boolean rightOf(AnnotationFS aX, AnnotationFS aY) {
+    int xBegin = aX.getBegin();
+    return xBegin >= aY.getEnd() && xBegin != aY.getBegin();
+  }
+
+  public static boolean leftOf(int aXBegin, int aXEnd, int aYBegin, int aYEnd) {
+    return aYBegin >= aXEnd && aXBegin != aYBegin;
+  }
+
+  public static boolean leftOf(AnnotationFS aX, int aYBegin, int aYEnd) {
+    return aYBegin >= aX.getEnd() && aX.getBegin() != aYBegin;
+  }
+
+  /**
+   * X ends before or at the position that Y starts.
+   * 
+   * @param aX
+   *          X
+   * @param aY
+   *          Y
+   * @return whether X left of Y.
+   */
+  public static boolean leftOf(AnnotationFS aX, AnnotationFS aY) {
+    return aY.getBegin() >= aX.getEnd() && aX.getBegin() != aY.getBegin();
+  }
+}
diff --git a/uimaj-core/src/test/java/org/apache/uima/cas/text/AnnotationPredicatesTest.java b/uimaj-core/src/test/java/org/apache/uima/cas/text/AnnotationPredicatesTest.java
new file mode 100644
index 0000000..55e7801
--- /dev/null
+++ b/uimaj-core/src/test/java/org/apache/uima/cas/text/AnnotationPredicatesTest.java
@@ -0,0 +1,410 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ * 
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.uima.cas.text;
+
+import static org.apache.uima.cas.text.AnnotationPredicatesTest.RelativePosition.*;
+import static java.lang.Integer.MAX_VALUE;
+import static java.util.Arrays.asList;
+import static org.apache.uima.cas.text.AnnotationPredicates.colocated;
+import static org.apache.uima.cas.text.AnnotationPredicates.coveredBy;
+import static org.apache.uima.cas.text.AnnotationPredicates.covers;
+import static org.apache.uima.cas.text.AnnotationPredicates.*;
+import static org.apache.uima.cas.text.AnnotationPredicates.overlaps;
+import static org.apache.uima.cas.text.AnnotationPredicates.overlapsLeft;
+import static org.apache.uima.cas.text.AnnotationPredicates.overlapsRight;
+import static org.apache.uima.cas.text.AnnotationPredicates.rightOf;
+
+import org.apache.uima.cas.CAS;
+import org.apache.uima.cas.Type;
+import org.apache.uima.util.CasCreationUtils;
+import org.assertj.core.api.AutoCloseableSoftAssertions;
+import org.junit.Test;
+
+public class AnnotationPredicatesTest {
+    
+    static enum RelativePosition {
+      /**
+       * A starts and ends at the same position as B.
+       */
+      COLOCATED,
+      
+      /**
+       * The intersection of the spans A and B is non-empty. If either A or B have a zero-width,
+       * then the intersection is considered to be non-empty if the begin of A is either within B
+       * or the same as the begin of B - and vice versa.
+       */
+      OVERLAPPING,
+      
+      /**
+       * A is starting before or at the same position as B and ends before B ends.
+       */
+      OVERLAPPING_LEFT,
+      
+      /**
+       * A is starting after B starts and ends after or at the same position as B.
+       */
+      OVERLAPPING_RIGHT,
+      
+      /**
+       * A is starting before or at the same position as B and ends after or at the same position as
+       * B.
+       */
+      COVERING,
+      
+      /**
+       * B is starting before or at the same position as A and ends after or at the same position as
+       * A.
+       */
+      COVERED_BY,
+
+      /**
+       * A ends before or at the position that B starts.
+       */
+      LEFT_OF,
+
+      /**
+       * A starts at or after the position that B ends.
+       */
+      RIGHT_OF
+}
+  
+    @Test
+    public void thatCoversWithIntervalsWorks() throws Exception {
+      assertPosition(COVERING, AnnotationPredicates::covers);
+    }
+
+    @Test
+    public void thatCoversWithAnnotationAndIntervalWorks() throws Exception {
+      CAS cas = CasCreationUtils.createCas();
+      Type type = cas.getAnnotationType();
+      
+      assertPosition(COVERING, (beginA, endA, beginB,
+              endB) -> covers(cas.createAnnotation(type, beginA, endA), beginB, endB));
+    }
+
+    @Test
+    public void thatCoversWithAnnotationsWorks() throws Exception {
+      CAS cas = CasCreationUtils.createCas();
+      Type type = cas.getAnnotationType();
+      
+      assertPosition(COVERING,
+              (beginA, endA, beginB, endB) -> covers(cas.createAnnotation(type, beginA, endA),
+                      cas.createAnnotation(type, beginB, endB)));
+    }
+    
+    @Test
+    public void thatCoveredByWithIntervalsWorks() throws Exception {
+      assertPosition(COVERED_BY, AnnotationPredicates::coveredBy);
+    }
+
+    @Test
+    public void thatCoveredByWithAnnotationAndIntervalWorks() throws Exception {
+      CAS cas = CasCreationUtils.createCas();
+      Type type = cas.getAnnotationType();
+      
+      assertPosition(COVERED_BY, (beginA, endA, beginB,
+              endB) -> coveredBy(cas.createAnnotation(type, beginA, endA), beginB, endB));
+    }
+
+    @Test
+    public void thatCoveredByWithAnnotationsWorks() throws Exception {
+      CAS cas = CasCreationUtils.createCas();
+      Type type = cas.getAnnotationType();
+      
+      assertPosition(COVERED_BY,
+              (beginA, endA, beginB, endB) -> coveredBy(cas.createAnnotation(type, beginA, endA),
+                      cas.createAnnotation(type, beginB, endB)));
+    }
+
+    @Test
+    public void thatColocatedWithIntervalsWorks() throws Exception {
+      assertPosition(COLOCATED, AnnotationPredicates::colocated);
+      
+      // It must also work if we switch the of the spans
+      assertPosition(COLOCATED,
+              (beginA, endA, beginB, endB) -> colocated(beginB, endB, beginA, endA));
+    }
+
+    @Test
+    public void thatColocatedWithAnnotationAndIntervalWorks() throws Exception
+    {
+      CAS cas = CasCreationUtils.createCas();
+      Type type = cas.getAnnotationType();
+      
+      assertPosition(COLOCATED, (beginA, endA, beginB,
+              endB) -> colocated(cas.createAnnotation(type, beginA, endA), beginB, endB));
+      
+      // It must also work if we switch the of the spans
+      assertPosition(COLOCATED, (beginA, endA, beginB,
+              endB) -> colocated(cas.createAnnotation(type, beginB, endB), beginA, endA));
+    }
+
+    @Test
+    public void thatColocatedWithAnnotationsWorks() throws Exception
+    {
+      CAS cas = CasCreationUtils.createCas();
+      Type type = cas.getAnnotationType();
+      
+      assertPosition(COLOCATED,
+              (beginA, endA, beginB, endB) -> colocated(cas.createAnnotation(type, beginA, endA),
+                      cas.createAnnotation(type, beginB, endB)));
+      
+      // It must also work if we switch the of the spans
+      assertPosition(COLOCATED,
+              (beginA, endA, beginB, endB) -> colocated(cas.createAnnotation(type, beginB, endB),
+                      cas.createAnnotation(type, beginA, endA)));
+    }
+    
+    @Test
+    public void thatOverlapsLeftWithIntervalsWorks() throws Exception {
+      assertPosition(OVERLAPPING_LEFT, AnnotationPredicates::overlapsLeft);
+    }
+
+    @Test
+    public void thatOverlapsLeftWithAnnotationAndIntervalWorks() throws Exception {
+      CAS cas = CasCreationUtils.createCas();
+      Type type = cas.getAnnotationType();
+      
+      assertPosition(OVERLAPPING_LEFT, (beginA, endA, beginB,
+              endB) -> overlapsLeft(cas.createAnnotation(type, beginA, endA), beginB, endB));
+    }
+
+    @Test
+    public void thatOverlapsLeftWithAnnotationsWorks() throws Exception {
+      CAS cas = CasCreationUtils.createCas();
+      Type type = cas.getAnnotationType();
+      
+      assertPosition(OVERLAPPING_LEFT,
+              (beginA, endA, beginB, endB) -> overlapsLeft(cas.createAnnotation(type, beginA, endA),
+                      cas.createAnnotation(type, beginB, endB)));
+    }
+
+    @Test
+    public void thatOverlapsRightWithIntervalsWorks() throws Exception {
+      assertPosition(OVERLAPPING_RIGHT, AnnotationPredicates::overlapsRight);
+    }
+
+    @Test
+    public void thatOverlapsRightWithAnnotationAndIntervalWorks() throws Exception {
+      CAS cas = CasCreationUtils.createCas();
+      Type type = cas.getAnnotationType();
+      
+      assertPosition(OVERLAPPING_RIGHT, (beginA, endA, beginB,
+              endB) -> overlapsRight(cas.createAnnotation(type, beginA, endA), beginB, endB));
+    }
+
+    @Test
+    public void thatOverlapsRightWithAnnotationsWorks() throws Exception {
+      CAS cas = CasCreationUtils.createCas();
+      Type type = cas.getAnnotationType();
+      
+      assertPosition(OVERLAPPING_RIGHT,
+              (beginA, endA, beginB, endB) -> overlapsRight(cas.createAnnotation(type, beginA, endA),
+                      cas.createAnnotation(type, beginB, endB)));
+    }
+    
+    @Test
+    public void thatOverlapsWithIntervalsWorks() throws Exception {
+      assertPosition(OVERLAPPING, AnnotationPredicates::overlaps);
+
+      // It must also work if we switch the of the spans
+      assertPosition(OVERLAPPING,
+              (beginA, endA, beginB, endB) -> overlaps(beginB, endB, beginA, endA));
+    }
+
+    @Test
+    public void thatOverlapsWithAnnotationAndIntervalWorks() throws Exception
+    {
+      CAS cas = CasCreationUtils.createCas();
+      Type type = cas.getAnnotationType();
+      
+      assertPosition(OVERLAPPING, (beginA, endA, beginB,
+              endB) -> overlaps(cas.createAnnotation(type, beginA, endA), beginB, endB));
+      
+      // It must also work if we switch the of the spans
+      assertPosition(OVERLAPPING, (beginA, endA, beginB,
+              endB) -> overlaps(cas.createAnnotation(type, beginB, endB), beginA, endA));
+    }
+
+    @Test
+    public void thatOverlapsWithAnnotationsWorks() throws Exception
+    {
+      CAS cas = CasCreationUtils.createCas();
+      Type type = cas.getAnnotationType();
+      
+      assertPosition(OVERLAPPING,
+              (beginA, endA, beginB, endB) -> overlaps(cas.createAnnotation(type, beginA, endA),
+                      cas.createAnnotation(type, beginB, endB)));
+      
+      // It must also work if we switch the of the spans
+      assertPosition(OVERLAPPING,
+              (beginA, endA, beginB, endB) -> overlaps(cas.createAnnotation(type, beginB, endB),
+                      cas.createAnnotation(type, beginA, endA)));
+    }
+    
+    @Test
+    public void thatLeftOfWithIntervalsWorks() throws Exception {
+      assertPosition(LEFT_OF, AnnotationPredicates::leftOf);
+    }
+
+    @Test
+    public void thatLeftOfWithAnnotationAndIntervalWorks() throws Exception {
+      CAS cas = CasCreationUtils.createCas();
+      Type type = cas.getAnnotationType();
+      
+      assertPosition(LEFT_OF, (beginA, endA, beginB,
+              endB) -> leftOf(cas.createAnnotation(type, beginA, endA), beginB, endB));
+    }
+
+    @Test
+    public void thatLeftOfWithAnnotationsWorks() throws Exception {
+      CAS cas = CasCreationUtils.createCas();
+      Type type = cas.getAnnotationType();
+      
+      assertPosition(LEFT_OF,
+              (beginA, endA, beginB, endB) -> leftOf(cas.createAnnotation(type, beginA, endA),
+                      cas.createAnnotation(type, beginB, endB)));
+    }
+
+    @Test
+    public void thatRightOfWithIntervalsWorks() throws Exception {
+      assertPosition(RIGHT_OF, AnnotationPredicates::rightOf);
+    }
+
+    @Test
+    public void thatRightOfWithAnnotationAndIntervalWorks() throws Exception {
+      CAS cas = CasCreationUtils.createCas();
+      Type type = cas.getAnnotationType();
+
+      assertPosition(RIGHT_OF, (beginA, endA, beginB,
+              endB) -> rightOf(cas.createAnnotation(type, beginA, endA), beginB, endB));
+    }
+
+    @Test
+    public void thatRightOfWithAnnotationsWorks() throws Exception {
+      CAS cas = CasCreationUtils.createCas();
+      Type type = cas.getAnnotationType();
+
+      assertPosition(RIGHT_OF,
+              (beginA, endA, beginB, endB) -> rightOf(cas.createAnnotation(type, beginA, endA),
+                      cas.createAnnotation(type, beginB, endB)));
+    }
+
+    @FunctionalInterface
+    private static interface RelativePositionPredicate {
+      boolean apply(int beginA, int endA, int beginB, int endB);
+    }
+
+    public void assertPosition(RelativePosition aCondition, RelativePositionPredicate aPredicate)
+            throws Exception {
+        // Define a fixed interval around which we build most of the tests by applying different
+        // selections to it
+        int begin = 10;
+        int end = 20;
+
+        try (AutoCloseableSoftAssertions softly = new AutoCloseableSoftAssertions()) {
+          softly.assertThat(aPredicate.apply(begin, end, begin - 1, begin - 1))
+              .as("Zero-width B before A begins (| ###)")
+              .isEqualTo(asList(RIGHT_OF).contains(aCondition));
+        
+          softly.assertThat(aPredicate.apply(begin, end, 0, begin - 1))
+              .as("B begins and ends before A begins ([---] ###)")
+              .isEqualTo(asList(RIGHT_OF).contains(aCondition));
+        
+          softly.assertThat(aPredicate.apply(begin, end, 0, begin))
+              .as("B begins before and ends where A begins ([---]###)")
+              .isEqualTo(asList(RIGHT_OF).contains(aCondition));
+
+          softly.assertThat(aPredicate.apply(begin, end, begin, begin))
+              .as("Zero-width B where A begins (|###)")
+              .isEqualTo(asList(OVERLAPPING, COVERING).contains(aCondition));
+
+          softly.assertThat(aPredicate.apply(begin, end, 0, begin + 1))
+              .as("B begins before and ends within A ([--#]##)")
+              .isEqualTo(asList(OVERLAPPING, OVERLAPPING_RIGHT).contains(aCondition));
+        
+          softly.assertThat(aPredicate.apply(begin, end, begin + 1, end - 1))
+              .as("B begins and ends within A (#[#]#)")
+              .isEqualTo(asList(OVERLAPPING, COVERING).contains(aCondition));
+
+          softly.assertThat(aPredicate.apply(begin, end, begin + 1, end))
+              .as("B begins after and ends at A's boundries (#[##])")
+              .isEqualTo(asList(OVERLAPPING, COVERING).contains(aCondition));
+
+          softly.assertThat(aPredicate.apply(begin, end, begin - 1, end + 1))
+              .as("B begins and ends at A's boundries ([-###-])")
+              .isEqualTo(asList(OVERLAPPING, COVERED_BY).contains(aCondition));
+          
+          softly.assertThat(aPredicate.apply(begin, end, begin, end))
+              .as("B begins and ends at A's boundries ([###])")
+              .isEqualTo(asList(OVERLAPPING, COLOCATED, COVERED_BY, COVERING).contains(aCondition));
+        
+          softly.assertThat(aPredicate.apply(begin, end, begin + 1, begin + 1))
+              .as("Zero-width B within A (#|#)")
+              .isEqualTo(asList(OVERLAPPING, COVERING).contains(aCondition));
+
+          softly.assertThat(aPredicate.apply(begin, end, begin, end - 1))
+              .as("B begins at and ends before A's boundries ([##]#)")
+              .isEqualTo(asList(OVERLAPPING, COVERING).contains(aCondition));
+
+          softly.assertThat(aPredicate.apply(begin, end, end - 1 , MAX_VALUE))
+              .as("B begins before and ends within A (##[#--])")
+              .isEqualTo(asList(OVERLAPPING, OVERLAPPING_LEFT).contains(aCondition));
+
+          softly.assertThat(aPredicate.apply(begin, end, end, MAX_VALUE))
+              .as("B begins at A's end and ends after A (###[---])")
+              .isEqualTo(asList(LEFT_OF).contains(aCondition));
+
+          softly.assertThat(aPredicate.apply(begin, end, end, end))
+              .as("Zero-width B at A's end (###|)")
+              .isEqualTo(asList(LEFT_OF).contains(aCondition));
+
+          softly.assertThat(aPredicate.apply(begin, end, end + 1, MAX_VALUE))
+              .as("B begins and ends after A (### [---])")
+              .isEqualTo(asList(LEFT_OF).contains(aCondition));
+        
+          softly.assertThat(aPredicate.apply(begin, end, end + 1, end + 1))
+              .as("Zero-width B after A's end (### |)")
+              .isEqualTo(asList(LEFT_OF).contains(aCondition));
+        
+          begin = 10;
+          end = 10;
+          
+          softly.assertThat(aPredicate.apply(begin, end, 20, 30))
+              .as("Zero-width A before B start (# [---])")
+              .isEqualTo(asList(LEFT_OF).contains(aCondition));
+    
+          softly.assertThat(aPredicate.apply(begin, end, 10, 20))
+              .as("Zero-width A at B's start (#---])")
+              .isEqualTo(asList(OVERLAPPING, COVERED_BY).contains(aCondition));
+          
+          softly.assertThat(aPredicate.apply(begin, end, 0, 10))
+              .as("Zero-width A at B's end ([---#)")
+              .isEqualTo(asList(RIGHT_OF).contains(aCondition));
+    
+          softly.assertThat(aPredicate.apply(begin, end, 10, 10))
+              .as("Zero-width A matches zero-width B start/end (#)")
+              .isEqualTo(asList(OVERLAPPING, COVERED_BY, COVERING, COLOCATED).contains(aCondition));
+          
+          softly.assertThat(aPredicate.apply(begin, end, 0, 5))
+              .as("Zero-width A after B's end ([---] #)")
+              .isEqualTo(asList(RIGHT_OF).contains(aCondition));
+        }
+    }
+}
\ No newline at end of file