You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@uima.apache.org by sc...@apache.org on 2016/10/04 21:33:44 UTC

svn commit: r1763331 - in /uima/uimaj/branches/experiment-v3-jcas/uimaj-core/src: main/java/org/apache/uima/cas/impl/ test/java/org/apache/uima/cas/test/

Author: schor
Date: Tue Oct  4 21:33:44 2016
New Revision: 1763331

URL: http://svn.apache.org/viewvc?rev=1763331&view=rev
Log:
[UIMA-5115] fix fsIterator_backwards to start and end, make following / preceding impls work, add tests

Modified:
    uima/uimaj/branches/experiment-v3-jcas/uimaj-core/src/main/java/org/apache/uima/cas/impl/FsIterator_backwards.java
    uima/uimaj/branches/experiment-v3-jcas/uimaj-core/src/main/java/org/apache/uima/cas/impl/FsIterator_subtypes_snapshot.java
    uima/uimaj/branches/experiment-v3-jcas/uimaj-core/src/main/java/org/apache/uima/cas/impl/LLUnambiguousIteratorImpl.java
    uima/uimaj/branches/experiment-v3-jcas/uimaj-core/src/main/java/org/apache/uima/cas/impl/SelectFSs_impl.java
    uima/uimaj/branches/experiment-v3-jcas/uimaj-core/src/test/java/org/apache/uima/cas/test/AnnotationIteratorTest.java

Modified: uima/uimaj/branches/experiment-v3-jcas/uimaj-core/src/main/java/org/apache/uima/cas/impl/FsIterator_backwards.java
URL: http://svn.apache.org/viewvc/uima/uimaj/branches/experiment-v3-jcas/uimaj-core/src/main/java/org/apache/uima/cas/impl/FsIterator_backwards.java?rev=1763331&r1=1763330&r2=1763331&view=diff
==============================================================================
--- uima/uimaj/branches/experiment-v3-jcas/uimaj-core/src/main/java/org/apache/uima/cas/impl/FsIterator_backwards.java (original)
+++ uima/uimaj/branches/experiment-v3-jcas/uimaj-core/src/main/java/org/apache/uima/cas/impl/FsIterator_backwards.java Tue Oct  4 21:33:44 2016
@@ -34,6 +34,7 @@ class FsIterator_backwards<T extends Fea
     
   FsIterator_backwards(FSIterator<T> iterator) {
     this.it = (LowLevelIterator<T>) iterator;
+    it.moveToLast();
   }
 
   public int ll_indexSize() {

Modified: uima/uimaj/branches/experiment-v3-jcas/uimaj-core/src/main/java/org/apache/uima/cas/impl/FsIterator_subtypes_snapshot.java
URL: http://svn.apache.org/viewvc/uima/uimaj/branches/experiment-v3-jcas/uimaj-core/src/main/java/org/apache/uima/cas/impl/FsIterator_subtypes_snapshot.java?rev=1763331&r1=1763330&r2=1763331&view=diff
==============================================================================
--- uima/uimaj/branches/experiment-v3-jcas/uimaj-core/src/main/java/org/apache/uima/cas/impl/FsIterator_subtypes_snapshot.java (original)
+++ uima/uimaj/branches/experiment-v3-jcas/uimaj-core/src/main/java/org/apache/uima/cas/impl/FsIterator_subtypes_snapshot.java Tue Oct  4 21:33:44 2016
@@ -41,7 +41,7 @@ public class FsIterator_subtypes_snapsho
     this.is_unordered = flatIndex.getIndexingStrategy() != FSIndex.SORTED_INDEX;
   }
   
-  public FsIterator_subtypes_snapshot(T[] snapshot, LowLevelIndex<FeatureStructure> index, boolean is_unordered) {
+  public FsIterator_subtypes_snapshot(T[] snapshot, LowLevelIndex<T> index, boolean is_unordered) {
     this.indexForComparator = (LowLevelIndex<T>) index;
     this.snapshot = snapshot;
     this.is_unordered = is_unordered;
@@ -158,7 +158,7 @@ public class FsIterator_subtypes_snapsho
   @Override
   public FSIterator<T> copy() {
     FsIterator_subtypes_snapshot<T> it = new FsIterator_subtypes_snapshot<T>(
-        this.snapshot, (LowLevelIndex<FeatureStructure>) this.indexForComparator, this.is_unordered);
+        this.snapshot, (LowLevelIndex<T>) this.indexForComparator, this.is_unordered);
     it.pos = pos;
     return it;
   }

Modified: uima/uimaj/branches/experiment-v3-jcas/uimaj-core/src/main/java/org/apache/uima/cas/impl/LLUnambiguousIteratorImpl.java
URL: http://svn.apache.org/viewvc/uima/uimaj/branches/experiment-v3-jcas/uimaj-core/src/main/java/org/apache/uima/cas/impl/LLUnambiguousIteratorImpl.java?rev=1763331&r1=1763330&r2=1763331&view=diff
==============================================================================
--- uima/uimaj/branches/experiment-v3-jcas/uimaj-core/src/main/java/org/apache/uima/cas/impl/LLUnambiguousIteratorImpl.java (original)
+++ uima/uimaj/branches/experiment-v3-jcas/uimaj-core/src/main/java/org/apache/uima/cas/impl/LLUnambiguousIteratorImpl.java Tue Oct  4 21:33:44 2016
@@ -34,7 +34,7 @@ import org.apache.uima.jcas.tcas.Annotat
 public class LLUnambiguousIteratorImpl<T extends FeatureStructure> extends FsIterator_subtypes_snapshot<T> {
 
   public LLUnambiguousIteratorImpl(LowLevelIterator<FeatureStructure> it) {
-    super((T[]) createItemsArray(it), it.ll_getIndex(), false);
+    super((T[]) createItemsArray(it), (LowLevelIndex<T>) it.ll_getIndex(), false);
   }
   
   private static Annotation[] createItemsArray(LowLevelIterator<FeatureStructure> it) {

Modified: uima/uimaj/branches/experiment-v3-jcas/uimaj-core/src/main/java/org/apache/uima/cas/impl/SelectFSs_impl.java
URL: http://svn.apache.org/viewvc/uima/uimaj/branches/experiment-v3-jcas/uimaj-core/src/main/java/org/apache/uima/cas/impl/SelectFSs_impl.java?rev=1763331&r1=1763330&r2=1763331&view=diff
==============================================================================
--- uima/uimaj/branches/experiment-v3-jcas/uimaj-core/src/main/java/org/apache/uima/cas/impl/SelectFSs_impl.java (original)
+++ uima/uimaj/branches/experiment-v3-jcas/uimaj-core/src/main/java/org/apache/uima/cas/impl/SelectFSs_impl.java Tue Oct  4 21:33:44 2016
@@ -83,7 +83,6 @@ public class SelectFSs_impl <T extends F
   private boolean isNullOK = false;
   private boolean isUnordered = false;
   private boolean isBackwards = false;
-  private boolean isAt = false;
   private boolean isFollowing = false;
   private boolean isPreceding = false;
   
@@ -444,7 +443,8 @@ public class SelectFSs_impl <T extends F
     final boolean isUseAnnotationIndex = 
         ((index != null) && (index instanceof AnnotationIndex)) ||
         isNonOverlapping || 
-        boundsUse != BoundsUse.notBounded;
+        boundsUse != BoundsUse.notBounded ||
+        isFollowing || isPreceding;
     
     if (isUseAnnotationIndex) {
       forceAnnotationIndex();  // throws if non-null index not an annotation index
@@ -479,6 +479,14 @@ public class SelectFSs_impl <T extends F
     }
   }
   
+  private void decr(FSIterator<T> it) {
+    if (isBackwards) {
+      it.moveToNext();
+    } else {
+      it.moveToPrevious();
+    }
+  }
+  
   /*********************************
    * terminal operations
    * returning other than SelectFSs
@@ -496,10 +504,37 @@ public class SelectFSs_impl <T extends F
   /**
    * F S I t e r a t o r
    * -------------------
-   * ignored: backwards (because the fsIterator explicitly goes forwards and backwards)
    */
   @Override
   public FSIterator<T> fsIterator() {
+    if (isFollowing && isBackwards) {
+      isBackwards = false;
+      T[] a = (T[]) asArray(fsIterator1());
+      FSIterator<T> it = new FsIterator_backwards<>(new FsIterator_subtypes_snapshot<T>(a, (LowLevelIndex<T>) index, false));
+      return (limit == 0)
+          ? it
+            // rewrap with limit - needs to be outer shell to get right invalid behavior
+          : new FsIterator_limited<>(it, limit);          
+    }
+    
+    if (isPreceding) {
+      boolean bkwd = isBackwards;
+      isBackwards = true;
+      T[] a = (T[]) asArray(fsIterator1());
+      FSIterator<T> it = new FsIterator_subtypes_snapshot<T>(a, (LowLevelIndex<T>) index, false);
+      if (!bkwd) {
+        it = new FsIterator_backwards<>(it); // because array is backwards
+      }
+      return (limit == 0) 
+          ? it
+            // rewrap with limit - needs to be outer shell to get right invalid behavior
+          : new FsIterator_limited<>(it, limit); 
+    }
+    
+    return fsIterator1();
+  }
+  
+  private FSIterator<T> fsIterator1() {
     prepareTerminalOp();
     FSIterator<T> it = isAllViews 
                       ? new FsIterator_aggregation_common<T>(getPlainIteratorsForAllViews(), null)
@@ -507,9 +542,11 @@ public class SelectFSs_impl <T extends F
 
     maybePosition(it);
     maybeShift(it);
-    return  (limit == -1) ? it : new FsIterator_limited<>(it, limit);
+    return (limit == -1) ? it : new FsIterator_limited<>(it, limit);    
   }
   
+  
+  
   private FSIterator<T>[] getPlainIteratorsForAllViews() {
     final int nbrViews = view.getNumberOfViews();
     FSIterator<T>[] ita = new FSIterator[nbrViews];
@@ -607,17 +644,28 @@ public class SelectFSs_impl <T extends F
    */
   @Override
   public T[] asArray(Class<T> clazz) {
+    return asArray(fsIterator(), clazz);
+  }
+
+  private T[] asArray(FSIterator<T> it, Class<T> clazz) {
     List<T> al = new ArrayList<>();
-    FSIterator<T> it = fsIterator();
     while (it.isValid()) {
-      al.add(it.getNvc());
-      it.moveToNextNvc();
-    }
-    
+      al.add(it.getNvc());  // limit iterator might cause it to become invalid
+      it.moveToNext();
+    }    
     T[] r = (T[]) Array.newInstance(clazz, al.size());
+    return al.toArray(r);    
+  }
+  
+  private FeatureStructure[] asArray(FSIterator<T> it) {
+    List<? super T> al = new ArrayList<>();
+    while (it.isValid()) {
+      al.add(it.getNvc());
+      it.moveToNext(); // limit iterator might cause it to become invalid
+    }    
+    FeatureStructure[] r = (FeatureStructure[]) Array.newInstance(FeatureStructure.class,  al.size());
     return al.toArray(r);
   }
-
   /**
    * Iterator respects backwards
    * 
@@ -866,7 +914,7 @@ public class SelectFSs_impl <T extends F
     
     it.moveTo(startingFs);
     
-    if (index != null && index instanceof AnnotationIndex) {
+    if (index != null && index instanceof AnnotationIndex && !isFollowing && !isPreceding) {
       if (!isTypePriority) {
         int begin = ((Annotation)startingFs).getBegin();
         int end = ((Annotation)startingFs).getEnd();
@@ -885,11 +933,16 @@ public class SelectFSs_impl <T extends F
       }
     }
     
-    // guaranteed by above not-bounded
     if (isFollowing) {
-      it.moveToNext();
+      final int end = ((Annotation)startingFs).getEnd();
+      while (it.isValid() && ((Annotation)it.get()).getBegin() < end) {
+        it.moveToNext();
+      }
     } else if (isPreceding) {
-      it.moveToPrevious();
+      final int begin = ((Annotation)startingFs).getBegin();
+      while (it.isValid() && ((Annotation)it.get()).getEnd() > begin) {
+        it.moveToPrevious();
+      }
     }
     
     return it;
@@ -1016,13 +1069,16 @@ public class SelectFSs_impl <T extends F
 //  }
   
   private SelectFSs<T> commonFollowing(TOP fs, int offset) {
-//    validateSinglePosition(fs, offset);
+    this.startingFs = fs;
+    this.shift = offset;
     isFollowing = true;
     return this;
   }
 
   private SelectFSs<T> commonPreceding(TOP fs, int offset) {
 //    validateSinglePosition(fs, offset);
+    this.startingFs = fs;
+    this.shift = offset;
     isPreceding = true;
     isBackwards = true; // always iterate backwards
     return this;

Modified: uima/uimaj/branches/experiment-v3-jcas/uimaj-core/src/test/java/org/apache/uima/cas/test/AnnotationIteratorTest.java
URL: http://svn.apache.org/viewvc/uima/uimaj/branches/experiment-v3-jcas/uimaj-core/src/test/java/org/apache/uima/cas/test/AnnotationIteratorTest.java?rev=1763331&r1=1763330&r2=1763331&view=diff
==============================================================================
--- uima/uimaj/branches/experiment-v3-jcas/uimaj-core/src/test/java/org/apache/uima/cas/test/AnnotationIteratorTest.java (original)
+++ uima/uimaj/branches/experiment-v3-jcas/uimaj-core/src/test/java/org/apache/uima/cas/test/AnnotationIteratorTest.java Tue Oct  4 21:33:44 2016
@@ -422,6 +422,10 @@ public class AnnotationIteratorTest exte
     assertTrue(x);
     cas.<AnnotationFS>select().coveredBy(3, 5).singleOrNull();
     
+    select_it = sselect(annotIndex).following(2, 39).limit(2).fsIterator();
+    assertCountLimit("Following", 2, select_it);
+    select_it = sselect(annotIndex).following(2, 39).backwards().limit(2).fsIterator();
+    assertCountLimit("Following", 2, select_it);
   }
   
   private String flatStateMsg(String s) {