Skip to content

Commit

Permalink
#4060 - Clean up redundant code in annotation handlers
Browse files Browse the repository at this point in the history
- Remove redundant code
- Clean up code
  • Loading branch information
reckart committed Jun 9, 2023
1 parent be78046 commit eb5b63a
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,12 @@
import org.apache.wicket.request.Request;
import org.springframework.core.annotation.Order;

import de.tudarmstadt.ukp.clarin.webanno.api.annotation.page.AnnotationPageBase;
import de.tudarmstadt.ukp.clarin.webanno.support.JSONUtil;
import de.tudarmstadt.ukp.inception.diam.editor.config.DiamAutoConfig;
import de.tudarmstadt.ukp.inception.diam.model.ajax.DefaultAjaxResponse;
import de.tudarmstadt.ukp.inception.diam.model.compact.CompactRangeList;
import de.tudarmstadt.ukp.inception.rendering.editorstate.AnnotatorState;
import de.tudarmstadt.ukp.inception.rendering.model.Range;
import de.tudarmstadt.ukp.inception.rendering.selection.Selection;
import de.tudarmstadt.ukp.inception.rendering.vmodel.VID;
import de.tudarmstadt.ukp.inception.schema.adapter.AnnotationException;

/**
* <p>
Expand All @@ -60,53 +56,32 @@ public String getCommand()
public DefaultAjaxResponse handle(AjaxRequestTarget aTarget, Request aRequest)
{
try {
AnnotationPageBase page = getPage();
CAS cas = page.getEditorCas();
actionSpan(aTarget, aRequest.getRequestParameters(), cas);
var page = getPage();
var cas = page.getEditorCas();
var state = getAnnotatorState();
var range = getRangeFromRequest(state, aRequest.getRequestParameters(), cas);

state.getSelection().selectSpan(cas, range.getBegin(), range.getEnd());
page.getAnnotationActionHandler().actionCreateOrUpdate(aTarget, cas);

return new DefaultAjaxResponse(getAction(aRequest));
}
catch (Exception e) {
return handleError("Unable to create span annotation", e);
}
}

private void actionSpan(AjaxRequestTarget aTarget, IRequestParameters aRequestParameters,
CAS aCas)
throws IOException, AnnotationException
{
AnnotationPageBase page = (AnnotationPageBase) aTarget.getPage();
AnnotatorState state = getAnnotatorState();

// This is the span the user has marked in the browser in order to create a new slot-filler
// annotation OR the span of an existing annotation which the user has selected.
var userSelectedSpan = getOffsetsFromRequest(state, aRequestParameters, aCas);

if (state.isSlotArmed()) {
// When filling a slot, the current selection is *NOT* changed. The
// Span annotation which owns the slot that is being filled remains
// selected!
page.getAnnotationActionHandler().actionFillSlot(aTarget, aCas,
userSelectedSpan.getBegin(), userSelectedSpan.getEnd(), VID.NONE_ID);
return;
}

Selection selection = state.getSelection();
selection.selectSpan(aCas, userSelectedSpan.getBegin(), userSelectedSpan.getEnd());
page.getAnnotationActionHandler().actionCreateOrUpdate(aTarget, aCas);
}

/**
* Extract offset information from the current request. These are either offsets of an existing
* selected annotations or offsets contained in the request for the creation of a new
* annotation.
*/
static Range getOffsetsFromRequest(AnnotatorState aState, IRequestParameters request, CAS aCas)
static Range getRangeFromRequest(AnnotatorState aState, IRequestParameters request, CAS aCas)
throws IOException
{
String offsets = request.getParameterValue(PARAM_OFFSETS).toString();
var offsets = request.getParameterValue(PARAM_OFFSETS).toString();

CompactRangeList offsetLists = JSONUtil.getObjectMapper().readValue(offsets,
CompactRangeList.class);
var offsetLists = JSONUtil.getObjectMapper().readValue(offsets, CompactRangeList.class);

int annotationBegin = aState.getWindowBeginOffset() + offsetLists.get(0).getBegin();
int annotationEnd = aState.getWindowBeginOffset()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,10 @@
*/
package de.tudarmstadt.ukp.inception.diam.editor.actions;

import org.apache.uima.cas.CAS;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.request.Request;
import org.springframework.core.annotation.Order;

import de.tudarmstadt.ukp.clarin.webanno.api.annotation.page.AnnotationPageBase;
import de.tudarmstadt.ukp.inception.diam.editor.config.DiamAutoConfig;
import de.tudarmstadt.ukp.inception.diam.model.ajax.DefaultAjaxResponse;

Expand All @@ -48,13 +46,12 @@ public String getCommand()
public DefaultAjaxResponse handle(AjaxRequestTarget aTarget, Request aRequest)
{
try {
AnnotationPageBase page = getPage();
CAS cas = page.getEditorCas();

// When filling a slot, the current selection is *NOT* changed. The
// Span annotation which owns the slot that is being filled remains
// selected!
page.getAnnotationActionHandler().actionFillSlot(aTarget, cas, 0, 0, getVid(aRequest));
var page = getPage();
var cas = page.getEditorCas();
var slotFillerId = getVid(aRequest);
// When filling a slot, the current selection is *NOT* changed. The Span annotation
// which owns the slot that is being filled remains selected!
page.getAnnotationActionHandler().actionFillSlot(aTarget, cas, slotFillerId);

return new DefaultAjaxResponse(getAction(aRequest));
}
Expand All @@ -66,6 +63,7 @@ public DefaultAjaxResponse handle(AjaxRequestTarget aTarget, Request aRequest)
@Override
public boolean accepts(Request aRequest)
{
return super.accepts(aRequest) && getAnnotatorState().isSlotArmed();
return super.accepts(aRequest) && getAnnotatorState().isSlotArmed()
&& getVid(aRequest).isSet();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@
import de.tudarmstadt.ukp.inception.diam.model.ajax.DefaultAjaxResponse;
import de.tudarmstadt.ukp.inception.diam.model.compact.CompactRange;
import de.tudarmstadt.ukp.inception.diam.model.compact.CompactRangeList;
import de.tudarmstadt.ukp.inception.rendering.editorstate.AnnotatorState;
import de.tudarmstadt.ukp.inception.rendering.vmodel.VID;
import de.tudarmstadt.ukp.inception.schema.adapter.AnnotationException;

/**
Expand Down Expand Up @@ -77,36 +75,32 @@ private void actionSpan(AjaxRequestTarget aTarget, IRequestParameters aRequestPa
CAS aCas)
throws IOException, AnnotationException
{
AnnotationPageBase page = (AnnotationPageBase) aTarget.getPage();

// This is the span the user has marked in the browser in order to create a new slot-filler
// annotation OR the span of an existing annotation which the user has selected.
CompactRange userSelectedSpan = getOffsetsFromRequest(aTarget, aRequestParameters, aCas);
var range = getRangeFromRequest(aTarget, aRequestParameters, aCas);

// When filling a slot, the current selection is *NOT* changed. The
// Span annotation which owns the slot that is being filled remains
// selected!
page.getAnnotationActionHandler().actionFillSlot(aTarget, aCas, userSelectedSpan.getBegin(),
userSelectedSpan.getEnd(), VID.NONE_ID);
// When filling a slot, the current selection is *NOT* changed. The Span annotation which
// owns the slot that is being filled remains selected!
getPage().getAnnotationActionHandler().actionFillSlot(aTarget, aCas, range.getBegin(),
range.getEnd());
}

/**
* Extract offset information from the current request. These are either offsets of an existing
* selected annotations or offsets contained in the request for the creation of a new
* annotation.
*/
private CompactRange getOffsetsFromRequest(AjaxRequestTarget aTarget,
IRequestParameters request, CAS aCas)
private CompactRange getRangeFromRequest(AjaxRequestTarget aTarget, IRequestParameters request,
CAS aCas)
throws IOException
{
// Create new span annotation - in this case we get the offset information from the
// request
String offsets = request.getParameterValue(PARAM_OFFSETS).toString();
var offsets = request.getParameterValue(PARAM_OFFSETS).toString();

CompactRangeList offsetLists = JSONUtil.getObjectMapper().readValue(offsets,
CompactRangeList.class);
var offsetLists = JSONUtil.getObjectMapper().readValue(offsets, CompactRangeList.class);

AnnotatorState state = getAnnotatorState();
var state = getAnnotatorState();
int annotationBegin = state.getWindowBeginOffset() + offsetLists.get(0).getBegin();
int annotationEnd = state.getWindowBeginOffset()
+ offsetLists.get(offsetLists.size() - 1).getEnd();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
*/
package de.tudarmstadt.ukp.inception.diam.editor.actions;

import static de.tudarmstadt.ukp.inception.diam.editor.actions.CreateSpanAnnotationHandler.getOffsetsFromRequest;
import static de.tudarmstadt.ukp.inception.diam.editor.actions.CreateSpanAnnotationHandler.getRangeFromRequest;

import java.io.IOException;

Expand Down Expand Up @@ -70,7 +70,7 @@ public DefaultAjaxResponse handle(AjaxRequestTarget aTarget, Request aRequest)
CAS cas = page.getEditorCas();
var vid = getVid(aRequest);
AnnotatorState state = getAnnotatorState();
var range = getOffsetsFromRequest(state, aRequest.getRequestParameters(), cas);
var range = getRangeFromRequest(state, aRequest.getRequestParameters(), cas);
moveSpan(aTarget, cas, vid, range);
return new DefaultAjaxResponse(getAction(aRequest));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
*/
package de.tudarmstadt.ukp.inception.diam.editor.actions;

import static de.tudarmstadt.ukp.inception.diam.editor.actions.CreateSpanAnnotationHandler.getOffsetsFromRequest;
import static de.tudarmstadt.ukp.inception.diam.editor.actions.CreateSpanAnnotationHandler.getRangeFromRequest;

import org.apache.uima.cas.CAS;
import org.apache.wicket.ajax.AjaxRequestTarget;
Expand Down Expand Up @@ -58,7 +58,7 @@ public DefaultAjaxResponse handle(AjaxRequestTarget aTarget, Request aRequest)
IRequestParameters requestParameters = aRequest.getRequestParameters();

if (!requestParameters.getParameterValue(PARAM_OFFSETS).isEmpty()) {
Range offsets = getOffsetsFromRequest(getAnnotatorState(), requestParameters, cas);
Range offsets = getRangeFromRequest(getAnnotatorState(), requestParameters, cas);
page.getAnnotationActionHandler().actionJump(aTarget, offsets.getBegin(),
offsets.getEnd());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ void thatSelectionIsClippedToDocumentBoundaries_begin() throws Exception
params.setParameterValue(CreateSpanAnnotationHandler.PARAM_OFFSETS,
toJsonString(new CompactRangeList(new CompactRange(begin, end))));

var range = sut.getOffsetsFromRequest(state, params, cas);
var range = sut.getRangeFromRequest(state, params, cas);

assertThat(range.getBegin()).isEqualTo(0);
assertThat(range.getEnd()).isEqualTo(cas.getDocumentText().length());
Expand All @@ -81,7 +81,7 @@ void thatSelectionIsClippedToDocumentBoundaries_end() throws Exception
params.setParameterValue(CreateSpanAnnotationHandler.PARAM_OFFSETS,
toJsonString(new CompactRangeList(new CompactRange(begin, end))));

var range = sut.getOffsetsFromRequest(state, params, cas);
var range = sut.getRangeFromRequest(state, params, cas);

assertThat(range.getBegin()).isEqualTo(0);
assertThat(range.getEnd()).isEqualTo(end);
Expand Down

0 comments on commit eb5b63a

Please sign in to comment.