Java Code Examples for org.apache.uima.cas.FSIterator#moveToFirst()
The following examples show how to use
org.apache.uima.cas.FSIterator#moveToFirst() .
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: IndexComparitorTest.java From uima-uimaj with Apache License 2.0 | 6 votes |
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 2
Source File: FSCollectionFactory.java From uima-uimafit with Apache License 2.0 | 6 votes |
@Override public int size() { // Unfortunately FSIterator does not expose the sizes of its internal collection, // neither the current position although FSIteratorAggregate has a private field // with that information. if (sizeCache == -1) { synchronized (this) { if (sizeCache == -1) { FSIterator<T> clone = index.copy(); clone.moveToFirst(); sizeCache = 0; while (clone.isValid()) { sizeCache++; clone.moveToNext(); } } } } return sizeCache; }
Example 3
Source File: CasUtil.java From uima-uimafit with Apache License 2.0 | 6 votes |
/** * 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 4
Source File: FSTreeModel.java From uima-uimaj with Apache License 2.0 | 6 votes |
/** * Update. * * @param indexName the index name * @param index the index * @param cas1 the cas 1 */ public void update(String indexName, FSIndex index, CAS cas1) { // this.indexName = indexName; this.cas = (CASImpl) cas1; final int size = index.size(); this.rootString = "<html><font color=green>" + indexName + "</font> - <font color=blue>" + index.getType().getName() + "</font> [" + size + "]</html>"; this.root = new FSNode(this, FSNode.DISPLAY_NODE, null, 0, null); this.fss = new ArrayList<>(); FSIterator<TOP> it = index.iterator(); int count = 0; for (it.moveToFirst(); it.isValid(); it.moveToNext()) { TOP fs = it.get(); this.fss.add(new FSNode(this, getNodeType(fs.getType()), fs, fs._id(), count)); ++count; } List<FSTreeNode> kids = createArrayChildren(0, size, this.fss, this); this.root.setChildren(kids); Object[] path = new Object[1]; path[0] = this.root; TreeModelEvent event = new TreeModelEvent(this.root, path); for (int i = 0; i < this.treeModelListeners.size(); i++) { this.treeModelListeners.get(i).treeStructureChanged(event); } }
Example 5
Source File: FsIterator_aggregation_common.java From uima-uimaj with Apache License 2.0 | 6 votes |
public void moveToNextNvc() { FSIterator<T> it = nonEmptyIterators[current_it_idx]; it.moveToNextNvc(); if (it.isValid()) { return; } final int nbrIt = nonEmptyIterators.length; for (int i = current_it_idx + 1; i < nbrIt; i++) { it = nonEmptyIterators[i]; it.moveToFirst(); if (it.isValid()) { current_it_idx = i; return; } } current_it_idx = -1; // invalid position }
Example 6
Source File: FilteredIteratorTest.java From uima-uimaj with Apache License 2.0 | 6 votes |
private void iterAndCount2b() { FSIterator<AnnotationFS> it = cas.getAnnotationIndex(tokenType).iterator(); ConstraintFactory cf = this.cas.getConstraintFactory(); FSTypeConstraint tc = cf.createTypeConstraint(); tc.add(sepType); tc.add(eosType.getName()); ArrayList<String> path = new ArrayList<>(); path.add(tokenTypeFeat.getShortName()); FSMatchConstraint cons = cf.embedConstraint(path, tc); it = this.cas.createFilteredIterator(it, cons); int count = 0; for (it.moveToFirst(); it.isValid(); it.moveToNext()) { ++count; } assertTrue(count == 4); }
Example 7
Source File: AnnotationWriter.java From uima-uimaj with Apache License 2.0 | 6 votes |
/** * print the cas content to the output file * * @param aCAS * CasContainer which has been populated by the TAEs * * @throws ResourceProcessException * if there is an error in processing the Resource * * @see org.apache.uima.collection.base_cpm.CasObjectProcessor#processCas(CAS) */ public synchronized void processCas(CAS aCAS) throws ResourceProcessException { try { // iterate and print annotations FSIterator<Annotation> typeIterator = aCAS.getCurrentView().<Annotation>getAnnotationIndex().iterator(); for (typeIterator.moveToFirst(); typeIterator.isValid(); typeIterator.moveToNext()) { AnnotationFS annot = typeIterator.get(); this.fileWriter.write(annot.getCoveredText()); this.fileWriter.write(System.getProperty("line.separator")); this.fileWriter.write(annot.toString()); } this.fileWriter.flush(); } catch (Exception ex) { ex.printStackTrace(); } }
Example 8
Source File: CASImpl.java From uima-uimaj with Apache License 2.0 | 5 votes |
public <T extends AnnotationFS> T getDocumentAnnotationNoCreate() { if (this == this.svd.baseCAS) { // base CAS has no document return null; } FSIterator<Annotation> it = getDocAnnotIter(); it.moveToFirst(); // revalidate in case index updated if (it.isValid()) { Annotation r = it.get(); return (T) (inPearContext() ? pearConvert(r) : r); } return null; }
Example 9
Source File: IteratorTest.java From uima-uimaj with Apache License 2.0 | 5 votes |
private void fastFailTst(FSIndex<FeatureStructure> index, boolean isShouldFail) { FSIterator<FeatureStructure> it = index.iterator(); it.moveToLast(); it.moveToFirst(); // moved to first, 2.7.0, because new bag iterator is more forgiving re concurrentmodexception FeatureStructure a = it.get(); cas.removeFsFromIndexes(a); cas.addFsToIndexes(a); expectCCE(a, it, isShouldFail); expectCCE(a, it, false); // ok because above expectCCE reset the iterator }
Example 10
Source File: ArrayIndexTest.java From uima-uimaj with Apache License 2.0 | 5 votes |
private static final int countIndexMembers(FSIndex<? extends FeatureStructure> idx) { FSIterator<? extends FeatureStructure> it = idx.iterator(); int count = 0; for (it.moveToFirst(); it.isValid(); it.moveToNext()) { ++count; } return count; }
Example 11
Source File: IteratorTest.java From uima-uimaj with Apache License 2.0 | 5 votes |
private void verifyHaveSubset(FSIterator<?> x, int nbr, Type type) { x.moveToFirst(); int i = 0; while (x.hasNext()) { i++; assertEquals(type, x.get().getType()); x.moveToNext(); } assertEquals(nbr, i); }
Example 12
Source File: FilteredIteratorTest.java From uima-uimaj with Apache License 2.0 | 5 votes |
private void iterAndCount2a() { String lemma = "the"; FSIterator<AnnotationFS> it = cas.getAnnotationIndex(tokenType).iterator(); FSStringConstraint type1Constraint = cas.getConstraintFactory().createStringConstraint(); type1Constraint.equals(lemma); ArrayList<String> path = new ArrayList<>(); path.add(lemmaFeat.getShortName()); FSMatchConstraint cons = cas.getConstraintFactory().embedConstraint(path, type1Constraint); it = cas.createFilteredIterator(it, cons); int count = 0; for (it.moveToFirst(); it.isValid(); it.moveToNext()) { ++count; } // ///////////////////////////////////////////////////////////// // Count instances of tokens with lemma "the". // Create an iterator over Token annotations. FSIndex<AnnotationFS> tokenIndex = cas.getAnnotationIndex(tokenType); FSIterator<AnnotationFS> tokenIt = tokenIndex.iterator(); // Create a counter. int theCount = 0; // Iterate over the tokens. for (tokenIt.moveToFirst(); tokenIt.isValid(); tokenIt.moveToNext()) { AnnotationFS tok = tokenIt.get(); if (tok.getStringValue(lemmaFeat).equals(lemma)) { ++theCount; // System.out.println("Found token: " + tok.getCoveredText()); } } assertTrue(count == theCount); // System.out.println( // "Number of tokens with \"" + lemma + "\": " + theCount); // System.out.println("Number of tokens overall: " + tokenIndex.size()); // System.out.println("Count: " + count); // assertTrue(count == 4); }
Example 13
Source File: FilteredIteratorTest.java From uima-uimaj with Apache License 2.0 | 5 votes |
private void iterAndCount2() { String lemma = "the"; // create filtered iterator over Tokens of type 1 FSIterator<AnnotationFS> it = cas.getAnnotationIndex(tokenType).iterator(); FSStringConstraint type1Constraint = cas.getConstraintFactory().createStringConstraint(); type1Constraint.equals(lemma); FeaturePath path = cas.createFeaturePath(); path.addFeature(lemmaFeat); FSMatchConstraint cons = cas.getConstraintFactory().embedConstraint(path, type1Constraint); it = cas.createFilteredIterator(it, cons); int count = 0; for (it.moveToFirst(); it.isValid(); it.moveToNext()) { ++count; } // ///////////////////////////////////////////////////////////// // Count instances of tokens with lemma "the". // Create an iterator over Token annotations. FSIndex<AnnotationFS> tokenIndex = cas.getAnnotationIndex(tokenType); FSIterator<AnnotationFS> tokenIt = tokenIndex.iterator(); // Create a counter. int theCount = 0; // Iterate over the tokens. for (tokenIt.moveToFirst(); tokenIt.isValid(); tokenIt.moveToNext()) { AnnotationFS tok = tokenIt.get(); if (tok.getStringValue(lemmaFeat).equals(lemma)) { ++theCount; // System.out.println("Found token: " + tok.getCoveredText()); } } assertTrue(count == theCount); // System.out.println( // "Number of tokens with \"" + lemma + "\": " + theCount); // System.out.println("Number of tokens overall: " + tokenIndex.size()); // System.out.println("Count: " + count); // assertTrue(count == 4); }
Example 14
Source File: SerializationReinitTest.java From uima-uimaj with Apache License 2.0 | 4 votes |
private void createSentences() throws CASException { // TypeSystem ts = cas.getTypeSystem(); // Type eosType = ts.getType(EOS_TYPE); // Type tokenType = ts.getType(TOKEN_TYPE); // //assert(tokenType != null); // Type sentenceType = ts.getType(SENT_TYPE); // Feature tokenTypeFeature = ts.getFeature(TOKEN_TYPE_FEAT); // Feature startFeature = ts.getFeature(CAS.START_FEAT); // Feature endFeature = ts.getFeature(CAS.END_FEAT); // System.out.println("\nCreating sentence annotations."); // Get a handle to the index repository. FSIndexRepository indexRepository = cas.getIndexRepository(); // assert(indexRepository != null); Iterator<String> labelIt = indexRepository.getLabels(); assertTrue(labelIt != null); // Get the standard index for tokens. FSIndex<AnnotationFS> tokenIndex = cas.getAnnotationIndex(tokenType); // assert(tokenIndex != null); // Get an iterator over tokens. FSIterator<AnnotationFS> it = tokenIndex.iterator(); // assert(it != null); // Now create sentences. We do this as follows: a sentence starts where // the first token after an EOS starts, and ends with an EOS. long time = System.currentTimeMillis(); int endOfSentenceCounter = 0; it.moveToFirst(); boolean lookForStart = true; int start = 0, end; // Initialize start to pacify compiler. FeatureStructure tokenFS, sentFS; while (it.isValid()) { if (lookForStart) { // If we're looking for the start of a sentence, just grab the start // of the current FS. start = it.get().getIntValue(startFeature); lookForStart = false; } else { // Check if we've reached the end of a sentence. tokenFS = it.get(); if (tokenFS.getFeatureValue(tokenTypeFeature).getType() == eosType) { end = tokenFS.getIntValue(endFeature); sentFS = cas.createFS(sentenceType); sentFS.setIntValue(startFeature, start); sentFS.setIntValue(endFeature, end); cas.getIndexRepository().addFS(sentFS); ++endOfSentenceCounter; lookForStart = true; } } it.moveToNext(); } time = System.currentTimeMillis() - time; // System.out.println("Created " + endOfSentenceCounter + " sentences: " + new TimeSpan(time)); }
Example 15
Source File: SerializationNoMDTest.java From uima-uimaj with Apache License 2.0 | 4 votes |
private void createSentences() { // TypeSystem ts = cas.getTypeSystem(); // Type eosType = ts.getType(EOS_TYPE); // Type tokenType = ts.getType(TOKEN_TYPE); // //assert(tokenType != null); // Type sentenceType = ts.getType(SENT_TYPE); // Feature tokenTypeFeature = ts.getFeature(TOKEN_TYPE_FEAT); // Feature startFeature = ts.getFeature(CAS.START_FEAT); // Feature endFeature = ts.getFeature(CAS.END_FEAT); // System.out.println("\nCreating sentence annotations."); // Get a handle to the index repository. FSIndexRepository indexRepository = cas.getIndexRepository(); // assert(indexRepository != null); Iterator<String> labelIt = indexRepository.getLabels(); assertTrue(labelIt != null); // Get the standard index for tokens. FSIndex<AnnotationFS> tokenIndex = cas.getAnnotationIndex(tokenType); // assert(tokenIndex != null); // Get an iterator over tokens. FSIterator<AnnotationFS> it = tokenIndex.iterator(); // assert(it != null); // Now create sentences. We do this as follows: a sentence starts where // the first token after an EOS starts, and ends with an EOS. long time = System.currentTimeMillis(); int endOfSentenceCounter = 0; it.moveToFirst(); boolean lookForStart = true; int start = 0, end; // Initialize start to pacify compiler. FeatureStructure tokenFS, sentFS; while (it.isValid()) { if (lookForStart) { // If we're looking for the start of a sentence, just grab the start // of the current FS. start = it.get().getIntValue(startFeature); lookForStart = false; } else { // Check if we've reached the end of a sentence. tokenFS = it.get(); if (tokenFS.getFeatureValue(tokenTypeFeature).getType() == eosType) { end = tokenFS.getIntValue(endFeature); sentFS = cas.createFS(sentenceType); sentFS.setIntValue(startFeature, start); sentFS.setIntValue(endFeature, end); cas.getIndexRepository().addFS(sentFS); ++endOfSentenceCounter; lookForStart = true; } } it.moveToNext(); } time = System.currentTimeMillis() - time; // System.out.println("Created " + endOfSentenceCounter + " sentences: " + new TimeSpan(time)); }
Example 16
Source File: GetAllIndexedTest.java From uima-uimaj with Apache License 2.0 | 4 votes |
/** * 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 17
Source File: AnnotationIteratorTest.java From uima-uimaj with Apache License 2.0 | 4 votes |
private void assertCountLimit(String msg, int expected, FSIterator<? extends Annotation> it) { assertCountCmn(msg, expected, it); it.moveToFirst(); assertFalse(it.isValid()); }
Example 18
Source File: AnnotationIteratorTest.java From uima-uimaj with Apache License 2.0 | 4 votes |
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 19
Source File: AbbreviationsExpanderAnnotator.java From bluima with Apache License 2.0 | 4 votes |
/** * 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 20
Source File: BlueCasUtil.java From bluima with Apache License 2.0 | 4 votes |
/** * 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); }