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

The following examples show how to use org.apache.uima.cas.Type#isArray() . 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: TypeSystemUtil.java    From uima-uimaj with Apache License 2.0 6 votes vote down vote up
/**
 * Convert a {@link TypeSystem} to an equivalent {@link TypeSystemDescription}.
 * 
 * @param aTypeSystem
 *          type system object to convert
 * @return a TypeSystemDescription that is equivalent to <code>aTypeSystem</code>
 */
public static TypeSystemDescription typeSystem2TypeSystemDescription(TypeSystem aTypeSystem) {
  ResourceSpecifierFactory fact = UIMAFramework.getResourceSpecifierFactory();
  TypeSystemDescription tsDesc = fact.createTypeSystemDescription();
  Iterator<Type> typeIter = aTypeSystem.getTypeIterator();
  List<TypeDescription> typeDescs = new ArrayList<>();
  while (typeIter.hasNext()) {
    Type type = typeIter.next();
    if (!type.getName().startsWith("uima.cas") && !type.getName().equals("uima.tcas.Annotation") &&
        !type.isArray()) {
      typeDescs.add(type2TypeDescription(type, aTypeSystem));
    }
  }
  TypeDescription[] typeDescArr = new TypeDescription[typeDescs.size()];
  typeDescs.toArray(typeDescArr);
  tsDesc.setTypes(typeDescArr);

  return tsDesc;
}
 
Example 2
Source File: TypeSystemUtil.java    From uima-uimaj with Apache License 2.0 6 votes vote down vote up
/**
 * Convert a {@link Feature} to an equivalent {@link FeatureDescription}.
 * 
 * @param aFeature
 *          feature object to convert
 * @return a FeatureDescription that is equivalent to <code>aFeature</code>
 */
public static FeatureDescription feature2FeatureDescription(Feature aFeature) {
  FeatureDescription featDesc = UIMAFramework.getResourceSpecifierFactory()
          .createFeatureDescription();
  featDesc.setName(aFeature.getShortName());
  if (aFeature.isMultipleReferencesAllowed()) {
    featDesc.setMultipleReferencesAllowed(true);
  }
  Type rangeType = aFeature.getRange();
  //special check for array range types, which are represented in the CAS as
  //elementType[] but in the descriptor as an FSArray with an <elementType>
  if (rangeType.isArray() && !rangeType.getComponentType().isPrimitive()) {
    featDesc.setRangeTypeName(CAS.TYPE_NAME_FS_ARRAY);
    String elementTypeName = rangeType.getComponentType().getName();
    if (!CAS.TYPE_NAME_TOP.equals(elementTypeName)) {
      featDesc.setElementType(elementTypeName);
    }
  }
  else {
    featDesc.setRangeTypeName(rangeType.getName());
  }
  return featDesc;
}
 
Example 3
Source File: CreateFeatureStructureDialog.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
/**
 * Initializes a the current instance.
 *
 * @param parentShell the parent shell
 * @param superType the super type
 * @param typeSystem the type system
 */
protected CreateFeatureStructureDialog(Shell parentShell, Type superType, TypeSystem typeSystem) {

  super(parentShell);

  this.superType = superType;

  this.typeSystem = typeSystem;

  if (!superType.isArray()) {
    title = "Choose type";
    message = "Please choose the type to create.";
  } else {
    title = "Array size";
    message = "Please enter the size of the array.";
  }

  filterTypes = new HashSet<>();
  filterTypes.add(typeSystem.getType(CAS.TYPE_NAME_ARRAY_BASE));
  filterTypes.add(typeSystem.getType(CAS.TYPE_NAME_BYTE));
  filterTypes.add(typeSystem.getType(CAS.TYPE_NAME_ANNOTATION_BASE));
  filterTypes.add(typeSystem.getType(CAS.TYPE_NAME_SHORT));
  filterTypes.add(typeSystem.getType(CAS.TYPE_NAME_LONG));
  filterTypes.add(typeSystem.getType(CAS.TYPE_NAME_FLOAT));
  filterTypes.add(typeSystem.getType(CAS.TYPE_NAME_DOUBLE));
  filterTypes.add(typeSystem.getType(CAS.TYPE_NAME_BOOLEAN));
  filterTypes.add(typeSystem.getType(CAS.TYPE_NAME_FLOAT));
  filterTypes.add(typeSystem.getType(CAS.TYPE_NAME_INTEGER));
  filterTypes.add(typeSystem.getType(CAS.TYPE_NAME_SOFA));
  filterTypes.add(typeSystem.getType(CAS.TYPE_NAME_STRING));
}
 
Example 4
Source File: FeatureStructureContentProvider.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
@Override
public Object[] getElements(Object inputElement) {

  if (inputElement != null) {

    FeatureStructure featureStructure = (FeatureStructure) inputElement;

    Type type = featureStructure.getType();

    if (!type.isArray()) {

      Collection<FeatureValue> featureValues = new LinkedList<>();

      for (Feature feature : type.getFeatures()) {
        featureValues.add(new FeatureValue(mDocument, featureStructure, feature));
      }

      return featureValues.toArray();
    }
    else {
      int size = arraySize(featureStructure);

      ArrayValue arrayValues[] = new ArrayValue[size];

      for (int i = 0; i < size; i++) {
        arrayValues[i] = new ArrayValue(featureStructure, i);
      }

      return arrayValues;
    }

  } else {
    return new Object[0];
  }
}
 
Example 5
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 6
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 7
Source File: TypeSystemImpl.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
/**
 * Does one type inherit from the other?
 * 
 * There are two versions. 
 * 
 *  Fast: but only works after commit:  aTypeImplInstance.subsumes(otherTypeImpl)
 *  Slower: this routine.
 *  This routine is called from fast one if not committed.
 * 
 * @param superType
 *          Supertype.
 * @param subType
 *          Subtype.
 * @return <code>true</code> iff <code>sub</code> inherits from <code>super</code>.
 */
@Override
public boolean subsumes(Type superType, Type subType) {
  if (superType == subType) 
    return true;
  
  if (isCommitted()) {
    return ((TypeImpl)superType).subsumes((TypeImpl)subType);
  }
  
  if (superType.isArray()) {
    return ((TypeImpl_array)superType).subsumes((TypeImpl)subType);  // doesn't need to be committed
  }
      
  if (subType == fsArrayType) {
    return superType == topType || 
           superType == arrayBaseType; 
  }

  if (subType.isArray()) {
    // If the subtype is an array, and the supertype is not, then the
    // supertype must be top, or the abstract array base.
    return ((superType == topType) || (superType == arrayBaseType));
  }
  
  return ((TypeImpl)subType).hasSupertype((TypeImpl) superType);
}
 
Example 8
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;
}