|
| 1 | +/* |
| 2 | + * Licensed to the Technische Universität Darmstadt under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The Technische Universität Darmstadt |
| 6 | + * licenses this file to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | +package de.tudarmstadt.ukp.inception.annotation.layer.relation; |
| 19 | + |
| 20 | +import static de.tudarmstadt.ukp.inception.annotation.layer.relation.RelationLayerSupport.FEAT_REL_SOURCE; |
| 21 | +import static de.tudarmstadt.ukp.inception.annotation.layer.relation.RelationLayerSupport.FEAT_REL_TARGET; |
| 22 | +import static java.util.Arrays.asList; |
| 23 | +import static java.util.Collections.emptyList; |
| 24 | +import static org.apache.commons.lang3.StringUtils.substringAfter; |
| 25 | + |
| 26 | +import java.io.IOException; |
| 27 | +import java.io.Serializable; |
| 28 | +import java.lang.invoke.MethodHandles; |
| 29 | +import java.util.List; |
| 30 | +import java.util.Optional; |
| 31 | + |
| 32 | +import org.apache.commons.lang3.NotImplementedException; |
| 33 | +import org.apache.uima.cas.CAS; |
| 34 | +import org.apache.uima.cas.FeatureStructure; |
| 35 | +import org.apache.uima.resource.metadata.TypeDescription; |
| 36 | +import org.apache.uima.resource.metadata.TypeSystemDescription; |
| 37 | +import org.apache.wicket.MarkupContainer; |
| 38 | +import org.apache.wicket.markup.html.panel.EmptyPanel; |
| 39 | +import org.apache.wicket.markup.html.panel.Panel; |
| 40 | +import org.apache.wicket.model.IModel; |
| 41 | +import org.slf4j.Logger; |
| 42 | +import org.slf4j.LoggerFactory; |
| 43 | +import org.springframework.beans.factory.annotation.Autowired; |
| 44 | + |
| 45 | +import de.tudarmstadt.ukp.clarin.webanno.api.annotation.config.AnnotationAutoConfiguration; |
| 46 | +import de.tudarmstadt.ukp.clarin.webanno.model.AnnotationFeature; |
| 47 | +import de.tudarmstadt.ukp.clarin.webanno.model.AnnotationLayer; |
| 48 | +import de.tudarmstadt.ukp.inception.editor.action.AnnotationActionHandler; |
| 49 | +import de.tudarmstadt.ukp.inception.rendering.editorstate.AnnotatorState; |
| 50 | +import de.tudarmstadt.ukp.inception.rendering.editorstate.FeatureState; |
| 51 | +import de.tudarmstadt.ukp.inception.schema.api.adapter.AnnotationException; |
| 52 | +import de.tudarmstadt.ukp.inception.schema.api.feature.FeatureEditor; |
| 53 | +import de.tudarmstadt.ukp.inception.schema.api.feature.FeatureSupport; |
| 54 | +import de.tudarmstadt.ukp.inception.schema.api.feature.FeatureType; |
| 55 | +import de.tudarmstadt.ukp.inception.support.json.JSONUtil; |
| 56 | + |
| 57 | +/** |
| 58 | + * Extension providing image-related features for annotations. |
| 59 | + * <p> |
| 60 | + * This class is exposed as a Spring Component via |
| 61 | + * {@link AnnotationAutoConfiguration#relationEndpointFeatureSupport}. |
| 62 | + * </p> |
| 63 | + */ |
| 64 | +public class RelationEndpointFeatureSupport |
| 65 | + implements FeatureSupport<RelationEndpointFeatureTraits> |
| 66 | +{ |
| 67 | + private final static Logger LOG = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); |
| 68 | + |
| 69 | + public static final String PREFIX_SOURCE = "rel-source:"; |
| 70 | + public static final String PREFIX_TARGET = "rel-target:"; |
| 71 | + |
| 72 | + private String featureSupportId; |
| 73 | + |
| 74 | + @Autowired |
| 75 | + public RelationEndpointFeatureSupport() |
| 76 | + { |
| 77 | + // Nothing to do |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + public String getId() |
| 82 | + { |
| 83 | + return featureSupportId; |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public void setBeanName(String aBeanName) |
| 88 | + { |
| 89 | + featureSupportId = aBeanName; |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + public Optional<FeatureType> getFeatureType(AnnotationFeature aFeature) |
| 94 | + { |
| 95 | + if (!RelationLayerSupport.TYPE.equals(aFeature.getLayer().getType())) { |
| 96 | + return Optional.empty(); |
| 97 | + } |
| 98 | + |
| 99 | + if (FEAT_REL_SOURCE.equals(aFeature.getName())) { |
| 100 | + return Optional.of( |
| 101 | + new FeatureType(aFeature.getType(), "Relation source", featureSupportId, true)); |
| 102 | + } |
| 103 | + |
| 104 | + if (FEAT_REL_TARGET.equals(aFeature.getName())) { |
| 105 | + return Optional.of( |
| 106 | + new FeatureType(aFeature.getType(), "Relation target", featureSupportId, true)); |
| 107 | + } |
| 108 | + |
| 109 | + return Optional.empty(); |
| 110 | + } |
| 111 | + |
| 112 | + @Override |
| 113 | + public List<FeatureType> getSupportedFeatureTypes(AnnotationLayer aAnnotationLayer) |
| 114 | + { |
| 115 | + if (!RelationLayerSupport.TYPE.equals(aAnnotationLayer.getType())) { |
| 116 | + return emptyList(); |
| 117 | + } |
| 118 | + |
| 119 | + var attachType = aAnnotationLayer.getAttachType().getName(); |
| 120 | + return asList( // |
| 121 | + new FeatureType(PREFIX_SOURCE + attachType, "Relation source", featureSupportId, |
| 122 | + true), // |
| 123 | + new FeatureType(PREFIX_TARGET + attachType, "Relation target", featureSupportId, |
| 124 | + true)); |
| 125 | + } |
| 126 | + |
| 127 | + @Override |
| 128 | + public boolean accepts(AnnotationFeature aFeature) |
| 129 | + { |
| 130 | + return aFeature.getType().startsWith(PREFIX_SOURCE) |
| 131 | + || aFeature.getType().startsWith(PREFIX_TARGET); |
| 132 | + } |
| 133 | + |
| 134 | + @Override |
| 135 | + public Panel createTraitsEditor(String aId, IModel<AnnotationFeature> aFeatureModel) |
| 136 | + { |
| 137 | + return new EmptyPanel(aId); |
| 138 | + } |
| 139 | + |
| 140 | + @Override |
| 141 | + public FeatureEditor createEditor(String aId, MarkupContainer aOwner, |
| 142 | + AnnotationActionHandler aHandler, IModel<AnnotatorState> aStateModel, |
| 143 | + IModel<FeatureState> aFeatureStateModel) |
| 144 | + { |
| 145 | + return null; |
| 146 | + } |
| 147 | + |
| 148 | + @Override |
| 149 | + public RelationEndpointFeatureTraits readTraits(AnnotationFeature aFeature) |
| 150 | + { |
| 151 | + RelationEndpointFeatureTraits traits = null; |
| 152 | + try { |
| 153 | + traits = JSONUtil.fromJsonString(RelationEndpointFeatureTraits.class, |
| 154 | + aFeature.getTraits()); |
| 155 | + } |
| 156 | + catch (IOException e) { |
| 157 | + LOG.error("Unable to read traits", e); |
| 158 | + } |
| 159 | + |
| 160 | + if (traits == null) { |
| 161 | + traits = new RelationEndpointFeatureTraits(); |
| 162 | + } |
| 163 | + |
| 164 | + return traits; |
| 165 | + } |
| 166 | + |
| 167 | + @Override |
| 168 | + public void writeTraits(AnnotationFeature aFeature, RelationEndpointFeatureTraits aTraits) |
| 169 | + { |
| 170 | + try { |
| 171 | + aFeature.setTraits(JSONUtil.toJsonString(aTraits)); |
| 172 | + } |
| 173 | + catch (IOException e) { |
| 174 | + LOG.error("Unable to write traits", e); |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + @Override |
| 179 | + public void generateFeature(TypeSystemDescription aTSD, TypeDescription aTD, |
| 180 | + AnnotationFeature aFeature) |
| 181 | + { |
| 182 | + if (aFeature.getType().startsWith(PREFIX_SOURCE)) { |
| 183 | + aTD.addFeature(aFeature.getName(), "", |
| 184 | + substringAfter(aFeature.getType(), PREFIX_SOURCE)); |
| 185 | + } |
| 186 | + else if (aFeature.getType().startsWith(PREFIX_TARGET)) { |
| 187 | + aTD.addFeature(aFeature.getName(), "", |
| 188 | + substringAfter(aFeature.getType(), PREFIX_TARGET)); |
| 189 | + } |
| 190 | + else { |
| 191 | + throw new IllegalStateException( |
| 192 | + "Unsupported feature type [" + aFeature.getType() + "]"); |
| 193 | + } |
| 194 | + } |
| 195 | + |
| 196 | + @Override |
| 197 | + public <V> V unwrapFeatureValue(AnnotationFeature aFeature, CAS aCAS, Object aValue) |
| 198 | + { |
| 199 | + throw new NotImplementedException("Relation endpoints do not support unwrapFeatureValue"); |
| 200 | + } |
| 201 | + |
| 202 | + @Override |
| 203 | + public Serializable wrapFeatureValue(AnnotationFeature aFeature, CAS aCAS, Object aValue) |
| 204 | + { |
| 205 | + throw new NotImplementedException("Relation endpoints do not support wrapFeatureValue"); |
| 206 | + } |
| 207 | + |
| 208 | + @Override |
| 209 | + public <V> V getFeatureValue(AnnotationFeature aFeature, FeatureStructure aFS) |
| 210 | + { |
| 211 | + throw new NotImplementedException("Relation endpoints do not support getFeatureValue"); |
| 212 | + } |
| 213 | + |
| 214 | + @Override |
| 215 | + public void setFeatureValue(CAS aCas, AnnotationFeature aFeature, int aAddress, Object aValue) |
| 216 | + throws AnnotationException |
| 217 | + { |
| 218 | + throw new NotImplementedException("Relation endpoints do not support setFeatureValue"); |
| 219 | + } |
| 220 | + |
| 221 | + @Override |
| 222 | + public boolean isUsingDefaultOptions(AnnotationFeature aFeature) |
| 223 | + { |
| 224 | + return false; |
| 225 | + } |
| 226 | + |
| 227 | + @Override |
| 228 | + public boolean isAccessible(AnnotationFeature aFeature) |
| 229 | + { |
| 230 | + return false; |
| 231 | + } |
| 232 | +} |
0 commit comments