Java Code Examples for org.apache.uima.cas.CAS#TYPE_NAME_FLOAT

The following examples show how to use org.apache.uima.cas.CAS#TYPE_NAME_FLOAT . 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: PrimitiveUimaIndexingSupport.java    From inception with Apache License 2.0 6 votes vote down vote up
@Override
public boolean accepts(AnnotationFeature aFeature)
{
    switch (aFeature.getMultiValueMode()) {
    case NONE:
        switch (aFeature.getType()) {
        case CAS.TYPE_NAME_INTEGER: // fallthrough
        case CAS.TYPE_NAME_FLOAT: // fallthrough
        case CAS.TYPE_NAME_BOOLEAN: // fallthrough
        case CAS.TYPE_NAME_STRING: 
            return true;
        default:
            return false;
        }
    case ARRAY: // fallthrough
    default:
        return false;
    }
}
 
Example 2
Source File: WebAnnoCasUtil.java    From webanno with Apache License 2.0 6 votes vote down vote up
/**
 * Get the feature value of this {@code Feature} on this annotation
 */
private static Object getFeatureValue(FeatureStructure aFS, Feature aFeature)
{
    switch (aFeature.getRange().getName()) {
    case CAS.TYPE_NAME_STRING:
        return aFS.getFeatureValueAsString(aFeature);
    case CAS.TYPE_NAME_BOOLEAN:
        return aFS.getBooleanValue(aFeature);
    case CAS.TYPE_NAME_FLOAT:
        return aFS.getFloatValue(aFeature);
    case CAS.TYPE_NAME_INTEGER:
        return aFS.getIntValue(aFeature);
    case CAS.TYPE_NAME_BYTE:
        return aFS.getByteValue(aFeature);
    case CAS.TYPE_NAME_DOUBLE:
        return aFS.getDoubleValue(aFeature);
    case CAS.TYPE_NAME_LONG:
        aFS.getLongValue(aFeature);
    case CAS.TYPE_NAME_SHORT:
        aFS.getShortValue(aFeature);
    default:
        return null;
    // return aFS.getFeatureValue(aFeature);
    }
}
 
Example 3
Source File: WebAnnoCasUtil.java    From webanno with Apache License 2.0 6 votes vote down vote up
public static <T> T getFeature(FeatureStructure aFS, String aFeatureName)
{
    Feature feature = aFS.getType().getFeatureByBaseName(aFeatureName);

    if (feature == null) {
        throw new IllegalArgumentException("Type [" + aFS.getType().getName()
                + "] has no feature called [" + aFeatureName + "]");
    }

    switch (feature.getRange().getName()) {
    case CAS.TYPE_NAME_STRING:
        return (T) aFS.getStringValue(feature);
    case CAS.TYPE_NAME_BOOLEAN:
        return (T) (Boolean) aFS.getBooleanValue(feature);
    case CAS.TYPE_NAME_FLOAT:
        return (T) (Float) aFS.getFloatValue(feature);
    case CAS.TYPE_NAME_INTEGER:
        return (T) (Integer) aFS.getIntValue(feature);
    default:
        throw new IllegalArgumentException("Cannot get value of feature [" + feature.getName()
                + "] with type [" + feature.getRange().getName() + "]");
    }
}
 
Example 4
Source File: TypeSystemImpl.java    From uima-uimaj with Apache License 2.0 6 votes vote down vote up
/**
   * The range type of features may include 
   *   special uima types that are not creatable, such as the primitive ones
   *   like integer, or string, or subtypes of string.
   * Other types are reference types
   */
  boolean classifyAsRefType(String name, TypeImpl superType) {
    switch(name) {
    case CAS.TYPE_NAME_BOOLEAN:
    case CAS.TYPE_NAME_BYTE:
    case CAS.TYPE_NAME_SHORT:
    case CAS.TYPE_NAME_INTEGER:
    case CAS.TYPE_NAME_LONG:
    case CAS.TYPE_NAME_FLOAT:
    case CAS.TYPE_NAME_DOUBLE:
    case CAS.TYPE_NAME_STRING:
//    case CAS.TYPE_NAME_JAVA_OBJECT:
//    case CAS.TYPE_NAME_MAP:
//    case CAS.TYPE_NAME_LIST:
      return false;
    }
    // superType is null for TOP, which is a Ref type
    if (superType != null && superType.getName().equals(CAS.TYPE_NAME_STRING)) { // can't compare to stringType - may not be set yet
      return false;
    }
    return true;
  }
 
Example 5
Source File: NewFeatureUtils.java    From baleen with Apache License 2.0 5 votes vote down vote up
private static CommonArrayFS getCommonArrayFS(JCas jCas, Type componentType, Collection<?> list) {
  CommonArrayFS fs = null;

  switch (componentType.getName()) {
    case CAS.TYPE_NAME_STRING:
      fs = getStringArray(jCas, list);
      break;
    case CAS.TYPE_NAME_INTEGER:
      fs = getIntegerArray(jCas, list);
      break;
    case CAS.TYPE_NAME_FLOAT:
      fs = getFloatArray(jCas, list);
      break;
    case CAS.TYPE_NAME_BOOLEAN:
      fs = getBooleanArray(jCas, list);
      break;
    case CAS.TYPE_NAME_BYTE:
      fs = getByteArray(jCas, list);
      break;
    case CAS.TYPE_NAME_SHORT:
      fs = getShortArray(jCas, list);
      break;
    case CAS.TYPE_NAME_LONG:
      fs = getLongArray(jCas, list);
      break;
    case CAS.TYPE_NAME_DOUBLE:
      fs = getDoubleArray(jCas, list);
      break;
    default:
      break;
  }
  return fs;
}
 
Example 6
Source File: FeatureUtils.java    From baleen with Apache License 2.0 5 votes vote down vote up
/**
 * Convert a UIMA feature to a Java object of the correct type
 *
 * @param f UIMA CAS Feature
 * @param a UIMA CAS Annotation
 * @return Java object, or null if unable to convert
 */
public static Object featureToObject(Feature f, Annotation a) {
  Object ret = null;

  switch (f.getRange().getName()) {
    case CAS.TYPE_NAME_STRING:
      ret = StringToObject.convertStringToObject(a.getStringValue(f));
      break;
    case CAS.TYPE_NAME_INTEGER:
      ret = a.getIntValue(f);
      break;
    case CAS.TYPE_NAME_FLOAT:
      ret = a.getFloatValue(f);
      break;
    case CAS.TYPE_NAME_BOOLEAN:
      ret = a.getBooleanValue(f);
      break;
    case CAS.TYPE_NAME_BYTE:
      ret = a.getByteValue(f);
      break;
    case CAS.TYPE_NAME_SHORT:
      ret = a.getShortValue(f);
      break;
    case CAS.TYPE_NAME_LONG:
      ret = a.getLongValue(f);
      break;
    case CAS.TYPE_NAME_DOUBLE:
      ret = a.getDoubleValue(f);
      break;
    default:
      ret = null;
  }

  return ret;
}
 
Example 7
Source File: AbstractJsonConsumer.java    From baleen with Apache License 2.0 5 votes vote down vote up
/**
 * Write primitive value.
 *
 * @param generator the generator
 * @param annotation the annotation
 * @param feature the feature
 * @throws IOException Signals that an I/O exception has occurred.
 */
private void writePrimitiveValue(
    JsonGenerator generator, FeatureStructure annotation, Feature feature) throws IOException {
  String range = feature.getRange().getName();
  switch (range) {
    case CAS.TYPE_NAME_INTEGER:
      generator.writeNumber(annotation.getIntValue(feature));
      break;
    case CAS.TYPE_NAME_FLOAT:
      generator.writeNumber(annotation.getFloatValue(feature));
      break;
    case CAS.TYPE_NAME_STRING:
      generator.writeString(annotation.getStringValue(feature));
      break;
    case CAS.TYPE_NAME_BOOLEAN:
      generator.writeBoolean(annotation.getBooleanValue(feature));
      break;
    case CAS.TYPE_NAME_BYTE:
      generator.writeNumber(annotation.getByteValue(feature));
      break;
    case CAS.TYPE_NAME_SHORT:
      generator.writeNumber(annotation.getShortValue(feature));
      break;
    case CAS.TYPE_NAME_LONG:
      generator.writeNumber(annotation.getLongValue(feature));
      break;
    case CAS.TYPE_NAME_DOUBLE:
      generator.writeNumber(annotation.getDoubleValue(feature));
      break;
    default:
      getMonitor().warn("Unexpected primitive type: " + range);
      break;
  }
}
 
Example 8
Source File: NumberFeatureSupport.java    From webanno with Apache License 2.0 5 votes vote down vote up
@Override
public FeatureEditor createEditor(String aId, MarkupContainer aOwner,
        AnnotationActionHandler aHandler, final IModel<AnnotatorState> aStateModel,
        final IModel<FeatureState> aFeatureStateModel)
{
    AnnotationFeature feature = aFeatureStateModel.getObject().feature;
    
    if (!accepts(feature)) {
        throw unsupportedFeatureTypeException(feature);
    }

    NumberFeatureTraits traits = readTraits(feature);

    switch (feature.getType()) {
    case CAS.TYPE_NAME_INTEGER: {
        if (traits.getEditorType().equals(NumberFeatureTraits.EDITOR_TYPE.RADIO_BUTTONS)) {
            int min = (int) traits.getMinimum();
            int max = (int) traits.getMaximum();
            List<Integer> range = IntStream.range(min, max + 1).boxed()
                    .collect(Collectors.toList());
            return new RatingFeatureEditor(aId, aOwner, aFeatureStateModel, range);
        }
        else {
            return new NumberFeatureEditor(aId, aOwner, aFeatureStateModel, traits);
        }
    }
    case CAS.TYPE_NAME_FLOAT: {
        return new NumberFeatureEditor(aId, aOwner, aFeatureStateModel, traits);
    }
    default:
        throw unsupportedFeatureTypeException(feature);
    }
}
 
Example 9
Source File: NumberFeatureEditor.java    From webanno with Apache License 2.0 5 votes vote down vote up
public NumberFeatureEditor(String aId, MarkupContainer aItem, IModel<FeatureState> aModel,
        NumberFeatureTraits aTraits)
{
    super(aId, aItem, new CompoundPropertyModel<>(aModel));
    
    switch (getModelObject().feature.getType()) {
    case CAS.TYPE_NAME_INTEGER: {
        Options options = new Options();
        options.set("format", "'n0'");
        field = new NumberTextField<>("value", Integer.class, options);
        if (aTraits.isLimited()) {
            field.setMinimum(aTraits.getMinimum());
            field.setMaximum(aTraits.getMaximum());
        }
        break;
    }
    case CAS.TYPE_NAME_FLOAT: {
        field = new NumberTextField<>("value", Float.class);
        if (aTraits.isLimited()) {
            field.setMinimum(aTraits.getMinimum().floatValue());
            field.setMaximum(aTraits.getMaximum().floatValue());
        }
        break;
    }
    default:
        throw new IllegalArgumentException("Type [" + getModelObject().feature.getType()
                + "] cannot be rendered as a numeric input field");
    }
    
    add(field);
}
 
Example 10
Source File: WebAnnoCasUtil.java    From webanno with Apache License 2.0 5 votes vote down vote up
public static boolean isPrimitiveType(Type aType)
{
    switch (aType.getName()) {
    case CAS.TYPE_NAME_STRING: // fallthrough
    case CAS.TYPE_NAME_BOOLEAN: // fallthrough
    case CAS.TYPE_NAME_FLOAT: // fallthrough
    case CAS.TYPE_NAME_INTEGER:
        return true;
    default:
        return false;
    }
}
 
Example 11
Source File: TypeSystemAnalysisTest.java    From webanno with Apache License 2.0 5 votes vote down vote up
@Test
public void testSpanWithPrimitiveFeatures() throws Exception
{
    TypeSystemDescription tsd = createTypeSystemDescription("tsd/spanWithPrimitiveFeatures");
    TypeSystemAnalysis analysis = TypeSystemAnalysis.of(tsd);
    
    AnnotationLayer spanLayer = new AnnotationLayer();
    spanLayer.setName("webanno.custom.Span");
    spanLayer.setUiName("Span");
    spanLayer.setType(WebAnnoConst.SPAN_TYPE);
    spanLayer.setAnchoringMode(AnchoringMode.CHARACTERS);
    spanLayer.setOverlapMode(OverlapMode.ANY_OVERLAP);
    spanLayer.setCrossSentence(true);
    
    AnnotationFeature stringFeature = new AnnotationFeature(
            "stringFeature", CAS.TYPE_NAME_STRING);
    AnnotationFeature intFeature = new AnnotationFeature(
            "intFeature", CAS.TYPE_NAME_INTEGER);
    AnnotationFeature booleanFeature = new AnnotationFeature(
            "booleanFeature", CAS.TYPE_NAME_BOOLEAN);
    AnnotationFeature floatFeature = new AnnotationFeature(
            "floatFeature", CAS.TYPE_NAME_FLOAT);

    SoftAssertions softly = new SoftAssertions();
    softly.assertThat(analysis.getLayers())
        .containsExactly(spanLayer)
        .usingFieldByFieldElementComparator();
    softly.assertThat(analysis.getFeatures(spanLayer.getName()))
        .containsExactlyInAnyOrder(stringFeature, intFeature, booleanFeature, floatFeature)
        .usingFieldByFieldElementComparator();
    softly.assertAll();
}
 
Example 12
Source File: NewFeatureUtils.java    From baleen with Apache License 2.0 4 votes vote down vote up
/**
 * Set the primitve value for the given feature on the given annotation
 *
 * @param annotation to add the feature
 * @param feature to add
 * @param value to add
 */
public static void setPrimitive(
    final BaleenAnnotation annotation, final Feature feature, final Object value) {

  // Nothing to do
  if (value == null) {
    return;
  }

  // We could do more type conversion here (got double -> want integer)
  // but fail fast is better - this is a common / standard type system after all.

  switch (feature.getRange().getName()) {
    case CAS.TYPE_NAME_STRING:
      annotation.setStringValue(feature, value.toString()); // Can be stored as different type
      break;
    case CAS.TYPE_NAME_INTEGER:
      annotation.setIntValue(feature, ((Number) value).intValue());
      break;
    case CAS.TYPE_NAME_FLOAT:
      annotation.setFloatValue(feature, ((Number) value).floatValue());
      break;
    case CAS.TYPE_NAME_BOOLEAN:
      annotation.setBooleanValue(feature, (boolean) value);
      break;
    case CAS.TYPE_NAME_BYTE:
      annotation.setByteValue(feature, (byte) value);
      break;
    case CAS.TYPE_NAME_SHORT:
      annotation.setShortValue(feature, ((Number) value).shortValue());
      break;
    case CAS.TYPE_NAME_LONG:
      annotation.setLongValue(feature, ((Number) value).longValue());
      break;
    case CAS.TYPE_NAME_DOUBLE:
      annotation.setDoubleValue(feature, ((Number) value).doubleValue());
      break;
    default:
      break;
  }
}
 
Example 13
Source File: WebAnnoCasUtil.java    From webanno with Apache License 2.0 4 votes vote down vote up
public static boolean isNativeUimaType(String aType)
{
    Validate.notNull(aType, "Type must not be null");
    
    switch (aType) {
    case CAS.TYPE_NAME_ANNOTATION:
    case CAS.TYPE_NAME_ANNOTATION_BASE:
    case CAS.TYPE_NAME_ARRAY_BASE:
    case CAS.TYPE_NAME_BOOLEAN:
    case CAS.TYPE_NAME_BOOLEAN_ARRAY:
    case CAS.TYPE_NAME_BYTE:
    case CAS.TYPE_NAME_BYTE_ARRAY:
    case CAS.TYPE_NAME_DOCUMENT_ANNOTATION:
    case CAS.TYPE_NAME_DOUBLE:
    case CAS.TYPE_NAME_DOUBLE_ARRAY:
    case CAS.TYPE_NAME_EMPTY_FLOAT_LIST:
    case CAS.TYPE_NAME_EMPTY_FS_LIST:
    case CAS.TYPE_NAME_EMPTY_INTEGER_LIST:
    case CAS.TYPE_NAME_EMPTY_STRING_LIST:
    case CAS.TYPE_NAME_FLOAT:
    case CAS.TYPE_NAME_FLOAT_ARRAY:
    case CAS.TYPE_NAME_FLOAT_LIST:
    case CAS.TYPE_NAME_FS_ARRAY:
    case CAS.TYPE_NAME_FS_LIST:
    case CAS.TYPE_NAME_INTEGER:
    case CAS.TYPE_NAME_INTEGER_ARRAY:
    case CAS.TYPE_NAME_INTEGER_LIST:
    case CAS.TYPE_NAME_LIST_BASE:
    case CAS.TYPE_NAME_LONG:
    case CAS.TYPE_NAME_LONG_ARRAY:
    case CAS.TYPE_NAME_NON_EMPTY_FLOAT_LIST:
    case CAS.TYPE_NAME_NON_EMPTY_FS_LIST:
    case CAS.TYPE_NAME_NON_EMPTY_INTEGER_LIST:
    case CAS.TYPE_NAME_NON_EMPTY_STRING_LIST:
    case CAS.TYPE_NAME_SHORT:
    case CAS.TYPE_NAME_SHORT_ARRAY:
    case CAS.TYPE_NAME_SOFA:
    case CAS.TYPE_NAME_STRING:
    case CAS.TYPE_NAME_STRING_ARRAY:
    case CAS.TYPE_NAME_STRING_LIST:
    case CAS.TYPE_NAME_TOP:
        return true;
    }
    
    return false;
}
 
Example 14
Source File: WebAnnoCasUtil.java    From webanno with Apache License 2.0 4 votes vote down vote up
/**
 * Set a feature value.
 *
 * @param aFS
 *            the feature structure.
 * @param aFeature
 *            the feature within the annotation whose value to set. If this parameter is
 *            {@code null} then nothing happens.
 * @param aValue
 *            the feature value.
 */
public static void setFeature(FeatureStructure aFS, AnnotationFeature aFeature, Object aValue)
{
    if (aFeature == null) {
        return;
    }

    Feature feature = aFS.getType().getFeatureByBaseName(aFeature.getName());

    switch (aFeature.getMultiValueMode()) {
    case NONE: {
        String effectiveType = aFeature.getType();
        if (effectiveType.contains(":")) {
            effectiveType = CAS.TYPE_NAME_STRING;
        }
        
        // Sanity check
        if (!Objects.equals(effectiveType, feature.getRange().getName())) {
            throw new IllegalArgumentException("On [" + aFS.getType().getName() + "] feature ["
                    + aFeature.getName() + "] actual type [" + feature.getRange().getName()
                    + "] does not match expected feature type [" + effectiveType + "].");
        }

        switch (effectiveType) {
        case CAS.TYPE_NAME_STRING:
            aFS.setStringValue(feature, (String) aValue);
            break;
        case CAS.TYPE_NAME_BOOLEAN:
            aFS.setBooleanValue(feature, aValue != null ? (boolean) aValue : false);
            break;
        case CAS.TYPE_NAME_FLOAT:
            aFS.setFloatValue(feature, aValue != null ? (float) aValue : 0.0f);
            break;
        case CAS.TYPE_NAME_INTEGER:
            aFS.setIntValue(feature, aValue != null ? (int) aValue : 0);
            break;
        default:
            throw new IllegalArgumentException("Cannot set value of feature ["
                    + aFeature.getName() + "] with type [" + feature.getRange().getName()
                    + "] to [" + aValue + "]");
        }
        break;
    }
    case ARRAY: {
        switch (aFeature.getLinkMode()) {
        case WITH_ROLE: {
            // Get type and features - we need them later in the loop
            setLinkFeature(aFS, aFeature, (List<LinkWithRoleModel>) aValue, feature);
            break;
        }
        default:
            throw new IllegalArgumentException("Unsupported link mode ["
                    + aFeature.getLinkMode() + "] on feature [" + aFeature.getName() + "]");
        }
        break;
    }
    default:
        throw new IllegalArgumentException("Unsupported multi-value mode ["
                + aFeature.getMultiValueMode() + "] on feature [" + aFeature.getName() + "]");
    }
}
 
Example 15
Source File: TypeSystemAnalysisTest.java    From webanno with Apache License 2.0 4 votes vote down vote up
@Test
public void testRelationWithPrimitiveFeatures() throws Exception
{
    TypeSystemDescription tsd = createTypeSystemDescription(
            "tsd/relationWithPrimitiveFeatures");
    TypeSystemAnalysis analysis = TypeSystemAnalysis.of(tsd);
    
    AnnotationLayer relationLayer = new AnnotationLayer();
    relationLayer.setName("webanno.custom.Relation");
    relationLayer.setUiName("Relation");
    relationLayer.setType(WebAnnoConst.RELATION_TYPE);
    relationLayer.setAnchoringMode(AnchoringMode.CHARACTERS);
    relationLayer.setOverlapMode(OverlapMode.ANY_OVERLAP);
    relationLayer.setCrossSentence(true);

    AnnotationLayer relationTargetLayer = new AnnotationLayer();
    relationTargetLayer.setName("webanno.custom.RelationTarget");
    relationTargetLayer.setUiName("RelationTarget");
    relationTargetLayer.setType(WebAnnoConst.SPAN_TYPE);
    relationTargetLayer.setAnchoringMode(AnchoringMode.CHARACTERS);
    relationTargetLayer.setOverlapMode(OverlapMode.ANY_OVERLAP);
    relationTargetLayer.setCrossSentence(true);

    AnnotationFeature stringFeature = new AnnotationFeature(
            "stringFeature", CAS.TYPE_NAME_STRING);
    AnnotationFeature intFeature = new AnnotationFeature(
            "intFeature", CAS.TYPE_NAME_INTEGER);
    AnnotationFeature booleanFeature = new AnnotationFeature(
            "booleanFeature", CAS.TYPE_NAME_BOOLEAN);
    AnnotationFeature floatFeature = new AnnotationFeature(
            "floatFeature", CAS.TYPE_NAME_FLOAT);

    SoftAssertions softly = new SoftAssertions();
    softly.assertThat(analysis.getLayers())
        .containsExactlyInAnyOrder(relationLayer, relationTargetLayer)
        .usingFieldByFieldElementComparator();
    softly.assertThat(analysis.getFeatures(relationLayer.getName()))
            .containsExactlyInAnyOrder(stringFeature, intFeature, booleanFeature, floatFeature)
            .usingFieldByFieldElementComparator();
    softly.assertAll();
}