Search in sources :

Example 1 with MutableInstance

use of eu.esdihumboldt.hale.common.instance.model.MutableInstance in project hale by halestudio.

the class XLSInstanceReader method createInstanceCollection.

/**
 * create instances, see
 * {@link CSVInstanceReader#execute(ProgressIndicator, IOReporter)}
 *
 * @param row the current row
 * @param reporter the reporter of the writer
 * @param solveNestedProperties true, if schema should not be flat <b>(not
 *            implemented yet)</b>
 */
@SuppressWarnings("javadoc")
private void createInstanceCollection(List<String> row, IOReporter reporter) {
    MutableInstance instance = new DefaultInstance(type, null);
    // int propertyIndex = 0;
    for (int index = 0; index < propAr.length; index++) {
        String part = null;
        if (index < row.size())
            part = row.get(index);
        if (part != null) {
            PropertyDefinition property = propAr[index];
            if (part.isEmpty()) {
                // FIXME make this configurable
                part = null;
            }
            Object value = part;
            if (value != null) {
                Binding binding = property.getPropertyType().getConstraint(Binding.class);
                try {
                    if (!binding.getBinding().equals(String.class)) {
                        ConversionService conversionService = HalePlatform.getService(ConversionService.class);
                        if (conversionService.canConvert(String.class, binding.getBinding())) {
                            value = conversionService.convert(part, binding.getBinding());
                        } else {
                            throw new IllegalStateException("Conversion not possible!");
                        }
                    }
                } catch (Exception e) {
                    reporter.error(new IOMessageImpl("Cannot convert property value to {0}", e, line, -1, binding.getBinding().getSimpleName()));
                }
                instance.addProperty(property.getName(), value);
            }
        // propertyIndex++;
        }
    }
    instances.add(instance);
}
Also used : Binding(eu.esdihumboldt.hale.common.schema.model.constraint.type.Binding) ConversionService(org.springframework.core.convert.ConversionService) DefaultInstance(eu.esdihumboldt.hale.common.instance.model.impl.DefaultInstance) MutableInstance(eu.esdihumboldt.hale.common.instance.model.MutableInstance) IOMessageImpl(eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl) PropertyDefinition(eu.esdihumboldt.hale.common.schema.model.PropertyDefinition) IOProviderConfigurationException(eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException) IOException(java.io.IOException)

Example 2 with MutableInstance

use of eu.esdihumboldt.hale.common.instance.model.MutableInstance in project hale by halestudio.

the class Retype method execute.

@Override
public void execute(String transformationIdentifier, TransformationEngine engine, Map<String, String> executionParameters, TransformationLog log, Cell cell) {
    // for each source instance create a target instance
    TypeDefinition targetType = getTarget().values().iterator().next().getDefinition().getDefinition();
    MutableInstance target = null;
    // structural rename
    boolean structuralRename = getOptionalParameter(RenameFunction.PARAMETER_STRUCTURAL_RENAME, Value.of(false)).as(Boolean.class, false);
    if (structuralRename) {
        boolean ignoreNamespaces = getOptionalParameter(RenameFunction.PARAMETER_IGNORE_NAMESPACES, Value.of(false)).as(Boolean.class, false);
        boolean copyGeometries = getOptionalParameter(RenameFunction.PARAMETER_COPY_GEOMETRIES, Value.of(true)).as(Boolean.class);
        target = doStructuralRename(getSource(), targetType, ignoreNamespaces, copyGeometries, log);
    }
    if (target == null) {
        target = getInstanceFactory().createInstance(targetType);
    }
    getPropertyTransformer().publish(getSource(), target, log, cell);
}
Also used : MutableInstance(eu.esdihumboldt.hale.common.instance.model.MutableInstance) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition) DefaultTypeDefinition(eu.esdihumboldt.hale.common.schema.model.impl.DefaultTypeDefinition)

Example 3 with MutableInstance

use of eu.esdihumboldt.hale.common.instance.model.MutableInstance in project hale by halestudio.

the class IndexMergeHandler method merge.

private Instance merge(InstanceCollection instances, IndexMergeConfig mergeConfig) {
    TypeDefinition type;
    try (ResourceIterator<Instance> it = instances.iterator()) {
        if (instances.hasSize() && instances.size() == 1) {
            // early exit if only one instance to merge
            return it.next();
        } else {
            type = it.next().getDefinition();
        }
    }
    MutableInstance result = getInstanceFactory().createInstance(type);
    /*
		 * FIXME This a first VERY basic implementation, where only the first
		 * item in each property path is regarded, and that whole tree is added
		 * only once (from the first instance). XXX This especially will be a
		 * problem, if a path contains a choice. XXX For more advanced stuff we
		 * need more advanced test cases.
		 */
    Set<QName> rootNames = new HashSet<QName>();
    Set<QName> nonKeyRootNames = new HashSet<QName>();
    // collect path roots
    for (List<QName> path : mergeConfig.keyProperties) {
        rootNames.add(path.get(0));
    }
    for (List<QName> path : mergeConfig.additionalProperties) {
        nonKeyRootNames.add(path.get(0));
    }
    // XXX what about metadata?!
    // XXX for now only retain IDs
    Set<Object> ids = new HashSet<Object>();
    try (ResourceIterator<Instance> it = instances.iterator()) {
        while (it.hasNext()) {
            Instance instance = it.next();
            for (QName name : instance.getPropertyNames()) {
                if (rootNames.contains(name)) {
                    /*
						 * Property is merge key -> only use first occurrence
						 * (as all entries need to be the same)
						 * 
						 * TODO adapt if multiple keys are possible per instance
						 */
                    addFirstOccurrence(result, instance, name);
                } else if (nonKeyRootNames.contains(name)) {
                    /*
						 * Property is additional merge property.
						 * 
						 * Traditional behavior: Only keep unique values.
						 * 
						 * XXX should this be configurable?
						 */
                    addUnique(result, instance, name);
                } else if (mergeConfig.autoDetect) {
                    /*
						 * Auto-detection is enabled.
						 * 
						 * Only keep unique values.
						 * 
						 * XXX This differs from the traditional behavior in
						 * that there only the first value would be used, but
						 * only if all values were equal. That cannot be easily
						 * checked in an iterative approach.
						 */
                    addUnique(result, instance, name);
                } else {
                    /*
						 * Property is not to be merged.
						 * 
						 * XXX but we could do some kind of aggregation
						 * 
						 * XXX for now just add all values
						 */
                    addValues(result, instance, name);
                }
            }
            List<Object> instanceIDs = instance.getMetaData(InstanceMetadata.METADATA_ID);
            for (Object id : instanceIDs) {
                ids.add(id);
            }
        }
    }
    // store metadata IDs
    result.setMetaData(InstanceMetadata.METADATA_ID, ids.toArray());
    return result;
}
Also used : 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) MutableInstance(eu.esdihumboldt.hale.common.instance.model.MutableInstance) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition) HashSet(java.util.HashSet)

Example 4 with MutableInstance

use of eu.esdihumboldt.hale.common.instance.model.MutableInstance in project hale by halestudio.

the class GroovyRetype method execute.

@Override
public void execute(String transformationIdentifier, TransformationEngine engine, Map<String, String> executionParameters, TransformationLog log, Cell cell) throws TransformationException {
    // for each source instance create a target instance
    TypeDefinition targetType = getTarget().values().iterator().next().getDefinition().getDefinition();
    InstanceBuilder builder = new InstanceBuilder(false);
    Binding binding = createBinding(getSource(), cell, builder, log, getExecutionContext(), targetType);
    try {
        GroovyService service = getExecutionContext().getService(GroovyService.class);
        Script script = GroovyUtil.getScript(this, binding, service);
        Iterable<MutableInstance> targets = GroovyUtil.evaluateAll(script, builder, targetType, service, log);
        for (MutableInstance target : targets) {
            getPropertyTransformer().publish(getSource(), target, log, cell);
        }
    } catch (TransformationException e) {
        throw e;
    } catch (NoResultException e) {
        log.info(log.createMessage("Skipping target instance because received NoResultException from script", null));
    } catch (Exception e) {
        throw new TransformationException(e.getMessage(), e);
    }
}
Also used : Binding(groovy.lang.Binding) Script(groovy.lang.Script) TransformationException(eu.esdihumboldt.hale.common.align.transformation.function.TransformationException) MutableInstance(eu.esdihumboldt.hale.common.instance.model.MutableInstance) NoResultException(eu.esdihumboldt.hale.common.align.transformation.function.impl.NoResultException) GroovyService(eu.esdihumboldt.util.groovy.sandbox.GroovyService) TransformationException(eu.esdihumboldt.hale.common.align.transformation.function.TransformationException) NoResultException(eu.esdihumboldt.hale.common.align.transformation.function.impl.NoResultException) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition) InstanceBuilder(eu.esdihumboldt.hale.common.instance.groovy.InstanceBuilder)

Example 5 with MutableInstance

use of eu.esdihumboldt.hale.common.instance.model.MutableInstance in project hale by halestudio.

the class GroovyUtil method evaluate.

/**
 * Evaluate a Groovy type script.
 *
 * @param script the script
 * @param builder the instance builder
 * @param type the type of the instance to create
 * @param service the Groovy service
 * @param log the log
 * @return the created instance
 * @throws TransformationException if the target binding does not contain
 *             exactly one result after script evaluation or an internal
 *             error occurs
 * @throws NoResultException if the script implies that no result should be
 *             created
 */
public static MutableInstance evaluate(Script script, final InstanceBuilder builder, final TypeDefinition type, GroovyService service, SimpleLog log) throws TransformationException, NoResultException {
    Iterable<MutableInstance> results = evaluateAll(script, builder, type, service, log);
    Iterator<MutableInstance> it = results.iterator();
    MutableInstance result;
    if (it.hasNext()) {
        result = it.next();
        if (it.hasNext()) {
            throw new TransformationException("Cell script does not produce exactly one result.");
        }
    } else {
        throw new NoResultException();
    }
    return result;
}
Also used : TransformationException(eu.esdihumboldt.hale.common.align.transformation.function.TransformationException) MutableInstance(eu.esdihumboldt.hale.common.instance.model.MutableInstance) NoResultException(eu.esdihumboldt.hale.common.align.transformation.function.impl.NoResultException)

Aggregations

MutableInstance (eu.esdihumboldt.hale.common.instance.model.MutableInstance)22 DefaultInstance (eu.esdihumboldt.hale.common.instance.model.impl.DefaultInstance)10 Instance (eu.esdihumboldt.hale.common.instance.model.Instance)9 QName (javax.xml.namespace.QName)8 TypeDefinition (eu.esdihumboldt.hale.common.schema.model.TypeDefinition)7 DefaultTypeDefinition (eu.esdihumboldt.hale.common.schema.model.impl.DefaultTypeDefinition)4 MultiValue (eu.esdihumboldt.cst.MultiValue)3 TransformationException (eu.esdihumboldt.hale.common.align.transformation.function.TransformationException)3 Group (eu.esdihumboldt.hale.common.instance.model.Group)3 MutableGroup (eu.esdihumboldt.hale.common.instance.model.MutableGroup)3 DefaultGroup (eu.esdihumboldt.hale.common.instance.model.impl.DefaultGroup)3 DefaultPropertyDefinition (eu.esdihumboldt.hale.common.schema.model.impl.DefaultPropertyDefinition)3 Geometry (com.vividsolutions.jts.geom.Geometry)2 NoResultException (eu.esdihumboldt.hale.common.align.transformation.function.impl.NoResultException)2 FamilyInstance (eu.esdihumboldt.hale.common.instance.model.FamilyInstance)2 Filter (eu.esdihumboldt.hale.common.instance.model.Filter)2 InstanceCollection (eu.esdihumboldt.hale.common.instance.model.InstanceCollection)2 GeometryProperty (eu.esdihumboldt.hale.common.schema.geometry.GeometryProperty)2 PropertyDefinition (eu.esdihumboldt.hale.common.schema.model.PropertyDefinition)2 HasValueFlag (eu.esdihumboldt.hale.common.schema.model.constraint.type.HasValueFlag)2