Author: rec Date: Sun May 12 12:10:24 2013 New Revision: 1481534 URL: http://svn.apache.org/r1481534 Log: [UIMA-2808] - JCasUtil Subiterator returns annotations which are not within borders of the container (parent) annotation if parameter "strict" is set to "false" - Unambigous subiterator was always returning first annotation of index type due to missing boundary check. Changed logic to void pre-fetching "current" annotation and avoid overlap check with "current" annotation while it's not set yet. - Added test case.
Modified: uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/cas/impl/Subiterator.java uima/uimaj/trunk/uimaj-core/src/test/java/org/apache/uima/cas/test/AnnotationIteratorTest.java Modified: uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/cas/impl/Subiterator.java URL: http://svn.apache.org/viewvc/uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/cas/impl/Subiterator.java?rev=1481534&r1=1481533&r2=1481534&view=diff ============================================================================== --- uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/cas/impl/Subiterator.java (original) +++ uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/cas/impl/Subiterator.java Sun May 12 12:10:24 2013 @@ -132,13 +132,11 @@ public class Subiterator<T extends Annot if (!it.isValid()) { return; } - current = it.get(); - this.list.add(current); - it.moveToNext(); + current = null; while (it.isValid()) { next = it.get(); - // If the next annotation overlaps, skip it. - if (next.getBegin() < current.getEnd()) { + // If the next annotation overlaps, skip it. Don't check while there is no "current" yet. + if ((current != null) && (next.getBegin() < current.getEnd())) { it.moveToNext(); continue; } Modified: uima/uimaj/trunk/uimaj-core/src/test/java/org/apache/uima/cas/test/AnnotationIteratorTest.java URL: http://svn.apache.org/viewvc/uima/uimaj/trunk/uimaj-core/src/test/java/org/apache/uima/cas/test/AnnotationIteratorTest.java?rev=1481534&r1=1481533&r2=1481534&view=diff ============================================================================== --- uima/uimaj/trunk/uimaj-core/src/test/java/org/apache/uima/cas/test/AnnotationIteratorTest.java (original) +++ uima/uimaj/trunk/uimaj-core/src/test/java/org/apache/uima/cas/test/AnnotationIteratorTest.java Sun May 12 12:10:24 2013 @@ -279,6 +279,36 @@ public class AnnotationIteratorTest exte assertTrue(false); } } + + /** + * UIMA-2808 - There was a bug in Subiterator causing the first annotation of the type of the + * index the subiterator was applied to always to be returned, even if outside the boundary + * annotation. + */ + public void testSubiteratorOnIndex() { + try { + // 0 0 1 1 2 2 3 3 4 4 5 + // 0 5 0 5 0 5 0 5 0 5 0 + this.cas.setDocumentText("Sentence A with no value. Sentence B with value 377."); + } catch (CASRuntimeException e) { + assertTrue(false); + } + + cas.addFsToIndexes(cas.createAnnotation(this.sentenceType, 0, 25)); + cas.addFsToIndexes(cas.createAnnotation(this.sentenceType, 26, 52)); + cas.addFsToIndexes(cas.createAnnotation(this.tokenType, 48, 51)); + + AnnotationIndex<AnnotationFS> si = cas.getAnnotationIndex(this.sentenceType); + for (AnnotationFS sa : si) { + AnnotationIndex<AnnotationFS> ti = cas.getAnnotationIndex(this.tokenType); + FSIterator<AnnotationFS> ti2 = ti.subiterator(sa, false, false); + + while (ti2.hasNext()) { + AnnotationFS t = ti2.next(); + assertTrue("Subiterator returned annotation outside boundaries", t.getBegin() < sa.getEnd()); + } + } + } public static void main(String[] args) { AnnotationIteratorTest test = new AnnotationIteratorTest(null);