Search in sources :

Example 51 with AnnotatorState

use of de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.AnnotatorState in project webanno by webanno.

the class PreferencesUtil method loadPreferences.

/**
 * Set annotation preferences of users for a given project such as window size, annotation
 * layers,... reading from the file system.
 *
 * @param aUsername
 *            The {@link User} for whom we need to read the preference (preferences are stored
 *            per user)
 * @param aRepositoryService the repository service.
 * @param aAnnotationService the annotation service.
 * @param aBModel
 *            The {@link AnnotatorState} that will be populated with preferences from the
 *            file
 * @param aMode the mode.
 * @throws BeansException hum?
 * @throws IOException hum?
 */
public static void loadPreferences(String aUsername, SettingsService aSettingsService, ProjectService aRepositoryService, AnnotationSchemaService aAnnotationService, AnnotatorState aBModel, Mode aMode) throws BeansException, IOException {
    AnnotationPreference preference = new AnnotationPreference();
    BeanWrapper wrapper = new BeanWrapperImpl(preference);
    // get annotation preference from file system
    try {
        Properties props = aRepositoryService.loadUserSettings(aUsername, aBModel.getProject());
        for (Entry<Object, Object> entry : props.entrySet()) {
            String property = entry.getKey().toString();
            int index = property.indexOf(".");
            String propertyName = property.substring(index + 1);
            String mode = property.substring(0, index);
            if (wrapper.isWritableProperty(propertyName) && mode.equals(aMode.getName())) {
                if (AnnotationPreference.class.getDeclaredField(propertyName).getGenericType() instanceof ParameterizedType) {
                    if (entry.getValue().toString().startsWith("[")) {
                        // its a list
                        List<String> value = Arrays.asList(StringUtils.replaceChars(entry.getValue().toString(), "[]", "").split(","));
                        if (!value.get(0).equals("")) {
                            wrapper.setPropertyValue(propertyName, value);
                        }
                    } else if (entry.getValue().toString().startsWith("{")) {
                        // its a map
                        String s = StringUtils.replaceChars(entry.getValue().toString(), "{}", "");
                        Map<String, String> value = Arrays.stream(s.split(",")).map(x -> x.split("=")).collect(Collectors.toMap(x -> x[0], x -> x[1]));
                        wrapper.setPropertyValue(propertyName, value);
                    }
                } else {
                    wrapper.setPropertyValue(propertyName, entry.getValue());
                }
            }
        }
        // set layers according to preferences
        List<AnnotationLayer> enabledLayers = aAnnotationService.listAnnotationLayer(aBModel.getProject()).stream().filter(// only allow enabled layers
        l -> l.isEnabled()).collect(Collectors.toList());
        List<Long> hiddenLayerIds = preference.getHiddenAnnotationLayerIds();
        enabledLayers = enabledLayers.stream().filter(l -> !hiddenLayerIds.contains(l.getId())).collect(Collectors.toList());
        aBModel.setAnnotationLayers(enabledLayers);
        // Get color preferences for each layer, init with legacy if not found
        Map<Long, ColoringStrategyType> colorPerLayer = preference.getColorPerLayer();
        for (AnnotationLayer layer : aAnnotationService.listAnnotationLayer(aBModel.getProject())) {
            if (!colorPerLayer.containsKey(layer.getId())) {
                colorPerLayer.put(layer.getId(), ColoringStrategyType.LEGACY);
            }
        }
    }// no preference found
     catch (Exception e) {
        // If no layer preferences are defined,
        // then just assume all enabled layers are preferred
        List<AnnotationLayer> enabledLayers = aAnnotationService.listAnnotationLayer(aBModel.getProject()).stream().filter(// only allow enabled layers
        l -> l.isEnabled()).collect(Collectors.toList());
        aBModel.setAnnotationLayers(enabledLayers);
        preference.setWindowSize(aSettingsService.getNumberOfSentences());
        // add default coloring strategy
        Map<Long, ColoringStrategyType> colorPerLayer = new HashMap<>();
        for (AnnotationLayer layer : aBModel.getAnnotationLayers()) {
            colorPerLayer.put(layer.getId(), ColoringStrategy.getBestInitialStrategy(aAnnotationService, layer, preference));
        }
        preference.setColorPerLayer(colorPerLayer);
    }
    aBModel.setPreferences(preference);
}
Also used : Arrays(java.util.Arrays) Properties(java.util.Properties) ProjectService(de.tudarmstadt.ukp.clarin.webanno.api.ProjectService) AnnotatorState(de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.AnnotatorState) BeanWrapper(org.springframework.beans.BeanWrapper) IOException(java.io.IOException) HashMap(java.util.HashMap) BeansException(org.springframework.beans.BeansException) ColoringStrategy(de.tudarmstadt.ukp.clarin.webanno.api.annotation.coloring.ColoringStrategy) Mode(de.tudarmstadt.ukp.clarin.webanno.model.Mode) Collectors(java.util.stream.Collectors) StringUtils(org.apache.commons.lang3.StringUtils) BeanWrapperImpl(org.springframework.beans.BeanWrapperImpl) SettingsService(de.tudarmstadt.ukp.clarin.webanno.api.SettingsService) AnnotationSchemaService(de.tudarmstadt.ukp.clarin.webanno.api.AnnotationSchemaService) List(java.util.List) ParameterizedType(java.lang.reflect.ParameterizedType) User(de.tudarmstadt.ukp.clarin.webanno.security.model.User) AnnotationPreference(de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.AnnotationPreference) AnnotationLayer(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationLayer) Map(java.util.Map) Entry(java.util.Map.Entry) SecurityContextHolder(org.springframework.security.core.context.SecurityContextHolder) ColoringStrategyType(de.tudarmstadt.ukp.clarin.webanno.api.annotation.coloring.ColoringStrategy.ColoringStrategyType) BeanWrapperImpl(org.springframework.beans.BeanWrapperImpl) Properties(java.util.Properties) AnnotationLayer(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationLayer) IOException(java.io.IOException) BeansException(org.springframework.beans.BeansException) ParameterizedType(java.lang.reflect.ParameterizedType) BeanWrapper(org.springframework.beans.BeanWrapper) List(java.util.List) AnnotationPreference(de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.AnnotationPreference) HashMap(java.util.HashMap) Map(java.util.Map) ColoringStrategyType(de.tudarmstadt.ukp.clarin.webanno.api.annotation.coloring.ColoringStrategy.ColoringStrategyType)

Example 52 with AnnotatorState

use of de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.AnnotatorState in project webanno by webanno.

the class DocumentNamePanel method getLabel.

private String getLabel() {
    StringBuilder sb = new StringBuilder();
    AnnotatorState state = getModelObject();
    if (state.getProject() != null) {
        sb.append(state.getProject().getName());
    }
    sb.append("/");
    if (state.getDocument() != null) {
        sb.append(state.getDocument().getName());
    }
    if (RuntimeConfigurationType.DEVELOPMENT.equals(getApplication().getConfigurationType())) {
        sb.append(" (");
        if (state.getProject() != null) {
            sb.append(state.getProject().getId());
        }
        sb.append("/");
        if (state.getDocument() != null) {
            sb.append(state.getDocument().getId());
        }
        sb.append(")");
    }
    return sb.toString();
}
Also used : AnnotatorState(de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.AnnotatorState)

Example 53 with AnnotatorState

use of de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.AnnotatorState in project webanno by webanno.

the class AnnotationDetailEditorPanel method loadFeatureEditorModels.

public void loadFeatureEditorModels(JCas aJCas, AjaxRequestTarget aTarget) throws AnnotationException {
    LOG.trace("loadFeatureEditorModels()");
    AnnotatorState state = getModelObject();
    Selection selection = state.getSelection();
    List<FeatureState> featureStates = state.getFeatureStates();
    for (FeatureState featureState : featureStates) {
        if (StringUtils.isNotBlank(featureState.feature.getLinkTypeName())) {
            featureState.value = new ArrayList<>();
        }
    }
    try {
        if (selection.isSpan()) {
            annotationFeatureForm.updateLayersDropdown();
        }
        if (selection.getAnnotation().isSet()) {
            // If an existing annotation was selected, take the feature editor model values from
            // there
            AnnotationFS annoFs = selectByAddr(aJCas, state.getSelection().getAnnotation().getId());
            // Try obtaining the layer from the feature structure
            AnnotationLayer layer;
            try {
                layer = annotationService.getLayer(state.getProject(), annoFs);
                state.setSelectedAnnotationLayer(layer);
                LOG.trace(String.format("loadFeatureEditorModels() selectedLayer set from selection: %s", state.getSelectedAnnotationLayer().getUiName()));
            } catch (NoResultException e) {
                clearFeatureEditorModels(aTarget);
                throw new IllegalStateException("Unknown layer [" + annoFs.getType().getName() + "]", e);
            }
            // selected span annotation
            if (!selection.isArc() && !state.getPreferences().isRememberLayer()) {
                state.setSelectedAnnotationLayer(layer);
            }
            loadFeatureEditorModelsCommon(aTarget, aJCas, layer, annoFs, null);
        } else {
            if (selection.isArc()) {
                // Avoid creation of arcs on locked layers
                if (state.getSelectedAnnotationLayer() != null && state.getSelectedAnnotationLayer().isReadonly()) {
                    state.setSelectedAnnotationLayer(new AnnotationLayer());
                } else {
                    loadFeatureEditorModelsCommon(aTarget, aJCas, state.getSelectedAnnotationLayer(), null, state.getRememberedArcFeatures());
                }
            } else {
                loadFeatureEditorModelsCommon(aTarget, aJCas, state.getSelectedAnnotationLayer(), null, state.getRememberedSpanFeatures());
            }
        }
        annotationFeatureForm.updateRememberLayer();
        if (aTarget != null) {
            aTarget.add(annotationFeatureForm);
        }
    } catch (Exception e) {
        throw new AnnotationException(e);
    }
}
Also used : AnnotationFS(org.apache.uima.cas.text.AnnotationFS) Selection(de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.Selection) AnnotatorState(de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.AnnotatorState) AnnotationException(de.tudarmstadt.ukp.clarin.webanno.api.annotation.exception.AnnotationException) NoResultException(javax.persistence.NoResultException) AnnotationLayer(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationLayer) FeatureState(de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.FeatureState) NoResultException(javax.persistence.NoResultException) AnnotationException(de.tudarmstadt.ukp.clarin.webanno.api.annotation.exception.AnnotationException) UIMAException(org.apache.uima.UIMAException) IOException(java.io.IOException)

Example 54 with AnnotatorState

use of de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.AnnotatorState in project webanno by webanno.

the class AnnotationDetailEditorPanel method setSlot.

@SuppressWarnings("unchecked")
private void setSlot(AjaxRequestTarget aTarget, JCas aJCas, int aAnnotationId) {
    AnnotatorState state = getModelObject();
    // Set an armed slot
    if (!state.getSelection().isArc() && state.isSlotArmed()) {
        List<LinkWithRoleModel> links = (List<LinkWithRoleModel>) state.getFeatureState(state.getArmedFeature()).value;
        LinkWithRoleModel link = links.get(state.getArmedSlot());
        link.targetAddr = aAnnotationId;
        link.label = selectByAddr(aJCas, aAnnotationId).getCoveredText();
    }
    // Auto-commit if working on existing annotation
    if (state.getSelection().getAnnotation().isSet()) {
        try {
            actionCreateOrUpdate(aTarget, aJCas);
        } catch (Exception e) {
            handleException(this, aTarget, e);
        }
    }
    state.clearArmedSlot();
}
Also used : LinkWithRoleModel(de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.LinkWithRoleModel) AnnotatorState(de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.AnnotatorState) List(java.util.List) ArrayList(java.util.ArrayList) NoResultException(javax.persistence.NoResultException) AnnotationException(de.tudarmstadt.ukp.clarin.webanno.api.annotation.exception.AnnotationException) UIMAException(org.apache.uima.UIMAException) IOException(java.io.IOException)

Example 55 with AnnotatorState

use of de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.AnnotatorState in project webanno by webanno.

the class AnnotationDetailEditorPanel method createNewAnnotation.

private void createNewAnnotation(AjaxRequestTarget aTarget, TypeAdapter aAdapter, JCas aJCas) throws AnnotationException, IOException {
    AnnotatorState state = getModelObject();
    if (state.getSelection().isArc()) {
        if (aAdapter instanceof SpanAdapter) {
            error("Layer [" + aAdapter.getLayer().getUiName() + "] does not support arc annotation.");
            aTarget.addChildren(getPage(), IFeedback.class);
        } else if (aAdapter instanceof ArcAdapter) {
            createNewRelationAnnotation((ArcAdapter) aAdapter, aJCas);
        } else if (aAdapter instanceof ChainAdapter) {
            createNewChainLinkAnnotation((ChainAdapter) aAdapter, aJCas);
        } else {
            throw new IllegalStateException("I don't know how to use [" + aAdapter.getClass().getSimpleName() + "] in this situation.");
        }
    } else {
        if (aAdapter instanceof SpanAdapter) {
            createNewSpanAnnotation(aTarget, (SpanAdapter) aAdapter, aJCas);
        } else if (aAdapter instanceof ChainAdapter) {
            createNewChainElement((ChainAdapter) aAdapter, aJCas);
        } else {
            throw new IllegalStateException("I don't know how to use [" + aAdapter.getClass().getSimpleName() + "] in this situation.");
        }
    }
}
Also used : ArcAdapter(de.tudarmstadt.ukp.clarin.webanno.api.annotation.adapter.ArcAdapter) AnnotatorState(de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.AnnotatorState) SpanAdapter(de.tudarmstadt.ukp.clarin.webanno.api.annotation.adapter.SpanAdapter) ChainAdapter(de.tudarmstadt.ukp.clarin.webanno.api.annotation.adapter.ChainAdapter)

Aggregations

AnnotatorState (de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.AnnotatorState)84 JCas (org.apache.uima.jcas.JCas)37 IOException (java.io.IOException)26 AnnotationException (de.tudarmstadt.ukp.clarin.webanno.api.annotation.exception.AnnotationException)23 AnnotationFS (org.apache.uima.cas.text.AnnotationFS)22 UIMAException (org.apache.uima.UIMAException)20 AnnotationLayer (de.tudarmstadt.ukp.clarin.webanno.model.AnnotationLayer)16 Sentence (de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Sentence)16 ArrayList (java.util.ArrayList)16 AnnotationFeature (de.tudarmstadt.ukp.clarin.webanno.model.AnnotationFeature)14 List (java.util.List)13 AnnotationDocument (de.tudarmstadt.ukp.clarin.webanno.model.AnnotationDocument)12 TypeAdapter (de.tudarmstadt.ukp.clarin.webanno.api.annotation.adapter.TypeAdapter)9 SourceDocument (de.tudarmstadt.ukp.clarin.webanno.model.SourceDocument)9 SuggestionBuilder (de.tudarmstadt.ukp.clarin.webanno.ui.curation.component.model.SuggestionBuilder)9 Map (java.util.Map)9 NoResultException (javax.persistence.NoResultException)9 AjaxRequestTarget (org.apache.wicket.ajax.AjaxRequestTarget)9 FeatureState (de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.FeatureState)8 Selection (de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.Selection)8