Search in sources :

Example 1 with ServiceManager

use of eu.esdihumboldt.hale.common.core.service.ServiceManager in project hale by halestudio.

the class ClassificationHandler method getSourceExpressionAsCQL.

/**
 * @see eu.esdihumboldt.hale.io.appschema.writer.internal.AbstractPropertyTransformationHandler#getSourceExpressionAsCQL()
 */
@Override
protected String getSourceExpressionAsCQL() {
    Property source = AppSchemaMappingUtils.getSourceProperty(propertyCell);
    PropertyDefinition sourceDef = source.getDefinition().getDefinition();
    Property target = AppSchemaMappingUtils.getTargetProperty(propertyCell);
    PropertyDefinition targetDef = target.getDefinition().getDefinition();
    String sourceName = source.getDefinition().getDefinition().getName().getLocalPart();
    ListMultimap<String, ParameterValue> parameters = propertyCell.getTransformationParameters();
    LookupTable lookup = ClassificationMappingUtil.getClassificationLookup(parameters, new ServiceManager(ServiceManager.SCOPE_PROJECT));
    if (lookup == null) {
        log.warn("No classification specified");
        return "''";
    } else {
        String cqlTemplate = "if_then_else(in(%s), Recode(%s,%s), %s)";
        // build args to Recode function
        StringBuilder recodeArgsBuilder = new StringBuilder();
        Map<Value, Value> valueMap = lookup.asMap();
        int counter = 0;
        for (Value sourceValue : valueMap.keySet()) {
            Value targetValue = valueMap.get(sourceValue);
            String sourceLiteral = asCqlLiteral(sourceDef, sourceValue.as(String.class));
            String targetLiteral = asCqlLiteral(targetDef, targetValue.as(String.class));
            recodeArgsBuilder.append(sourceLiteral).append(",").append(targetLiteral);
            if (counter < valueMap.size() - 1) {
                recodeArgsBuilder.append(",");
            }
            counter++;
        }
        String recodeArgs = recodeArgsBuilder.toString();
        // build args for in function
        List<String> values = new ArrayList<String>();
        for (Value v : valueMap.keySet()) {
            String valueLiteral = asCqlLiteral(sourceDef, v.as(String.class));
            values.add(valueLiteral);
        }
        values.add(0, sourceName);
        String inArgs = Joiner.on(",").join(values);
        // determine what to put in the "else" branch, based on
        // transformation parameters
        String elsePart = null;
        List<ParameterValue> notClassifiedParam = parameters.get(PARAMETER_NOT_CLASSIFIED_ACTION);
        String notClassifiedAction = null;
        if (notClassifiedParam != null && notClassifiedParam.size() > 0) {
            notClassifiedAction = notClassifiedParam.get(0).as(String.class);
        } else {
            notClassifiedAction = USE_NULL_ACTION;
        }
        if (USE_SOURCE_ACTION.equals(notClassifiedAction))
            elsePart = sourceName;
        else if (notClassifiedAction.startsWith(USE_FIXED_VALUE_ACTION_PREFIX))
            elsePart = asCqlLiteral(targetDef, notClassifiedAction.substring(notClassifiedAction.indexOf(':') + 1));
        else if (USE_NULL_ACTION.equals(notClassifiedAction))
            elsePart = "Expression.NIL";
        return String.format(cqlTemplate, inArgs, sourceName, recodeArgs, elsePart);
    }
}
Also used : ParameterValue(eu.esdihumboldt.hale.common.align.model.ParameterValue) ArrayList(java.util.ArrayList) PropertyDefinition(eu.esdihumboldt.hale.common.schema.model.PropertyDefinition) ServiceManager(eu.esdihumboldt.hale.common.core.service.ServiceManager) LookupTable(eu.esdihumboldt.hale.common.lookup.LookupTable) ParameterValue(eu.esdihumboldt.hale.common.align.model.ParameterValue) Value(eu.esdihumboldt.hale.common.core.io.Value) Property(eu.esdihumboldt.hale.common.align.model.Property)

Example 2 with ServiceManager

use of eu.esdihumboldt.hale.common.core.service.ServiceManager in project hale by halestudio.

the class ReprojectGeometryTest method transformData.

@Override
protected List<Instance> transformData(TransformationExample example) throws Exception {
    ConceptualSchemaTransformer transformer = new ConceptualSchemaTransformer();
    DefaultInstanceSink sink = new DefaultInstanceSink();
    final Map<Class<?>, Object> customServices = new HashMap<>();
    customServices.put(FunctionService.class, new AlignmentFunctionService(example.getAlignment()));
    customServices.put(TransformationFunctionService.class, new AlignmentTransformationFunctionService(example.getAlignment()));
    final ServiceProvider serviceProvider = new ServiceProvider() {

        private final ServiceProvider projectScope = new ServiceManager(ServiceManager.SCOPE_PROJECT);

        @SuppressWarnings("unchecked")
        @Override
        public <T> T getService(Class<T> serviceInterface) {
            if (customServices.containsKey(serviceInterface)) {
                return (T) customServices.get(serviceInterface);
            }
            // FIXME global scope not supported yet
            return projectScope.getService(serviceInterface);
        }
    };
    transformer.transform(example.getAlignment(), example.getSourceInstances(), sink, serviceProvider, new NullProgressIndicator());
    return sink.getInstances();
}
Also used : AlignmentTransformationFunctionService(eu.esdihumboldt.hale.common.align.service.impl.AlignmentTransformationFunctionService) ConceptualSchemaTransformer(eu.esdihumboldt.cst.ConceptualSchemaTransformer) HashMap(java.util.HashMap) ServiceManager(eu.esdihumboldt.hale.common.core.service.ServiceManager) DefaultInstanceSink(eu.esdihumboldt.hale.common.align.transformation.service.impl.DefaultInstanceSink) ServiceProvider(eu.esdihumboldt.hale.common.core.service.ServiceProvider) NullProgressIndicator(eu.esdihumboldt.hale.common.core.io.impl.NullProgressIndicator) AlignmentFunctionService(eu.esdihumboldt.hale.common.align.service.impl.AlignmentFunctionService)

Example 3 with ServiceManager

use of eu.esdihumboldt.hale.common.core.service.ServiceManager in project hale by halestudio.

the class ConceptualSchemaTransformerTest method transformData.

@Override
protected List<Instance> transformData(TransformationExample example) throws Exception {
    ConceptualSchemaTransformer transformer = new ConceptualSchemaTransformer();
    ThreadSafeInstanceSink<DefaultInstanceSink> sink = new ThreadSafeInstanceSink<>(new DefaultInstanceSink());
    final Map<Class<?>, Object> customServices = new HashMap<>();
    customServices.put(FunctionService.class, new AlignmentFunctionService(example.getAlignment()));
    customServices.put(TransformationFunctionService.class, new AlignmentTransformationFunctionService(example.getAlignment()));
    InstanceIndexServiceImpl indexService = new InstanceIndexServiceImpl();
    customServices.put(InstanceIndexService.class, indexService);
    final ServiceProvider serviceProvider = new ServiceProvider() {

        private final ServiceProvider projectScope = new ServiceManager(ServiceManager.SCOPE_PROJECT);

        @SuppressWarnings("unchecked")
        @Override
        public <T> T getService(Class<T> serviceInterface) {
            if (customServices.containsKey(serviceInterface)) {
                return (T) customServices.get(serviceInterface);
            }
            // FIXME global scope not supported yet
            return projectScope.getService(serviceInterface);
        }
    };
    indexService.addPropertyMappings(example.getAlignment().getActiveTypeCells(), serviceProvider);
    InstanceCollection source = example.getSourceInstances();
    try (ResourceIterator<Instance> it = source.iterator()) {
        while (it.hasNext()) {
            indexService.add(it.next(), source);
        }
    }
    transformer.transform(example.getAlignment(), source, sink, serviceProvider, new NullProgressIndicator());
    return sink.getDecoratee().getInstances();
}
Also used : AlignmentTransformationFunctionService(eu.esdihumboldt.hale.common.align.service.impl.AlignmentTransformationFunctionService) HashMap(java.util.HashMap) Instance(eu.esdihumboldt.hale.common.instance.model.Instance) DefaultInstanceSink(eu.esdihumboldt.hale.common.align.transformation.service.impl.DefaultInstanceSink) InstanceCollection(eu.esdihumboldt.hale.common.instance.model.InstanceCollection) NullProgressIndicator(eu.esdihumboldt.hale.common.core.io.impl.NullProgressIndicator) AlignmentFunctionService(eu.esdihumboldt.hale.common.align.service.impl.AlignmentFunctionService) InstanceIndexServiceImpl(eu.esdihumboldt.hale.common.instance.index.InstanceIndexServiceImpl) ConceptualSchemaTransformer(eu.esdihumboldt.cst.ConceptualSchemaTransformer) ServiceManager(eu.esdihumboldt.hale.common.core.service.ServiceManager) ServiceProvider(eu.esdihumboldt.hale.common.core.service.ServiceProvider) ThreadSafeInstanceSink(eu.esdihumboldt.hale.common.align.transformation.service.impl.ThreadSafeInstanceSink)

Aggregations

ServiceManager (eu.esdihumboldt.hale.common.core.service.ServiceManager)3 ConceptualSchemaTransformer (eu.esdihumboldt.cst.ConceptualSchemaTransformer)2 AlignmentFunctionService (eu.esdihumboldt.hale.common.align.service.impl.AlignmentFunctionService)2 AlignmentTransformationFunctionService (eu.esdihumboldt.hale.common.align.service.impl.AlignmentTransformationFunctionService)2 DefaultInstanceSink (eu.esdihumboldt.hale.common.align.transformation.service.impl.DefaultInstanceSink)2 NullProgressIndicator (eu.esdihumboldt.hale.common.core.io.impl.NullProgressIndicator)2 ServiceProvider (eu.esdihumboldt.hale.common.core.service.ServiceProvider)2 HashMap (java.util.HashMap)2 ParameterValue (eu.esdihumboldt.hale.common.align.model.ParameterValue)1 Property (eu.esdihumboldt.hale.common.align.model.Property)1 ThreadSafeInstanceSink (eu.esdihumboldt.hale.common.align.transformation.service.impl.ThreadSafeInstanceSink)1 Value (eu.esdihumboldt.hale.common.core.io.Value)1 InstanceIndexServiceImpl (eu.esdihumboldt.hale.common.instance.index.InstanceIndexServiceImpl)1 Instance (eu.esdihumboldt.hale.common.instance.model.Instance)1 InstanceCollection (eu.esdihumboldt.hale.common.instance.model.InstanceCollection)1 LookupTable (eu.esdihumboldt.hale.common.lookup.LookupTable)1 PropertyDefinition (eu.esdihumboldt.hale.common.schema.model.PropertyDefinition)1 ArrayList (java.util.ArrayList)1