Java Code Examples for org.apache.uima.resource.metadata.TypeSystemDescription#setTypes()

The following examples show how to use org.apache.uima.resource.metadata.TypeSystemDescription#setTypes() . 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: CasCreationUtils.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
/**
 * Extracts a TypeSystem definition from a CasData.
 * 
 * @param aCasData
 *                the CAS Data from which to extract the type system
 * 
 * @return a description of a TypeSystem to which the CAS Data conforms
 */
public static TypeSystemDescription convertData2TypeSystem(CasData aCasData) {
  TypeSystemDescription result = UIMAFramework.getResourceSpecifierFactory()
      .createTypeSystemDescription();
  Iterator<FeatureStructure> iter = aCasData.getFeatureStructures();
  List<TypeDescription> typesArr = new ArrayList<>();
  while (iter.hasNext()) {
    FeatureStructure casFS = iter.next();
    TypeDescription newType = UIMAFramework.getResourceSpecifierFactory().createTypeDescription();
    newType.setName(casFS.getType());
    newType.setSupertypeName("uima.tcas.annotation");
    newType.setDescription("CasData Type");
    String features[] = casFS.getFeatureNames();
    if (features != null) {
      for (int i = 0; i < features.length; i++) {
        String featName = features[i];
        String rangeName = "";
        String description = "";
        PrimitiveValue pVal = (PrimitiveValue) casFS.getFeatureValue(featName);
        if (pVal.get().getClass().getName().equals("java.lang.String")) {
          System.out.println(" the feature is a String ");
          rangeName = "uima.cas.String";
          description = " featue of the casDataType";
        }
        newType.addFeature(featName, description, rangeName);
      }
    }
    typesArr.add(newType);
  }
  TypeDescription td[] = new TypeDescription[typesArr.size()];
  for (int j = 0; j < typesArr.size(); j++) {
    td[j] = typesArr.get(j);
  }
  result.setTypes(td);
  return result;
}
 
Example 3
Source File: CasCreationUtilsTest.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
public void testSetupTypeSystem() throws Exception {
  try {
    // test that duplicate feature names on supertype and subtype works
    // regardless of the order in which the types appear in the TypeSystemDescription
    TypeSystemDescription tsd1 = new TypeSystemDescription_impl();
    TypeDescription supertype = tsd1.addType("test.Super", "", "uima.cas.TOP");
    supertype.addFeature("testfeat", "", "uima.cas.Integer");
    TypeDescription subtype = tsd1.addType("test.Sub", "", "test.Super");
    subtype.addFeature("testfeat", "", "uima.cas.Integer");

    CASMgr casMgr = CASFactory.createCAS();
    CasCreationUtils.setupTypeSystem(casMgr, tsd1);
    assertNotNull(casMgr.getTypeSystemMgr().getType("test.Super")
            .getFeatureByBaseName("testfeat"));

    TypeSystemDescription tsd2 = new TypeSystemDescription_impl();
    tsd2.setTypes(new TypeDescription[] { subtype, supertype });

    casMgr = CASFactory.createCAS();
    CasCreationUtils.setupTypeSystem(casMgr, tsd2);
    assertNotNull(casMgr.getTypeSystemMgr().getType("test.Super")
            .getFeatureByBaseName("testfeat"));

  } catch (ResourceInitializationException e) {
    JUnitExtension.handleException(e);
  }
}
 
Example 4
Source File: Ecore2UimaTypeSystem.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
/**
 * Converts an Ecore model to a UIMA TypeSytemDescription.
 * 
 * @param aEcoreResource
 *          An EMF Resource containing the Ecore model
 * @param aOptions
 *          a Map defining options for the conversion. Valid keys for this map are defined as
 *          constants on this class.
 * 
 * @return The UIMA TypeSystemDescription corresponding to the Ecore model
 * @throws URISyntaxException
 *           if there is a problem reading from the resource
 */
public static TypeSystemDescription ecore2UimaTypeSystem(Resource aEcoreResource, Map aOptions)
        throws URISyntaxException {
  if (aOptions == null) {
    aOptions = Collections.EMPTY_MAP;
  }

  TypeSystemDescription tsDesc = uimaFactory.createTypeSystemDescription();

  // try to get descriptive info from EAnnotation with NS "http://uima.apache.org",
  // on the first EPackage in the Resource
  EPackage ePackage = (EPackage) aEcoreResource.getContents().get(0);
  EAnnotation eannot = ePackage.getEAnnotation("http://uima.apache.org");
  if (eannot != null) {
    tsDesc.setName((String) eannot.getDetails().get("name"));
    tsDesc.setDescription((String) eannot.getDetails().get("description"));
    tsDesc.setVendor((String) eannot.getDetails().get("vendor"));
    tsDesc.setVersion((String) eannot.getDetails().get("version"));
  }

  // convert types
  List types = new ArrayList();
  Iterator iter = aEcoreResource.getContents().iterator();
  while (iter.hasNext()) {
    Object obj = iter.next();
    if (obj instanceof EPackage) {
      ePackage2UimaTypes((EPackage) obj, types, aOptions);
    }
  }
  TypeDescription[] typeArr = new TypeDescription[types.size()];
  types.toArray(typeArr);
  tsDesc.setTypes(typeArr);
  return tsDesc;
}
 
Example 5
Source File: TypeSection.java    From uima-uimaj with Apache License 2.0 2 votes vote down vote up
/**
 * Removes the type.
 *
 * @param td the td
 * @param tsd the tsd
 */
private void removeType(TypeDescription td, TypeSystemDescription tsd) {
  tsd.setTypes((TypeDescription[]) Utility.removeElementFromArray(tsd.getTypes(), td,
          TypeDescription.class));
}