Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#4360 - Clean up code #4365

Merged
merged 7 commits into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -318,9 +318,9 @@ public void actionShowSelectedDocument(AjaxRequestTarget aTarget, SourceDocument

protected void handleException(AjaxRequestTarget aTarget, Exception aException)
{
if (aException instanceof ReplaceHandlerException) {
if (aException instanceof ReplaceHandlerException replaceHandlerException) {
// Let Wicket redirects still work
throw (ReplaceHandlerException) aException;
throw replaceHandlerException;
}

LoggerFactory.getLogger(getClass()).error("Error: " + aException.getMessage(), aException);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,8 +262,8 @@ public ArrayList<LinkWithRoleModel> wrapFeatureValue(AnnotationFeature aFeature,
}

FeatureStructure[] values = null;
if (aValue instanceof ArrayFS) {
values = ((ArrayFS<?>) aValue).toArray();
if (aValue instanceof ArrayFS array) {
values = array.toArray();
}

if (aValue instanceof Collection) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,14 @@ public <C> IConverter<C> getConverter(Class<C> aType)
public Object convertToObject(String aValue, Locale aLocale) throws ConversionException
{
Object value = originalConverter.convertToObject(aValue, aLocale);
if (value instanceof String) {
return value;
if (value instanceof String stringValue) {
return stringValue;
}
else if (value instanceof Tag) {
return ((Tag) value).getName();
else if (value instanceof Tag tag) {
return tag.getName();
}
else if (value instanceof ReorderableTag) {
return ((ReorderableTag) value).getName();
else if (value instanceof ReorderableTag reorderableTag) {
return reorderableTag.getName();
}
else {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ public class ChainLayerSupport
{
private final static Logger LOG = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());

@SuppressWarnings("deprecation")
public static final String TYPE = WebAnnoConst.CHAIN_TYPE;

private final ApplicationEventPublisher eventPublisher;
private final LayerBehaviorRegistry layerBehaviorsRegistry;

Expand Down Expand Up @@ -88,7 +91,7 @@ public void setBeanName(String aBeanName)
@Override
public void afterPropertiesSet() throws Exception
{
types = asList(new LayerType(WebAnnoConst.CHAIN_TYPE, "Chain", layerSupportId));
types = asList(new LayerType(TYPE, "Chain", layerSupportId));
}

@Override
Expand All @@ -100,7 +103,7 @@ public List<LayerType> getSupportedLayerTypes()
@Override
public boolean accepts(AnnotationLayer aLayer)
{
return WebAnnoConst.CHAIN_TYPE.equals(aLayer.getType());
return TYPE.equals(aLayer.getType());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,17 @@
import static de.tudarmstadt.ukp.inception.rendering.vmodel.VCommentType.ERROR;
import static java.util.Collections.emptyList;
import static org.apache.uima.fit.util.CasUtil.getType;
import static org.apache.uima.fit.util.CasUtil.select;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.NavigableMap;
import java.util.TreeMap;

import org.apache.commons.lang3.tuple.Pair;
import org.apache.uima.cas.CAS;
import org.apache.uima.cas.Feature;
import org.apache.uima.cas.Type;
import org.apache.uima.cas.text.AnnotationFS;
import org.apache.uima.jcas.tcas.Annotation;

import de.tudarmstadt.ukp.clarin.webanno.api.annotation.exception.MultipleSentenceCoveredException;
import de.tudarmstadt.ukp.clarin.webanno.api.annotation.util.WebAnnoCasUtil;
Expand Down Expand Up @@ -85,15 +81,15 @@ public void onRender(TypeAdapter aAdapter, VDocument aResponse,
return;
}

for (Entry<AnnotationFS, VArc> e : aAnnoToArcIdx.entrySet()) {
CAS cas = e.getKey().getCAS();
for (var e : aAnnoToArcIdx.entrySet()) {
var cas = e.getKey().getCAS();

if (!isBeginInSameSentence(cas,
ICasUtil.selectAnnotationByAddr(cas, e.getValue().getSource().getId())
.getBegin(),
ICasUtil.selectAnnotationByAddr(cas, e.getValue().getTarget().getId())
.getBegin())) {
aResponse.add(new VComment(new VID(e.getKey()), ERROR,
aResponse.add(new VComment(VID.of(e.getKey()), ERROR,
"Crossing sentence boundaries is not permitted."));
}
}
Expand All @@ -107,33 +103,33 @@ public List<Pair<LogMessage, AnnotationFS>> onValidate(TypeAdapter aAdapter, CAS
return emptyList();
}

RelationAdapter adapter = (RelationAdapter) aAdapter;
Type type = getType(aCas, aAdapter.getAnnotationTypeName());
Feature targetFeature = type.getFeatureByBaseName(adapter.getTargetFeatureName());
Feature sourceFeature = type.getFeatureByBaseName(adapter.getSourceFeatureName());
var adapter = (RelationAdapter) aAdapter;
var type = getType(aCas, aAdapter.getAnnotationTypeName());
var targetFeature = type.getFeatureByBaseName(adapter.getTargetFeatureName());
var sourceFeature = type.getFeatureByBaseName(adapter.getSourceFeatureName());

// If there are no annotations on this layer, nothing to do
Collection<AnnotationFS> annotations = select(aCas, type);
var annotations = aCas.<Annotation> select(type);
if (annotations.isEmpty()) {
return emptyList();
}

// Prepare feedback message list
List<Pair<LogMessage, AnnotationFS>> messages = new ArrayList<>();
var messages = new ArrayList<Pair<LogMessage, AnnotationFS>>();

// Build indexes to allow quickly looking up the sentence by its begin/end offsets. Since
// The indexes are navigable, we can also find the sentences starting/ending closes to a
// particular offset, even if it is not the start/end offset of a sentence.
NavigableMap<Integer, AnnotationFS> sentBeginIdx = new TreeMap<>();
NavigableMap<Integer, AnnotationFS> sentEndIdx = new TreeMap<>();
for (AnnotationFS sent : select(aCas, getType(aCas, Sentence.class))) {
var sentBeginIdx = new TreeMap<Integer, AnnotationFS>();
var sentEndIdx = new TreeMap<Integer, AnnotationFS>();
for (var sent : aCas.select(Sentence.class).asList()) {
sentBeginIdx.put(sent.getBegin(), sent);
sentEndIdx.put(sent.getEnd(), sent);
}

for (AnnotationFS fs : annotations) {
AnnotationFS sourceFs = (AnnotationFS) fs.getFeatureValue(sourceFeature);
AnnotationFS targetFs = (AnnotationFS) fs.getFeatureValue(targetFeature);
for (var fs : annotations) {
var sourceFs = (AnnotationFS) fs.getFeatureValue(sourceFeature);
var targetFs = (AnnotationFS) fs.getFeatureValue(targetFeature);

Entry<Integer, AnnotationFS> s1 = sentBeginIdx.floorEntry(sourceFs.getBegin());
Entry<Integer, AnnotationFS> s2 = sentEndIdx.ceilingEntry(targetFs.getEnd());
Expand All @@ -153,6 +149,5 @@ public List<Pair<LogMessage, AnnotationFS>> onValidate(TypeAdapter aAdapter, CAS
}

return messages;

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

import static de.tudarmstadt.ukp.inception.support.WebAnnoConst.FEAT_REL_SOURCE;
import static de.tudarmstadt.ukp.inception.support.WebAnnoConst.FEAT_REL_TARGET;
import static de.tudarmstadt.ukp.inception.support.WebAnnoConst.RELATION_TYPE;
import static java.util.Arrays.asList;
import static java.util.stream.Collectors.toList;
import static org.apache.uima.cas.CAS.TYPE_NAME_ANNOTATION;
Expand All @@ -43,6 +42,7 @@
import de.tudarmstadt.ukp.inception.schema.api.feature.FeatureSupportRegistry;
import de.tudarmstadt.ukp.inception.schema.api.layer.LayerSupport_ImplBase;
import de.tudarmstadt.ukp.inception.schema.api.layer.LayerType;
import de.tudarmstadt.ukp.inception.support.WebAnnoConst;

/**
* <p>
Expand All @@ -54,6 +54,9 @@ public class RelationLayerSupport
extends LayerSupport_ImplBase<RelationAdapter, RelationLayerTraits>
implements InitializingBean
{
@SuppressWarnings("deprecation")
public static final String TYPE = WebAnnoConst.RELATION_TYPE;

private final ApplicationEventPublisher eventPublisher;
private final LayerBehaviorRegistry layerBehaviorsRegistry;

Expand Down Expand Up @@ -85,7 +88,7 @@ public void setBeanName(String aBeanName)
@Override
public void afterPropertiesSet() throws Exception
{
types = asList(new LayerType(RELATION_TYPE, "Relation", layerSupportId));
types = asList(new LayerType(TYPE, "Relation", layerSupportId));
}

@Override
Expand All @@ -97,7 +100,7 @@ public List<LayerType> getSupportedLayerTypes()
@Override
public boolean accepts(AnnotationLayer aLayer)
{
return RELATION_TYPE.equals(aLayer.getType());
return TYPE.equals(aLayer.getType());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,10 @@
import de.tudarmstadt.ukp.inception.schema.api.adapter.AnnotationComparator;
import de.tudarmstadt.ukp.inception.schema.api.adapter.AnnotationException;
import de.tudarmstadt.ukp.inception.schema.api.adapter.TypeAdapter;
import de.tudarmstadt.ukp.inception.support.WebAnnoConst;
import de.tudarmstadt.ukp.inception.support.logging.LogMessage;

/**
* Handles the {@link OverlapMode} setting for {@link WebAnnoConst#RELATION_TYPE relation layers}.
* Handles the {@link OverlapMode} setting for {@link RelationLayerSupport relation layers}.
* <p>
* This class is exposed as a Spring Component via
* {@code AnnotationServiceAutoConfiguration#relationOverlapBehavior}.
Expand Down Expand Up @@ -151,17 +150,17 @@ public void onRender(TypeAdapter aAdapter, VDocument aResponse,
overlapping);

overlapping.forEach(fs -> aResponse
.add(new VComment(new VID(fs), ERROR, "Overlap is not permitted.")));
.add(new VComment(VID.of(fs), ERROR, "Overlap is not permitted.")));

stacking.forEach(fs -> aResponse
.add(new VComment(new VID(fs), ERROR, "Stacking is not permitted.")));
.add(new VComment(VID.of(fs), ERROR, "Stacking is not permitted.")));
break;
}
case STACKING_ONLY: {
// Here, we must find all overlapping relations because they are not permitted
overlappingNonStackingRelations(sortedRelations, sourceFeature, targetFeature)
.forEach(fs -> aResponse
.add(new VComment(new VID(fs), ERROR, "Only stacking is permitted.")));
.add(new VComment(VID.of(fs), ERROR, "Only stacking is permitted.")));
break;
}
case OVERLAP_ONLY:
Expand All @@ -172,37 +171,37 @@ public void onRender(TypeAdapter aAdapter, VDocument aResponse,
// can be multiple relations going out from the same sourceFS, we need to consider all
// of them for potential stacking.
stackingRelations(sortedRelations, sourceFeature, targetFeature).forEach(fs -> aResponse
.add(new VComment(new VID(fs), ERROR, "Stacking is not permitted.")));
.add(new VComment(VID.of(fs), ERROR, "Stacking is not permitted.")));
break;
}
}

@Override
public List<Pair<LogMessage, AnnotationFS>> onValidate(TypeAdapter aAdapter, CAS aCas)
{
final AnnotationLayer layer = aAdapter.getLayer();
final RelationAdapter adapter = (RelationAdapter) aAdapter;
final Type type = getType(aCas, adapter.getAnnotationTypeName());
final Feature sourceFeature = type.getFeatureByBaseName(adapter.getSourceFeatureName());
final Feature targetFeature = type.getFeatureByBaseName(adapter.getTargetFeatureName());
final var layer = aAdapter.getLayer();
final var adapter = (RelationAdapter) aAdapter;
final var type = getType(aCas, adapter.getAnnotationTypeName());
final var sourceFeature = type.getFeatureByBaseName(adapter.getSourceFeatureName());
final var targetFeature = type.getFeatureByBaseName(adapter.getTargetFeatureName());

List<Pair<LogMessage, AnnotationFS>> messages = new ArrayList<>();
var messages = new ArrayList<Pair<LogMessage, AnnotationFS>>();

switch (layer.getOverlapMode()) {
case ANY_OVERLAP:
return emptyList();
case NO_OVERLAP: {
Set<AnnotationFS> overlapping = new HashSet<>();
Set<AnnotationFS> stacking = new HashSet<>();
var overlapping = new HashSet<AnnotationFS>();
var stacking = new HashSet<AnnotationFS>();

overlappingOrStackingRelations(select(aCas, type), sourceFeature, targetFeature,
stacking, overlapping);

for (AnnotationFS fs : overlapping) {
for (var fs : overlapping) {
messages.add(Pair.of(LogMessage.error(this, "Overlapping relation at [%d-%d]",
fs.getBegin(), fs.getEnd()), fs));
}
for (AnnotationFS fs : stacking) {
for (var fs : stacking) {
messages.add(Pair.of(LogMessage.error(this, "Stacked relation at [%d-%d]",
fs.getBegin(), fs.getEnd()), fs));
}
Expand Down Expand Up @@ -234,8 +233,8 @@ private void overlappingOrStackingRelations(Collection<AnnotationFS> aRelations,
Feature sourceFeature, Feature targetFeature, Collection<AnnotationFS> aStacking,
Collection<AnnotationFS> aOverlapping)
{
for (AnnotationFS rel1 : aRelations) {
for (AnnotationFS rel2 : aRelations) {
for (var rel1 : aRelations) {
for (var rel2 : aRelations) {
if (rel1.equals(rel2)) {
continue;
}
Expand All @@ -255,9 +254,9 @@ else if (overlapping(rel1, rel2, sourceFeature, targetFeature)) {
private Set<AnnotationFS> overlappingNonStackingRelations(Collection<AnnotationFS> aRelations,
Feature sourceFeature, Feature targetFeature)
{
Set<AnnotationFS> overlapping = new HashSet<>();
for (AnnotationFS rel1 : aRelations) {
for (AnnotationFS rel2 : aRelations) {
var overlapping = new HashSet<AnnotationFS>();
for (var rel1 : aRelations) {
for (var rel2 : aRelations) {
if (rel1.equals(rel2)) {
continue;
}
Expand All @@ -281,11 +280,11 @@ private Set<AnnotationFS> stackingRelations(Collection<AnnotationFS> aRelations,
// that a relation A->B does not count as stacked on a relation B->A). But since there
// can be multiple relations going out from the same sourceFS, we need to consider all
// of them for potential stacking.
Set<AnnotationFS> stacking = new HashSet<>();
List<AnnotationFS> candidates = new ArrayList<>();
for (AnnotationFS fs : aRelations) {
AnnotationFS sourceFs = (AnnotationFS) fs.getFeatureValue(sourceFeature);
AnnotationFS targetFs = (AnnotationFS) fs.getFeatureValue(targetFeature);
var stacking = new HashSet<AnnotationFS>();
var candidates = new ArrayList<AnnotationFS>();
for (var fs : aRelations) {
var sourceFs = (AnnotationFS) fs.getFeatureValue(sourceFeature);
var targetFs = (AnnotationFS) fs.getFeatureValue(targetFeature);

// If there are no stacking candidates at the current position yet, collect the
// first
Expand All @@ -302,9 +301,9 @@ else if (candidates.get(0).getBegin() != fs.getBegin()
// If there are already stacking candidates, check if the current FS is stacking on
// any of them. If yes, generate an error message
else {
for (AnnotationFS cand : candidates) {
FeatureStructure candidateOriginFS = cand.getFeatureValue(sourceFeature);
FeatureStructure candidateTargetFS = cand.getFeatureValue(targetFeature);
for (var cand : candidates) {
var candidateOriginFS = cand.getFeatureValue(sourceFeature);
var candidateTargetFS = cand.getFeatureValue(targetFeature);

if (stacking(candidateOriginFS, candidateTargetFS, sourceFs, targetFs)) {
stacking.add(fs);
Expand Down
Loading