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 17:28:52 UTC

svn commit: r1763309 - in /uima/uimaj/branches/experiment-v3-jcas/uimaj-core/src: main/java/org/apache/uima/cas/SelectFSs.java main/java/org/apache/uima/cas/impl/SelectFSs_impl.java test/java/org/apache/uima/cas/test/AnnotationIteratorTest.java

Author: schor
Date: Tue Oct  4 17:28:51 2016
New Revision: 1763309

URL: http://svn.apache.org/viewvc?rev=1763309&view=rev
Log:
[UIMA-5115] support get() defaulting to null ok, but get(n) defaulting to null not-ok. add shifted( n ). fix spliterator to not duplicate work done by fsiterator. make single and singleNullOK ignore nullOK setting. A few more tests for get() and get(xx)

Modified:
    uima/uimaj/branches/experiment-v3-jcas/uimaj-core/src/main/java/org/apache/uima/cas/SelectFSs.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/SelectFSs.java
URL: http://svn.apache.org/viewvc/uima/uimaj/branches/experiment-v3-jcas/uimaj-core/src/main/java/org/apache/uima/cas/SelectFSs.java?rev=1763309&r1=1763308&r2=1763309&view=diff
==============================================================================
--- uima/uimaj/branches/experiment-v3-jcas/uimaj-core/src/main/java/org/apache/uima/cas/SelectFSs.java (original)
+++ uima/uimaj/branches/experiment-v3-jcas/uimaj-core/src/main/java/org/apache/uima/cas/SelectFSs.java Tue Oct  4 17:28:51 2016
@@ -80,8 +80,8 @@ public interface SelectFSs<T extends Fea
   SelectFSs<T> allViews();
   SelectFSs<T> allViews(boolean allViews);
   
-  SelectFSs<T> nullOK();  // applies to get() and single()
-  SelectFSs<T> nullOK(boolean nullOk);  // applies to get() and single()
+  SelectFSs<T> nullOK();  // applies to get()
+  SelectFSs<T> nullOK(boolean nullOk);  // applies to get()
     
   SelectFSs<T> unordered();                  // ignored if not ordered index
   SelectFSs<T> unordered(boolean unordered); // ignored if not ordered index
@@ -92,10 +92,6 @@ public interface SelectFSs<T extends Fea
 //  SelectFSs<T> noSubtypes();
 //  SelectFSs<T> noSubtypes(boolean noSubtypes);
 
-  // ---------------------------------
-  // bounding limits specified
-  // as part of subselection style
-  // ---------------------------------
   
   // ---------------------------------
   // starting position specification
@@ -113,6 +109,8 @@ public interface SelectFSs<T extends Fea
   // shifts, if any, occur afterwards
   //   - can be positive or negative
   // ---------------------------------
+  SelectFSs<T> shifted(int shiftAmount); 
+  
   SelectFSs<T> startAt(TOP fs);  // Ordered
   SelectFSs<T> startAt(int begin, int end);   // AI
   
@@ -171,10 +169,15 @@ public interface SelectFSs<T extends Fea
   
   // returning one item
   
-  T get();          // returns first element or null if empty
-  T single();       // throws if not exactly 1 element
+  T get();          // returns first element or null if empty (unless nullOK(false) specified)
+  T single();       // throws if not exactly 1 element, throws if null
   T singleOrNull(); // throws if more than 1 element, returns single or null
-  T get(TOP fs);          // returns first element or null if empty
+   // next are positioning alternatives
+   // get(...) throws if null (unless nullOK specified)
+  T get(int offset);          // returns first element or null if empty after positioning
+  T single(int offset);       // throws if not exactly 1 element
+  T singleOrNull(int offset); // throws if more than 1 element, returns single or null  
+  T get(TOP fs);          // returns first element or null if empty after positioning
   T single(TOP fs);       // throws if not exactly 1 element
   T singleOrNull(TOP fs); // throws if more than 1 element, returns single or null
   T get(TOP fs, int offset);          // returns first element or null if empty

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=1763309&r1=1763308&r2=1763309&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 17:28:51 2016
@@ -87,6 +87,8 @@ public class SelectFSs_impl <T extends F
   private boolean isFollowing = false;
   private boolean isPreceding = false;
   
+  private boolean isNullOkSpecified = false; // for complex defaulting of get(), get(n)
+  
   private BoundsUse boundsUse = null; 
   
   private TOP startingFs = null;
@@ -264,11 +266,13 @@ public class SelectFSs_impl <T extends F
   @Override
   public SelectFSs_impl<T> nullOK() { // applies to get() and single()
     this.isNullOK = true;
+    this.isNullOkSpecified = true;
     return this;
   }  
   @Override
   public SelectFSs_impl<T> nullOK(boolean bNullOk) {  // applies to get() and single()
     this.isNullOK = bNullOk;
+    this.isNullOkSpecified = true;
     return this;
   }
     
@@ -304,6 +308,13 @@ public class SelectFSs_impl <T extends F
   /*********************************
    * starting position
    *********************************/
+  
+  @Override
+  public SelectFSs_impl<T> shifted(int shiftAmount) {
+    this.shift = shiftAmount;
+    return this;
+  }
+  
   @Override
   public SelectFSs_impl<T> startAt(TOP fs) {  // Ordered
     this.startingFs = fs;
@@ -499,7 +510,6 @@ public class SelectFSs_impl <T extends F
     return  (limit == -1) ? it : new FsIterator_limited<>(it, limit);
   }
   
-  
   private FSIterator<T>[] getPlainIteratorsForAllViews() {
     final int nbrViews = view.getNumberOfViews();
     FSIterator<T>[] ita = new FSIterator[nbrViews];
@@ -630,12 +640,12 @@ public class SelectFSs_impl <T extends F
 
       private final FSIterator<T> it = fsIterator();
       
+      private final Comparator<? super T> comparator = 
+          (index != null && index.getIndexingStrategy() == FSIndex.SORTED_INDEX) 
+            ? (Comparator<? super T>)index : null;
+                                                          
       private final int characteristics;
-      
-      private final Comparator<? super T> comparator;
-
-      { 
-        prepareTerminalOp();
+      { // set the characteristics and comparator
         // always set
         int c = Spliterator.IMMUTABLE | Spliterator.NONNULL | Spliterator.DISTINCT;
         
@@ -644,21 +654,13 @@ public class SelectFSs_impl <T extends F
         }
         
         // set per indexing strategy
-        int kind = (null == index) ? -1 : index.getIndexingStrategy();
-        if (kind == FSIndex.SORTED_INDEX) {
-          c |= Spliterator.ORDERED | Spliterator.SORTED;
-          comparator = (Comparator<? super T>) index; 
-        } else {
-          comparator = null;
-        }
-        if (kind == FSIndex.SET_INDEX) {
-          c |= Spliterator.ORDERED;
+        switch ((null == index) ? -1 : index.getIndexingStrategy()) {
+        case FSIndex.SORTED_INDEX: c |= Spliterator.ORDERED | Spliterator.SORTED; break;
+        case FSIndex.SET_INDEX: c |= Spliterator.ORDERED; break;
+        default: // do nothing
         }
         
-        characteristics = c;
-        if (isBackwards) {
-          it.moveToLast();
-        }
+        characteristics = c;        
       }
       
       @Override
@@ -697,20 +699,25 @@ public class SelectFSs_impl <T extends F
   
   /*
    * returns the item the select is pointing to, or null 
-   * uses isNullOK 
+   * if nullOK(false) then throws on null
    * (non-Javadoc)
    * @see org.apache.uima.cas.SelectFSs#get()
    */
   @Override
   public T get() {
+    return getNullChk(true);
+  }
+  
+  private T getNullChk(boolean isDoSetTest) {
     FSIterator<T> it = fsIterator();
     if (it.isValid()) {
       return it.getNvc();
     }
-    if (!isNullOK) {
+    if ((!isDoSetTest || isNullOkSpecified) && !isNullOK) {
       throw new CASRuntimeException(CASRuntimeException.SELECT_GET_NO_INSTANCES, ti.getName(), maybeMsgPosition());
     }
     return null;
+    
   }
 
   /*
@@ -721,7 +728,7 @@ public class SelectFSs_impl <T extends F
   @Override
   public T single() {
     T v = singleOrNull();
-    if (v == null && !isNullOK) {
+    if (v == null) {
       throw new CASRuntimeException(CASRuntimeException.SELECT_GET_NO_INSTANCES, ti.getName(), maybeMsgPosition());
     }
     return v;
@@ -746,10 +753,30 @@ public class SelectFSs_impl <T extends F
     return null;
   }
   
+  
+  
+  @Override
+  public T get(int offset) {
+    this.shift = offset;
+    return getNullChk(false);
+  }
+
+  @Override
+  public T single(int offset) {
+    this.shift = offset;
+    return single();
+  }
+
+  @Override
+  public T singleOrNull(int offset) {
+    this.shift = offset;
+    return singleOrNull();
+  }
+
   @Override
   public T get(TOP fs) {
     startAt(fs);
-    return get();
+    return getNullChk(false);
   }
 
   @Override
@@ -767,7 +794,7 @@ public class SelectFSs_impl <T extends F
   @Override
   public T get(TOP fs, int offset) {
     startAt(fs, offset);
-    return get();
+    return getNullChk(false);
   }
 
   @Override
@@ -785,7 +812,7 @@ public class SelectFSs_impl <T extends F
   @Override
   public T get(int begin, int end) {
     startAt(begin, end);
-    return get();
+    return getNullChk(false);
   }
 
   @Override
@@ -803,7 +830,7 @@ public class SelectFSs_impl <T extends F
   @Override
   public T get(int begin, int end, int offset) {
     startAt(begin, end, offset);
-    return get();
+    return getNullChk(false);
   }
 
   @Override
@@ -1013,7 +1040,7 @@ public class SelectFSs_impl <T extends F
   }
   
   private Stream<T> stream() {
-    return StreamSupport.stream(spliterator(), false);
+    return StreamSupport.stream(spliterator(), false); // false = default not parallel
   }
 
   

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=1763309&r1=1763308&r2=1763309&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 17:28:51 2016
@@ -23,6 +23,7 @@ import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collections;
 import java.util.List;
+import java.util.NoSuchElementException;
 
 import org.apache.uima.cas.CAS;
 import org.apache.uima.cas.CASRuntimeException;
@@ -38,6 +39,8 @@ import org.apache.uima.jcas.tcas.Annotat
 
 import junit.framework.TestCase;
 
+import static org.apache.uima.cas.SelectFSs.sselect;
+
 /**
  * Class comment for FilteredIteratorTest.java goes here.
  * 
@@ -278,10 +281,10 @@ public class AnnotationIteratorTest exte
 //      FSIndexFlat.enabled ? it instanceof FSIndexFlat.FSIteratorFlat : it instanceof FSIteratorWrapper);   
     assertCount("Normal ambiguous annot iterator", annotCount, it);
     assertCount("Normal ambiguous select annot iterator", annotCount, select_it);
-    assertEquals(annotCount, SelectFSs.sselect(annotIndex).toArray().length);  // stream op
-    assertEquals(annotCount, SelectFSs.sselect(annotIndex).asArray(AnnotationFS.class).length);  // select op
+    assertEquals(annotCount, sselect(annotIndex).toArray().length);  // stream op
+    assertEquals(annotCount, sselect(annotIndex).asArray(AnnotationFS.class).length);  // select op
     // backwards
-    select_it = SelectFSs.sselect(annotIndex).backwards().fsIterator();
+    select_it = sselect(annotIndex).backwards().fsIterator();
     assertCount("Normal select backwards ambiguous annot iterator", annotCount, select_it);
     
     it = annotIndex.iterator(false);  // false means create an unambiguous iterator
@@ -297,7 +300,7 @@ public class AnnotationIteratorTest exte
     assertCount("Unambigous select sentence iterator", 5, select_it);
     select_it = sentIndex.<AnnotationFS>select().nonOverlapping().fsIterator();
     assertCount("Unambigous select sentence iterator", 5, select_it);
-    select_it = SelectFSs.sselect(sentIndex).nonOverlapping().fsIterator();
+    select_it = sselect(sentIndex).nonOverlapping().fsIterator();
     assertCount("Unambigous select sentence iterator", 5, select_it);
     
     
@@ -331,16 +334,16 @@ public class AnnotationIteratorTest exte
     assertCount("Subiterator over annot ambiguous not-strict", 40, it);
     
     // covered by implies endWithinBounds
-    select_it = SelectFSs.sselect(annotIndex).coveredBy(bigBound).fsIterator();
+    select_it = sselect(annotIndex).coveredBy(bigBound).fsIterator();
     assertCount("Subiterator select over annot ambiguous not-strict", 33, select_it);
     select_it = annotIndex.<AnnotationFS>select().coveredBy(bigBound).fsIterator();
     assertCount("Subiterator select over annot ambiguous not-strict", 33, select_it);
-    select_it = SelectFSs.sselect(annotIndex).coveredBy(bigBound).endWithinBounds(false).fsIterator();
+    select_it = sselect(annotIndex).coveredBy(bigBound).endWithinBounds(false).fsIterator();
     assertCount("Subiterator select over annot ambiguous not-strict", 40, select_it);
     
     it = annotIndex.subiterator(bigBound, false, false);  // unambiguous, not strict
     assertCount("Subiterator over annot, unambiguous, not-strict", 4, it);
-    select_it = SelectFSs.sselect(annotIndex).nonOverlapping().coveredBy(bigBound).endWithinBounds(false).fsIterator();
+    select_it = sselect(annotIndex).nonOverlapping().coveredBy(bigBound).endWithinBounds(false).fsIterator();
     assertCount("Subiterator select over annot unambiguous not-strict", 4, select_it);
     
     AnnotationFS sent = this.cas.getAnnotationIndex(this.sentenceType).iterator().get();
@@ -357,6 +360,37 @@ public class AnnotationIteratorTest exte
     assertCount("Subiteratover over sent ambiguous", 5, it);
     it = sentIndex.subiterator(bigBound, false, false);
     assertCount("Subiteratover over sent unambiguous", 1, it); 
+    
+    // single, get, nullOK
+         
+    assertTrue(cas.<AnnotationFS>select().nonOverlapping().get().getType().getShortName().equals("DocumentAnnotation"));
+    boolean x = false;
+    try {
+      assertNull(cas.<AnnotationFS>select().nullOK(false).coveredBy(3, 3).get());
+    } catch (CASRuntimeException e) {
+      if (e.hasMessageKey(CASRuntimeException.SELECT_GET_NO_INSTANCES)) {
+        x= true;
+      }
+    }
+    assertTrue(x);
+    assertNull(cas.<AnnotationFS>select().coveredBy(3, 3).get());
+    assertNotNull(cas.<AnnotationFS>select().get(3));
+    assertNull(cas.<AnnotationFS>select().nullOK().coveredBy(3, 5).get(3));
+    x = false;
+    try {
+      cas.<AnnotationFS>select().coveredBy(3, 5). get(3);
+    } catch (CASRuntimeException e) {
+      if (e.hasMessageKey(CASRuntimeException.SELECT_GET_NO_INSTANCES)) {
+        x= true;
+      }
+    }
+    assertTrue(cas.<AnnotationFS>select().nonOverlapping().get().getType().getShortName().equals("DocumentAnnotation"));
+    
+    select_it = cas.<AnnotationFS>select().nonOverlapping().fsIterator(); 
+    assertCount("Unambiguous select annot iterator", 1, select_it);  // because of document Annotation - spans the whole range
+    select_it = cas.<AnnotationFS>select().nonOverlapping().backwards(true).fsIterator();
+    assertCount("Unambiguous select backwards annot iterator", 1, select_it);  // because of document Annotation - spans the whole range
+    
   }
   
   private String flatStateMsg(String s) {