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

The following examples show how to use org.apache.uima.resource.metadata.TypeSystemDescription#getType() . 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: RecommenderTestHelper.java    From inception with Apache License 2.0 6 votes vote down vote up
public static void addScoreFeature(CAS aCas, String aTypeName, String aFeatureName)
        throws IOException, UIMAException
{
    String scoreFeatureName = aFeatureName + FEATURE_NAME_SCORE_SUFFIX;
    String scoreExplanationFeatureName = aFeatureName + FEATURE_NAME_SCORE_EXPLANATION_SUFFIX;

    TypeSystemDescription tsd = typeSystem2TypeSystemDescription(aCas.getTypeSystem());
    TypeDescription typeDescription = tsd.getType(aTypeName);
    typeDescription.addFeature(scoreFeatureName, "Confidence feature", TYPE_NAME_DOUBLE);
    typeDescription.addFeature(scoreExplanationFeatureName, "Confidence explanation feature", 
            TYPE_NAME_STRING);
    typeDescription.addFeature(FEATURE_NAME_IS_PREDICTION, "Is prediction", TYPE_NAME_BOOLEAN);

    AnnotationSchemaService schemaService = new AnnotationSchemaServiceImpl();
    schemaService.upgradeCas(aCas, tsd);
}
 
Example 2
Source File: RecommendationServiceImpl.java    From inception with Apache License 2.0 5 votes vote down vote up
public CAS cloneAndMonkeyPatchCAS(Project aProject, CAS aSourceCas, CAS aTargetCas)
    throws UIMAException, IOException
{
    try (StopWatch watch = new StopWatch(log, "adding score features")) {
        TypeSystemDescription tsd = annoService.getFullProjectTypeSystem(aProject);

        for (AnnotationLayer layer : annoService.listAnnotationLayer(aProject)) {
            TypeDescription td = tsd.getType(layer.getName());

            if (td == null) {
                log.trace("Could not monkey patch type [{}]", layer.getName());
                continue;
            }

            for (FeatureDescription feature : td.getFeatures()) {
                String scoreFeatureName = feature.getName() + FEATURE_NAME_SCORE_SUFFIX;
                td.addFeature(scoreFeatureName, "Score feature", CAS.TYPE_NAME_DOUBLE);
                
                String scoreExplanationFeatureName = feature.getName() + 
                        FEATURE_NAME_SCORE_EXPLANATION_SUFFIX;
                td.addFeature(scoreExplanationFeatureName, "Score explanation feature", 
                        CAS.TYPE_NAME_STRING);
            }

            td.addFeature(FEATURE_NAME_IS_PREDICTION, "Is Prediction", CAS.TYPE_NAME_BOOLEAN);
        }

        annoService.upgradeCas(aSourceCas, aTargetCas, tsd);
    }

    return aTargetCas;
}
 
Example 3
Source File: AnnotationSchemaServiceImpl.java    From webanno with Apache License 2.0 5 votes vote down vote up
private void exportBuiltInTypeDescription(TypeSystemDescription aSource,
        TypeSystemDescription aTarget, String aType)
{
    TypeDescription builtInType = aSource.getType(aType);
    
    if (builtInType == null) {
        throw new IllegalArgumentException(
                "No type description found for type [" + aType + "]");
    }
    
    TypeDescription clonedType = aTarget.addType(builtInType.getName(),
            builtInType.getDescription(), builtInType.getSupertypeName());
    
    if (builtInType.getFeatures() != null) {
        for (FeatureDescription feature : builtInType.getFeatures()) {
            clonedType.addFeature(feature.getName(), feature.getDescription(),
                    feature.getRangeTypeName(), feature.getElementType(),
                    feature.getMultipleReferencesAllowed());
            
            // Export types referenced by built-in types also as built-in types. Note that
            // it is conceptually impossible for built-in types to refer to custom types, so
            // this is cannot lead to a custom type being exported as a built-in type.
            if (
                    feature.getElementType() != null && 
                    !isNativeUimaType(feature.getElementType()) &&
                    aTarget.getType(feature.getElementType()) == null
            ) {
                exportBuiltInTypeDescription(aSource, aTarget, feature.getElementType());
            }
            else if (
                    feature.getRangeTypeName() != null && 
                    !isNativeUimaType(feature.getRangeTypeName()) &&
                    aTarget.getType(feature.getRangeTypeName()) == null
            ) {
                exportBuiltInTypeDescription(aSource, aTarget, feature.getRangeTypeName());
            }
        }
    }
}
 
Example 4
Source File: AbstractImportablePartSection.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
/**
 * Gets the local type definition.
 *
 * @param td the td
 * @return the local type definition
 */
protected TypeDescription getLocalTypeDefinition(TypeDescription td) {
  TypeSystemDescription tsdLocal = getTypeSystemDescription();
  if (null == tsdLocal)
    return null;
  return tsdLocal.getType(td.getName());
}
 
Example 5
Source File: TypeSystemAnalysis.java    From webanno with Apache License 2.0 4 votes vote down vote up
private void analyzeType(TypeSystem aTS, TypeSystemDescription aTSD, TypeDescription aTD) {
    log.trace("Analyzing [{}]", aTD.getName());
    
    Type type = aTS.getType(aTD.getName());
    
    // Skip built-in UIMA types
    if (aTD.getName().startsWith("uima.tcas.")) {
        log.debug("[{}] is a built-in UIMA type. Skipping.", aTD.getName());
        return;
    }
    
    // Skip document metadata types
    if (aTS.subsumes(aTS.getType(CAS.TYPE_NAME_DOCUMENT_ANNOTATION), type)) {
        log.debug("[{}] is a document-level annotation. Skipping.", type.getName());
        return;
    }

    // Skip DKPro Core Token and Sentence which as handled specially by WebAnno
    if (
        "de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Sentence".equals(aTD.getName()) ||
        "de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Token".equals(aTD.getName())
    ) {
        log.debug("[{}] is a DKPro Core segmentation type. Skipping.", aTD.getName());
        return;
    }
    
    Optional<RelationDetails> relationDetails = analyzeRelationLayer(aTS, type);
    Optional<ChainDetails> chainDetails = analyzeChainLayer(aTS, type);
    boolean isChain = chainDetails.isPresent();
    
    // Layers must be sub-types of Annotation (unless they are chains)
    if (!isChain && !aTS.subsumes(aTS.getType(CAS.TYPE_NAME_ANNOTATION), type)) {
        log.debug("[{}] is not an annotation type. Skipping.", aTD.getName());
        return;
    }

    // Actually, layers must inherit directly from Annotation (unless they are chains)
    // because WebAnno presently does not support DKPro Core elevated types (e.g. NOUN).
    if (isElevatedType(aTS, type)) {
        log.debug("[{}] looks like an elevated type. Skipping.", aTD.getName());
        return;
    }

    AnnotationLayer layer = new AnnotationLayer();
    Optional<? extends LayerDetails> details;
    if (isChain) {
        details = chainDetails;
        layer.setName(removeEnd(aTD.getName(), "Chain"));
        layer.setUiName(removeEnd(type.getShortName(), "Chain"));
        layer.setType(CHAIN_TYPE);
        chainDetailMap.put(layer.getName(), chainDetails.get());
    }
    else if (relationDetails.isPresent()) {
        details = relationDetails;
        layer.setName(aTD.getName());
        layer.setUiName(type.getShortName());
        layer.setType(RELATION_TYPE);
        relationDetailMap.put(layer.getName(), relationDetails.get());
    }
    else if (isSpanLayer(aTS, type)) {
        details = Optional.empty();
        layer.setName(aTD.getName());
        layer.setUiName(type.getShortName());
        layer.setType(SPAN_TYPE);
    }
    else {
        log.debug("Unable to determine layer type for [{}]", type.getName());
        return;
    }
    
    log.debug("[{}] seems to be a {}", aTD.getName(), layer.getType());
    
    layer.setDescription(trimToNull(aTD.getDescription()));
    
    layer.setEnabled(true);
    layer.setBuiltIn(false);
    
    // We cannot determine good values for these without looking at actual annotations, thus
    // we choose the most relaxed/permissive configuration here.
    layer.setOverlapMode(OverlapMode.ANY_OVERLAP);
    layer.setCrossSentence(true);
    layer.setAnchoringMode(AnchoringMode.CHARACTERS);
    layer.setLinkedListBehavior(false);
    
    layers.add(layer);
    
    // If the current layer is a chain layer (chain head), then we do not record
    // any features for it - instead we record the features of the link type.
    if (CHAIN_TYPE.equals(layer.getType())) {
        TypeDescription linkTypeDescription = aTSD.getType(layer.getName() + "Link");
        analyzeFeatures(layer, aTS, linkTypeDescription, details);
    }
    else {
        analyzeFeatures(layer, aTS, aTD, details);
    }
}
 
Example 6
Source File: AnnotationSchemaServiceImpl.java    From webanno with Apache License 2.0 4 votes vote down vote up
@Override
@Transactional(noRollbackFor = NoResultException.class)
public AnnotationLayer findLayer(Project aProject, String aName)
{
    // If there is a layer definition for the given name, then return it immediately
    Optional<AnnotationLayer> layer = getLayerInternal(aName, aProject);
    if (layer.isPresent()) {
        return layer.get();
    }
    
    TypeSystemDescription tsd;
    try {
        tsd = getFullProjectTypeSystem(aProject);
    }
    catch (ResourceInitializationException e) {
        throw new RuntimeException(e);
    }

    TypeDescription type = tsd.getType(aName);
    // If the super-type is not covered by the type system, then it is most likely a
    // UIMA built-in type. In this case we can stop the search since we do not have
    // layer definitions for UIMA built-in types.
    if (type == null) {
        throw new NoResultException("Type [" + aName + "] not found in the type system");
    }
    
    // If there is no layer definition for the given type name, try using the type system
    // definition to determine a suitable layer definition for a super type of the given type.
    while (true) {
        // If there is no super type, then we cannot find a suitable layer definition
        if (type.getSupertypeName() == null) {
            throw new NoResultException(
                    "No more super-types - no suitable layer definition found for type ["
                            + aName + "]");
        }

        // If there is a super-type then see if there is layer definition for it
        type = tsd.getType(type.getSupertypeName());

        // If the super-type is not covered by the type system, then it is most likely a
        // UIMA built-in type. In this case we can stop the search since we do not have
        // layer definitions for UIMA built-in types.
        if (type == null) {
            throw new NoResultException(
                    "Super-type not in type system - no suitable layer definition found for type ["
                            + aName + "]");
        }

        layer = getLayerInternal(type.getName(), aProject);
        
        // If the a layer definition of the given type was found, return it
        if (layer.isPresent()) {
            return layer.get();
        }
        
        // Otherwise attempt going one level higher in the inheritance hierarchy
    }
}
 
Example 7
Source File: AnnotationSchemaServiceImpl.java    From webanno with Apache License 2.0 4 votes vote down vote up
@Override
@Transactional
public void importUimaTypeSystem(Project aProject, TypeSystemDescription aTSD)
    throws ResourceInitializationException
{
    TypeSystemDescription builtInTypes = createTypeSystemDescription();
    
    TypeSystemAnalysis analysis = TypeSystemAnalysis.of(aTSD);
    for (AnnotationLayer l : analysis.getLayers()) {
        // Modifications/imports of built-in layers are not supported
        if (builtInTypes.getType(l.getName()) != null) {
            continue;
        }
        
        // If a custom layer does not exist yet, create it
        if (!existsLayer(l.getName(), aProject)) {
            l.setProject(aProject);

            // Need to set the attach type
            if (WebAnnoConst.RELATION_TYPE.equals(l.getType())) {
                RelationDetails relDetails = analysis.getRelationDetails(l.getName());

                AnnotationLayer attachLayer;
                try {
                    // First check if this type is already in the project
                    attachLayer = findLayer(aProject,
                            relDetails.getAttachLayer());
                }
                catch (NoResultException e) {
                    // If it does not exist in the project yet, then we create it
                    attachLayer = analysis.getLayer(relDetails.getAttachLayer());
                    attachLayer.setProject(aProject);
                    createLayer(attachLayer);
                }

                l.setAttachType(attachLayer);
            }

            createLayer(l);
        }

        // Import the features for the layer except if the layer is a built-in layer.
        // We must not touch the built-in layers because WebAnno may rely on their
        // structure. This is a conservative measure for now any may be relaxed in the
        // future.
        AnnotationLayer persistedLayer = findLayer(aProject, l.getName());
        if (!persistedLayer.isBuiltIn()) {
            for (AnnotationFeature f : analysis.getFeatures(l.getName())) {
                if (!existsFeature(f.getName(), persistedLayer)) {
                    f.setProject(aProject);
                    f.setLayer(persistedLayer);
                    createFeature(f);
                }
            }
        }
    }
}
 
Example 8
Source File: CasCreationUtilsTest.java    From uima-uimaj with Apache License 2.0 4 votes vote down vote up
public void testMergeDelegateAnalysisEngineTypeSystems() throws Exception {
  try {
    File descFile = JUnitExtension
            .getFile("TextAnalysisEngineImplTest/AggregateTaeForMergeTest.xml");
    AnalysisEngineDescription desc = UIMAFramework.getXMLParser().parseAnalysisEngineDescription(
            new XMLInputSource(descFile));
    Map mergedTypes = new HashMap();
    TypeSystemDescription typeSys = CasCreationUtils.mergeDelegateAnalysisEngineTypeSystems(desc,
            UIMAFramework.newDefaultResourceManager(), mergedTypes);

    // test results of merge
    Assert.assertEquals(8, typeSys.getTypes().length);

    TypeDescription type0 = typeSys.getType("NamedEntity");
    Assert.assertNotNull(type0);
    Assert.assertEquals("uima.tcas.Annotation", type0.getSupertypeName());
    Assert.assertEquals(1, type0.getFeatures().length);

    TypeDescription type1 = typeSys.getType("Person");
    Assert.assertNotNull(type1);
    Assert.assertEquals("NamedEntity", type1.getSupertypeName());
    Assert.assertEquals(1, type1.getFeatures().length);

    TypeDescription type2 = typeSys.getType("Place");
    Assert.assertNotNull(type2);
    Assert.assertEquals("NamedEntity", type2.getSupertypeName());
    Assert.assertEquals(3, type2.getFeatures().length);

    TypeDescription type3 = typeSys.getType("Org");
    Assert.assertNotNull(type3);
    Assert.assertEquals("uima.tcas.Annotation", type3.getSupertypeName());
    Assert.assertEquals(0, type3.getFeatures().length);

    TypeDescription type4 = typeSys.getType("DocumentStructure");
    Assert.assertNotNull(type4);
    Assert.assertEquals("uima.tcas.Annotation", type4.getSupertypeName());
    Assert.assertEquals(0, type4.getFeatures().length);

    TypeDescription type5 = typeSys.getType("Paragraph");
    Assert.assertNotNull(type5);
    Assert.assertEquals("DocumentStructure", type5.getSupertypeName());
    Assert.assertEquals(0, type5.getFeatures().length);

    TypeDescription type6 = typeSys.getType("Sentence");
    Assert.assertNotNull(type6);
    Assert.assertEquals("DocumentStructure", type6.getSupertypeName());
    Assert.assertEquals(0, type6.getFeatures().length);

    TypeDescription type7 = typeSys.getType("test.flowController.Test");
    Assert.assertNotNull(type7);
    Assert.assertEquals("uima.tcas.Annotation", type7.getSupertypeName());
    Assert.assertEquals(1, type7.getFeatures().length);

    // Place has merged features, Person has different supertype
    assertEquals(2, mergedTypes.size());
    assertTrue(mergedTypes.containsKey("Place"));
    assertTrue(mergedTypes.containsKey("Person"));

    // make sure one-arg version doesn't fail
    CasCreationUtils.mergeDelegateAnalysisEngineTypeSystems(desc);

  } catch (Exception e) {
    JUnitExtension.handleException(e);
  }
}
 
Example 9
Source File: AnalysisEngine_implTest.java    From uima-uimaj with Apache License 2.0 4 votes vote down vote up
public void testProcessDelegateAnalysisEngineMetaData() throws Exception {
  try {
    // create aggregate analysis engine whose delegates each declare
    // type system, type priorities, and indexes
    XMLInputSource in = new XMLInputSource(JUnitExtension
            .getFile("TextAnalysisEngineImplTest/AggregateTaeForMergeTest.xml"));
    AnalysisEngineDescription desc = UIMAFramework.getXMLParser().parseAnalysisEngineDescription(in);
    AggregateAnalysisEngine_impl ae = new AggregateAnalysisEngine_impl();
    ae.initialize(desc, Collections.EMPTY_MAP);
    // initialize method automatically calls processDelegateAnalysisEngineMetaData()

    // test results of merge
    // TypeSystem
    TypeSystemDescription typeSys = ae.getAnalysisEngineMetaData().getTypeSystem();
    assertEquals(8, typeSys.getTypes().length);

    TypeDescription type0 = typeSys.getType("NamedEntity");
    assertNotNull(type0);
    assertEquals("uima.tcas.Annotation", type0.getSupertypeName());
    assertEquals(1, type0.getFeatures().length);

    TypeDescription type1 = typeSys.getType("Person");
    assertNotNull(type1);
    assertEquals("NamedEntity", type1.getSupertypeName());
    assertEquals(1, type1.getFeatures().length);

    TypeDescription type2 = typeSys.getType("Place");
    assertNotNull(type2);
    assertEquals("NamedEntity", type2.getSupertypeName());
    assertEquals(3, type2.getFeatures().length);

    TypeDescription type3 = typeSys.getType("Org");
    assertNotNull(type3);
    assertEquals("uima.tcas.Annotation", type3.getSupertypeName());
    assertEquals(0, type3.getFeatures().length);

    TypeDescription type4 = typeSys.getType("DocumentStructure");
    assertNotNull(type4);
    assertEquals("uima.tcas.Annotation", type4.getSupertypeName());
    assertEquals(0, type4.getFeatures().length);

    TypeDescription type5 = typeSys.getType("Paragraph");
    assertNotNull(type5);
    assertEquals("DocumentStructure", type5.getSupertypeName());
    assertEquals(0, type5.getFeatures().length);

    TypeDescription type6 = typeSys.getType("Sentence");
    assertNotNull(type6);
    assertEquals("DocumentStructure", type6.getSupertypeName());
    assertEquals(0, type6.getFeatures().length);

    TypeDescription type7 = typeSys.getType("test.flowController.Test");
    assertNotNull(type7);
    assertEquals("uima.tcas.Annotation", type7.getSupertypeName());
    assertEquals(1, type7.getFeatures().length);

    // TypePriorities
    TypePriorities pri = ae.getAnalysisEngineMetaData().getTypePriorities();
    assertNotNull(pri);
    TypePriorityList[] priLists = pri.getPriorityLists();
    assertEquals(3, priLists.length);
    String[] list0 = priLists[0].getTypes();
    String[] list1 = priLists[1].getTypes();
    String[] list2 = priLists[2].getTypes();
    // order of the three lists is not defined
    assertTrue((list0.length == 2 && list1.length == 2 && list2.length == 3)
            || (list0.length == 2 && list1.length == 3 && list2.length == 2)
            || (list0.length == 3 && list1.length == 2 && list2.length == 2));

    // Indexes
    FsIndexDescription[] indexes = ae.getAnalysisEngineMetaData().getFsIndexes();
    assertEquals(3, indexes.length);
    // order of indexes is not defined
    String label0 = indexes[0].getLabel();
    String label1 = indexes[1].getLabel();
    String label2 = indexes[2].getLabel();
    assertTrue(label0.equals("DocStructIndex") || label1.equals("DocStructIndex")
            || label2.equals("DocStructIndex"));
    assertTrue(label0.equals("PlaceIndex") || label1.equals("PlaceIndex")
            || label2.equals("PlaceIndex"));
    assertTrue(label0.equals("FlowControllerTestIndex")
            || label1.equals("FlowControllerTestIndex")
            || label2.equals("FlowControllerTestIndex"));

    // test that we can create a CAS
    CAS cas = ae.newCAS();
    TypeSystem ts = cas.getTypeSystem();
    assertNotNull(ts.getType("NamedEntity"));
    assertNotNull(ts.getType("Person"));
    assertNotNull(ts.getType("Place"));
    assertNotNull(ts.getType("Org"));
    assertNotNull(ts.getType("DocumentStructure"));
    assertNotNull(ts.getType("Paragraph"));
    assertNotNull(ts.getType("Sentence"));
    assertNotNull(ts.getType("test.flowController.Test"));
  } catch (Exception e) {
    JUnitExtension.handleException(e);
  }
}