Search in sources :

Example 16 with TransformationException

use of eu.esdihumboldt.hale.common.align.transformation.function.TransformationException in project hale by halestudio.

the class AbstractScriptedPropertyTransformation method evaluate.

@Override
protected final ListMultimap<String, Object> evaluate(String transformationIdentifier, E engine, ListMultimap<String, PropertyValue> variables, ListMultimap<String, PropertyEntityDefinition> resultNames, Map<String, String> executionParameters, TransformationLog log) throws TransformationException {
    ListMultimap<String, ParameterValue> originalParameters = getParameters();
    ListMultimap<String, Value> transformedParameters = ArrayListMultimap.create();
    if (originalParameters != null) {
        for (Map.Entry<String, ParameterValue> entry : originalParameters.entries()) {
            if (!entry.getValue().needsProcessing()) {
                Value value = entry.getValue().intern();
                if (!value.isRepresentedAsDOM()) {
                    value = Value.simple(getExecutionContext().getVariables().replaceVariables(value.getStringRepresentation()));
                }
                transformedParameters.put(entry.getKey(), value);
            } else {
                // type is a script
                ScriptFactory factory = ScriptExtension.getInstance().getFactory(entry.getValue().getType());
                if (factory == null)
                    throw new TransformationException("Couldn't find factory for script id " + entry.getValue().getType());
                Script script;
                try {
                    script = factory.createExtensionObject();
                } catch (Exception e) {
                    throw new TransformationException("Couldn't create script from factory", e);
                }
                Object result;
                try {
                    String scriptStr = entry.getValue().as(String.class);
                    if (script.requiresReplacedTransformationVariables()) {
                        // replace transformation variables
                        scriptStr = getExecutionContext().getVariables().replaceVariables(scriptStr);
                    }
                    result = script.evaluate(scriptStr, variables.values(), getExecutionContext());
                } catch (ScriptException e) {
                    throw new TransformationException("Couldn't evaluate a transformation parameter", e);
                }
                // XXX use conversion service instead of valueOf?
                transformedParameters.put(entry.getKey(), Value.simple(result));
            }
        }
    }
    this.transformedParameters = Multimaps.unmodifiableListMultimap(transformedParameters);
    return evaluateImpl(transformationIdentifier, engine, variables, resultNames, executionParameters, log);
}
Also used : Script(eu.esdihumboldt.hale.common.scripting.Script) TransformationException(eu.esdihumboldt.hale.common.align.transformation.function.TransformationException) ParameterValue(eu.esdihumboldt.hale.common.align.model.ParameterValue) TransformationException(eu.esdihumboldt.hale.common.align.transformation.function.TransformationException) ScriptException(javax.script.ScriptException) ScriptException(javax.script.ScriptException) ParameterValue(eu.esdihumboldt.hale.common.align.model.ParameterValue) PropertyValue(eu.esdihumboldt.hale.common.align.transformation.function.PropertyValue) Value(eu.esdihumboldt.hale.common.core.io.Value) Map(java.util.Map) ScriptFactory(eu.esdihumboldt.hale.common.scripting.ScriptFactory)

Example 17 with TransformationException

use of eu.esdihumboldt.hale.common.align.transformation.function.TransformationException in project hale by halestudio.

the class IndexJoinHandler method partitionInstances.

/**
 * @see eu.esdihumboldt.hale.common.align.transformation.function.InstanceHandler#partitionInstances(eu.esdihumboldt.hale.common.instance.model.InstanceCollection,
 *      java.lang.String,
 *      eu.esdihumboldt.hale.common.align.transformation.engine.TransformationEngine,
 *      com.google.common.collect.ListMultimap, java.util.Map,
 *      eu.esdihumboldt.hale.common.align.transformation.report.TransformationLog)
 */
@Override
public ResourceIterator<FamilyInstance> partitionInstances(InstanceCollection instances, String transformationIdentifier, TransformationEngine engine, ListMultimap<String, ParameterValue> transformationParameters, Map<String, String> executionParameters, TransformationLog log) throws TransformationException {
    if (transformationParameters == null || !transformationParameters.containsKey(PARAMETER_JOIN) || transformationParameters.get(PARAMETER_JOIN).isEmpty()) {
        throw new TransformationException("No join parameter defined");
    }
    JoinHandler fallbackHandler = new JoinHandler();
    InstanceIndexService indexService = serviceProvider.getService(InstanceIndexService.class);
    if (indexService == null) {
        log.warn(MessageFormat.format("Index service not available, falling back to join handler {0}", fallbackHandler.getClass().getCanonicalName()));
        return fallbackHandler.partitionInstances(instances, transformationIdentifier, engine, transformationParameters, executionParameters, log);
    }
    JoinParameter joinParameter = transformationParameters.get(PARAMETER_JOIN).get(0).as(JoinParameter.class);
    String validation = joinParameter.validate();
    if (validation != null) {
        throw new TransformationException("Join parameter invalid: " + validation);
    }
    List<TypeEntityDefinition> types = joinParameter.getTypes();
    JoinDefinition joinDefinition = JoinUtil.getJoinDefinition(joinParameter);
    // remember instances of first type to start join afterwards
    Collection<ResolvableInstanceReference> startInstances = new LinkedList<ResolvableInstanceReference>();
    List<Object> inputInstanceIds = new ArrayList<>();
    try (ResourceIterator<Instance> it = instances.iterator()) {
        while (it.hasNext()) {
            Instance i = InstanceDecorator.getRoot(it.next());
            // remember instances of first type
            if (i.getDefinition().equals(types.get(0).getDefinition())) {
                startInstances.add(new ResolvableInstanceReference(instances.getReference(i), instances));
            }
            if (!Identifiable.is(i)) {
                log.warn(MessageFormat.format("At least one instance does not have an ID, falling back to join handler {0}", fallbackHandler.getClass().getCanonicalName()));
                return fallbackHandler.partitionInstances(instances, transformationIdentifier, engine, transformationParameters, executionParameters, log);
            }
            inputInstanceIds.add(Identifiable.getId(i));
        }
    }
    return new IndexJoinIterator(startInstances, joinDefinition, indexService);
}
Also used : TransformationException(eu.esdihumboldt.hale.common.align.transformation.function.TransformationException) FamilyInstance(eu.esdihumboldt.hale.common.instance.model.FamilyInstance) Instance(eu.esdihumboldt.hale.common.instance.model.Instance) ArrayList(java.util.ArrayList) JoinParameter(eu.esdihumboldt.hale.common.align.model.functions.join.JoinParameter) LinkedList(java.util.LinkedList) TypeEntityDefinition(eu.esdihumboldt.hale.common.align.model.impl.TypeEntityDefinition) JoinDefinition(eu.esdihumboldt.cst.functions.core.join.JoinUtil.JoinDefinition) ResolvableInstanceReference(eu.esdihumboldt.hale.common.instance.model.ResolvableInstanceReference) InstanceIndexService(eu.esdihumboldt.hale.common.instance.index.InstanceIndexService)

Example 18 with TransformationException

use of eu.esdihumboldt.hale.common.align.transformation.function.TransformationException in project hale by halestudio.

the class JoinHandler method partitionInstances.

// For now no support for using the same type more than once in a join.
/**
 * @see eu.esdihumboldt.hale.common.align.transformation.function.InstanceHandler#partitionInstances(eu.esdihumboldt.hale.common.instance.model.InstanceCollection,
 *      java.lang.String,
 *      eu.esdihumboldt.hale.common.align.transformation.engine.TransformationEngine,
 *      com.google.common.collect.ListMultimap, java.util.Map,
 *      eu.esdihumboldt.hale.common.align.transformation.report.TransformationLog)
 */
@Override
public ResourceIterator<FamilyInstance> partitionInstances(InstanceCollection instances, String transformationIdentifier, TransformationEngine engine, ListMultimap<String, ParameterValue> transformationParameters, Map<String, String> executionParameters, TransformationLog log) throws TransformationException {
    if (transformationParameters == null || !transformationParameters.containsKey(PARAMETER_JOIN) || transformationParameters.get(PARAMETER_JOIN).isEmpty()) {
        throw new TransformationException("No join parameter defined");
    }
    JoinParameter joinParameter = transformationParameters.get(PARAMETER_JOIN).get(0).as(JoinParameter.class);
    String validation = joinParameter.validate();
    if (validation != null)
        throw new TransformationException("Join parameter invalid: " + validation);
    List<TypeEntityDefinition> types = joinParameter.getTypes();
    JoinDefinition joinDefinition = JoinUtil.getJoinDefinition(joinParameter);
    // JoinProperty -> (Value -> Collection<Reference>)
    Map<PropertyEntityDefinition, Multimap<Object, InstanceReference>> index = new HashMap<>();
    for (PropertyEntityDefinition property : joinDefinition.properties.values()) index.put(property, ArrayListMultimap.<Object, InstanceReference>create());
    // remember instances of first type to start join afterwards
    Collection<InstanceReference> startInstances = new LinkedList<InstanceReference>();
    // iterate once over all instances
    ResourceIterator<Instance> iterator = instances.iterator();
    try {
        while (iterator.hasNext()) {
            Instance next = iterator.next();
            // remember instances of first type
            if (next.getDefinition().equals(types.get(0).getDefinition())) {
                startInstances.add(instances.getReference(next));
            }
            // fill index over needed properties
            for (PropertyEntityDefinition property : joinDefinition.properties.get(next.getDefinition())) {
                // XXX what about null? for now ignore null values
                // XXX how to treat multiple values? must all be equal (in
                // order?) or only one?
                Collection<Object> values = AlignmentUtil.getValues(next, property, true);
                if (values != null && !values.isEmpty()) {
                    // XXX take only first value for now
                    index.get(property).put(valueProcessor.processValue(values.iterator().next(), property), instances.getReference(next));
                }
            }
        }
    } finally {
        iterator.close();
    }
    return new JoinIterator(instances, startInstances, joinDefinition.directParent, index, joinDefinition.joinTable, valueProcessor);
}
Also used : TransformationException(eu.esdihumboldt.hale.common.align.transformation.function.TransformationException) HashMap(java.util.HashMap) FamilyInstance(eu.esdihumboldt.hale.common.instance.model.FamilyInstance) Instance(eu.esdihumboldt.hale.common.instance.model.Instance) JoinParameter(eu.esdihumboldt.hale.common.align.model.functions.join.JoinParameter) LinkedList(java.util.LinkedList) ArrayListMultimap(com.google.common.collect.ArrayListMultimap) ListMultimap(com.google.common.collect.ListMultimap) Multimap(com.google.common.collect.Multimap) TypeEntityDefinition(eu.esdihumboldt.hale.common.align.model.impl.TypeEntityDefinition) PropertyEntityDefinition(eu.esdihumboldt.hale.common.align.model.impl.PropertyEntityDefinition) JoinDefinition(eu.esdihumboldt.cst.functions.core.join.JoinUtil.JoinDefinition) InstanceReference(eu.esdihumboldt.hale.common.instance.model.InstanceReference)

Example 19 with TransformationException

use of eu.esdihumboldt.hale.common.align.transformation.function.TransformationException in project hale by halestudio.

the class IndexMergeHandler method partitionInstances.

/**
 * @see eu.esdihumboldt.cst.functions.core.merge.AbstractMergeHandler#partitionInstances(eu.esdihumboldt.hale.common.instance.model.InstanceCollection,
 *      java.lang.String,
 *      eu.esdihumboldt.hale.common.align.transformation.engine.TransformationEngine,
 *      com.google.common.collect.ListMultimap, java.util.Map,
 *      eu.esdihumboldt.hale.common.align.transformation.report.TransformationLog)
 */
@Override
public ResourceIterator<FamilyInstance> partitionInstances(InstanceCollection instances, String transformationIdentifier, TransformationEngine engine, ListMultimap<String, ParameterValue> transformationParameters, Map<String, String> executionParameters, TransformationLog log) throws TransformationException {
    PropertiesMergeHandler fallbackHandler = new PropertiesMergeHandler();
    InstanceIndexService indexService = serviceProvider.getService(InstanceIndexService.class);
    if (indexService == null) {
        log.warn(MessageFormat.format("Index service not available, falling back to merge handler {0}", fallbackHandler.getClass().getCanonicalName()));
        return fallbackHandler.partitionInstances(instances, transformationIdentifier, engine, transformationParameters, executionParameters, log);
    }
    final IndexMergeConfig mergeConfig = createMergeConfiguration(transformationParameters);
    QName typeName;
    try (ResourceIterator<Instance> it = instances.iterator()) {
        if (it.hasNext()) {
            typeName = it.next().getDefinition().getName();
        } else {
            // Nothing to partition
            return new ResourceIterator<FamilyInstance>() {

                @Override
                public boolean hasNext() {
                    return false;
                }

                @Override
                public FamilyInstance next() {
                    return null;
                }

                @Override
                public void close() {
                // Do nothing
                }
            };
        }
    }
    // Querying the index will yield a result over all instances. We must,
    // however, be able to operate only on the given input instances instead
    // of all instances.
    // We must, therefore, be able to uniquely identify every instance in
    // the index, so that we can retain from the index query only the
    // relevant instances.
    List<Object> inputInstanceIds = new ArrayList<>();
    try (ResourceIterator<Instance> it = instances.iterator()) {
        while (it.hasNext()) {
            Instance i = InstanceDecorator.getRoot(it.next());
            if (!Identifiable.is(i)) {
                log.warn(MessageFormat.format("At least one instance does not have an ID, falling back to merge handler {0}", fallbackHandler.getClass().getCanonicalName()));
                return fallbackHandler.partitionInstances(instances, transformationIdentifier, engine, transformationParameters, executionParameters, log);
            }
            inputInstanceIds.add(Identifiable.getId(i));
        }
    }
    Collection<Collection<ResolvableInstanceReference>> partitionedIndex = indexService.groupBy(typeName, mergeConfig.keyProperties);
    // Remove instance groups from the partitioned index where none of the
    // instances in the group are in the processed instances.
    partitionedIndex.removeIf(part -> !part.stream().map(ref -> ref.getId()).anyMatch(id -> inputInstanceIds.contains(id)));
    Iterator<Collection<ResolvableInstanceReference>> it = partitionedIndex.iterator();
    return new ResourceIterator<FamilyInstance>() {

        @Override
        public boolean hasNext() {
            return it.hasNext();
        }

        @Override
        public FamilyInstance next() {
            Collection<ResolvableInstanceReference> instanceRefs = it.next();
            InstanceCollection instancesToBeMerged = new DefaultInstanceCollection(instanceRefs.stream().map(ref -> ref.resolve()).collect(Collectors.toList()));
            return new FamilyInstanceImpl(merge(instancesToBeMerged, mergeConfig));
        }

        @Override
        public void close() {
        // TODO Auto-generated method stub
        }
    };
}
Also used : MergeUtil(eu.esdihumboldt.hale.common.align.model.functions.merge.MergeUtil) ListMultimap(com.google.common.collect.ListMultimap) ServiceProviderAware(eu.esdihumboldt.hale.common.core.service.ServiceProviderAware) ResolvableInstanceReference(eu.esdihumboldt.hale.common.instance.model.ResolvableInstanceReference) InstanceFactory(eu.esdihumboldt.hale.common.instance.model.InstanceFactory) InstanceIndexService(eu.esdihumboldt.hale.common.instance.index.InstanceIndexService) FamilyInstance(eu.esdihumboldt.hale.common.instance.model.FamilyInstance) MessageFormat(java.text.MessageFormat) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Identifiable(eu.esdihumboldt.hale.common.instance.model.Identifiable) MutableInstance(eu.esdihumboldt.hale.common.instance.model.MutableInstance) Map(java.util.Map) Instance(eu.esdihumboldt.hale.common.instance.model.Instance) TransformationException(eu.esdihumboldt.hale.common.align.transformation.function.TransformationException) InstanceDecorator(eu.esdihumboldt.hale.common.instance.model.impl.InstanceDecorator) InstanceHandler(eu.esdihumboldt.hale.common.align.transformation.function.InstanceHandler) DeepIterableKey(eu.esdihumboldt.hale.common.instance.index.DeepIterableKey) Iterator(java.util.Iterator) MergeFunction(eu.esdihumboldt.hale.common.align.model.functions.MergeFunction) Collection(java.util.Collection) TransformationLog(eu.esdihumboldt.hale.common.align.transformation.report.TransformationLog) Set(java.util.Set) TransformationEngine(eu.esdihumboldt.hale.common.align.transformation.engine.TransformationEngine) ServiceProvider(eu.esdihumboldt.hale.common.core.service.ServiceProvider) ParameterValue(eu.esdihumboldt.hale.common.align.model.ParameterValue) DefaultInstanceCollection(eu.esdihumboldt.hale.common.instance.model.impl.DefaultInstanceCollection) Collectors(java.util.stream.Collectors) InstanceMetadata(eu.esdihumboldt.hale.common.instance.model.InstanceMetadata) InstanceCollection(eu.esdihumboldt.hale.common.instance.model.InstanceCollection) FamilyInstanceImpl(eu.esdihumboldt.hale.common.align.transformation.function.impl.FamilyInstanceImpl) HalePlatform(eu.esdihumboldt.hale.common.core.HalePlatform) List(java.util.List) ResourceIterator(eu.esdihumboldt.hale.common.instance.model.ResourceIterator) QName(javax.xml.namespace.QName) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition) FamilyInstance(eu.esdihumboldt.hale.common.instance.model.FamilyInstance) MutableInstance(eu.esdihumboldt.hale.common.instance.model.MutableInstance) Instance(eu.esdihumboldt.hale.common.instance.model.Instance) QName(javax.xml.namespace.QName) DefaultInstanceCollection(eu.esdihumboldt.hale.common.instance.model.impl.DefaultInstanceCollection) InstanceCollection(eu.esdihumboldt.hale.common.instance.model.InstanceCollection) ArrayList(java.util.ArrayList) DefaultInstanceCollection(eu.esdihumboldt.hale.common.instance.model.impl.DefaultInstanceCollection) FamilyInstanceImpl(eu.esdihumboldt.hale.common.align.transformation.function.impl.FamilyInstanceImpl) Collection(java.util.Collection) DefaultInstanceCollection(eu.esdihumboldt.hale.common.instance.model.impl.DefaultInstanceCollection) InstanceCollection(eu.esdihumboldt.hale.common.instance.model.InstanceCollection) ResourceIterator(eu.esdihumboldt.hale.common.instance.model.ResourceIterator) ResolvableInstanceReference(eu.esdihumboldt.hale.common.instance.model.ResolvableInstanceReference) InstanceIndexService(eu.esdihumboldt.hale.common.instance.index.InstanceIndexService)

Example 20 with TransformationException

use of eu.esdihumboldt.hale.common.align.transformation.function.TransformationException in project hale by halestudio.

the class CalculateLength method evaluate.

/**
 * @see eu.esdihumboldt.hale.common.align.transformation.function.impl.AbstractSingleTargetPropertyTransformation#evaluate(java.lang.String,
 *      eu.esdihumboldt.hale.common.align.transformation.engine.TransformationEngine,
 *      com.google.common.collect.ListMultimap, java.lang.String,
 *      eu.esdihumboldt.hale.common.align.model.impl.PropertyEntityDefinition,
 *      java.util.Map,
 *      eu.esdihumboldt.hale.common.align.transformation.report.TransformationLog)
 */
@Override
protected Object evaluate(String transformationIdentifier, TransformationEngine engine, ListMultimap<String, PropertyValue> variables, String resultName, PropertyEntityDefinition resultProperty, Map<String, String> executionParameters, TransformationLog log) throws TransformationException, NoResultException {
    // get input geometry
    PropertyValue input = variables.get(null).get(0);
    Object inputValue = input.getValue();
    // depth first traverser that on cancel continues traversal but w/o the
    // children of the current object
    InstanceTraverser traverser = new DepthFirstInstanceTraverser(true);
    GeometryFinder geoFind = new GeometryFinder(null);
    traverser.traverse(inputValue, geoFind);
    List<GeometryProperty<?>> geoms = geoFind.getGeometries();
    Geometry geom = null;
    if (geoms.size() > 1) {
        int length = 0;
        for (GeometryProperty<?> geoProp : geoms) {
            length += geoProp.getGeometry().getLength();
        }
        return length;
    } else {
        geom = geoms.get(0).getGeometry();
    }
    if (geom != null) {
        return geom.getLength();
    } else {
        throw new TransformationException("Geometry for calculate length could not be retrieved.");
    }
}
Also used : Geometry(com.vividsolutions.jts.geom.Geometry) DepthFirstInstanceTraverser(eu.esdihumboldt.hale.common.instance.helper.DepthFirstInstanceTraverser) InstanceTraverser(eu.esdihumboldt.hale.common.instance.helper.InstanceTraverser) GeometryProperty(eu.esdihumboldt.hale.common.schema.geometry.GeometryProperty) TransformationException(eu.esdihumboldt.hale.common.align.transformation.function.TransformationException) GeometryFinder(eu.esdihumboldt.hale.common.instance.geometry.GeometryFinder) PropertyValue(eu.esdihumboldt.hale.common.align.transformation.function.PropertyValue) DepthFirstInstanceTraverser(eu.esdihumboldt.hale.common.instance.helper.DepthFirstInstanceTraverser)

Aggregations

TransformationException (eu.esdihumboldt.hale.common.align.transformation.function.TransformationException)30 NoResultException (eu.esdihumboldt.hale.common.align.transformation.function.impl.NoResultException)11 PropertyValue (eu.esdihumboldt.hale.common.align.transformation.function.PropertyValue)9 Geometry (com.vividsolutions.jts.geom.Geometry)8 Instance (eu.esdihumboldt.hale.common.instance.model.Instance)8 ParameterValue (eu.esdihumboldt.hale.common.align.model.ParameterValue)7 GeometryFinder (eu.esdihumboldt.hale.common.instance.geometry.GeometryFinder)7 DepthFirstInstanceTraverser (eu.esdihumboldt.hale.common.instance.helper.DepthFirstInstanceTraverser)7 InstanceTraverser (eu.esdihumboldt.hale.common.instance.helper.InstanceTraverser)7 TypeDefinition (eu.esdihumboldt.hale.common.schema.model.TypeDefinition)6 GeometryProperty (eu.esdihumboldt.hale.common.schema.geometry.GeometryProperty)5 TypeEntityDefinition (eu.esdihumboldt.hale.common.align.model.impl.TypeEntityDefinition)4 DefaultGeometryProperty (eu.esdihumboldt.hale.common.instance.geometry.DefaultGeometryProperty)4 InstanceBuilder (eu.esdihumboldt.hale.common.instance.groovy.InstanceBuilder)4 FamilyInstance (eu.esdihumboldt.hale.common.instance.model.FamilyInstance)4 MutableInstance (eu.esdihumboldt.hale.common.instance.model.MutableInstance)4 CRSDefinition (eu.esdihumboldt.hale.common.schema.geometry.CRSDefinition)4 GroovyService (eu.esdihumboldt.util.groovy.sandbox.GroovyService)4 Binding (groovy.lang.Binding)4 Script (groovy.lang.Script)4