Java Code Examples for org.apache.uima.jcas.cas.TOP#getFeatureValue()

The following examples show how to use org.apache.uima.jcas.cas.TOP#getFeatureValue() . 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: XCASSerializer.java    From uima-uimaj with Apache License 2.0 6 votes vote down vote up
private void encodeFeatures(TOP fs, AttributesImpl attrs) {
  TypeImpl ti = fs._getTypeImpl();
  
  for (FeatureImpl fi : ti.getFeatureImpls()) {
    String attrValue;
    if (fi.getRangeImpl().isRefType) {
      TOP v = fs.getFeatureValue(fi);
      attrValue = (null == v) ? null : Integer.toString(v._id);
    } else {
      attrValue = fs.getFeatureValueAsString(fi);
    }
    if (attrValue != null) {
      addAttribute(attrs, featureNames[fi.getCode()], attrValue);
    }
  }
}
 
Example 2
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 3
Source File: XCASSerializer.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
private void enqueueFeatures(TOP fs, int heapValue) {
  TypeImpl ti = fs._getTypeImpl();
  
  if (fs instanceof UimaSerializable) {
    ((UimaSerializable)fs)._save_to_cas_data();
  }
  for (FeatureImpl fi : ti.getFeatureImpls()) {
    if (fi.getRangeImpl().isRefType) {
      TOP v = fs.getFeatureValue(fi);
      if (null != v) {
        enqueue(v);
      }
    }
  }
}
 
Example 4
Source File: CASImpl.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
/**
 * Handle some unusual backwards compatibility cases
 *   featureCode = 0 - implies getting the type code
 *   feature range is int - normal
 *   feature range is a fs reference, return the id 
 *   feature range is a string: add the string if not already present to the string heap, return the int handle.
 * @param fsRef -
 * @param featureCode -
 * @return -
 */
@Override
public final int ll_getIntValue(int fsRef, int featureCode) {
  TOP fs = getFsFromId_checked(fsRef);
  if (featureCode == 0) {
    return fs._getTypeImpl().getCode(); // case where the type is being requested
  }
  FeatureImpl fi = getFeatFromCode_checked(featureCode);
  
  SlotKind kind = fi.getSlotKind();
  switch(kind) {
  case Slot_HeapRef:
    return fs.getFeatureValue(fi)._id;

  case Slot_Boolean:
  case Slot_Byte:
  case Slot_Short:
  case Slot_Int:
  case Slot_Float:
    return fs._getIntValueNc(fi);
    
  case Slot_StrRef:
    return getCodeForString(fs._getStringValueNc(fi));
    
  case Slot_LongRef:
    return getCodeForLong(fs._getLongValueNc(fi));
  case Slot_DoubleRef:
    return getCodeForLong(CASImpl.double2long(fs._getDoubleValueNc(fi)));
    
  default: throw new CASRuntimeException(
        CASRuntimeException.INAPPROP_RANGE, 
        fi.getName(), 
        "int", 
        fi.getRange().getName());
  }
}
 
Example 5
Source File: CASImpl.java    From uima-uimaj with Apache License 2.0 4 votes vote down vote up
/**
   * Special considerations:
   *   Interface with corruption checking
   *   For backwards compatibility:
   *     handle cases where feature is:
   *       int - normal
   *       0 - change type code
   *       a ref: treat int as FS "addr"
   *       not an int: handle like v2 where reasonable
   */
  @Override
  public final void ll_setIntValue(int fsRef, int featureCode, int value) {
    TOP fs = getFsFromId_checked(fsRef);
    
    if (featureCode == 0) {
      switchFsType(fs, value);
      return;
    }
    
    FeatureImpl fi = getFeatFromCode_checked(featureCode);
    
    if (fs._getTypeImpl().isArray()) {
      throw new UnsupportedOperationException("ll_setIntValue not permitted to set a feature of an array");
    }
    SlotKind kind = fi.getSlotKind();
    
    switch(kind) {
    case Slot_HeapRef: 
      if (fi.getCode() == annotBaseSofaFeatCode) {
        // setting the sofa ref of an annotationBase
        // can't change this so just verify it's the same
        TOP sofa = fs.getFeatureValue(fi);
        if (sofa._id != value) {
          throw new UnsupportedOperationException("ll_setIntValue not permitted to change a sofaRef feature");          
        }
        return;  // if the same, just ignore, already set
      }
      
      TOP ref = fs._casView.getFsFromId_checked(value);
      fs.setFeatureValue(fi, ref); // does the right feature check, too
      return;
 
    case Slot_Boolean: 
    case Slot_Byte: 
    case Slot_Short: 
    case Slot_Int:
    case Slot_Float:
      fs._setIntValueCJ(fi, value); 
      break;
      
    case Slot_StrRef:
      String s = getStringForCode(value);
      if (s == null && value != 0) {
        Misc.internalError(new Exception("ll_setIntValue got null string for non-0 handle: " + value));
      }
      fs._setRefValueNfcCJ(fi, getStringForCode(value));
      break;
      
    case Slot_LongRef:
    case Slot_DoubleRef:
      Long lng = getLongForCode(value);
      if (lng == null) {
        Misc.internalError(new Exception("ll_setIntValue got null Long/Double for handle: " + value));
      }
      fs._setLongValueNfcCJ(fi, lng);
      break;
      
    default: 
      CASRuntimeException e = new CASRuntimeException(CASRuntimeException.INAPPROP_RANGE, fi.getName(), "int", fi.getRange().getName());
//      System.err.println("debug " + e);
      throw e;  
    }  
  }
 
Example 6
Source File: CasComparer.java    From uima-uimaj with Apache License 2.0 4 votes vote down vote up
private int compareArrayFSs(TOP arrayFS1fs, Feature feat1, TOP arrayFS2fs, Feature feat2, Set<TOP> visited) {

  CommonArrayFS arrayFS1 = (CommonArrayFS)arrayFS1fs.getFeatureValue(feat1);
  CommonArrayFS arrayFS2 = (CommonArrayFS)arrayFS2fs.getFeatureValue(feat2);
  
  if (null == arrayFS1 && null == arrayFS2)return 0; // are equal
  if (null == arrayFS1) return chkEqual(-1,  "Array FS1 is null, but Array FS2 is not");
  if (null == arrayFS2) return chkEqual(-1,  "Array FS2 is null, but Array FS1 is not");

  int r, len;
  if (0 != (r = Integer.compare(len = arrayFS1.size(), arrayFS2.size()))) {
    return chkEqual(r, "ArrayFSs are different sizes, fs1 size is %d, fs2 size is %d", arrayFS1.size(), arrayFS2.size());
  }
  // are same size
  r = validateSameType(arrayFS1, arrayFS2);
  if (0 != r) return r;
  
  switch(getArrayType(arrayFS1)) {
  case FS:
    for (int j = 0; j < len; j++) {
      if (0 != (r = compare1((TOP)((FSArray)arrayFS1).get(j), (TOP)((FSArray)arrayFS2).get(j), visited))) return r;
    }
    break;
  case BOOLEAN:
    for (int j = 0; j < len; j++) {
      if (0 != (r = compBoolean(((BooleanArrayFS)arrayFS1).get(j), ((BooleanArrayFS)arrayFS2).get(j)))) return r;
    }
    break;
  case BYTE:
    for (int j = 0; j < len; j++) {
      if (0 != (r = compLong(((ByteArrayFS)arrayFS1).get(j), ((ByteArrayFS)arrayFS2).get(j)))) return r;
    }
    break;
  case SHORT:
    for (int j = 0; j < len; j++) {
      if (0 != (r = compLong(((ShortArrayFS)arrayFS1).get(j), ((ShortArrayFS)arrayFS2).get(j)))) return r;
    }
    break;
  case INT:
    for (int j = 0; j < len; j++) {
      if (0 != (r = compLong(((IntArrayFS)arrayFS1).get(j), ((IntArrayFS)arrayFS2).get(j)))) return r;
    }
    break;
  case LONG:
    for (int j = 0; j < len; j++) {
      if (0 != (r = compLong(((LongArrayFS)arrayFS1).get(j), ((LongArrayFS)arrayFS2).get(j)))) return r;
    }
    break;
  case FLOAT:
    for (int j = 0; j < len; j++) {
      if (0 != (r = compDouble(((FloatArrayFS)arrayFS1).get(j), ((FloatArrayFS)arrayFS2).get(j)))) return r;
    }
    break;
  case DOUBLE:
    for (int j = 0; j < len; j++) {
      if (0 != (r = compDouble(((DoubleArrayFS)arrayFS1).get(j), ((DoubleArrayFS)arrayFS2).get(j)))) return r;
    }
    break;
  case STRING:
    for (int j = 0; j < len; j++) {
      if (0 != (r = compStr(((StringArrayFS)arrayFS1).get(j), ((StringArrayFS)arrayFS2).get(j)))) {
        return chkEqual(r, "String miscompare, s1 = %s, s2 = %s", ((StringArrayFS)arrayFS1).get(j), ((StringArrayFS)arrayFS2).get(j));
      }
    }
    break;
  }
  return 0;  // all were equal
}