use of de.tudarmstadt.ukp.clarin.webanno.api.annotation.exception.AnnotationException in project webanno by webanno.
the class ArcAdapter method interalAddToCas.
/**
* @param aWindowBegin
* begin offset of the first visible sentence
* @param aWindowEnd
* end offset of the last visible sentence
*/
private AnnotationFS interalAddToCas(JCas aJCas, int aWindowBegin, int aWindowEnd, AnnotationFS aOriginFs, AnnotationFS aTargetFs) throws AnnotationException {
Type type = getType(aJCas.getCas(), annotationTypeName);
Feature dependentFeature = type.getFeatureByBaseName(targetFeatureName);
Feature governorFeature = type.getFeatureByBaseName(sourceFeatureName);
Type spanType = getType(aJCas.getCas(), attachType);
AnnotationFS dependentFs = null;
AnnotationFS governorFs = null;
// presently visible on screen is sufficient - we don't have to scan the whole CAS.
for (AnnotationFS fs : selectCovered(aJCas.getCas(), type, aWindowBegin, aWindowEnd)) {
if (attachFeatureName != null) {
Feature arcSpanFeature = spanType.getFeatureByBaseName(attachFeatureName);
dependentFs = (AnnotationFS) fs.getFeatureValue(dependentFeature).getFeatureValue(arcSpanFeature);
governorFs = (AnnotationFS) fs.getFeatureValue(governorFeature).getFeatureValue(arcSpanFeature);
} else {
dependentFs = (AnnotationFS) fs.getFeatureValue(dependentFeature);
governorFs = (AnnotationFS) fs.getFeatureValue(governorFeature);
}
if (dependentFs == null || governorFs == null) {
log.warn("Relation [" + getLayer().getName() + "] with id [" + getAddr(fs) + "] has loose ends - ignoring during while checking for duplicates.");
continue;
}
// update the label of the existing arc
if (!allowStacking && isDuplicate(governorFs, aOriginFs, dependentFs, aTargetFs)) {
throw new AnnotationException("Cannot create another annotation of layer [" + getLayer().getUiName() + "] at this location - stacking is not " + "enabled for this layer.");
}
}
// It is new ARC annotation, create it
dependentFs = aTargetFs;
governorFs = aOriginFs;
// for POS annotation, since custom span layers do not have attach feature
if (attachFeatureName != null) {
dependentFs = selectCovered(aJCas.getCas(), spanType, dependentFs.getBegin(), dependentFs.getEnd()).get(0);
governorFs = selectCovered(aJCas.getCas(), spanType, governorFs.getBegin(), governorFs.getEnd()).get(0);
}
if (dependentFs == null || governorFs == null) {
throw new AnnotationException("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.
AnnotationFS newAnnotation = aJCas.getCas().createAnnotation(type, dependentFs.getBegin(), dependentFs.getEnd());
newAnnotation.setFeatureValue(dependentFeature, dependentFs);
newAnnotation.setFeatureValue(governorFeature, governorFs);
aJCas.addFsToIndexes(newAnnotation);
return newAnnotation;
}
use of de.tudarmstadt.ukp.clarin.webanno.api.annotation.exception.AnnotationException in project webanno by webanno.
the class SpanAdapter method createAnnotation.
/**
* A Helper method to add annotation to CAS
*/
private Integer createAnnotation(AnnotatorState aState, CAS aCas, int aBegin, int aEnd) throws AnnotationException {
// If stacking is not allowed and there already is an annotation, then return the address
// of the existing annotation.
Type type = CasUtil.getType(aCas, getAnnotationTypeName());
for (AnnotationFS fs : CasUtil.selectCovered(aCas, type, aBegin, aEnd)) {
if (fs.getBegin() == aBegin && fs.getEnd() == aEnd) {
if (!allowStacking) {
return getAddr(fs);
}
}
}
AnnotationFS newAnnotation = aCas.createAnnotation(type, aBegin, aEnd);
// 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 AnnotationException("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);
publishEvent(new SpanCreatedEvent(this, aState.getDocument(), aState.getUser().getUsername(), newAnnotation));
return getAddr(newAnnotation);
}
use of de.tudarmstadt.ukp.clarin.webanno.api.annotation.exception.AnnotationException in project webanno by webanno.
the class AnnotationDetailEditorPanel method loadFeatureEditorModels.
public void loadFeatureEditorModels(JCas aJCas, AjaxRequestTarget aTarget) throws AnnotationException {
LOG.trace("loadFeatureEditorModels()");
AnnotatorState state = getModelObject();
Selection selection = state.getSelection();
List<FeatureState> featureStates = state.getFeatureStates();
for (FeatureState featureState : featureStates) {
if (StringUtils.isNotBlank(featureState.feature.getLinkTypeName())) {
featureState.value = new ArrayList<>();
}
}
try {
if (selection.isSpan()) {
annotationFeatureForm.updateLayersDropdown();
}
if (selection.getAnnotation().isSet()) {
// If an existing annotation was selected, take the feature editor model values from
// there
AnnotationFS annoFs = selectByAddr(aJCas, state.getSelection().getAnnotation().getId());
// Try obtaining the layer from the feature structure
AnnotationLayer layer;
try {
layer = annotationService.getLayer(state.getProject(), annoFs);
state.setSelectedAnnotationLayer(layer);
LOG.trace(String.format("loadFeatureEditorModels() selectedLayer set from selection: %s", state.getSelectedAnnotationLayer().getUiName()));
} catch (NoResultException e) {
clearFeatureEditorModels(aTarget);
throw new IllegalStateException("Unknown layer [" + annoFs.getType().getName() + "]", e);
}
// selected span annotation
if (!selection.isArc() && !state.getPreferences().isRememberLayer()) {
state.setSelectedAnnotationLayer(layer);
}
loadFeatureEditorModelsCommon(aTarget, aJCas, layer, annoFs, null);
} else {
if (selection.isArc()) {
// Avoid creation of arcs on locked layers
if (state.getSelectedAnnotationLayer() != null && state.getSelectedAnnotationLayer().isReadonly()) {
state.setSelectedAnnotationLayer(new AnnotationLayer());
} else {
loadFeatureEditorModelsCommon(aTarget, aJCas, state.getSelectedAnnotationLayer(), null, state.getRememberedArcFeatures());
}
} else {
loadFeatureEditorModelsCommon(aTarget, aJCas, state.getSelectedAnnotationLayer(), null, state.getRememberedSpanFeatures());
}
}
annotationFeatureForm.updateRememberLayer();
if (aTarget != null) {
aTarget.add(annotationFeatureForm);
}
} catch (Exception e) {
throw new AnnotationException(e);
}
}
use of de.tudarmstadt.ukp.clarin.webanno.api.annotation.exception.AnnotationException in project webanno by webanno.
the class MergeCas method addSlotArcAnnotation.
private static void addSlotArcAnnotation(SpanAdapter aAdapter, JCas aJcas, String aFSArcaddress, JCas aClickedJCas, AnnotationFS aClickedFS) throws AnnotationException {
List<AnnotationFS> merges = MergeCas.getMergeFS(aClickedFS, aJcas).collect(Collectors.toList());
AnnotationFS targetFs;
if (merges.size() == 0) {
throw new AnnotationException("The base annotation do not exist. Please add it first. ");
}
AnnotationFS mergeFs = merges.get(0);
int fiIndex = Integer.parseInt(aFSArcaddress.split("\\.")[1]);
int liIndex = Integer.parseInt(aFSArcaddress.split("\\.")[2]);
AnnotationFeature slotFeature = null;
LinkWithRoleModel linkRole = null;
int fi = 0;
f: for (AnnotationFeature feat : aAdapter.listFeatures()) {
if (MultiValueMode.ARRAY.equals(feat.getMultiValueMode()) && LinkMode.WITH_ROLE.equals(feat.getLinkMode())) {
List<LinkWithRoleModel> links = aAdapter.getFeatureValue(feat, aClickedFS);
for (int li = 0; li < links.size(); li++) {
LinkWithRoleModel link = links.get(li);
if (fi == fiIndex && li == liIndex) {
slotFeature = feat;
List<AnnotationFS> targets = checkAndGetTargets(aJcas, aClickedJCas, selectByAddr(aClickedJCas, link.targetAddr));
targetFs = targets.get(0);
link.targetAddr = getAddr(targetFs);
linkRole = link;
break f;
}
}
}
fi++;
}
List<LinkWithRoleModel> links = aAdapter.getFeatureValue(slotFeature, mergeFs);
//
LinkWithRoleModel duplicateLink = null;
for (LinkWithRoleModel lr : links) {
if (lr.targetAddr == linkRole.targetAddr) {
duplicateLink = lr;
break;
}
}
links.add(linkRole);
links.remove(duplicateLink);
setFeature(mergeFs, slotFeature, links);
}
Aggregations