Java Code Examples for org.apache.uima.cas.Type#isPrimitive()

The following examples show how to use org.apache.uima.cas.Type#isPrimitive() . 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: Primitives.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
/**
 * Retrieve the primitive java class for a primitive type.
 *
 * @param ts the ts
 * @param type the type
 * @return the primitive java class
 */
public static Class<?> getPrimitiveClass(TypeSystem ts, Type type) {
  if (!type.isPrimitive())
    throw new IllegalArgumentException("Type " + type.getName() + " is not primitive!");
  
  // Note:
  // In a UIMA type system *only* the primitive string type can be 
  // sub-typed.
  
  if (ts.getType(CAS.TYPE_NAME_BOOLEAN).equals(type)) {
    return Boolean.class;
  }
  else if (ts.getType(CAS.TYPE_NAME_BYTE).equals(type)) {
    return Byte.class;
  }
  else if (ts.getType(CAS.TYPE_NAME_SHORT).equals(type)) {
    return Short.class;
  }
  else if (ts.getType(CAS.TYPE_NAME_INTEGER).equals(type)) {
    return Integer.class;
  }
  else if (ts.getType(CAS.TYPE_NAME_LONG).equals(type)) {
    return Long.class;
  }
  else if (ts.getType(CAS.TYPE_NAME_FLOAT).equals(type)) {
    return Float.class;
  }
  else if (ts.getType(CAS.TYPE_NAME_DOUBLE).equals(type)) {
    return Double.class;
  }
  else if (ts.getType(CAS.TYPE_NAME_STRING).equals(type) || 
          ts.subsumes(ts.getType(CAS.TYPE_NAME_STRING), type)) {
    return String.class;
  }
  else {
    throw new IllegalStateException("Unexpected primitive type: " + type.getName());
  }
}
 
Example 2
Source File: AddFeatureDialog.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
@Override
  public TypesWithNameSpaces getTypeSystemInfoList() {
    TypesWithNameSpaces result = super.getTypeSystemInfoList();
    Type[] allTypes = (Type[]) editor.allTypes.get().values().toArray(new Type[0]);
/*    Arrays.sort(allTypes, new Comparator() {

      public int compare(Object o1, Object o2) {
        Type t1 = (Type) o1;
        Type t2 = (Type) o2;
        return t1.getShortName().compareTo(t2.getShortName());
      }
    });
    */
    for (int i = 0; i < allTypes.length; i++) {
      Type type = allTypes[i];
      if (typeFilter == ONLY_NON_PRIMITIVE_TYPES) {
        if (!type.isPrimitive()) {
          result.add(type.getName());
        }
      } else {
        result.add(type.getName());
      }
    }
    if (typeFilter == ALL_TYPES)
      allTypesList = result;
    return result;
  }
 
Example 3
Source File: CasAnnotationViewer.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
/**
   * Get feature value in string, if value is not another annotation and not
   * an array of annotations.
   *
   * @param aFS the a FS
   * @param feature the feature
   * @return the feature value in string
   */
  private String getFeatureValueInString(FeatureStructure aFS, Feature feature) {
    if (this.cas == null || this.typeSystem == null || this.stringType == null || this.fsArrayType == null) {
      return "null";
    }

    Type rangeType = feature.getRange();
    if (this.typeSystem.subsumes(this.fsArrayType, rangeType)) {
      // If the feature is an FSArray, cannot render it as simple as "name=value".
      return "*FSArray*";
    }
    if (this.typeSystem.subsumes(this.stringType, rangeType)) {
      return checkString(aFS.getStringValue(feature), "null", 64);
    }
    if (rangeType.isPrimitive()) {
      return checkString(aFS.getFeatureValueAsString(feature), "null", 64);
    }
    if (rangeType.isArray()) {
//      String rangeTypeName = rangeType.getName();
      CommonArrayFS arrayFS = (CommonArrayFS) aFS.getFeatureValue(feature);
      String[] values = (arrayFS == null) ? null : arrayFS.toStringArray();
      if (values == null || values.length == 0) {
        return "null";
      }

      StringBuffer displayValue = new StringBuffer();
      displayValue.append("[");
      for (int i = 0; i < values.length - 1; i++) {
        displayValue.append(values[i]);
        displayValue.append(",");
      }
      displayValue.append(values[values.length - 1]);
      displayValue.append("]");
      return displayValue.toString();
    }

    // If none of the above, then it is an annotation object. Cannot render it as simple as "name=value".
    return "*FS*";
  }
 
Example 4
Source File: FSIndexRepositoryImpl.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
/**
 * @see org.apache.uima.cas.FSIndexRepository#getIndex(String, Type)
 * Find iicp by label (for type it was defined for)
 *   - if not found, return null
 * 
 * Also return null if is an array type of some non-primitive, not TOP (???)
 *   
 * Throw exception if type not subsumed by the top level type
 * 
 * Search all iicps for the type to find the one with same indexing strategy and keys as the iicp for the label  
 */

public <T extends FeatureStructure> FSIndex<T> getIndex(String label, Type type) {
  
  // iicp is for the type the index was defined for
  final FsIndex_iicp<TOP> iicp = this.name2indexMap.get(label);
  if (iicp == null) {
    return null;
  }
  // Why is this necessary?
  // probably because we don't support indexes over FSArray<some-particular-type>
  if (type.isArray()) {
    final Type componentType = type.getComponentType();
    if ((componentType != null) && !componentType.isPrimitive()
        && !componentType.getName().equals(CAS.TYPE_NAME_TOP)) {
      return null;
    }
  }
  
  final TypeImpl indexType = iicp.fsIndex_singletype.getTypeImpl();
  final TypeImpl ti = (TypeImpl) type;
  if (!indexType.subsumes(ti)) {
    throw new CASRuntimeException(CASRuntimeException.TYPE_NOT_IN_INDEX, label, type.getName(), indexType.getName());
  }

  // Since we found an index for the correct type, and 
  // named indexes at creation time create all their subtype iicps, find() must return a
  // valid result
  return (FSIndex<T>) this.getIndexBySpec(ti.getCode(), iicp.getIndexingStrategy(), iicp.getComparatorImplForIndexSpecs()); 
}
 
Example 5
Source File: FeatureStructureImplC.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
private void _check_feature_range_is_FeatureStructure(Feature feat, FeatureStructureImplC fs) {
  Type range = feat.getRange();
  if (range.isPrimitive()) {
    throw new CASRuntimeException(CASRuntimeException.INAPPROP_RANGE_NOT_FS,
        feat.getName(), fs.getType().getName(), feat.getRange().getName() );
  }
}
 
Example 6
Source File: EditViewPage.java    From uima-uimaj with Apache License 2.0 4 votes vote down vote up
/**
 * Creates the FS.
 *
 * @param type the type
 * @param arraySize the array size
 * @return the feature structure
 */
FeatureStructure createFS(Type type, int arraySize) {

  if (type.isPrimitive()) {
    throw new IllegalArgumentException("Cannot create FS for primitive type!");
  }

  FeatureStructure fs;

  TypeSystem ts = document.getCAS().getTypeSystem();

  if (type.isArray()) {
    switch (type.getName()) {
      case CAS.TYPE_NAME_BOOLEAN_ARRAY:
        fs = document.getCAS().createBooleanArrayFS(arraySize);
        break;
      case CAS.TYPE_NAME_BYTE_ARRAY:
        fs = document.getCAS().createByteArrayFS(arraySize);
        break;
      case CAS.TYPE_NAME_SHORT_ARRAY:
        fs = document.getCAS().createShortArrayFS(arraySize);
        break;
      case CAS.TYPE_NAME_INTEGER_ARRAY:
        fs = document.getCAS().createIntArrayFS(arraySize);
        break;
      case CAS.TYPE_NAME_LONG_ARRAY:
        fs = document.getCAS().createLongArrayFS(arraySize);
        break;
      case CAS.TYPE_NAME_FLOAT_ARRAY:
        fs = document.getCAS().createFloatArrayFS(arraySize);
        break;
      case CAS.TYPE_NAME_DOUBLE_ARRAY:
        fs = document.getCAS().createDoubleArrayFS(arraySize);
        break;
      case CAS.TYPE_NAME_STRING_ARRAY:
        fs = document.getCAS().createStringArrayFS(arraySize);
        break;
      case CAS.TYPE_NAME_FS_ARRAY:
        fs = document.getCAS().createArrayFS(arraySize);
        break;
      default:
        throw new CasEditorError("Unkown array type: " + type.getName() + "!");
    }
  }
  else if (ts.subsumes(ts.getType(CAS.TYPE_NAME_ANNOTATION), type)) {

	// get begin of selection from editor, if any
	// TODO: Add an interface to retrieve the span from the editor

	int begin = 0;
	int end = 0;

	if (editor instanceof AnnotationEditor) {
	  Point selection = ((AnnotationEditor) editor).getSelection();

	  begin = selection.x;
	  end = selection.y;
	}

	fs = document.getCAS().createAnnotation(type, begin, end);
  }
  else if (!type.isArray()) {
    fs = document.getCAS().createFS(type);
  }
  else {
    throw new TaeError("Unexpected error!");
  }

  return fs;
}
 
Example 7
Source File: TypeSystemImpl.java    From uima-uimaj with Apache License 2.0 4 votes vote down vote up
public boolean isInInt(Type rangeType) {
  return (rangeType == null) 
           ? false
           : (rangeType.isPrimitive() && !subsumes(stringType, rangeType));
}
 
Example 8
Source File: FSIndexComparatorImpl.java    From uima-uimaj with Apache License 2.0 4 votes vote down vote up
private boolean checkType(Type t) {
  return t.isPrimitive();
}
 
Example 9
Source File: TypeSystemImpl.java    From uima-uimaj with Apache License 2.0 3 votes vote down vote up
/**
 * Given a component type, return the parent type of the corresponding array type,
 * without needing the corresponding array type to exist (yet).
 *    component type ->  (member of) array type  -> UIMA parent type of that array type.
 *    
 * The UIMA Type parent of an array is either
 *   ArrayBase (for primitive arrays, plus String[] and TOP[] (see below) 
 *   FsArray - for XYZ[] reference kinds of arrays
 *   
 * The UIMA Type parent chain goes like this:
 *   primitive_array -> ArrayBase -> TOP (primitive: boolean, byte, short, int, long, float, double, String)
 *   String[]        -> ArrayBase -> TOP
 *   TOP[]           -> ArrayBase -> TOP
 *    
 *   XYZ[]           -> FSArray    -> TOP  where XYZ is not a primitive, not String[], not TOP[]
 *   
 *   Arrays of TOP are handled differently from XYZ[] because 
 *     - the creation of the FSArray type requires
 *       -- the creation of TOP[] type, which requires (unless this is done)
 *       -- the recursive creation of FSArray type - which causes a null pointer exception
 *     
 *   Note that the UIMA super chain is not used very much (mainly for subsumption,
 *   and for this there is special case code anyways), so this doesn't really matter. (2015 Sept)
 *    
 * Note: the super type chain of the Java impl classes varies from the UIMA super type chain.
 *   It is used to factor out common behavior among classes of arrays.
 *   
 *   *********** NOTE: TBD update the rest of this comment for V3 **************
 *   
 *   For non-JCas (in V3, this is only the XYZ[] and TOP[] kinds of arrays)
 *     
 *     CommonArrayFSImpl  [ for arrays stored on the main heap ]
 *       ArrayFSImpl  (for arrays of FS)
 *       FloatArrayFSImpl
 *       IntArrayFSImpl
 *       StringArrayFSImpl
 *     CommonAuxArrayFSImpl  [ for arrays stored in Aux heaps ]
 *       BooleanArrayFSImpl
 *       ByteArrayFSImpl
 *       ShortArrayFSImpl
 *       LongArrayFSImpl
 *       DoubleArrayFSImpl
 *   
 *   For JCas: The corresponding types have only TOP as their supertypes
 *     but they implement the nonJCas interfaces for each subtype.
 *       Those interfaces implement CommonArrayFS interface
 *          
 * @param componentType the UIMA type of the component of the array
 * @return the parent type of the corresponding array type 
 */
private TypeImpl computeArrayParentFromComponentType(Type componentType) {
  if (componentType.isPrimitive() || (componentType == topType)) {
    return arrayBaseType;
  }
  return fsArrayType;
}