Java Code Examples for org.apache.uima.cas.Feature#getRange()

The following examples show how to use org.apache.uima.cas.Feature#getRange() . 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: AddIndexKeyDialog.java    From uima-uimaj with Apache License 2.0 6 votes vote down vote up
/**
 * Gets the sortable feature names.
 *
 * @param selectedTypeName the selected type name
 * @return an array of features whose range is primitive
 */
private String[] getSortableFeatureNames(String selectedTypeName) {
  Type selectedType = section.editor.getCurrentView().getTypeSystem().getType(selectedTypeName);
  List feats = selectedType.getFeatures();
  Collection sortableFeatureNames = new ArrayList();

  for (int i = 0; i < feats.size(); i++) {
    Feature feature = (Feature) feats.get(i);
    Type rangeType = feature.getRange();
    if (AbstractSection.isIndexableRange(rangeType.getName())) {
      if (!alreadyUsedKeys.contains(feature.getShortName()))
        sortableFeatureNames.add(feature.getShortName());
    }
  }
  String[] result = (String[]) sortableFeatureNames.toArray(stringArray0);
  Arrays.sort(result);
  return result;
}
 
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: 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: 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 5
Source File: FeatureStructureImplC.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
private void featureValueValidation(Feature feat, Object v) {
  TypeImpl range = (TypeImpl)feat.getRange();
  if ((range.isArray() && !isOkArray(range, v)) ||
      (!range.isArray() && (!range.subsumesValue(v)))) {
    throw new CASRuntimeException(CASRuntimeException.INAPPROP_RANGE, feat.getName(), range.getName(), (v == null) ? "null" : v.getClass().getName());
  }
}
 
Example 6
Source File: FeatureStructureImplC.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
private void subStringRangeCheck(Feature feat, String v) {
  Type range = feat.getRange();
  if (range instanceof TypeImpl_stringSubtype) {
    if (v != null) { // null values always OK
      ((TypeImpl_stringSubtype)range).validateIsInAllowedValues(v);
    }
  }     
}
 
Example 7
Source File: TypeSystemReinitTest.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
public void testReinitCASCompleteSerializerWithArrays() throws Exception {
  try {
    AnalysisEngineDescription aed = UIMAFramework.getXMLParser().parseAnalysisEngineDescription(
            new XMLInputSource(JUnitExtension
                    .getFile("ExampleTae/arrayTypeSerialization.xml")));
   
    CAS cas1 = CasCreationUtils.createCas(aed);
    cas1.setDocumentText("foo");
    CASCompleteSerializer ser = Serialization.serializeCASComplete((CASMgr) cas1);

    CAS tcas2 = CasCreationUtils.createCas(new TypeSystemDescription_impl(), null, null);
    CASImpl cas2 = ((CASImpl) tcas2).getBaseCAS();
    tcas2.setDocumentText("bar");

    // reinit
    //  This uses cas2 which only has a base type system to start, 
    //    and loads it from a complete serialization which has other new types
    cas2.getBinaryCasSerDes().reinit(ser);
    CAS tcas3 = cas2.getCurrentView();

    assertTrue(tcas2 == tcas3);
    assertNotNull(cas1.getTypeSystem().getType("Test.ArrayType"));
    assertNotNull(tcas3.getTypeSystem().getType("Test.ArrayType"));
    
    TypeSystemImpl ts = (TypeSystemImpl)cas2.getTypeSystem();
    Type arrayType = ts.getType("Test.ArrayType");
    Feature arrayFeat = arrayType.getFeatureByBaseName("arrayFeature");
    TypeImpl featRange = (TypeImpl)(arrayFeat.getRange());
   
    assertTrue(ts.ll_isArrayType(featRange.getCode()));
    assertFalse(arrayFeat.isMultipleReferencesAllowed());
    
  } catch (Exception e) {
    JUnitExtension.handleException(e);
  }
}
 
Example 8
Source File: CasCreationUtilsTest.java    From uima-uimaj with Apache License 2.0 4 votes vote down vote up
public void testAggregateWithImports() throws Exception {
  try {
    String pathSep = System.getProperty("path.separator");
    ResourceManager resMgr = UIMAFramework.newDefaultResourceManager();
    resMgr.setDataPath(JUnitExtension.getFile("TypeSystemDescriptionImplTest/dataPathDir")
            .getAbsolutePath()
            + pathSep
            + JUnitExtension.getFile("TypePrioritiesImplTest/dataPathDir").getAbsolutePath()
            + pathSep
            + JUnitExtension.getFile("FsIndexCollectionImplTest/dataPathDir").getAbsolutePath());

    File taeDescriptorWithImport = JUnitExtension
            .getFile("CasCreationUtilsTest/AggregateTaeWithImports.xml");
    AnalysisEngineDescription desc = UIMAFramework.getXMLParser().parseAnalysisEngineDescription(
            new XMLInputSource(taeDescriptorWithImport));
    ArrayList<AnalysisEngineDescription> mdList = new ArrayList<>();
    mdList.add(desc);
    CAS tcas = CasCreationUtils.createCas(mdList, UIMAFramework
            .getDefaultPerformanceTuningProperties(), resMgr);
    // check that imports were resolved correctly
    assertNotNull(tcas.getTypeSystem().getType("DocumentStructure"));
    assertNotNull(tcas.getTypeSystem().getType("NamedEntity"));
    assertNotNull(tcas.getTypeSystem().getType("TestType3"));
    assertNotNull(tcas.getTypeSystem().getType("Sentence"));

    assertNotNull(tcas.getIndexRepository().getIndex("TestIndex"));
    assertNotNull(tcas.getIndexRepository().getIndex("ReverseAnnotationIndex"));
    assertNotNull(tcas.getIndexRepository().getIndex("DocumentStructureIndex"));

    // Check elementType and multipleReferencesAllowed for array feature
    Feature arrayFeat = tcas.getTypeSystem().getFeatureByFullName("Paragraph:sentences");
    assertNotNull(arrayFeat);
    assertFalse(arrayFeat.isMultipleReferencesAllowed());
    Type sentenceArrayType = arrayFeat.getRange();
    assertNotNull(sentenceArrayType);
    assertTrue(sentenceArrayType.isArray());
    assertEquals(tcas.getTypeSystem().getType("Sentence"), sentenceArrayType.getComponentType());

    Feature arrayFeat2 = tcas.getTypeSystem().getFeatureByFullName(
            "Paragraph:testMultiRefAllowedFeature");
    assertNotNull(arrayFeat2);
    assertTrue(arrayFeat2.isMultipleReferencesAllowed());

    // test imports aren't resolved more than once
    Object spec1 = desc.getDelegateAnalysisEngineSpecifiers().get("Annotator1");
    assertNotNull(spec1);
    Object spec2 = desc.getDelegateAnalysisEngineSpecifiers().get("Annotator1");
    assertTrue(spec1 == spec2);

    // test removal
    desc.getDelegateAnalysisEngineSpecifiersWithImports().remove("Annotator1");
    assertTrue(desc.getDelegateAnalysisEngineSpecifiers().isEmpty());
  } catch (Exception e) {
    JUnitExtension.handleException(e);
  }
}