Java Code Examples for org.apache.uima.jcas.cas.FSArray#copyFromArray()

The following examples show how to use org.apache.uima.jcas.cas.FSArray#copyFromArray() . 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: CasCompare.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
/**
   * This is an optional pre-compare operation.
   * 
   * It is identical to the method above, except that
   * after sorting, it removes duplicates. 

   * @param fs the feature structure having the fsarray feature
   * @param feat the feature having the fsarray
   * @return a runnable, which (when invoked) updates the original array with the sorted result.
   */
  public Runnable sort_dedup_FSArray(TOP fs, Feature feat) {
    FSArray<?> fsArray = (FSArray<?>)(fs.getFeatureValue(feat));
    if (fsArray == null || fsArray.size() < 2) {
      return null;
    }
    TOP[] a = fsArray._getTheArray().clone();
    clearPrevFss();
    inSortContext = true;
    Arrays.sort(a, (TOP afs1, TOP afs2) -> {
      return compareRefs(afs1, afs2, null, null);
    });
    ArrayList<TOP> dedup = new ArrayList<>(a.length);
    TOP prev = null;
    for (TOP top : a) {
      if (top == prev) {
        continue;
      }
      prev = top;
      dedup.add(top);
    }
    TOP[] r = dedup.toArray(new TOP[dedup.size()]);
    if (r.length == a.length) {
      return () -> System.arraycopy(a, 0, fsArray._getTheArray(), 0, fsArray.size());
    } else {
      CASImpl cas = fs.getCASImpl();
      FSArray<?> fsa = (FSArray<?>) cas.createArray(fsArray._getTypeImpl(), r.length);
//      FSArray<?> fsa = new FSArray<>(fs.getJCas(), r.length);
      if (IS_SHOW_PROGRESS) {
        System.out.format("Dedup found dup in cas %d for type/feature %s, removed %d%n", working_on, feat.getName(), a.length - r.length);
      }
      fsa.copyFromArray(r, 0, 0, r.length);
      return () -> fs.setFeatureValue(feat, fsa);
    }
  }
 
Example 2
Source File: JCasTest.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
public void testFSArrayAPI() {
  FSArray sa = new FSArray<>(jcas, 2);
  TOP fs1 = new TOP(jcas);
  TOP fs2 = new TOP(jcas);
  TOP[] values = {fs1, fs2}; 
  sa.copyFromArray(values, 0, 0, 2);
  
  int i = 0;
  sa.iterator();
  
  for (Object s : sa) {
    assert(s.equals(values[i++]));
  }
}