Search in sources :

Example 1 with IllegalPlacementException

use of de.tudarmstadt.ukp.clarin.webanno.api.annotation.exception.IllegalPlacementException in project webanno by webanno.

the class RelationAdapter method createRelationAnnotation.

private AnnotationFS createRelationAnnotation(CAS cas, AnnotationFS originFS, AnnotationFS targetFS) throws AnnotationException {
    if (targetFS == null || originFS == null) {
        throw new IllegalPlacementException("Relation must have a source and a target!");
    }
    // Set the relation offsets in DKPro Core style - the relation recieves the offsets from
    // the dependent
    // If origin and target spans are multiple tokens, dependentFS.getBegin will be the
    // the begin position of the first token and dependentFS.getEnd will be the End
    // position of the last token.
    final Type type = getType(cas, getLayer().getName());
    final Feature dependentFeature = type.getFeatureByBaseName(targetFeatureName);
    final Feature governorFeature = type.getFeatureByBaseName(sourceFeatureName);
    AnnotationFS newAnnotation = cas.createAnnotation(type, targetFS.getBegin(), targetFS.getEnd());
    newAnnotation.setFeatureValue(dependentFeature, targetFS);
    newAnnotation.setFeatureValue(governorFeature, originFS);
    cas.addFsToIndexes(newAnnotation);
    return newAnnotation;
}
Also used : AnnotationFS(org.apache.uima.cas.text.AnnotationFS) Type(org.apache.uima.cas.Type) CasUtil.getType(org.apache.uima.fit.util.CasUtil.getType) IllegalPlacementException(de.tudarmstadt.ukp.clarin.webanno.api.annotation.exception.IllegalPlacementException) Feature(org.apache.uima.cas.Feature) AnnotationFeature(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationFeature) FSUtil.getFeature(org.apache.uima.fit.util.FSUtil.getFeature)

Example 2 with IllegalPlacementException

use of de.tudarmstadt.ukp.clarin.webanno.api.annotation.exception.IllegalPlacementException in project webanno by webanno.

the class SpanAdapter method createSpanAnnotation.

/**
 * A Helper method to add annotation to CAS
 */
private AnnotationFS createSpanAnnotation(CAS aCas, int aBegin, int aEnd) throws AnnotationException {
    Type type = CasUtil.getType(aCas, getAnnotationTypeName());
    AnnotationFS newAnnotation = aCas.createAnnotation(type, aBegin, aEnd);
    log.trace("Created span annotation {}-{} [{}]", newAnnotation.getBegin(), newAnnotation.getEnd(), newAnnotation.getCoveredText());
    // created annotation.
    if (getAttachFeatureName() != null) {
        Type theType = CasUtil.getType(aCas, getAttachTypeName());
        Feature attachFeature = theType.getFeatureByBaseName(getAttachFeatureName());
        if (CasUtil.selectCovered(aCas, theType, aBegin, aEnd).isEmpty()) {
            throw new IllegalPlacementException("No annotation of type [" + getAttachTypeName() + "] to attach to at location [" + aBegin + "-" + aEnd + "].");
        }
        CasUtil.selectCovered(aCas, theType, aBegin, aEnd).get(0).setFeatureValue(attachFeature, newAnnotation);
    }
    aCas.addFsToIndexes(newAnnotation);
    return newAnnotation;
}
Also used : AnnotationFS(org.apache.uima.cas.text.AnnotationFS) Type(org.apache.uima.cas.Type) IllegalPlacementException(de.tudarmstadt.ukp.clarin.webanno.api.annotation.exception.IllegalPlacementException) Feature(org.apache.uima.cas.Feature) AnnotationFeature(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationFeature)

Example 3 with IllegalPlacementException

use of de.tudarmstadt.ukp.clarin.webanno.api.annotation.exception.IllegalPlacementException in project webanno by webanno.

the class AnnotationDetailEditorPanel method actionCreateOrUpdate.

@Override
public void actionCreateOrUpdate(AjaxRequestTarget aTarget, CAS aCas) throws IOException, AnnotationException {
    LOG.trace("actionAnnotate");
    editorPage.ensureIsEditable();
    AnnotatorState state = getModelObject();
    // switches from the selected span layer to the relation layer that is attached to the span
    if (state.getSelection().isArc()) {
        LOG.trace("actionAnnotate() relation annotation - looking for attached layer");
        // FIXME REC I think this whole section which meddles around with the selected
        // annotation layer should be moved out of there to the place where we originally set
        // the annotation layer...!
        // Fetch the annotation representing the origin endpoint of the relation
        AnnotationFS originFS = selectAnnotationByAddr(aCas, state.getSelection().getOrigin());
        AnnotationFS targetFS = selectAnnotationByAddr(aCas, state.getSelection().getTarget());
        if (!originFS.getType().equals(targetFS.getType())) {
            reset(aTarget);
            onChange(aTarget);
            throw new IllegalPlacementException("Cannot create relation between spans on different layers");
        }
        // Fetch the annotation layer for the origin annotation
        AnnotationLayer originLayer = annotationService.findLayer(state.getProject(), originFS);
        AnnotationLayer previousLayer = state.getSelectedAnnotationLayer();
        // Chain layers consist of arcs and spans
        if (originLayer.getType().equals(CHAIN_TYPE)) {
            // one layer both for the span and arc annotation
            state.setSelectedAnnotationLayer(originLayer);
        } else // Otherwise, look up the possible relation layer(s) in the database.
        {
            state.setSelectedAnnotationLayer(getRelationLayerFor(originLayer).orElseThrow(() -> new IllegalPlacementException("No relation annotation allowed on layer [" + state.getDefaultAnnotationLayer().getUiName() + "]")));
        }
        state.setDefaultAnnotationLayer(originLayer);
        // If we switched layers, we need to initialize the feature editors for the new layer
        if (!Objects.equals(previousLayer, state.getSelectedAnnotationLayer())) {
            LOG.trace("Layer changed from {} to {} - need to reload feature editors", previousLayer, state.getSelectedAnnotationLayer());
            loadFeatureEditorModels(aTarget);
        }
    } else {
        // Re-set the selected layer from the drop-down since it might have changed if we
        // have previously created a relation annotation
        state.setSelectedAnnotationLayer(state.getDefaultAnnotationLayer());
    }
    internalCommitAnnotation(aTarget, aCas);
    internalCompleteAnnotation(aTarget, aCas);
    if (aTarget != null) {
        refresh(aTarget);
    // // After the annotation has been created, we need to re-render the button container
    // e.g.
    // // to make the buttons show up if previously no annotation was selected
    // aTarget.add(buttonContainer);
    // // Also update the features and selected annotation info
    // aTarget.add(selectedAnnotationInfoPanel, featureEditorListPanel, relationListPanel);
    }
    state.clearArmedSlot();
}
Also used : AnnotationFS(org.apache.uima.cas.text.AnnotationFS) AnnotatorState(de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.AnnotatorState) IllegalPlacementException(de.tudarmstadt.ukp.clarin.webanno.api.annotation.exception.IllegalPlacementException) AnnotationLayer(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationLayer)

Example 4 with IllegalPlacementException

use of de.tudarmstadt.ukp.clarin.webanno.api.annotation.exception.IllegalPlacementException in project webanno by webanno.

the class AnnotationDetailEditorPanel method actionFillSlot.

@Override
public void actionFillSlot(AjaxRequestTarget aTarget, CAS aCas, int aSlotFillerBegin, int aSlotFillerEnd, VID aExistingSlotFillerId) throws AnnotationException, IOException {
    assert aCas != null;
    AnnotatorState state = getModelObject();
    editorPage.ensureIsEditable();
    // If this method is called when no slot is armed, it must be a bug!
    if (!state.isSlotArmed()) {
        throw new IllegalStateException("No slot is armed.");
    }
    // If the user did not select an existing annotation but simply marked a span of text to
    // fill a slot, then we try to create a new span annotation corresponding to the slot
    // filler type defined in the feature. However, this only works if the slot filler is a
    // concrete span type defined in the project, not if the user simply defined
    // CAS.TYPE_NAME_ANNOTATION to allow for arbitrary slot fillers. In the latter case, we
    // abort the operation with an IllegalPlacementException.
    int slotFillerAddr;
    if (aExistingSlotFillerId.isNotSet()) {
        if (!CAS.TYPE_NAME_ANNOTATION.equals(state.getArmedFeature().feature.getType())) {
            SpanAdapter adapter = (SpanAdapter) annotationService.getAdapter(annotationService.findLayer(state.getProject(), state.getArmedFeature().feature.getType()));
            slotFillerAddr = getAddr(adapter.add(state.getDocument(), state.getUser().getUsername(), aCas, aSlotFillerBegin, aSlotFillerEnd));
        } else {
            throw new IllegalPlacementException("Unable to create annotation of type [" + CAS.TYPE_NAME_ANNOTATION + "]. Please click an annotation in stead of selecting new text.");
        }
    } else {
        slotFillerAddr = aExistingSlotFillerId.getId();
    }
    // Inject the slot filler into the respective slot
    FeatureStructure slotHostFS = selectFsByAddr(aCas, state.getArmedFeature().vid.getId());
    AnnotationLayer slotHostLayer = annotationService.findLayer(state.getProject(), slotHostFS);
    TypeAdapter slotHostAdapter = annotationService.getAdapter(slotHostLayer);
    List<LinkWithRoleModel> links = (List<LinkWithRoleModel>) state.getArmedFeature().value;
    LinkWithRoleModel link = links.get(state.getArmedSlot());
    link.targetAddr = slotFillerAddr;
    link.label = selectAnnotationByAddr(aCas, slotFillerAddr).getCoveredText();
    commitFeatureStatesToFeatureStructure(aTarget, state.getDocument(), state.getUser().getUsername(), aCas, state.getArmedFeature().vid.getId(), slotHostAdapter, asList(state.getArmedFeature()));
    // NOTE: we do NOT delegate to actionCreateOrUpdate here because most of the things
    // that actionCreateOrUpdate does are not required for slot filling and we also because
    // slot filling requires special treatment. This also means, we don't delegate to
    // internalCommitAnnotation and repeat the necessary code here. However, we DO delegate
    // to internalCompleteAnnotation to save the CAS after the annotation.
    internalCompleteAnnotation(aTarget, aCas);
    // the annotator state with the changes that we made to the CAS
    if (state.getSelection().getAnnotation().equals(state.getArmedFeature().vid)) {
        // Loading feature editor values from CAS
        loadFeatureEditorModels(aTarget);
    } else // ... if the SLOT HOST annotation is NOT open in the detail panel on the right, then
    // select SLOT FILLER an open it there
    {
        state.getSelection().selectSpan(selectAnnotationByAddr(aCas, slotFillerAddr));
        actionSelect(aTarget);
    }
    state.clearArmedSlot();
}
Also used : FeatureStructure(org.apache.uima.cas.FeatureStructure) LinkWithRoleModel(de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.LinkWithRoleModel) AnnotatorState(de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.AnnotatorState) TypeAdapter(de.tudarmstadt.ukp.clarin.webanno.api.annotation.adapter.TypeAdapter) SpanAdapter(de.tudarmstadt.ukp.clarin.webanno.api.annotation.adapter.SpanAdapter) IllegalPlacementException(de.tudarmstadt.ukp.clarin.webanno.api.annotation.exception.IllegalPlacementException) Arrays.asList(java.util.Arrays.asList) List(java.util.List) ArrayList(java.util.ArrayList) AnnotationLayer(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationLayer)

Example 5 with IllegalPlacementException

use of de.tudarmstadt.ukp.clarin.webanno.api.annotation.exception.IllegalPlacementException in project webanno by webanno.

the class SpanOverlapBehavior method onCreate.

@Override
public CreateSpanAnnotationRequest onCreate(TypeAdapter aAdapter, CreateSpanAnnotationRequest aRequest) throws AnnotationException {
    final CAS aCas = aRequest.getCas();
    final int aBegin = aRequest.getBegin();
    final int aEnd = aRequest.getEnd();
    Type type = getType(aCas, aAdapter.getAnnotationTypeName());
    switch(aAdapter.getLayer().getOverlapMode()) {
        case ANY_OVERLAP:
            // Nothing to check
            return aRequest;
        case NO_OVERLAP:
            boolean hasAnyOverlapping = !selectOverlapping(aCas, type, aBegin, aEnd).isEmpty();
            if (hasAnyOverlapping) {
                throw new IllegalPlacementException("Cannot create another annotation of layer [" + aAdapter.getLayer().getUiName() + "] at this location - no overlap or stacking is allowed for this layer.");
            }
            break;
        case OVERLAP_ONLY:
            boolean hasStacking = !selectAt(aCas, type, aBegin, aEnd).isEmpty();
            if (hasStacking) {
                throw new IllegalPlacementException("Cannot create another annotation of layer [" + aAdapter.getLayer().getUiName() + "] at this location - stacking is not allowed for this layer.");
            }
            break;
        case STACKING_ONLY:
            boolean hasOverlapping = selectOverlapping(aCas, type, aBegin, aEnd).stream().filter(fs -> !stacking(aRequest, fs)).findAny().isPresent();
            if (hasOverlapping) {
                throw new IllegalPlacementException("Cannot create another annotation of layer [" + aAdapter.getLayer().getUiName() + "] at this location - only stacking is allowed for this layer.");
            }
            break;
    }
    return aRequest;
}
Also used : Type(org.apache.uima.cas.Type) CasUtil.getType(org.apache.uima.fit.util.CasUtil.getType) CAS(org.apache.uima.cas.CAS) IllegalPlacementException(de.tudarmstadt.ukp.clarin.webanno.api.annotation.exception.IllegalPlacementException)

Aggregations

IllegalPlacementException (de.tudarmstadt.ukp.clarin.webanno.api.annotation.exception.IllegalPlacementException)6 Type (org.apache.uima.cas.Type)4 AnnotationFS (org.apache.uima.cas.text.AnnotationFS)4 AnnotationLayer (de.tudarmstadt.ukp.clarin.webanno.model.AnnotationLayer)3 Feature (org.apache.uima.cas.Feature)3 CasUtil.getType (org.apache.uima.fit.util.CasUtil.getType)3 AnnotatorState (de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.AnnotatorState)2 AnnotationFeature (de.tudarmstadt.ukp.clarin.webanno.model.AnnotationFeature)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 CAS (org.apache.uima.cas.CAS)2 FeatureStructure (org.apache.uima.cas.FeatureStructure)2 WebAnnoConst (de.tudarmstadt.ukp.clarin.webanno.api.WebAnnoConst)1 SpanAdapter (de.tudarmstadt.ukp.clarin.webanno.api.annotation.adapter.SpanAdapter)1 TypeAdapter (de.tudarmstadt.ukp.clarin.webanno.api.annotation.adapter.TypeAdapter)1 AnnotationException (de.tudarmstadt.ukp.clarin.webanno.api.annotation.exception.AnnotationException)1 LinkWithRoleModel (de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.LinkWithRoleModel)1 VID (de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.VID)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