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 14:25:51 UTC

svn commit: r1763282 - 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 14:25:51 2016
New Revision: 1763282

URL: http://svn.apache.org/viewvc?rev=1763282&view=rev
Log:
[UIMA-5115] support backwards

Added:
    uima/uimaj/branches/experiment-v3-jcas/uimaj-core/src/main/java/org/apache/uima/cas/impl/FsIterator_backwards.java
Modified:
    uima/uimaj/branches/experiment-v3-jcas/uimaj-core/src/main/java/org/apache/uima/cas/impl/FsIterator_limited.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

Added: 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=1763282&view=auto
==============================================================================
--- uima/uimaj/branches/experiment-v3-jcas/uimaj-core/src/main/java/org/apache/uima/cas/impl/FsIterator_backwards.java (added)
+++ uima/uimaj/branches/experiment-v3-jcas/uimaj-core/src/main/java/org/apache/uima/cas/impl/FsIterator_backwards.java Tue Oct  4 14:25:51 2016
@@ -0,0 +1,115 @@
+/*
+ * 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.impl;
+
+import java.util.NoSuchElementException;
+
+import org.apache.uima.cas.FSIterator;
+import org.apache.uima.cas.FeatureStructure;
+
+/**
+ * Wraps FSIterator<T>, runs it backwards
+ */
+class FsIterator_backwards<T extends FeatureStructure>  
+          implements LowLevelIterator<T> {
+  
+  final private LowLevelIterator<T> it; // not just for single-type iterators
+    
+  FsIterator_backwards(FSIterator<T> iterator) {
+    this.it = (LowLevelIterator<T>) iterator;
+  }
+
+  public int ll_indexSize() {
+    return it.ll_indexSize();
+  }
+
+  public LowLevelIndex<T> ll_getIndex() {
+    return it.ll_getIndex();
+  }
+
+  public boolean isValid() {
+    return it.isValid();
+  }
+
+  public T get() throws NoSuchElementException {
+    return it.get();
+  }
+
+  public T getNvc() {
+    return it.getNvc();
+  }
+
+  public void moveToNext() {
+    it.moveToPrevious();
+  }
+
+  public void moveToNextNvc() {
+    it.moveToPreviousNvc();
+  }
+
+  public void moveToPrevious() {
+    it.moveToNext();
+  }
+
+  public void moveToPreviousNvc() {
+    it.moveToNextNvc();
+  }
+
+  public void moveToFirst() {
+    it.moveToLast();
+  }
+
+  public void moveToLast() {
+    it.moveToFirst();
+  }
+
+  public void moveTo(FeatureStructure fs) {
+    it.moveTo(fs);  // moves to left most of equal, or one greater
+    LowLevelIndex<T> lli = ll_getIndex();
+    if (isValid()) {
+      if (lli.compare(get(), fs) == 0) {
+        // move to right most
+        while (true) {
+          it.moveToNextNvc();
+          if (!isValid() || lli.compare(get(), fs) != 0) {
+            break;
+          }
+        };
+        if (isValid()) {
+          it.moveToPreviousNvc();
+        } else {
+          it.moveToLast();
+        }
+      } else {
+        // is valid, but not equal - went to wrong side
+        it.moveToPreviousNvc();
+      }
+    } else {
+      // moved to one past the end.  Backwards: would be at the (backwards) first position
+      it.moveToLast();
+    }
+  }
+
+  public FSIterator<T> copy() {
+    return new FsIterator_backwards<T>(it.copy());
+  }
+
+
+}

Modified: uima/uimaj/branches/experiment-v3-jcas/uimaj-core/src/main/java/org/apache/uima/cas/impl/FsIterator_limited.java
URL: http://svn.apache.org/viewvc/uima/uimaj/branches/experiment-v3-jcas/uimaj-core/src/main/java/org/apache/uima/cas/impl/FsIterator_limited.java?rev=1763282&r1=1763281&r2=1763282&view=diff
==============================================================================
--- uima/uimaj/branches/experiment-v3-jcas/uimaj-core/src/main/java/org/apache/uima/cas/impl/FsIterator_limited.java (original)
+++ uima/uimaj/branches/experiment-v3-jcas/uimaj-core/src/main/java/org/apache/uima/cas/impl/FsIterator_limited.java Tue Oct  4 14:25:51 2016
@@ -96,7 +96,7 @@ class FsIterator_limited<T extends Featu
   }
 
   public FSIterator<T> copy() {
-    return iterator.copy();
+    return new FsIterator_limited<T>(iterator.copy(), limit);
   }
 
   public boolean isValid() {

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=1763282&r1=1763281&r2=1763282&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 14:25:51 2016
@@ -567,6 +567,11 @@ public class SelectFSs_impl <T extends F
         isSkipEquals,              
         v.indexRepository.getAnnotationFsComparator());
     }
+    
+    it = isBackwards ? new FsIterator_backwards<>(it) : it;
+    if (isBackwards) {
+      it.moveToFirst();  
+    }
     return it;
   }
     

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=1763282&r1=1763281&r2=1763282&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 14:25:51 2016
@@ -20,6 +20,8 @@
 package org.apache.uima.cas.test;
 
 import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
 import java.util.List;
 
 import org.apache.uima.cas.CAS;
@@ -278,11 +280,16 @@ public class AnnotationIteratorTest exte
     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
+    // backwards
+    select_it = SelectFSs.sselect(annotIndex).backwards().fsIterator();
+    assertCount("Normal select backwards ambiguous annot iterator", annotCount, select_it);
     
     it = annotIndex.iterator(false);  // false means create an unambiguous iterator
     assertCount("Unambiguous annot iterator", 1, it);  // because of document Annotation - spans the whole range
     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
     
     it = sentIndex.iterator(false);  //  false means create an unambiguous iterator
     assertCount("Unambigous sentence iterator", 5, it);
@@ -300,14 +307,25 @@ public class AnnotationIteratorTest exte
     select_it = cas.<AnnotationFS>select().coveredBy((Annotation) bigBound).endWithinBounds().fsIterator();
     assertCount("Subiterator select over annot with big bound, strict", 33, select_it);
 
-    select_it = cas.<AnnotationFS>select().coveredBy((Annotation) bigBound).limit(7).endWithinBounds().fsIterator();
+    select_it = cas.<AnnotationFS>select().coveredBy(bigBound).limit(7).endWithinBounds().fsIterator();
     assertCountLimit("Subiterator select limit 7 over annot with big bound, strict", 7, select_it);
 
+    Object[] o = cas.<AnnotationFS>select().coveredBy(bigBound).skip(3).toArray();
+    assertEquals(o.length, 30);
+    
+    Object[] o1 = cas.<AnnotationFS>select().coveredBy(bigBound).toArray();
+    List<AnnotationFS> l2 = cas.<AnnotationFS>select().coveredBy(bigBound).backwards().asList(AnnotationFS.class);
+    Collections.reverse(l2);
+    assertTrue(Arrays.equals(o1, l2.toArray()));
+    
     
     it = annotIndex.subiterator(bigBound, false, true);  // unambiguous, strict
     assertCount("Subiterator over annot unambiguous strict", 3, it);
     select_it = cas.<AnnotationFS>select().coveredBy((Annotation) bigBound).endWithinBounds().nonOverlapping().fsIterator();
     assertCount("Subiterator select over annot unambiguous strict", 3, select_it);
+    select_it = cas.<AnnotationFS>select().backwards().coveredBy((Annotation) bigBound).endWithinBounds().nonOverlapping().fsIterator();
+    assertCount("Subiterator select over annot unambiguous strict", 3, select_it);
+
 
     it = annotIndex.subiterator(bigBound, true, false);
     assertCount("Subiterator over annot ambiguous not-strict", 40, it);