Skip to content

Commit

Permalink
#4201 - Layer export as JSON does not include coloring rules
Browse files Browse the repository at this point in the history
- Align JSON layer export again to general project export code
  • Loading branch information
reckart committed Sep 20, 2023
1 parent 014da5b commit 2f5cd12
Showing 1 changed file with 44 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@

import static de.tudarmstadt.ukp.clarin.webanno.model.OverlapMode.ANY_OVERLAP;
import static de.tudarmstadt.ukp.clarin.webanno.model.OverlapMode.OVERLAP_ONLY;
import static de.tudarmstadt.ukp.clarin.webanno.support.WebAnnoConst.CHAIN_TYPE;
import static de.tudarmstadt.ukp.clarin.webanno.support.WebAnnoConst.COREFERENCE_RELATION_FEATURE;
import static de.tudarmstadt.ukp.clarin.webanno.support.WebAnnoConst.COREFERENCE_TYPE_FEATURE;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;

Expand All @@ -46,7 +48,9 @@
import de.tudarmstadt.ukp.clarin.webanno.model.ValidationMode;
import de.tudarmstadt.ukp.clarin.webanno.security.model.User;
import de.tudarmstadt.ukp.clarin.webanno.support.JSONUtil;
import de.tudarmstadt.ukp.clarin.webanno.support.WebAnnoConst;
import de.tudarmstadt.ukp.dkpro.core.api.lexmorph.type.morph.MorphologicalFeatures;
import de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Lemma;
import de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Token;
import de.tudarmstadt.ukp.inception.schema.AnnotationSchemaService;

/**
Expand Down Expand Up @@ -158,6 +162,7 @@ private static void setLayer(AnnotationSchemaService aAnnotationService, Annotat
aLayer.setName(aExLayer.getName());
aLayer.setProject(aProject);
aLayer.setType(aExLayer.getType());
aLayer.setTraits(aExLayer.getTraits());
aAnnotationService.createOrUpdateLayer(aLayer);
}

Expand All @@ -167,28 +172,27 @@ public static AnnotationLayer importLayerFile(AnnotationSchemaService annotation
{
String text = IOUtils.toString(aIS, "UTF-8");

ExportedAnnotationLayer[] exLayers = JSONUtil.getObjectMapper().readValue(text,
ExportedAnnotationLayer[].class);
var exLayers = JSONUtil.getObjectMapper().readValue(text, ExportedAnnotationLayer[].class);

// First import the layers but without setting the attach-layers/features
Map<String, ExportedAnnotationLayer> exLayersMap = new HashMap<>();
Map<String, AnnotationLayer> layersMap = new HashMap<>();
for (ExportedAnnotationLayer exLayer : exLayers) {
AnnotationLayer layer = createLayer(annotationService, project, exLayer);
for (var exLayer : exLayers) {
var layer = createLayer(annotationService, project, exLayer);
layersMap.put(layer.getName(), layer);
exLayersMap.put(layer.getName(), exLayer);
}

// Second fill in the attach-layer and attach-feature information
for (AnnotationLayer layer : layersMap.values()) {
ExportedAnnotationLayer exLayer = exLayersMap.get(layer.getName());
for (var layer : layersMap.values()) {
var exLayer = exLayersMap.get(layer.getName());
if (exLayer.getAttachType() != null) {
layer.setAttachType(layersMap.get(exLayer.getAttachType().getName()));
}
if (exLayer.getAttachFeature() != null) {
AnnotationLayer attachLayer = annotationService.findLayer(project,
var attachLayer = annotationService.findLayer(project,
exLayer.getAttachType().getName());
AnnotationFeature attachFeature = annotationService
var attachFeature = annotationService
.getFeature(exLayer.getAttachFeature().getName(), attachLayer);
layer.setAttachFeature(attachFeature);
}
Expand All @@ -213,8 +217,8 @@ private static AnnotationLayer createLayer(AnnotationSchemaService annotationSer
setLayer(annotationService, layer, aExLayer, project);
}

for (ExportedAnnotationFeature exfeature : aExLayer.getFeatures()) {
ExportedTagSet exTagset = exfeature.getTagSet();
for (var exfeature : aExLayer.getFeatures()) {
var exTagset = exfeature.getTagSet();
TagSet tagSet = null;
if (exTagset != null && annotationService.existsTagSet(exTagset.getName(), project)) {
tagSet = annotationService.getTagSet(exTagset.getName(), project);
Expand All @@ -231,7 +235,7 @@ else if (exTagset != null) {
setFeature(annotationService, feature, exfeature, project);
continue;
}
AnnotationFeature feature = new AnnotationFeature();
var feature = new AnnotationFeature();
feature.setLayer(layer);
feature.setTagset(tagSet);
setFeature(annotationService, feature, exfeature, project);
Expand All @@ -249,16 +253,21 @@ private static void setFeature(AnnotationSchemaService aAnnotationService,
aFeature.setVisible(aExFeature.isVisible());
aFeature.setUiName(aExFeature.getUiName());
aFeature.setProject(aProject);
aFeature.setLayer(aFeature.getLayer());
boolean isItChainedLayer = aFeature.getLayer().getType().equals(WebAnnoConst.CHAIN_TYPE);
if (isItChainedLayer && (aExFeature.getName().equals(WebAnnoConst.COREFERENCE_TYPE_FEATURE)
|| aExFeature.getName().equals(WebAnnoConst.COREFERENCE_RELATION_FEATURE))) {
aFeature.setName(aExFeature.getName());
boolean isItChainedLayer = CHAIN_TYPE.equals(aFeature.getLayer().getType());
if (isItChainedLayer && (COREFERENCE_TYPE_FEATURE.equals(aExFeature.getName())
|| COREFERENCE_RELATION_FEATURE.equals(aExFeature.getName()))) {
aFeature.setType(CAS.TYPE_NAME_STRING);
}
else if (Token._TypeName.equals(aFeature.getLayer().getName())
&& Token._FeatName_morph.equals(aExFeature.getName())
&& Lemma._TypeName.equals(aExFeature.getType())) {
// See https://github.com/inception-project/inception/issues/3080
aFeature.setType(MorphologicalFeatures._TypeName);
}
else {
aFeature.setType(aExFeature.getType());
}
aFeature.setName(aExFeature.getName());
aFeature.setRemember(aExFeature.isRemember());
aFeature.setRequired(aExFeature.isRequired());
aFeature.setHideUnconstraintFeature(aExFeature.isHideUnconstraintFeature());
Expand All @@ -268,7 +277,8 @@ private static void setFeature(AnnotationSchemaService aAnnotationService,
aFeature.setLinkTypeRoleFeatureName(aExFeature.getLinkTypeRoleFeatureName());
aFeature.setLinkTypeTargetFeatureName(aExFeature.getLinkTypeTargetFeatureName());
aFeature.setTraits(aExFeature.getTraits());

aFeature.setCuratable(aExFeature.isCuratable());
aFeature.setRank(aExFeature.getRank());
aAnnotationService.createFeature(aFeature);
}

Expand All @@ -278,15 +288,15 @@ public static ExportedAnnotationLayer exportLayerDetails(
Map<AnnotationFeature, ExportedAnnotationFeature> aFeatureToExFeature,
AnnotationLayer aLayer, AnnotationSchemaService aAnnotationService)
{
ExportedAnnotationLayer exLayer = new ExportedAnnotationLayer();
var exLayer = new ExportedAnnotationLayer();
exLayer.setAllowStacking(aLayer.isAllowStacking());
exLayer.setLockToTokenOffset(AnchoringMode.SINGLE_TOKEN.equals(aLayer.getAnchoringMode()));
exLayer.setMultipleTokens(AnchoringMode.TOKENS.equals(aLayer.getAnchoringMode()));
exLayer.setBuiltIn(aLayer.isBuiltIn());
exLayer.setReadonly(aLayer.isReadonly());
exLayer.setCrossSentence(aLayer.isCrossSentence());
exLayer.setDescription(aLayer.getDescription());
exLayer.setEnabled(aLayer.isEnabled());
exLayer.setLockToTokenOffset(AnchoringMode.SINGLE_TOKEN.equals(aLayer.getAnchoringMode()));
exLayer.setMultipleTokens(AnchoringMode.TOKENS.equals(aLayer.getAnchoringMode()));
exLayer.setOverlapMode(aLayer.getOverlapMode());
exLayer.setAnchoringMode(aLayer.getAnchoringMode());
exLayer.setValidationMode(aLayer.getValidationMode());
Expand All @@ -295,14 +305,15 @@ public static ExportedAnnotationLayer exportLayerDetails(
exLayer.setProjectName(aLayer.getProject().getName());
exLayer.setType(aLayer.getType());
exLayer.setUiName(aLayer.getUiName());
exLayer.setTraits(aLayer.getTraits());

if (aLayerToExLayer != null) {
aLayerToExLayer.put(aLayer, exLayer);
}

List<ExportedAnnotationFeature> exFeatures = new ArrayList<>();
for (AnnotationFeature feature : aAnnotationService.listAnnotationFeature(aLayer)) {
ExportedAnnotationFeature exFeature = new ExportedAnnotationFeature();
var exFeatures = new ArrayList<ExportedAnnotationFeature>();
for (var feature : aAnnotationService.listAnnotationFeature(aLayer)) {
var exFeature = new ExportedAnnotationFeature();
exFeature.setDescription(feature.getDescription());
exFeature.setEnabled(feature.isEnabled());
exFeature.setRemember(feature.isRemember());
Expand All @@ -319,18 +330,20 @@ public static ExportedAnnotationLayer exportLayerDetails(
exFeature.setLinkTypeRoleFeatureName(feature.getLinkTypeRoleFeatureName());
exFeature.setLinkTypeTargetFeatureName(feature.getLinkTypeTargetFeatureName());
exFeature.setTraits(feature.getTraits());
exFeature.setCuratable(feature.isCuratable());
exFeature.setRank(feature.getRank());

if (feature.getTagset() != null) {
TagSet tagSet = feature.getTagset();
ExportedTagSet exTagSet = new ExportedTagSet();
var tagSet = feature.getTagset();
var exTagSet = new ExportedTagSet();
exTagSet.setDescription(tagSet.getDescription());
exTagSet.setLanguage(tagSet.getLanguage());
exTagSet.setName(tagSet.getName());
exTagSet.setCreateTag(tagSet.isCreateTag());

List<ExportedTag> exportedTags = new ArrayList<>();
for (Tag tag : aAnnotationService.listTags(tagSet)) {
ExportedTag exTag = new ExportedTag();
var exportedTags = new ArrayList<ExportedTag>();
for (var tag : aAnnotationService.listTags(tagSet)) {
var exTag = new ExportedTag();
exTag.setDescription(tag.getDescription());
exTag.setName(tag.getName());
exportedTags.add(exTag);
Expand All @@ -343,6 +356,7 @@ public static ExportedAnnotationLayer exportLayerDetails(
aFeatureToExFeature.put(feature, exFeature);
}
}

exLayer.setFeatures(exFeatures);
return exLayer;
}
Expand Down

0 comments on commit 2f5cd12

Please sign in to comment.