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/09 22:26:52 UTC

[uima-uimaj] branch UIMA-6269-select-coveredBy-broken-on-edge-case updated: [UIMA-6269] select.coveredBy with includeAnnotationsWithEndBeyondBounds has broken edge case

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

rec pushed a commit to branch UIMA-6269-select-coveredBy-broken-on-edge-case
in repository https://gitbox.apache.org/repos/asf/uima-uimaj.git


The following commit(s) were added to refs/heads/UIMA-6269-select-coveredBy-broken-on-edge-case by this push:
     new 979c83b  [UIMA-6269] select.coveredBy with includeAnnotationsWithEndBeyondBounds has broken edge case
979c83b is described below

commit 979c83ba1e9b2afc5d2ac3d7dc97b247429e3260
Author: Richard Eckart de Castilho <re...@apache.org>
AuthorDate: Sat Oct 10 00:14:42 2020 +0200

    [UIMA-6269] select.coveredBy with includeAnnotationsWithEndBeyondBounds has broken edge case
    
    - Fixed edge case
    - Added unit tests
---
 .../java/org/apache/uima/cas/impl/Subiterator.java | 27 +++++++++++
 .../org/apache/uima/cas/impl/SelectFsTest.java     | 54 +++++++++++++++++++++-
 2 files changed, 80 insertions(+), 1 deletion(-)

diff --git a/uimaj-core/src/main/java/org/apache/uima/cas/impl/Subiterator.java b/uimaj-core/src/main/java/org/apache/uima/cas/impl/Subiterator.java
index 4bd3742..d77fa3e 100644
--- a/uimaj-core/src/main/java/org/apache/uima/cas/impl/Subiterator.java
+++ b/uimaj-core/src/main/java/org/apache/uima/cas/impl/Subiterator.java
@@ -446,6 +446,33 @@ public class Subiterator<T extends AnnotationFS> implements LowLevelIterator<T>
     
     case coveredBy:
       it.moveToNoReinit(boundingAnnot);
+      
+      if (!isStrict) {
+        // If the bounding annotation evaluates to being "greater" than any of the annotation in the
+        // index according to the index order, then the iterator comes back invalid. 
+        if (!it.isValid()) {
+            it.moveToLastNoReinit();
+        }
+        
+        // In any case, if not doing strict selection, we need to try seeking backwards because we
+        // may have skipped covered annotations which start within the selection range but do not
+        // end within it.
+        boolean wentBack = false;
+        while (it.isValid() && it.getNvc().getBegin() >= boundingAnnot.getBegin()) {
+          it.moveToPreviousNvc();
+          wentBack = true;
+        }
+        
+        if (wentBack) {
+          if (!it.isValid()) {
+            it.moveToFirstNoReinit();
+          }
+          else if (it.getNvc().getBegin() < boundingAnnot.getBegin()) {
+            it.moveToNextNvc();
+          }
+        }
+      }
+      
       //   if an annotation is present (found), position is on it, and if not,
       //   position is at the next annotation that is higher than (or invalid, if there is none)
       //     note that the next found position could be beyond the end.
diff --git a/uimaj-core/src/test/java/org/apache/uima/cas/impl/SelectFsTest.java b/uimaj-core/src/test/java/org/apache/uima/cas/impl/SelectFsTest.java
index db02f6a..b2089a9 100644
--- a/uimaj-core/src/test/java/org/apache/uima/cas/impl/SelectFsTest.java
+++ b/uimaj-core/src/test/java/org/apache/uima/cas/impl/SelectFsTest.java
@@ -29,9 +29,9 @@ import java.io.File;
 import java.util.Arrays;
 import java.util.Iterator;
 import java.util.List;
+import java.util.stream.Collectors;
 
 import org.apache.uima.UIMAFramework;
-import org.apache.uima.cas.CAS;
 import org.apache.uima.cas.text.AnnotationFS;
 import org.apache.uima.jcas.JCas;
 import org.apache.uima.jcas.tcas.Annotation;
@@ -247,4 +247,56 @@ public class SelectFsTest  {
         .as("Selection (0-1) including start position (0) but not end position (2)")
         .containsExactly(annotation);
   }
+  
+  @Test
+  public void thatCoveredByWithBeyondEndsCanSelectAnnotationsStartingAtSelectPosition2() {
+    cas.reset();
+    AnnotationFS a1 = cas.createAnnotation(cas.getAnnotationType(), 0, 4);
+    AnnotationFS a2 = cas.createAnnotation(cas.getAnnotationType(), 1, 3);
+    cas.addFsToIndexes(a1);
+    cas.addFsToIndexes(a2);
+
+    List<AnnotationFS> result = cas.select(Annotation.class)
+        .coveredBy(0, 2)
+        .includeAnnotationsWithEndBeyondBounds()
+        .collect(Collectors.toList());
+
+    assertThat(result).containsExactly(a1, a2);
+  }
+
+  @Test
+  public void thatCoveredByWithBeyondEndsCanSelectAnnotationsStartingAtSelectPosition3() {
+    cas.reset();
+    AnnotationFS a1 = cas.createAnnotation(cas.getAnnotationType(), 0, 5);
+    AnnotationFS a2 = cas.createAnnotation(cas.getAnnotationType(), 1, 4);
+    AnnotationFS a3 = cas.createAnnotation(cas.getAnnotationType(), 2, 6);
+    cas.addFsToIndexes(a1);
+    cas.addFsToIndexes(a2);
+    cas.addFsToIndexes(a3);
+
+    List<AnnotationFS> result = cas.select(Annotation.class)
+        .coveredBy(0, 3)
+        .includeAnnotationsWithEndBeyondBounds()
+        .collect(Collectors.toList());
+
+    assertThat(result).containsExactly(a1, a2, a3);
+  }
+
+  @Test
+  public void thatCoveredByWithBeyondEndsCanSelectAnnotationsStartingAtSelectPosition4() {
+    cas.reset();
+    AnnotationFS a1 = cas.createAnnotation(cas.getAnnotationType(), 0, 4);
+    AnnotationFS a2 = cas.createAnnotation(cas.getAnnotationType(), 1, 5);
+    AnnotationFS a3 = cas.createAnnotation(cas.getAnnotationType(), 2, 2);
+    cas.addFsToIndexes(a1);
+    cas.addFsToIndexes(a2);
+    cas.addFsToIndexes(a3);
+
+    List<AnnotationFS> result = cas.select(Annotation.class)
+        .coveredBy(0, 3)
+        .includeAnnotationsWithEndBeyondBounds()
+        .collect(Collectors.toList());
+
+    assertThat(result).containsExactly(a1, a2, a3);
+  }
 }