use of de.tudarmstadt.ukp.clarin.webanno.api.annotation.event.SpanCreatedEvent 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);
}
Aggregations