Search in sources :

Example 1 with ConversionService

use of org.dkpro.lab.conversion.ConversionService in project dkpro-lab by dkpro.

the class TaskBase method analyze.

protected void analyze(Class<?> aClazz, Class<? extends Annotation> aAnnotation, Map<String, String> props) {
    if (aClazz.getSuperclass() != null) {
        analyze(aClazz.getSuperclass(), aAnnotation, props);
    }
    for (Field field : aClazz.getDeclaredFields()) {
        field.setAccessible(true);
        try {
            if (field.isAnnotationPresent(aAnnotation)) {
                String name;
                Annotation annotation = field.getAnnotation(aAnnotation);
                if (StringUtils.isNotBlank(ParameterUtil.getName(annotation))) {
                    name = getClass().getName() + "|" + ParameterUtil.getName(annotation);
                } else {
                    name = getClass().getName() + "|" + field.getName();
                }
                String value = Util.toString(field.get(this));
                String oldValue = props.put(name, value);
                if (oldValue != null) {
                    throw new IllegalStateException("Discriminator/property name must be unique and cannot be used " + "on multiple fields in the same class [" + name + "]");
                }
                // Override with conversion service information if available
                Object object = field.get(this);
                ConversionService cs = aContext.getConversionService();
                if (cs.isRegistered(object)) {
                    props.put(name, cs.getDiscriminableValue(object));
                }
                log.debug("Found " + aAnnotation.getSimpleName() + " [" + name + "]: " + value);
            }
        } catch (IllegalAccessException e) {
            throw new IllegalStateException(e);
        } finally {
            field.setAccessible(false);
        }
    }
}
Also used : Field(java.lang.reflect.Field) ConversionService(org.dkpro.lab.conversion.ConversionService) Annotation(java.lang.annotation.Annotation)

Example 2 with ConversionService

use of org.dkpro.lab.conversion.ConversionService in project dkpro-lab by dkpro.

the class BatchTaskEngine method getLatestExecution.

/**
 * Locate the latest task execution compatible with the given task configuration.
 *
 * @param aContext
 *            the context of the current batch task.
 * @param aType
 *            the type of the task context to find.
 * @param aDiscriminators
 *            the discriminators of the task context to find.
 * @param aConfig
 *            the current parameter configuration.
 * @throws TaskContextNotFoundException
 *             if a matching task context could not be found.
 * @see ImportUtil#matchConstraints(Map, Map, boolean)
 */
private TaskContextMetadata getLatestExecution(TaskContext aContext, String aType, Map<String, String> aDiscriminators, Map<String, Object> aConfig) {
    // Convert parameter values to strings
    Map<String, String> config = new HashMap<String, String>();
    for (Entry<String, Object> e : aConfig.entrySet()) {
        config.put(e.getKey(), Util.toString(e.getValue()));
        // If the conversion service has a registered value override the constraint here
        // accordingly
        Object object = e.getValue();
        ConversionService cs = aContext.getConversionService();
        if (cs.isRegistered(object)) {
            config.put(e.getKey(), cs.getDiscriminableValue(object));
        }
    }
    StorageService storage = aContext.getStorageService();
    List<TaskContextMetadata> metas = storage.getContexts(aType, aDiscriminators);
    for (TaskContextMetadata meta : metas) {
        Map<String, String> discriminators = storage.retrieveBinary(meta.getId(), Task.DISCRIMINATORS_KEY, new PropertiesAdapter()).getMap();
        // interpret the discriminators as constraints on the current configuration.
        if (ImportUtil.matchConstraints(discriminators, config, false)) {
            return meta;
        }
    }
    throw ImportUtil.createContextNotFoundException(aType, aDiscriminators);
}
Also used : TaskContextMetadata(org.dkpro.lab.task.TaskContextMetadata) PropertiesAdapter(org.dkpro.lab.storage.impl.PropertiesAdapter) HashMap(java.util.HashMap) ConversionService(org.dkpro.lab.conversion.ConversionService) StorageService(org.dkpro.lab.storage.StorageService)

Aggregations

ConversionService (org.dkpro.lab.conversion.ConversionService)2 Annotation (java.lang.annotation.Annotation)1 Field (java.lang.reflect.Field)1 HashMap (java.util.HashMap)1 StorageService (org.dkpro.lab.storage.StorageService)1 PropertiesAdapter (org.dkpro.lab.storage.impl.PropertiesAdapter)1 TaskContextMetadata (org.dkpro.lab.task.TaskContextMetadata)1