Java Code Examples for org.apache.uima.cas.FSIterator#moveToPrevious()

The following examples show how to use org.apache.uima.cas.FSIterator#moveToPrevious() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CasUtil.java    From uima-uimafit with Apache License 2.0 6 votes vote down vote up
/**
 * This method exists simply as a convenience method for unit testing. It is not very efficient
 * and should not, in general be used outside the context of unit testing.
 * 
 * @param cas
 *          a CAS containing the annotation.
 * @param type
 *          a UIMA type.
 * @param index
 *          this can be either positive (0 corresponds to the first annotation of a type) or
 *          negative (-1 corresponds to the last annotation of a type.)
 * @return an annotation of the given type
 * @see <a href="package-summary.html#SortOrder">Order of selected feature structures</a>
 */
public static AnnotationFS selectByIndex(CAS cas, Type type, int index) {
  if (!cas.getTypeSystem().subsumes(cas.getAnnotationType(), type)) {
    throw new IllegalArgumentException("Type [" + type.getName() + "] is not an annotation type");
  }
  // withSnapshotIterators() not needed here since we return only one result   
  FSIterator<AnnotationFS> i = cas.getAnnotationIndex(type).iterator();
  int n = index;
  i.moveToFirst();
  if (n > 0) {
    while (n > 0 && i.isValid()) {
      i.moveToNext();
      n--;
    }
  }
  if (n < 0) {
    i.moveToLast();
    while (n < -1 && i.isValid()) {
      i.moveToPrevious();
      n++;
    }
  }

  return i.isValid() ? i.get() : null;
}
 
Example 2
Source File: SelectFSs_impl.java    From uima-uimaj with Apache License 2.0 6 votes vote down vote up
@Override
public T singleOrNull() {
  FSIterator<T> it = fsIterator();
  if (it.isValid()) {
    T v = it.getNvc();
    if (shift >= 0) {
      it.moveToNext();
    } else {
      it.moveToPrevious();
    }
    if (it.isValid()) {
      throw new CASRuntimeException(CASRuntimeException.SELECT_GET_TOO_MANY_INSTANCES, ti.getName(), maybeMsgPosition());
    }
    return v;
  }
  return null;
}
 
Example 3
Source File: SelectFSs_impl.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
private FSIterator<T> maybeShift(FSIterator<T> it) {
  if (shift != 0) {
    int ps = Math.abs(shift);
    
    for (int i = 0; i < ps; i++) {
      if (shift < 0) {
        it.moveToPrevious();
      } else {
        it.moveToNext();
      }
    }
  }
  return it;
}
 
Example 4
Source File: IteratorTest.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
public void testMoveTo() {
  // Add some arbitrary annotations
  for (int i = 0; i < 10; i++) {
    createFSs(i);  // add annotation, sentence, and 3 tokens, all with same begin / end
  }
  final int start = 5;
  final int end = 7;
  FSIndexRepository repo = this.cas.getIndexRepository();
  for (int i = 0; i < 10; i++) {
    // add 10 annotations with start 5, end 7
    AnnotationFS annotation = this.cas.createAnnotation(this.annotationType, start, end);  
    repo.addFS(annotation);
  }
  AnnotationFS match = this.cas.createAnnotation(this.annotationType, start, end);
  FSIndex<AnnotationFS> index = this.cas.getAnnotationIndex();
  FSIndex<AnnotationFS> ssIndex = index.withSnapshotIterators();
  FSIterator<AnnotationFS> it = index.iterator();
  assertEquals(60, it.size());
  FSIterator<AnnotationFS> ssit = ssIndex.iterator();
  assertEquals(60, it.size());
  it.moveTo(match);  // should move to left-most of the 10 with start=5 end=7
  ssit.moveTo(match);
  assertTrue(index.compare(match, it.get()) == 0);
  assertTrue(index.compare(match, ssit.get()) == 0);
  
  // The contract of moveTo() says that any preceding FS must be smaller.
  it.moveToPrevious();
  ssit.moveToPrevious();
  assertTrue(index.compare(match, it.get()) > 0);
  assertTrue(index.compare(match, ssit.get()) > 0);
}
 
Example 5
Source File: AbbreviationsExpanderAnnotator.java    From bluima with Apache License 2.0 4 votes vote down vote up
/**
 * REM: modified from CasUtils..
 * 
 * Get a list of annotations of the given annotation type constraint by a
 * start and end. It's good to provide an annotation that is close to the
 * region, to quickly move the iterator
 * 
 * @param cas
 *            a CAS.
 * @param type
 *            a UIMA type.
 * @param annotationCloseToTheRegion
 *            the covering annotation.
 * @return a return value.
 * @see Subiterator
 */
private static List<Annotation> selectMatching(CAS cas, final int begin,
        final int end, AnnotationFS annotationCloseToTheRegion) {

    final List<Annotation> list = new ArrayList<Annotation>();
    final FSIterator<AnnotationFS> it = cas.getAnnotationIndex().iterator();

    // Try to seek the insertion point.
    it.moveTo(annotationCloseToTheRegion);

    // If the insertion point is beyond the index, move back to the last.
    if (!it.isValid()) {
        it.moveToLast();
        if (!it.isValid()) {
            return list;
        }
    }

    // Ignore type priorities by seeking to the first that has the same
    // begin
    boolean moved = false;
    while (it.isValid() && (it.get()).getBegin() >= begin) {
        it.moveToPrevious();
        moved = true;
    }

    // If we moved, then we are now on one starting before the requested
    // begin, so we have to
    // move one ahead.
    if (moved) {
        it.moveToNext();
    }

    // If we managed to move outside the index, start at first.
    if (!it.isValid()) {
        it.moveToFirst();
    }

    // Skip annotations whose start is before the start parameter.
    while (it.isValid() && (it.get()).getBegin() < begin) {
        it.moveToNext();
    }

    while (it.isValid()) {
        AnnotationFS a = it.get();
        if (!(a instanceof Annotation))
            continue;

        // If the start of the current annotation is past the end parameter,
        // we're done.
        if (a.getBegin() > end) {
            break;
        }
        it.moveToNext();

        if (a.getBegin() == begin && a.getEnd() == end) {
            list.add((Annotation) a);
        }
    }
    return unmodifiableList(list);
}
 
Example 6
Source File: BlueCasUtil.java    From bluima with Apache License 2.0 4 votes vote down vote up
/**
 * REM: modified from CasUtils..
 * 
 * Get a list of annotations constraint by a certain annotation. Iterates
 * over all annotations to find the covered annotations. Does not use
 * subiterators and does not respect type prioritites. Was adapted from
 * {@link Subiterator}. Uses the same approach except that type priorities
 * are ignored.
 * 
 * @param cas
 *            a CAS.
 * @param coveringAnnotation
 *            the covering annotation.
 * @see Subiterator
 */
public static List<Annotation> selectCovered(CAS cas,
        AnnotationFS coveringAnnotation) {
    final int begin = coveringAnnotation.getBegin();
    final int end = coveringAnnotation.getEnd();

    final List<Annotation> list = new ArrayList<Annotation>();
    final FSIterator<AnnotationFS> it = cas.getAnnotationIndex().iterator();

    // Try to seek the insertion point.
    it.moveTo(coveringAnnotation);

    // If the insertion point is beyond the index, move back to the last.
    if (!it.isValid()) {
        it.moveToLast();
        if (!it.isValid()) {
            return list;
        }
    }

    // Ignore type priorities by seeking to the first that has the same
    // begin
    boolean moved = false;
    while (it.isValid() && (it.get()).getBegin() >= begin) {
        it.moveToPrevious();
        moved = true;
    }

    // If we moved, then we are now on one starting before the requested
    // begin, so we have to
    // move one ahead.
    if (moved) {
        it.moveToNext();
    }

    // If we managed to move outside the index, start at first.
    if (!it.isValid()) {
        it.moveToFirst();
    }

    // Skip annotations whose start is before the start parameter.
    while (it.isValid() && (it.get()).getBegin() < begin) {
        it.moveToNext();
    }

    boolean strict = true;
    while (it.isValid()) {
        AnnotationFS a = it.get();
        if (!(a instanceof Annotation))
            continue;

        // If the start of the current annotation is past the end parameter,
        // we're done.
        if (a.getBegin() > end) {
            break;
        }
        it.moveToNext();
        if (strict && a.getEnd() > end) {
            continue;
        }

        checkArgument(a.getBegin() >= coveringAnnotation.getBegin(),
                "Illegal begin " + a.getBegin() + " in ["
                        + coveringAnnotation.getBegin() + ".."
                        + coveringAnnotation.getEnd() + "]");
        checkArgument(
                a.getEnd() <= coveringAnnotation.getEnd(),
                "Illegal end " + a.getEnd() + " in ["
                        + coveringAnnotation.getBegin() + ".."
                        + coveringAnnotation.getEnd() + "]");

        if (!a.equals(coveringAnnotation) && !BlueCasUtil.isDocAnnot(a)) {
            list.add((Annotation) a);
        }
    }
    return unmodifiableList(list);
}
 
Example 7
Source File: CasUtil.java    From uima-uimafit with Apache License 2.0 4 votes vote down vote up
/**
 * Return an annotation preceding or following of a given reference annotation. If the type
 * parameter corresponds to the type or a subtype of the anchor annotation and the relative
 * position is 0, then the anchor annotation is returned.
 * 
 * @param cas
 *          a CAS containing the annotation.
 * @param type
 *          a type.
 * @param aAnchor
 *          anchor annotation
 * @param aPosition
 *          relative position to access. A negative value selects a preceding annotation while a
 *          positive number selects a following annotation.
 * @return the addressed annotation.
 * @throws IndexOutOfBoundsException
 *           if the relative position points beyond the type index bounds.
 * @throws IllegalArgumentException
 *           if the relative position is {@code 0} and the anchor type does not subsume the
 *           selected type.
 * @see <a href="package-summary.html#SortOrder">Order of selected feature structures</a>
 */
public static AnnotationFS selectSingleRelative(CAS cas, Type type, AnnotationFS aAnchor,
        int aPosition) {
  if (!cas.getTypeSystem().subsumes(cas.getAnnotationType(), type)) {
    throw new IllegalArgumentException("Type [" + type.getName() + "] is not an annotation type");
  }

  // move to first previous annotation
  FSIterator<AnnotationFS> itr = cas.getAnnotationIndex(type).iterator();
  itr.moveTo(aAnchor);

  if (aPosition < 0) {
    // If the insertion point is beyond the index, move back to the last.
    if (!itr.isValid()) {
      itr.moveToLast();
      if (!itr.isValid()) {
        throw new IndexOutOfBoundsException("Reached end of index while seeking.");
      }
    }

    // No need to do additional seeks here (as done in selectCovered) because the current method
    // does not have to worry about type priorities - it never returns annotations that have
    // the same offset as the reference annotation.

    // make sure we're past the beginning of the reference annotation
    while (itr.isValid() && itr.get().getEnd() > aAnchor.getBegin()) {
      itr.moveToPrevious();
    }

    for (int i = 0; i < (-aPosition - 1) && itr.isValid(); ++i, itr.moveToPrevious()) {
      // Seeking
    }

    if (!itr.isValid()) {
      throw new IndexOutOfBoundsException("Reached end of index while seeking.");
    } else {
      return itr.get();
    }
  } else if (aPosition > 0) {
    // When seeking forward, there is no need to check if the insertion point is beyond the
    // index. If it was, there would be nothing beyond it that could be found and returned.
    // The moveTo operation also does not yield an iterator being invalid because it points
    // *before the first* index entry, at max it points *to the first* index entry, so this
    // case also does not need to be handled.
    
    // No need to do additional seeks here (as done in selectCovered) because the current method
    // does not have to worry about type priorities - it never returns annotations that have
    // the same offset as the reference annotation.
    
    // make sure we're past the end of the reference annotation
    while (itr.isValid() && itr.get().getBegin() < aAnchor.getEnd()) {
      itr.moveToNext();
    }

    for (int i = 0; i < (aPosition - 1) && itr.isValid(); ++i, itr.moveToPrevious()) {
      // Seeking
    }

    if (!itr.isValid()) {
      throw new IndexOutOfBoundsException("Reached end of index while seeking.");
    } else {
      return itr.get();
    }
  } else if (cas.getTypeSystem().subsumes(aAnchor.getType(), type)) {
    return aAnchor;
  }
  else {
    throw new IllegalArgumentException(
            "Relative position cannot be 0 if the type of the anchor annotator does not subsume "
            + "the selected type.");
  }
}
 
Example 8
Source File: CasUtil.java    From uima-uimafit with Apache License 2.0 4 votes vote down vote up
/**
 * Returns the n annotations preceding the given annotation
 * 
 * @param cas
 *          a CAS.
 * @param type
 *          a UIMA type.
 * @param annotation
 *          anchor annotation
 * @param count
 *          number of annotations to collect
 * @return List of aType annotations preceding anchor annotation
 * @see <a href="package-summary.html#SortOrder">Order of selected feature structures</a>
 */
public static List<AnnotationFS> selectPreceding(CAS cas, Type type, AnnotationFS annotation,
        int count) {
  if (!cas.getTypeSystem().subsumes(cas.getAnnotationType(), type)) {
    throw new IllegalArgumentException("Type [" + type.getName() + "] is not an annotation type");
  }

  List<AnnotationFS> precedingAnnotations = new ArrayList<AnnotationFS>();

  // Seek annotation in index
  // withSnapshotIterators() not needed here since we copy the FSes to a list anyway    
  FSIterator<AnnotationFS> itr = cas.getAnnotationIndex(type).iterator();
  itr.moveTo(annotation);
  
  // If the insertion point is beyond the index, move back to the last.
  if (!itr.isValid()) {
    itr.moveToLast();
    if (!itr.isValid()) {
      return precedingAnnotations;
    }
  }

  // No need to do additional seeks here (as done in selectCovered) because the current method
  // does not have to worry about type priorities - it never returns annotations that have
  // the same offset as the reference annotation.
  
  // make sure we're past the beginning of the reference annotation
  while (itr.isValid() && itr.get().getEnd() > annotation.getBegin()) {
    itr.moveToPrevious();
  }

  // add annotations from the iterator into the result list
  for (int i = 0; i < count && itr.isValid(); itr.moveToPrevious()) {
    AnnotationFS cur = itr.get();
    if (cur.getEnd() <= annotation.getBegin()) {
      precedingAnnotations.add(itr.get());
      i++;
    }
  }

  // return in correct order
  Collections.reverse(precedingAnnotations);
  return precedingAnnotations;
}
 
Example 9
Source File: FSListIteratorImpl.java    From uima-uimaj with Apache License 2.0 4 votes vote down vote up
public FSListIteratorImpl(FSIterator<T> it) {
  super();
  this.forward = it;
  this.back = it.copy();
  it.moveToPrevious();
}
 
Example 10
Source File: SelectFSs_impl.java    From uima-uimaj with Apache License 2.0 4 votes vote down vote up
/**
   * works for AnnotationIndex or general index
   * 
   * position taken from startingFs (not necessarily an Annotation subtype)
   *   - goes to left-most "equal" using comparator, or if none equal, to the first one &gt; startingFs
   *     -- using moveTo(fs)
   * 
   * special processing for AnnotationIndex (only):
   *   - typePriority - use or ignore
   *     -- ignored: after moveTo(fs), moveToPrevious while begin and end ==
   *       // REMOVED see https://issues.apache.org/jira/browse/UIMA-5536 --- and if isPositionUsesType types are == 
   * @param it iterator to position
   * @return it positioned if needed
   */
  private FSIterator<T> maybePosition(FSIterator<T> it) {
    if (!it.isValid() || startingFs == null || boundsUse != BoundsUse.notBounded) {
      return it;
    }
    
    it.moveTo(startingFs);
    
    // next commented out because the underlying iterator already does this
//    if (index != null && index instanceof AnnotationIndex && !isFollowing && !isPreceding) {
//      if (!isTypePriority) {
//        int begin = ((Annotation)startingFs).getBegin();
//        int end = ((Annotation)startingFs).getEnd();
//        Type type = startingFs.getType();
//        Annotation fs = (Annotation) it.get();
//        while (begin == fs.getBegin() && end == fs.getEnd() 
////               && (!isPositionUsesType || type == fs.getType())
//               ) {
//          it.moveToPreviousNvc();
//          if (!it.isValid()) {
//            it.moveToFirst();
//            return it;
//          }
//          fs = (Annotation) it.get();
//        }
//        it.moveToNext();
//      }
//    }
    
    if (isFollowing) {
      final int end = ((Annotation)startingFs).getEnd();
      while (it.isValid() && ((Annotation)it.get()).getBegin() < end) {
        it.moveToNext();
      }
    } else if (isPreceding) {
      final int begin = ((Annotation)startingFs).getBegin();
      while (it.isValid() && ((Annotation)it.get()).getEnd() > begin) {
        it.moveToPrevious();
      }
    }
    
    return it;
  }
 
Example 11
Source File: AnnotationIteratorTest.java    From uima-uimaj with Apache License 2.0 4 votes vote down vote up
private void assertCount(String msg, int expected,  FSIterator<? extends Annotation> it) {
    int fssStart = assertCountCmn(msg, expected, it);
    msg = flatStateMsg(msg);
    int count = expected;
    if (count > 0) {
      // test moveTo(fs) in middle, first, and last
      AnnotationFS posFs = fss.get(fssStart + (count >> 1));
//      //debug
//      System.out.println(posFs.toString());
      
      // debug
       it.moveToLast();
       it.next();
       
       it.moveTo(posFs);
       assertEquals(msg, it.get().hashCode(), posFs.hashCode());
      
      posFs = fss.get(fssStart);
      it.moveTo(posFs);
      assertEquals(msg, it.get().hashCode(), posFs.hashCode());
      it.moveToFirst();
      assertEquals(msg, it.get().hashCode(), posFs.hashCode());
      
      posFs = fss.get(fssStart + count - 1);
      it.moveTo(posFs);
      assertEquals(msg, it.get().hashCode(), posFs.hashCode());
      it.moveToLast();
      assertEquals(msg, it.get().hashCode(), posFs.hashCode());
    } else {
      // count is 0
      it.moveToFirst();
      assertFalse(it.isValid());
      it.moveToLast();
      assertFalse(it.isValid());
      it.moveTo(cas.getDocumentAnnotation());
      assertFalse(it.isValid());    
    }
    
    // test movetoLast, moving backwards
    count = 0;
    for (it.moveToLast(); it.isValid(); it.moveToPrevious()) {
      ++count;
    }
    assertEquals(msg, expected, count);
  }
 
Example 12
Source File: GetAllIndexedTest.java    From uima-uimaj with Apache License 2.0 4 votes vote down vote up
/**
 * Test driver.
 */
public void testGetAllIndexed() throws Exception {
  initTest();
  FeatureStructure docAnnotation = this.cas.getDocumentAnnotation();
	assertNotNull(docAnnotation);
  ++this.fsCount;
  assertTrue(getIteratorSize(getAllIndexed()) == this.fsCount);
	final FeatureStructure otherAnnotationFS = this.cas.createFS(this.otherAnnotationType);
	FeatureStructure annotationFS = this.cas.createFS(this.annotationType);
	final FeatureStructure annotationBaseFS = this.cas.createFS(this.annotationBaseType);
	addFS(annotationFS);
  addFS(otherAnnotationFS);
  addFS(annotationBaseFS);
  addFS(this.cas.createFS(this.cas.getTypeSystem().getTopType()));
  assertTrue(getIteratorSize(this.cas.getAnnotationIndex().iterator()) == 2);
  addFS(createAnnot(0, 1));
  addFS(createAnnot(1, 2));
  addFS(createAnnot(2, 3));
  addFS(createAnnot(3, 4));
  
  // Iterate backwards, check only that it returns correct number of FSs
  FSIterator<FeatureStructure> it = getAllIndexed();
  int down = this.fsCount;
  for (it.moveToLast(); it.isValid(); it.moveToPrevious()) {
    --down;
  }
  assertTrue(down == 0);

  // Get all indexed, create copy and iterate in parallel.
  it = getAllIndexed();
  FSIterator<FeatureStructure> copy = it.copy();
  copy.moveToFirst();
  for (it.moveToFirst(); it.isValid(); it.moveToNext()) {
    assertTrue(copy.isValid());
    assertTrue(it.get().equals(copy.get()));
    copy.moveToNext();
  }
  assertFalse(copy.isValid());
  
  // Iterate over all indexed, create a copy at each stage, check that it gets same FS.
  for (it.moveToFirst(); it.isValid(); it.moveToNext()) {
    copy = it.copy();
    assertTrue(it.get().equals(copy.get()));
  }
  copy = it.copy();
  assertFalse(it.isValid());
  assertFalse(copy.isValid());
  
  //test getAllIndexed(Type)
  Type tokenType = this.cas.getTypeSystem().getType(TOKEN_TYPE);
  assertNotNull(tokenType);
  FSIterator<FeatureStructure> tokenIter = this.cas.getIndexRepository().getAllIndexedFS(tokenType);
  assertFalse(tokenIter.hasNext());
  Iterator<TOP> tokenIter2 = this.cas.getIndexedFSs(tokenType).iterator();
  assertFalse( tokenIter2.hasNext());
}
 
Example 13
Source File: AnnotationIndexTest.java    From uima-uimaj with Apache License 2.0 4 votes vote down vote up
private void vall(int outerIter, int innerIter) {
    long start = System.nanoTime();
    FSIterator<Annotation> it = ai.iterator();
   
//    if (innerIter == 55555) {
//      System.out.println("debug 55555");
//    }

    for (int i = 0; i < SZ; i ++) {
      Annotation fs = as[i];
//      startIter.get()[0] = innerIter > 10000 ? System.nanoTime() : -1;
//      long iterStart = System.nanoTime();
      it.moveTo(fs);
//      long inter2 = System.nanoTime();
//      long inter = inter2 - iterStart;
//      if (innerIter == 55555) {
//        System.out.format("moveTo for innerIter:         %,d item: %d took: %,5d %s%n", innerIter, i, inter, fs);
//      }
//      inter2 = System.nanoTime();
      it.moveToPrevious();
//      inter = System.nanoTime() - inter2;
//      if (innerIter == 55555) {
//        System.out.format("moveToPrevious for innerIter: %,d item: %d took: %,5d %s%n", innerIter, i, inter, fs);
//      }
//      if (innerIter > 10000) {
//        iterTimes.add(new Miter(outerIter, innerIter, i, System.nanoTime() - startIter.get()[0]));
//      }
      if (it.isValid()) {
        if (fs.getBegin() != it.get().getBegin() + 1) {
          System.out.println("debug mismatch");
          fail();
        }
      } else {
        if (fs.getBegin() != 0) {
          System.out.println("debug mismatch");
          fail();
        }
      }
    }
    long inc = System.nanoTime() - start;
    valTime += inc;
    
//    TOP[] cc = a.getInternalArrayDebug();
//    for (int i = 0; i < SZ; i++) {
//      if (cc[i] == null) {
//        System.out.println("debug found null");
//      }
//    }    
  }