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

The following examples show how to use org.apache.uima.cas.FSIterator#moveTo() . 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: IteratorTest.java    From uima-uimaj with Apache License 2.0 6 votes vote down vote up
public void testMoveToPastEnd() {  // https://issues.apache.org/jira/browse/UIMA-4094
  this.cas.getIndexRepository().addFS(
      this.cas.createAnnotation(this.annotationType, 1,2));
  
  AnnotationFS pastEnd = this.cas.createAnnotation(this.annotationType,  2,  3);
  FSIndex<AnnotationFS> index = this.cas.getAnnotationIndex();
  FSIterator<AnnotationFS> it = index.iterator();
  it.moveTo(pastEnd);
  assertFalse(it.isValid());
  
  index = index.withSnapshotIterators();
  it = index.iterator();
  assertEquals(1, it.size());
  it.moveTo(pastEnd);
  assertFalse(it.isValid());

}
 
Example 2
Source File: IteratorTest.java    From uima-uimaj with Apache License 2.0 6 votes vote down vote up
private void tstWord(FSIndex<FeatureStructure> index) {
  FSIterator<FeatureStructure> it = index.iterator();
  assertEquals(20, it.size());  // test size
  it.moveToLast();

  FeatureStructure fs = this.cas.createFS(wType);
  fs.setStringValue(wordFeat, "word1");

  // TEST moveTo() and get()
  it.moveTo(fs);

  assertSame(fs.getType(), it.get().getType());

  Type t1 = fs.getType();
  Type t2 = wordSetIndex.find(fs).getType();
  assertSame(t1, t2);

}
 
Example 3
Source File: IndexComparitorTest.java    From uima-uimaj with Apache License 2.0 6 votes vote down vote up
public void testSetUsesType() throws Exception {
  cas.reset();
  
  ir.addFS(createFs(type1, 1, 1));
  ir.addFS(createFs(type1Sub1, 1, 1));  // same fs keys, different type
  FeatureStructure testprobe = createFs(type1Sub1, 1, 1);  // not in index, used only for key values
  FeatureStructure testprobe2 = createFs(type1, 1, 1);
  
  assertEquals(2, sortedType1.size());
  assertEquals(2, setType1.size());
  
  FSIterator<FeatureStructure> it = setType1.iterator();
  it.moveTo(testprobe);
  assertEquals("Type1", it.get().getType().getShortName());
  it.moveTo(testprobe2);
  assertEquals("Type1", it.get().getType().getShortName());
  it.moveToFirst();
  assertEquals("Type1", it.next().getType().getShortName());
  assertEquals("Type1Sub1", it.next().getType().getShortName());
  
}
 
Example 4
Source File: CasUtil.java    From uima-uimafit with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the n annotations following 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 following anchor annotation
 * @see <a href="package-summary.html#SortOrder">Order of selected feature structures</a>
 */
public static List<AnnotationFS> selectFollowing(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");
  }

  // 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);

  // 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() < annotation.getEnd()) {
    itr.moveToNext();
  }

  // add annotations from the iterator into the result list
  List<AnnotationFS> followingAnnotations = new ArrayList<AnnotationFS>();
  for (int i = 0; i < count && itr.isValid(); i++, itr.moveToNext()) {
    followingAnnotations.add(itr.get());
  }
  return followingAnnotations;
}
 
Example 5
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 6
Source File: SubiteratorTest.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
private void iterateAndcheck(JCas jcas) {
  AnnotationIndex<Token> tokenIndex = jcas.getAnnotationIndex(Token.class);
  Annotation sentence = jcas.getAnnotationIndex(Sentence.class).iterator().next();
  FSIterator<Token> tokenIterator = tokenIndex.subiterator(sentence);
  Annotation token = tokenIndex.iterator().next();
  // debug token.toString();
  tokenIterator.moveTo(token); //throws ClassCastException 
  
  // check unambiguous iterator creation
  
  FSIterator<Token> it = tokenIndex.iterator(false);
  it.moveTo(token);
}
 
Example 7
Source File: AnnotationIteratorTest.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
public void testIterator1() {
  final int annotCount = setupTheCas();
  FSIndexRepository ir = this.cas.getIndexRepository();

  /***************************************************
   * iterate over them
   ***************************************************/
  fss = new ArrayList<>();
  callCount = -1;
  iterateOverAnnotations(annotCount, fss); // annotCount is the total number of sentences and tokens
  
  callCount = -1;
  iterateOverAnnotations(annotCount, fss);  // should be using flattened version
  
  /***************************************************
   * test skipping over multiple equal items at front
   ***************************************************/
  callCount = -1;
  fss.clear();
  isSave = true;
  AnnotationFS a1, a2;
  ir.addFS(a1 = this.cas.createAnnotation(this.tokenType, 1, 6));
  a1.setStringValue(lemmaFeat, "lemma1");
  ir.addFS(a2 = this.cas.createAnnotation(this.tokenType, 1, 6));
  a2.setStringValue(lemmaFeat, "lemma2");
  
  FSIterator<Annotation> it;
  AnnotationIndex<Annotation> tokenIndex = cas.getAnnotationIndex(tokenType);
  it = tokenIndex.subiterator(a1);
  assertCount("multi equal", 0, it);
  it = tokenIndex.subiterator(a1);
  // make a new iterator that hasn't been converted to a list form internally
  it.moveTo(cas.getDocumentAnnotation());
  assertFalse(it.isValid()); 
      
}
 
Example 8
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 9
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 10
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 11
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 12
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 13
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 14
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");
//      }
//    }    
  }