Search in sources :

Example 1 with AttachedAnnotation

use of de.tudarmstadt.ukp.clarin.webanno.api.AttachedAnnotation in project webanno by webanno.

the class AnnotationDetailEditorPanel method deleteAnnotation.

private void deleteAnnotation(CAS aCas, AnnotatorState state, AnnotationFS fs, AnnotationLayer layer, TypeAdapter adapter) {
    RequestCycle.get().find(AjaxRequestTarget.class).ifPresent(_target -> _target.addChildren(getPage(), IFeedback.class));
    // point to it directly or indirectly via the attachFeature.
    if (adapter instanceof SpanAdapter) {
        for (AttachedAnnotation rel : annotationService.getAttachedRels(layer, fs)) {
            RelationAdapter relationAdapter = (RelationAdapter) annotationService.findAdapter(state.getProject(), rel.getRelation());
            relationAdapter.delete(state.getDocument(), state.getUser().getUsername(), aCas, new VID(rel.getRelation()));
            info(generateMessage(relationAdapter.getLayer(), null, true));
        }
    }
    // to be deleted: the link feature must be the type of the FS or it must be generic.
    if (adapter instanceof SpanAdapter) {
        for (AnnotationFeature linkFeature : annotationService.listAttachedLinkFeatures(layer)) {
            Type linkHostType = CasUtil.getType(aCas, linkFeature.getLayer().getName());
            for (FeatureStructure linkHostFS : CasUtil.selectFS(aCas, linkHostType)) {
                List<LinkWithRoleModel> links = adapter.getFeatureValue(linkFeature, linkHostFS);
                Iterator<LinkWithRoleModel> i = links.iterator();
                boolean modified = false;
                while (i.hasNext()) {
                    LinkWithRoleModel link = i.next();
                    if (link.targetAddr == getAddr(fs)) {
                        i.remove();
                        info("Cleared slot [" + link.role + "] in feature [" + linkFeature.getUiName() + "] on [" + linkFeature.getLayer().getUiName() + "]");
                        LOG.debug("Cleared slot [" + link.role + "] in feature [" + linkFeature.getName() + "] on annotation [" + getAddr(linkHostFS) + "]");
                        modified = true;
                    }
                }
                if (modified) {
                    setFeature(linkHostFS, linkFeature, links);
                    // If the currently armed slot is part of this link, then we disarm the slot
                    // to avoid the armed slot no longer pointing at the index which the user
                    // had selected it to point at.
                    FeatureState armedFeature = state.getArmedFeature();
                    if (armedFeature != null && WebAnnoCasUtil.getAddr(linkHostFS) == armedFeature.vid.getId() && armedFeature.feature.equals(linkFeature)) {
                        state.clearArmedSlot();
                    }
                }
            }
        }
    }
    // relation.
    if (adapter instanceof RelationAdapter) {
    // Do nothing ;)
    }
    // Actually delete annotation
    adapter.delete(state.getDocument(), state.getUser().getUsername(), aCas, state.getSelection().getAnnotation());
    info(generateMessage(state.getSelectedAnnotationLayer(), null, true));
}
Also used : RelationAdapter(de.tudarmstadt.ukp.clarin.webanno.api.annotation.adapter.RelationAdapter) LinkWithRoleModel(de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.LinkWithRoleModel) FeatureState(de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.FeatureState) AjaxRequestTarget(org.apache.wicket.ajax.AjaxRequestTarget) VID(de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.VID) FeatureStructure(org.apache.uima.cas.FeatureStructure) Type(org.apache.uima.cas.Type) SpanAdapter(de.tudarmstadt.ukp.clarin.webanno.api.annotation.adapter.SpanAdapter) IFeedback(org.apache.wicket.feedback.IFeedback) AttachedAnnotation(de.tudarmstadt.ukp.clarin.webanno.api.AttachedAnnotation) AnnotationFeature(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationFeature)

Example 2 with AttachedAnnotation

use of de.tudarmstadt.ukp.clarin.webanno.api.AttachedAnnotation in project webanno by webanno.

the class AttachedAnnotationListPanel method getRelationInfo.

private List<AttachedAnnotationInfo> getRelationInfo() {
    Selection selection = getModelObject().getSelection();
    if (!selection.isSet() || selection.getAnnotation().getId() == NONE) {
        return Collections.emptyList();
    }
    CAS cas;
    try {
        cas = page.getEditorCas();
    } catch (IOException e) {
        // If we have trouble accessing the CAS, we probably never get here anyway...
        // the AnnotationPageBase should already have found the issue and displayed some
        // error to the user.
        LOG.error("Unable to access editor CAS", e);
        return Collections.emptyList();
    }
    AnnotationFS annoFs = selectAnnotationByAddr(cas, selection.getAnnotation().getId());
    VID localVid = new VID(annoFs);
    List<AttachedAnnotation> attachedAnnotations = new ArrayList<>();
    attachedAnnotations.addAll(schemaService.getAttachedRels(getModelObject().getSelectedAnnotationLayer(), annoFs));
    attachedAnnotations.addAll(schemaService.getAttachedLinks(getModelObject().getSelectedAnnotationLayer(), annoFs));
    Map<AnnotationLayer, List<AnnotationFeature>> featureCache = new HashMap<>();
    Map<AnnotationLayer, Renderer> rendererCache = new HashMap<>();
    Map<AnnotationLayer, TypeAdapter> adapterCache = new HashMap<>();
    List<AttachedAnnotationInfo> result = new ArrayList<>();
    for (AttachedAnnotation rel : attachedAnnotations) {
        AnnotationLayer layer = rel.getLayer();
        List<AnnotationFeature> features = featureCache.get(layer);
        TypeAdapter adapter;
        Renderer renderer;
        if (features == null) {
            features = schemaService.listSupportedFeatures(layer);
            featureCache.put(layer, features);
            adapter = schemaService.getAdapter(layer);
            adapterCache.put(layer, adapter);
            renderer = layerRegistry.getLayerSupport(layer).createRenderer(layer, () -> featureCache.get(layer));
            rendererCache.put(layer, renderer);
        } else {
            adapter = adapterCache.get(layer);
            renderer = rendererCache.get(layer);
        }
        Map<String, String> renderedFeatures;
        if (rel.getRelation() != null) {
            renderedFeatures = renderer.renderLabelFeatureValues(adapter, rel.getRelation(), features);
        } else {
            renderedFeatures = renderer.renderLabelFeatureValues(adapter, rel.getEndpoint(), features);
        }
        String labelText = TypeUtil.getUiLabelText(adapter, renderedFeatures);
        if (isEmpty(labelText)) {
            labelText = rel.getLayer().getUiName();
        } else {
            labelText = rel.getLayer().getUiName() + ": " + labelText;
        }
        AttachedAnnotationInfo i = new AttachedAnnotationInfo(layer, localVid, rel.getRelation() != null ? new VID(rel.getRelation()) : null, new VID(rel.getEndpoint()), labelText, rel.getEndpoint().getCoveredText(), rel.getDirection());
        result.add(i);
    }
    return result;
}
Also used : HashMap(java.util.HashMap) Selection(de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.Selection) ArrayList(java.util.ArrayList) IOException(java.io.IOException) AnnotationLayer(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationLayer) VID(de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.VID) AnnotationFS(org.apache.uima.cas.text.AnnotationFS) CAS(org.apache.uima.cas.CAS) Renderer(de.tudarmstadt.ukp.clarin.webanno.api.annotation.rendering.Renderer) TypeAdapter(de.tudarmstadt.ukp.clarin.webanno.api.annotation.adapter.TypeAdapter) ArrayList(java.util.ArrayList) Arrays.asList(java.util.Arrays.asList) List(java.util.List) AttachedAnnotation(de.tudarmstadt.ukp.clarin.webanno.api.AttachedAnnotation) AnnotationFeature(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationFeature)

Example 3 with AttachedAnnotation

use of de.tudarmstadt.ukp.clarin.webanno.api.AttachedAnnotation in project webanno by webanno.

the class AnnotationSchemaServiceImpl method getAttachedRels.

@Override
@Transactional
public List<AttachedAnnotation> getAttachedRels(AnnotationLayer aLayer, AnnotationFS aFs) {
    CAS cas = aFs.getCAS();
    List<AttachedAnnotation> result = new ArrayList<>();
    for (AnnotationLayer relationLayer : listAttachedRelationLayers(aLayer)) {
        RelationAdapter relationAdapter = (RelationAdapter) getAdapter(relationLayer);
        Type relationType = CasUtil.getType(cas, relationLayer.getName());
        Feature sourceFeature = relationType.getFeatureByBaseName(relationAdapter.getSourceFeatureName());
        Feature targetFeature = relationType.getFeatureByBaseName(relationAdapter.getTargetFeatureName());
        // This code is already prepared for the day that relations can go between
        // different layers and may have different attach features for the source and
        // target layers.
        Feature relationSourceAttachFeature = null;
        Feature relationTargetAttachFeature = null;
        if (relationAdapter.getAttachFeatureName() != null) {
            relationSourceAttachFeature = sourceFeature.getRange().getFeatureByBaseName(relationAdapter.getAttachFeatureName());
            relationTargetAttachFeature = targetFeature.getRange().getFeatureByBaseName(relationAdapter.getAttachFeatureName());
        }
        for (AnnotationFS relationFS : CasUtil.select(cas, relationType)) {
            if (!(relationFS instanceof AnnotationFS)) {
                continue;
            }
            // Here we get the annotations that the relation is pointing to in the UI
            AnnotationFS sourceFS;
            if (relationSourceAttachFeature != null) {
                sourceFS = (AnnotationFS) relationFS.getFeatureValue(sourceFeature).getFeatureValue(relationSourceAttachFeature);
            } else {
                sourceFS = (AnnotationFS) relationFS.getFeatureValue(sourceFeature);
            }
            AnnotationFS targetFS;
            if (relationTargetAttachFeature != null) {
                targetFS = (AnnotationFS) relationFS.getFeatureValue(targetFeature).getFeatureValue(relationTargetAttachFeature);
            } else {
                targetFS = (AnnotationFS) relationFS.getFeatureValue(targetFeature);
            }
            if (sourceFS == null || targetFS == null) {
                StringBuilder message = new StringBuilder();
                message.append("Relation [" + relationAdapter.getLayer().getName() + "] with id [" + getAddr(relationFS) + "] has loose ends - cannot identify attached annotations.");
                if (relationAdapter.getAttachFeatureName() != null) {
                    message.append("\nRelation [" + relationAdapter.getLayer().getName() + "] attached to feature [" + relationAdapter.getAttachFeatureName() + "].");
                }
                message.append("\nSource: " + sourceFS);
                message.append("\nTarget: " + targetFS);
                log.warn("{}", message.toString());
                continue;
            }
            boolean isIncoming = isSame(targetFS, aFs);
            boolean isOutgoing = isSame(sourceFS, aFs);
            if (isIncoming && isOutgoing) {
                result.add(new AttachedAnnotation(relationLayer, relationFS, sourceFS, LOOP));
            } else if (isIncoming) {
                result.add(new AttachedAnnotation(relationLayer, relationFS, sourceFS, INCOMING));
            } else if (isOutgoing) {
                result.add(new AttachedAnnotation(relationLayer, relationFS, targetFS, OUTGOING));
            }
        }
    }
    return result;
}
Also used : RelationAdapter(de.tudarmstadt.ukp.clarin.webanno.api.annotation.adapter.RelationAdapter) AnnotationFS(org.apache.uima.cas.text.AnnotationFS) Type(org.apache.uima.cas.Type) WebAnnoCasUtil.isNativeUimaType(de.tudarmstadt.ukp.clarin.webanno.api.annotation.util.WebAnnoCasUtil.isNativeUimaType) CAS(org.apache.uima.cas.CAS) ArrayList(java.util.ArrayList) AnnotationLayer(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationLayer) Feature(org.apache.uima.cas.Feature) AnnotationFeature(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationFeature) AttachedAnnotation(de.tudarmstadt.ukp.clarin.webanno.api.AttachedAnnotation) Transactional(org.springframework.transaction.annotation.Transactional)

Example 4 with AttachedAnnotation

use of de.tudarmstadt.ukp.clarin.webanno.api.AttachedAnnotation in project webanno by webanno.

the class AnnotationSchemaServiceImpl method getAttachedLinks.

@Override
@Transactional
public List<AttachedAnnotation> getAttachedLinks(AnnotationLayer aLayer, AnnotationFS aFs) {
    CAS cas = aFs.getCAS();
    List<AttachedAnnotation> result = new ArrayList<>();
    TypeAdapter adapter = getAdapter(aLayer);
    if (adapter instanceof SpanAdapter) {
        for (AnnotationFeature linkFeature : listAttachedLinkFeatures(aLayer)) {
            if (MultiValueMode.ARRAY.equals(linkFeature.getMultiValueMode()) && LinkMode.WITH_ROLE.equals(linkFeature.getLinkMode())) {
                // Fetch slot hosts that could link to the current FS and check if any of
                // them actually links to the current FS
                Type linkHost = CasUtil.getType(cas, linkFeature.getLayer().getName());
                for (FeatureStructure linkFS : CasUtil.selectFS(cas, linkHost)) {
                    if (!(linkFS instanceof AnnotationFS)) {
                        continue;
                    }
                    List<LinkWithRoleModel> links = adapter.getFeatureValue(linkFeature, linkFS);
                    for (int li = 0; li < links.size(); li++) {
                        LinkWithRoleModel link = links.get(li);
                        AnnotationFS linkTarget = selectByAddr(cas, AnnotationFS.class, link.targetAddr);
                        // our list of attached links.
                        if (isSame(linkTarget, aFs)) {
                            result.add(new AttachedAnnotation(linkFeature.getLayer(), (AnnotationFS) linkFS, INCOMING));
                        }
                    }
                }
            }
        }
    }
    return result;
}
Also used : LinkWithRoleModel(de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.LinkWithRoleModel) ArrayList(java.util.ArrayList) FeatureStructure(org.apache.uima.cas.FeatureStructure) AnnotationFS(org.apache.uima.cas.text.AnnotationFS) Type(org.apache.uima.cas.Type) WebAnnoCasUtil.isNativeUimaType(de.tudarmstadt.ukp.clarin.webanno.api.annotation.util.WebAnnoCasUtil.isNativeUimaType) CAS(org.apache.uima.cas.CAS) TypeAdapter(de.tudarmstadt.ukp.clarin.webanno.api.annotation.adapter.TypeAdapter) SpanAdapter(de.tudarmstadt.ukp.clarin.webanno.api.annotation.adapter.SpanAdapter) AttachedAnnotation(de.tudarmstadt.ukp.clarin.webanno.api.AttachedAnnotation) AnnotationFeature(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationFeature) Transactional(org.springframework.transaction.annotation.Transactional)

Example 5 with AttachedAnnotation

use of de.tudarmstadt.ukp.clarin.webanno.api.AttachedAnnotation in project webanno by webanno.

the class AnnotationDetailEditorPanel method checkAttachStatus.

private AttachStatus checkAttachStatus(AjaxRequestTarget aTarget, Project aProject, AnnotationFS aFS) {
    AnnotationLayer layer = annotationService.findLayer(aProject, aFS);
    AttachStatus attachStatus = new AttachStatus();
    List<AttachedAnnotation> attachedRels = annotationService.getAttachedRels(layer, aFS);
    boolean attachedToReadOnlyRels = attachedRels.stream().anyMatch(rel -> rel.getLayer().isReadonly());
    if (attachedToReadOnlyRels) {
        attachStatus.readOnlyAttached |= true;
    }
    attachStatus.attachCount += attachedRels.size();
    // We do not count these atm since they only exist for built-in layers and are not
    // visible in the UI for the user.
    // Set<AnnotationFS> attachedSpans = getAttachedSpans(aFS, layer);
    // boolean attachedToReadOnlySpans = attachedSpans.stream().anyMatch(relFS -> {
    // AnnotationLayer relLayer = annotationService.getLayer(aProject, relFS);
    // return relLayer.isReadonly();
    // });
    // if (attachedToReadOnlySpans) {
    // attachStatus.readOnlyAttached |= true;
    // }
    // attachStatus.attachCount += attachedSpans.size();
    List<AttachedAnnotation> attachedLinks = annotationService.getAttachedLinks(layer, aFS);
    boolean attachedToReadOnlyLinks = attachedLinks.stream().anyMatch(rel -> rel.getLayer().isReadonly());
    if (attachedToReadOnlyLinks) {
        attachStatus.readOnlyAttached |= true;
    }
    attachStatus.attachCount += attachedLinks.size();
    return attachStatus;
}
Also used : AnnotationLayer(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationLayer) AttachedAnnotation(de.tudarmstadt.ukp.clarin.webanno.api.AttachedAnnotation)

Aggregations

AttachedAnnotation (de.tudarmstadt.ukp.clarin.webanno.api.AttachedAnnotation)5 AnnotationFeature (de.tudarmstadt.ukp.clarin.webanno.model.AnnotationFeature)4 AnnotationLayer (de.tudarmstadt.ukp.clarin.webanno.model.AnnotationLayer)3 ArrayList (java.util.ArrayList)3 CAS (org.apache.uima.cas.CAS)3 Type (org.apache.uima.cas.Type)3 AnnotationFS (org.apache.uima.cas.text.AnnotationFS)3 RelationAdapter (de.tudarmstadt.ukp.clarin.webanno.api.annotation.adapter.RelationAdapter)2 SpanAdapter (de.tudarmstadt.ukp.clarin.webanno.api.annotation.adapter.SpanAdapter)2 TypeAdapter (de.tudarmstadt.ukp.clarin.webanno.api.annotation.adapter.TypeAdapter)2 LinkWithRoleModel (de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.LinkWithRoleModel)2 VID (de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.VID)2 WebAnnoCasUtil.isNativeUimaType (de.tudarmstadt.ukp.clarin.webanno.api.annotation.util.WebAnnoCasUtil.isNativeUimaType)2 FeatureStructure (org.apache.uima.cas.FeatureStructure)2 Transactional (org.springframework.transaction.annotation.Transactional)2 FeatureState (de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.FeatureState)1 Selection (de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.Selection)1 Renderer (de.tudarmstadt.ukp.clarin.webanno.api.annotation.rendering.Renderer)1 IOException (java.io.IOException)1 Arrays.asList (java.util.Arrays.asList)1