use of de.tudarmstadt.ukp.clarin.webanno.api.annotation.exception.AnnotationException in project webanno by webanno.
the class AnnotationDetailEditorPanel method actionCreateOrUpdate.
@Override
public void actionCreateOrUpdate(AjaxRequestTarget aTarget, JCas aJCas) throws IOException, AnnotationException {
LOG.trace("actionAnnotate");
if (isAnnotationFinished()) {
throw new AnnotationException("This document is already closed. Please ask your " + "project manager to re-open it via the Monitoring page");
}
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...!
AnnotationFS originFS = selectByAddr(aJCas, state.getSelection().getOrigin());
AnnotationLayer spanLayer = annotationService.getLayer(state.getProject(), originFS);
if (state.getPreferences().isRememberLayer() && // i.e. new annotation
state.getSelection().getAnnotation().isNotSet() && !spanLayer.equals(state.getDefaultAnnotationLayer())) {
throw new AnnotationException("No relation annotation allowed on layer [" + state.getDefaultAnnotationLayer().getUiName() + "]");
}
AnnotationLayer previousLayer = state.getSelectedAnnotationLayer();
// Chain layers consist of arcs and spans
if (spanLayer.getType().equals(WebAnnoConst.CHAIN_TYPE)) {
// one layer both for the span and arc annotation
state.setSelectedAnnotationLayer(spanLayer);
} else // Otherwise, look up the possible relation layer(s) in the database.
{
for (AnnotationLayer l : annotationService.listAnnotationLayer(state.getProject())) {
if ((l.getAttachType() != null && l.getAttachType().equals(spanLayer)) || (l.getAttachFeature() != null && l.getAttachFeature().getType().equals(spanLayer.getName()))) {
if (state.getAnnotationLayers().contains(l)) {
state.setSelectedAnnotationLayer(l);
} else {
state.setSelectedAnnotationLayer(null);
}
break;
}
}
}
state.setDefaultAnnotationLayer(spanLayer);
// 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(aJCas, 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(annotationFeatureForm.getLayerSelector().getModelObject());
}
internalCommitAnnotation(aTarget, aJCas);
internalCompleteAnnotation(aTarget, aJCas);
}
use of de.tudarmstadt.ukp.clarin.webanno.api.annotation.exception.AnnotationException in project webanno by webanno.
the class MergeCas method addRelationArcAnnotation.
static void addRelationArcAnnotation(JCas aJcas, AnnotationFS aClickedFS, boolean aIsAttachType, boolean aIsAllowStacking, AnnotationFS originFsClicked, AnnotationFS targetFsClicked) throws AnnotationException {
List<AnnotationFS> merges = MergeCas.getMergeFS(aClickedFS, aJcas).collect(Collectors.toList());
List<AnnotationFS> origins = MergeCas.getMergeFS(originFsClicked, aJcas).collect(Collectors.toList());
List<AnnotationFS> targets = MergeCas.getMergeFS(targetFsClicked, aJcas).collect(Collectors.toList());
// check if target/source exists in the mergeview
if (origins.size() == 0 || targets.size() == 0) {
throw new AnnotationException("Both the source and target annotation" + " should exist on the mergeview. Please first copy/create them");
}
AnnotationFS originFs = origins.get(0);
AnnotationFS targetFs = targets.get(0);
if (origins.size() > 1) {
throw new AnnotationException("Stacked sources exist in mergeview. Cannot copy this relation.");
}
if (targets.size() > 1) {
throw new AnnotationException("Stacked targets exist in mergeview. Cannot copy this relation.");
}
if (merges.size() > 0) {
throw new AnnotationException("The annotation already exists on the mergeview. " + "Add this manually to have stacked annotations");
}
// TODO: DKPro Core Dependency layer -> It should be done differently
if (aIsAttachType) {
Type type = aClickedFS.getType();
Feature sourceFeature = type.getFeatureByBaseName(WebAnnoConst.FEAT_REL_SOURCE);
originFsClicked = (AnnotationFS) aClickedFS.getFeatureValue(sourceFeature);
Feature targetFeature = type.getFeatureByBaseName(WebAnnoConst.FEAT_REL_TARGET);
targetFsClicked = (AnnotationFS) aClickedFS.getFeatureValue(targetFeature);
origins = MergeCas.getMergeFS(originFsClicked, aJcas).collect(Collectors.toList());
targets = MergeCas.getMergeFS(targetFsClicked, aJcas).collect(Collectors.toList());
originFs = origins.get(0);
targetFs = targets.get(0);
}
List<AnnotationFS> existingAnnos = MergeCas.getRelAnnosOnPosition(aClickedFS, originFs, targetFs, aJcas);
if (existingAnnos.size() == 0 || aIsAllowStacking) {
MergeCas.copyRelationAnnotation(aClickedFS, originFs, targetFs, aJcas);
} else {
MergeCas.modifyRelationAnnotation(aClickedFS, existingAnnos.get(0), aJcas);
}
}
use of de.tudarmstadt.ukp.clarin.webanno.api.annotation.exception.AnnotationException in project webanno by webanno.
the class SuggestionViewPanel method mergeArc.
private void mergeArc(IRequestParameters aRequest, UserAnnotationSegment aCurationUserSegment, JCas aJcas) throws AnnotationException, IOException, UIMAException, ClassNotFoundException {
int addressOriginClicked = aRequest.getParameterValue(PARAM_ORIGIN_SPAN_ID).toInt();
int addressTargetClicked = aRequest.getParameterValue(PARAM_TARGET_SPAN_ID).toInt();
String arcType = removePrefix(aRequest.getParameterValue(PARAM_TYPE).toString());
String fsArcaddress = aRequest.getParameterValue(PARAM_ARC_ID).toString();
AnnotatorState bModel = aCurationUserSegment.getAnnotatorState();
SourceDocument sourceDocument = bModel.getDocument();
// for correction and automation, the lower panel is the clickedJcase, from the suggestions
JCas clickedJCas;
if (!aCurationUserSegment.getAnnotatorState().getMode().equals(Mode.CURATION)) {
clickedJCas = correctionDocumentService.readCorrectionCas(sourceDocument);
} else {
User user = userRepository.get(aCurationUserSegment.getUsername());
AnnotationDocument clickedAnnotationDocument = documentService.getAnnotationDocument(sourceDocument, user);
clickedJCas = getJCas(bModel, clickedAnnotationDocument);
}
long layerId = TypeUtil.getLayerId(arcType);
AnnotationLayer layer = annotationService.getLayer(layerId);
TypeAdapter adapter = annotationService.getAdapter(layer);
int address = Integer.parseInt(fsArcaddress.split("\\.")[0]);
AnnotationFS clickedFS = selectByAddr(clickedJCas, address);
if (isCorefType(clickedFS)) {
throw new AnnotationException(" Coreference Annotation not supported in curation");
}
MergeCas.addArcAnnotation(adapter, aJcas, addressOriginClicked, addressTargetClicked, fsArcaddress, clickedJCas, clickedFS);
writeEditorCas(bModel, aJcas);
int sentenceNumber = getSentenceNumber(clickedJCas, clickedFS.getBegin());
bModel.setFocusUnitIndex(sentenceNumber);
// Update timestamp
bModel.getDocument().setSentenceAccessed(sentenceNumber);
if (bModel.getPreferences().isScrollPage()) {
Sentence sentence = selectSentenceAt(aJcas, bModel.getFirstVisibleUnitBegin(), bModel.getFirstVisibleUnitEnd());
sentence = findWindowStartCenteringOnSelection(aJcas, sentence, clickedFS.getBegin(), bModel.getProject(), bModel.getDocument(), bModel.getPreferences().getWindowSize());
bModel.setFirstVisibleUnit(sentence);
}
}
use of de.tudarmstadt.ukp.clarin.webanno.api.annotation.exception.AnnotationException in project webanno by webanno.
the class SuggestionViewPanel method createSpan.
private void createSpan(String spanType, AnnotatorState aBModel, JCas aMergeJCas, AnnotationDocument aAnnotationDocument, int aAddress) throws IOException, UIMAException, ClassNotFoundException, AnnotationException {
JCas clickedJCas = getJCas(aBModel, aAnnotationDocument);
AnnotationFS fsClicked = selectByAddr(clickedJCas, aAddress);
if (isCorefType(fsClicked)) {
throw new AnnotationException("Coreference Annotation not supported in curation");
}
long layerId = TypeUtil.getLayerId(spanType);
AnnotationLayer layer = annotationService.getLayer(layerId);
MergeCas.addSpanAnnotation(aBModel, annotationService, layer, aMergeJCas, fsClicked, layer.isAllowStacking());
writeEditorCas(aBModel, aMergeJCas);
// update timestamp
int sentenceNumber = getSentenceNumber(clickedJCas, fsClicked.getBegin());
aBModel.setFocusUnitIndex(sentenceNumber);
aBModel.getDocument().setSentenceAccessed(sentenceNumber);
if (aBModel.getPreferences().isScrollPage()) {
Sentence sentence = selectSentenceAt(aMergeJCas, aBModel.getFirstVisibleUnitBegin(), aBModel.getFirstVisibleUnitEnd());
sentence = findWindowStartCenteringOnSelection(aMergeJCas, sentence, fsClicked.getBegin(), aBModel.getProject(), aBModel.getDocument(), aBModel.getPreferences().getWindowSize());
aBModel.setFirstVisibleUnit(sentence);
}
}
use of de.tudarmstadt.ukp.clarin.webanno.api.annotation.exception.AnnotationException in project webanno by webanno.
the class SuggestionBuilder method getMergeCas.
/**
* Fetches the CAS that the user will be able to edit. In AUTOMATION/CORRECTION mode, this is
* the CAS for the CORRECTION_USER and in CURATION mode it is the CAS for the CURATION user.
*
* @param aBratAnnotatorModel
* the model.
* @param aDocument
* the source document.
* @param jCases
* the JCases.
* @param randomAnnotationDocument
* an annotation document.
* @return the JCas.
* @throws UIMAException
* hum?
* @throws ClassNotFoundException
* hum?
* @throws IOException
* if an I/O error occurs.
* @throws AnnotationException
* hum?
*/
public JCas getMergeCas(AnnotatorState aBratAnnotatorModel, SourceDocument aDocument, Map<String, JCas> jCases, AnnotationDocument randomAnnotationDocument, boolean aUpgrade) throws UIMAException, ClassNotFoundException, IOException, AnnotationException {
JCas mergeJCas = null;
try {
if (aBratAnnotatorModel.getMode().equals(Mode.AUTOMATION) || aBratAnnotatorModel.getMode().equals(Mode.CORRECTION)) {
// Upgrading should be an explicit action during the opening of a document at the
// end
// of the open dialog - it must not happen during editing because the CAS addresses
// are used as IDs in the UI
// repository.upgradeCasAndSave(aDocument, aBratAnnotatorModel.getMode(),
// aBratAnnotatorModel.getUser().getUsername());
mergeJCas = correctionDocumentService.readCorrectionCas(aDocument);
if (aUpgrade) {
correctionDocumentService.upgradeCorrectionCas(mergeJCas.getCas(), aDocument);
correctionDocumentService.writeCorrectionCas(mergeJCas, aDocument);
}
} else {
// Upgrading should be an explicit action during the opening of a document at the
// end
// of the open dialog - it must not happen during editing because the CAS addresses
// are used as IDs in the UI
// repository.upgradeCasAndSave(aDocument, aBratAnnotatorModel.getMode(),
// aBratAnnotatorModel.getUser().getUsername());
mergeJCas = curationDocumentService.readCurationCas(aDocument);
if (aUpgrade) {
curationDocumentService.upgradeCurationCas(mergeJCas.getCas(), aDocument);
curationDocumentService.writeCurationCas(mergeJCas, aDocument, true);
}
}
}// Create jcas, if it could not be loaded from the file system
catch (Exception e) {
if (aBratAnnotatorModel.getMode().equals(Mode.AUTOMATION) || aBratAnnotatorModel.getMode().equals(Mode.CORRECTION)) {
mergeJCas = createCorrectionCas(mergeJCas, aBratAnnotatorModel, randomAnnotationDocument);
} else {
mergeJCas = createCurationCas(aBratAnnotatorModel.getProject(), randomAnnotationDocument, jCases, aBratAnnotatorModel.getAnnotationLayers());
}
}
return mergeJCas;
}
Aggregations