Search in sources :

Example 1 with RelationLayerBehavior

use of de.tudarmstadt.ukp.clarin.webanno.api.annotation.adapter.RelationLayerBehavior in project webanno by webanno.

the class RelationRenderer method render.

@Override
public void render(final CAS aCas, List<AnnotationFeature> aFeatures, VDocument aResponse, int aWindowBegin, int aWindowEnd) {
    RelationAdapter typeAdapter = getTypeAdapter();
    Type type;
    Type spanType;
    try {
        type = getType(aCas, typeAdapter.getAnnotationTypeName());
        spanType = getType(aCas, typeAdapter.getAttachTypeName());
    } catch (IllegalArgumentException e) {
        // CAS does not contain any instances of them
        return;
    }
    List<AnnotationFeature> visibleFeatures = aFeatures.stream().filter(f -> f.isVisible() && f.isEnabled()).collect(Collectors.toList());
    Feature dependentFeature = type.getFeatureByBaseName(typeAdapter.getTargetFeatureName());
    Feature governorFeature = type.getFeatureByBaseName(typeAdapter.getSourceFeatureName());
    Feature arcSpanFeature = spanType.getFeatureByBaseName(typeAdapter.getAttachFeatureName());
    FeatureStructure dependentFs;
    FeatureStructure governorFs;
    Map<Integer, Set<Integer>> relationLinks = getRelationLinks(aCas, aWindowBegin, aWindowEnd, type, dependentFeature, governorFeature, arcSpanFeature);
    // if this is a governor for more than one dependent, avoid duplicate yield
    List<Integer> yieldDeps = new ArrayList<>();
    // Index mapping annotations to the corresponding rendered arcs
    Map<AnnotationFS, VArc> annoToArcIdx = new HashMap<>();
    for (AnnotationFS fs : selectCovered(aCas, type, aWindowBegin, aWindowEnd)) {
        if (typeAdapter.getAttachFeatureName() != null) {
            dependentFs = fs.getFeatureValue(dependentFeature).getFeatureValue(arcSpanFeature);
            governorFs = fs.getFeatureValue(governorFeature).getFeatureValue(arcSpanFeature);
        } else {
            dependentFs = fs.getFeatureValue(dependentFeature);
            governorFs = fs.getFeatureValue(governorFeature);
        }
        String bratTypeName = typeAdapter.getEncodedTypeName();
        Map<String, String> features = renderLabelFeatureValues(typeAdapter, fs, visibleFeatures);
        if (dependentFs == null || governorFs == null) {
            StringBuilder message = new StringBuilder();
            message.append("Relation [" + typeAdapter.getLayer().getName() + "] with id [" + getAddr(fs) + "] has loose ends - cannot render.");
            if (typeAdapter.getAttachFeatureName() != null) {
                message.append("\nRelation [" + typeAdapter.getLayer().getName() + "] attached to feature [" + typeAdapter.getAttachFeatureName() + "].");
            }
            message.append("\nDependent: " + dependentFs);
            message.append("\nGovernor: " + governorFs);
            RequestCycle requestCycle = RequestCycle.get();
            IPageRequestHandler handler = PageRequestHandlerTracker.getLastHandler(requestCycle);
            Page page = (Page) handler.getPage();
            page.warn(message.toString());
            continue;
        }
        VArc arc = new VArc(typeAdapter.getLayer(), fs, bratTypeName, governorFs, dependentFs, features);
        arc.addLazyDetails(getLazyDetails(typeAdapter, fs, aFeatures));
        annoToArcIdx.put(fs, arc);
        aResponse.add(arc);
        // Render errors if required features are missing
        renderRequiredFeatureErrors(visibleFeatures, fs, aResponse);
        if (relationLinks.keySet().contains(getAddr(governorFs)) && !yieldDeps.contains(getAddr(governorFs))) {
            yieldDeps.add(getAddr(governorFs));
            // sort the annotations (begin, end)
            List<Integer> sortedDepFs = new ArrayList<>(relationLinks.get(getAddr(governorFs)));
            sortedDepFs.sort(comparingInt(arg0 -> selectAnnotationByAddr(aCas, arg0).getBegin()));
            String cm = getYieldMessage(aCas, sortedDepFs);
            aResponse.add(new VComment(governorFs, VCommentType.YIELD, cm));
        }
    }
    for (RelationLayerBehavior behavior : behaviors) {
        behavior.onRender(typeAdapter, aResponse, annoToArcIdx);
    }
}
Also used : AnnotationFS(org.apache.uima.cas.text.AnnotationFS) Page(org.apache.wicket.Page) VCommentType(de.tudarmstadt.ukp.clarin.webanno.api.annotation.rendering.model.VCommentType) LoggerFactory(org.slf4j.LoggerFactory) RelationLayerBehavior(de.tudarmstadt.ukp.clarin.webanno.api.annotation.adapter.RelationLayerBehavior) CAS(org.apache.uima.cas.CAS) Feature(org.apache.uima.cas.Feature) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Type(org.apache.uima.cas.Type) RequestCycle(org.apache.wicket.request.cycle.RequestCycle) Map(java.util.Map) PageRequestHandlerTracker(org.apache.wicket.request.cycle.PageRequestHandlerTracker) WebAnnoCasUtil.selectAnnotationByAddr(de.tudarmstadt.ukp.clarin.webanno.api.annotation.util.WebAnnoCasUtil.selectAnnotationByAddr) FeatureStructure(org.apache.uima.cas.FeatureStructure) LayerSupportRegistry(de.tudarmstadt.ukp.clarin.webanno.api.annotation.layer.LayerSupportRegistry) WebAnnoCasUtil.getAddr(de.tudarmstadt.ukp.clarin.webanno.api.annotation.util.WebAnnoCasUtil.getAddr) Comparator.comparingInt(java.util.Comparator.comparingInt) Logger(org.slf4j.Logger) Collections.emptyList(java.util.Collections.emptyList) VArc(de.tudarmstadt.ukp.clarin.webanno.api.annotation.rendering.model.VArc) VComment(de.tudarmstadt.ukp.clarin.webanno.api.annotation.rendering.model.VComment) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) Collectors(java.util.stream.Collectors) RelationAdapter(de.tudarmstadt.ukp.clarin.webanno.api.annotation.adapter.RelationAdapter) CasUtil.selectCovered(org.apache.uima.fit.util.CasUtil.selectCovered) List(java.util.List) ConcurrentSkipListSet(java.util.concurrent.ConcurrentSkipListSet) AnnotationFeature(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationFeature) IPageRequestHandler(org.apache.wicket.core.request.handler.IPageRequestHandler) CasUtil.getType(org.apache.uima.fit.util.CasUtil.getType) AnnotationAwareOrderComparator(org.springframework.core.annotation.AnnotationAwareOrderComparator) VDocument(de.tudarmstadt.ukp.clarin.webanno.api.annotation.rendering.model.VDocument) FeatureSupportRegistry(de.tudarmstadt.ukp.clarin.webanno.api.annotation.feature.FeatureSupportRegistry) Set(java.util.Set) ConcurrentSkipListSet(java.util.concurrent.ConcurrentSkipListSet) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) RequestCycle(org.apache.wicket.request.cycle.RequestCycle) ArrayList(java.util.ArrayList) Page(org.apache.wicket.Page) Feature(org.apache.uima.cas.Feature) AnnotationFeature(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationFeature) FeatureStructure(org.apache.uima.cas.FeatureStructure) AnnotationFS(org.apache.uima.cas.text.AnnotationFS) AnnotationFeature(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationFeature) RelationAdapter(de.tudarmstadt.ukp.clarin.webanno.api.annotation.adapter.RelationAdapter) RelationLayerBehavior(de.tudarmstadt.ukp.clarin.webanno.api.annotation.adapter.RelationLayerBehavior) IPageRequestHandler(org.apache.wicket.core.request.handler.IPageRequestHandler) VComment(de.tudarmstadt.ukp.clarin.webanno.api.annotation.rendering.model.VComment) VCommentType(de.tudarmstadt.ukp.clarin.webanno.api.annotation.rendering.model.VCommentType) Type(org.apache.uima.cas.Type) CasUtil.getType(org.apache.uima.fit.util.CasUtil.getType) VArc(de.tudarmstadt.ukp.clarin.webanno.api.annotation.rendering.model.VArc)

Aggregations

RelationAdapter (de.tudarmstadt.ukp.clarin.webanno.api.annotation.adapter.RelationAdapter)1 RelationLayerBehavior (de.tudarmstadt.ukp.clarin.webanno.api.annotation.adapter.RelationLayerBehavior)1 FeatureSupportRegistry (de.tudarmstadt.ukp.clarin.webanno.api.annotation.feature.FeatureSupportRegistry)1 LayerSupportRegistry (de.tudarmstadt.ukp.clarin.webanno.api.annotation.layer.LayerSupportRegistry)1 VArc (de.tudarmstadt.ukp.clarin.webanno.api.annotation.rendering.model.VArc)1 VComment (de.tudarmstadt.ukp.clarin.webanno.api.annotation.rendering.model.VComment)1 VCommentType (de.tudarmstadt.ukp.clarin.webanno.api.annotation.rendering.model.VCommentType)1 VDocument (de.tudarmstadt.ukp.clarin.webanno.api.annotation.rendering.model.VDocument)1 WebAnnoCasUtil.getAddr (de.tudarmstadt.ukp.clarin.webanno.api.annotation.util.WebAnnoCasUtil.getAddr)1 WebAnnoCasUtil.selectAnnotationByAddr (de.tudarmstadt.ukp.clarin.webanno.api.annotation.util.WebAnnoCasUtil.selectAnnotationByAddr)1 AnnotationFeature (de.tudarmstadt.ukp.clarin.webanno.model.AnnotationFeature)1 ArrayList (java.util.ArrayList)1 Collections.emptyList (java.util.Collections.emptyList)1 Comparator.comparingInt (java.util.Comparator.comparingInt)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ConcurrentSkipListSet (java.util.concurrent.ConcurrentSkipListSet)1