Search in sources :

Example 1 with TransformationLog

use of eu.esdihumboldt.hale.common.align.transformation.report.TransformationLog in project hale by halestudio.

the class TransformationTreeContentProvider method createInstanceTree.

/**
 * Create a transformation tree based on a source instance.
 *
 * @param instance the source instance
 * @param typeCell the type cell
 * @param alignment the alignment
 * @return the transformation tree or <code>null</code>
 */
private TransformationTree createInstanceTree(Instance instance, Cell typeCell, Alignment alignment) {
    TransformationTree tree = new TransformationTreeImpl(alignment, typeCell);
    ReportLog<TransformationMessage> reporter = new DefaultTransformationReporter("Transformation tree", true);
    TransformationLog log = new CellLog(reporter, typeCell);
    // context matching
    // XXX instead through service/extension point?
    ContextMatcher matcher = new AsDeepAsPossible(null);
    matcher.findMatches(tree);
    // process and annotate the tree
    InstanceVisitor visitor = new InstanceVisitor(new FamilyInstanceImpl(instance), tree, log);
    tree.accept(visitor);
    // duplicate subtree as necessary
    DuplicationVisitor duplicationVisitor = new DuplicationVisitor(tree, log);
    tree.accept(duplicationVisitor);
    duplicationVisitor.doAugmentationTrackback();
    return tree;
}
Also used : TransformationTreeImpl(eu.esdihumboldt.hale.common.align.model.transformation.tree.impl.TransformationTreeImpl) TransformationMessage(eu.esdihumboldt.hale.common.align.transformation.report.TransformationMessage) FamilyInstanceImpl(eu.esdihumboldt.hale.common.align.transformation.function.impl.FamilyInstanceImpl) AsDeepAsPossible(eu.esdihumboldt.hale.common.align.model.transformation.tree.context.impl.matcher.AsDeepAsPossible) DefaultTransformationReporter(eu.esdihumboldt.hale.common.align.transformation.report.impl.DefaultTransformationReporter) TransformationTree(eu.esdihumboldt.hale.common.align.model.transformation.tree.TransformationTree) TransformationLog(eu.esdihumboldt.hale.common.align.transformation.report.TransformationLog) CellLog(eu.esdihumboldt.hale.common.align.transformation.report.impl.CellLog) ContextMatcher(eu.esdihumboldt.hale.common.align.model.transformation.tree.context.ContextMatcher) InstanceVisitor(eu.esdihumboldt.hale.common.align.model.transformation.tree.visitor.InstanceVisitor) DuplicationVisitor(eu.esdihumboldt.hale.common.align.model.transformation.tree.visitor.DuplicationVisitor)

Example 2 with TransformationLog

use of eu.esdihumboldt.hale.common.align.transformation.report.TransformationLog in project hale by halestudio.

the class FunctionExecutor method executeTransformation.

/**
 * Execute a property transformation.
 *
 * @param transformation the transformation factory
 * @param cell the alignment cell
 * @param sources the named source entities and nodes
 * @param targets the named target entities and nodes
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
protected void executeTransformation(PropertyTransformationFactory transformation, Cell cell, ListMultimap<String, Pair<SourceNode, Entity>> sources, ListMultimap<String, Pair<TargetNode, Entity>> targets) {
    TransformationLog cellLog = new CellLog(reporter, cell);
    PropertyTransformation<?> function;
    try {
        // TODO cache function objects?
        function = transformation.createExtensionObject();
    } catch (Exception e) {
        cellLog.error(cellLog.createMessage("Error creating transformation function.", e));
        return;
    }
    TransformationEngine engine = engines.get(transformation.getEngineId(), cellLog);
    if (engine == null) {
        // TODO instead try another transformation
        cellLog.error(cellLog.createMessage("Skipping property transformation: No matching transformation engine found", null));
        return;
    }
    // configure function
    // set expected result
    ListMultimap<String, PropertyEntityDefinition> expectedResult = ArrayListMultimap.create(targets.keySet().size(), 1);
    for (Entry<String, Pair<TargetNode, Entity>> targetEntry : targets.entries()) {
        EntityDefinition def = targetEntry.getValue().getSecond().getDefinition();
        expectedResult.put(targetEntry.getKey(), toPropertyEntityDefinition(def));
    }
    function.setExpectedResult(expectedResult);
    // set source variables
    ListMultimap<String, PropertyValue> variables = ArrayListMultimap.create();
    for (Entry<String, Pair<SourceNode, Entity>> sourceEntry : sources.entries()) {
        EntityDefinition def = sourceEntry.getValue().getSecond().getDefinition();
        SourceNode sourceNode = sourceEntry.getValue().getFirst();
        if (TransformationTreeUtil.isEager(cell, sourceNode, cellLog, context.getServiceProvider())) {
            // eager source - all values
            Object[] values = sourceNode.getAllValues();
            if (values != null) {
                for (int i = 0; i < values.length; i++) {
                    PropertyValue propertyValue = new PropertyValueImpl(values[i], toPropertyEntityDefinition(def));
                    variables.put(sourceEntry.getKey(), propertyValue);
                }
            }
        } else {
            // non-eager source - one value
            Object value = sourceNode.getValue();
            PropertyValue propertyValue = new PropertyValueImpl(value, toPropertyEntityDefinition(def));
            variables.put(sourceEntry.getKey(), propertyValue);
        }
    }
    function.setVariables(variables);
    // set parameters
    function.setParameters(cell.getTransformationParameters());
    // set context
    function.setExecutionContext(context.getCellContext(cell));
    // set target type
    TypeDefinition targetType = null;
    if (!targets.isEmpty()) {
        TargetNode target = targets.values().iterator().next().getFirst();
        targetType = target.getEntityDefinition().getType();
    }
    function.setTargetType(targetType);
    function.setTypeCell(typeCell.get());
    // execute function
    try {
        ((PropertyTransformation) function).execute(transformation.getIdentifier(), engine, transformation.getExecutionParameters(), cellLog, cell);
    } catch (Throwable e) {
        // TODO instead try another transformation?
        cellLog.error(cellLog.createMessage("Skipping property transformation: Executing property transformation failed.", e));
        return;
    }
    // apply function results
    ListMultimap<String, Object> results = function.getResults();
    if (results != null) {
        for (String name : results.keySet()) {
            List<Object> values = results.get(name);
            List<Pair<TargetNode, Entity>> nodes = targets.get(name);
            if (nodes.size() > values.size()) {
                cellLog.warn(cellLog.createMessage(MessageFormat.format("Transformation result misses values for result with name {0}", name), null));
            }
            if (values.size() > nodes.size()) {
                cellLog.warn(cellLog.createMessage(MessageFormat.format("More transformation results than target nodes for result with name {0}", name), null));
            }
            int count = Math.min(values.size(), nodes.size());
            // node...
            for (int i = 0; i < count; i++) {
                Object value = values.get(i);
                TargetNode node = nodes.get(i).getFirst();
                if (value instanceof MultiValue) {
                    MultiValue originalValue = (MultiValue) value;
                    MultiValue processedValue = new MultiValue(originalValue.size());
                    for (Object o : originalValue) {
                        processedValue.add(processValue(cellLog, function, o, node));
                    }
                    value = processedValue;
                } else {
                    value = processValue(cellLog, function, value, node);
                }
                /*
					 * TODO
					 * 
					 * set node value only if no result has already been set. If
					 * a value is already there and we are in a lower priority
					 * executor, we do not overwrite.
					 */
                if (!node.isDefined()) {
                    node.setResult(value);
                }
            }
        }
    }
}
Also used : TargetNode(eu.esdihumboldt.hale.common.align.model.transformation.tree.TargetNode) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition) SourceNode(eu.esdihumboldt.hale.common.align.model.transformation.tree.SourceNode) PropertyTransformation(eu.esdihumboldt.hale.common.align.transformation.function.PropertyTransformation) CellLog(eu.esdihumboldt.hale.common.align.transformation.report.impl.CellLog) MultiValue(eu.esdihumboldt.cst.MultiValue) Pair(eu.esdihumboldt.util.Pair) PropertyValueImpl(eu.esdihumboldt.hale.common.align.transformation.function.impl.PropertyValueImpl) PropertyValue(eu.esdihumboldt.hale.common.align.transformation.function.PropertyValue) ConversionException(org.springframework.core.convert.ConversionException) TransformationEngine(eu.esdihumboldt.hale.common.align.transformation.engine.TransformationEngine) PropertyEntityDefinition(eu.esdihumboldt.hale.common.align.model.impl.PropertyEntityDefinition) EntityDefinition(eu.esdihumboldt.hale.common.align.model.EntityDefinition) PropertyEntityDefinition(eu.esdihumboldt.hale.common.align.model.impl.PropertyEntityDefinition) TransformationLog(eu.esdihumboldt.hale.common.align.transformation.report.TransformationLog)

Example 3 with TransformationLog

use of eu.esdihumboldt.hale.common.align.transformation.report.TransformationLog 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 4 with TransformationLog

use of eu.esdihumboldt.hale.common.align.transformation.report.TransformationLog in project hale by halestudio.

the class ConceptualSchemaTransformer method doTypeTransformation.

/**
 * Execute a type transformation based on single type cell
 *
 * @param transformation the transformation to use
 * @param typeCell the type cell
 * @param target the target instance sink
 * @param source the source instances
 * @param alignment the alignment
 * @param engines the engine manager
 * @param transformer the property transformer
 * @param context the transformation execution context
 * @param reporter the reporter
 * @param progressIndicator the progress indicator
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
protected void doTypeTransformation(TypeTransformationFactory transformation, Cell typeCell, InstanceCollection source, InstanceSink target, Alignment alignment, EngineManager engines, PropertyTransformer transformer, TransformationContext context, TransformationReporter reporter, ProgressIndicator progressIndicator) {
    TransformationLog cellLog = new CellLog(reporter, typeCell);
    TypeTransformation<?> function;
    try {
        function = transformation.createExtensionObject();
    } catch (Exception e) {
        reporter.error(new TransformationMessageImpl(typeCell, "Error creating transformation function.", e));
        return;
    }
    TransformationEngine engine = engines.get(transformation.getEngineId(), cellLog);
    if (engine == null) {
        // TODO instead try another transformation
        cellLog.error(cellLog.createMessage("Skipping type transformation: No matching transformation engine found", null));
        return;
    }
    // prepare transformation configuration
    ListMultimap<String, Type> targetTypes = ArrayListMultimap.create();
    for (Entry<String, ? extends Entity> entry : typeCell.getTarget().entries()) {
        targetTypes.put(entry.getKey(), (Type) entry.getValue());
    }
    ListMultimap<String, ParameterValue> parameters = typeCell.getTransformationParameters();
    if (parameters != null) {
        parameters = Multimaps.unmodifiableListMultimap(parameters);
    }
    Map<String, String> executionParameters = transformation.getExecutionParameters();
    // break on cancel
    if (progressIndicator.isCanceled()) {
        return;
    }
    ResourceIterator<FamilyInstance> iterator;
    if (typeCell.getSource() == null || typeCell.getSource().isEmpty()) {
        // type cell w/o source
        // -> execute exactly once w/ null source
        source = null;
        iterator = new GenericResourceIteratorAdapter<Object, FamilyInstance>(Collections.singleton(null).iterator()) {

            @Override
            protected FamilyInstance convert(Object next) {
                return null;
            }
        };
    } else {
        // Step 1: selection
        // Select only instances that are relevant for the transformation.
        source = source.select(new TypeCellFilter(typeCell));
        // Step 2: partition
        // use InstanceHandler if available - for example merge or join
        function.setExecutionContext(context.getCellContext(typeCell));
        InstanceHandler instanceHandler = function.getInstanceHandler();
        if (instanceHandler != null) {
            injectTransformationContext(instanceHandler, context);
            progressIndicator.setCurrentTask("Perform instance partitioning");
            try {
                iterator = instanceHandler.partitionInstances(source, transformation.getFunctionId(), engine, parameters, executionParameters, cellLog);
            } catch (TransformationException e) {
                cellLog.error(cellLog.createMessage("Type transformation: partitioning failed", e));
                return;
            }
        } else {
            // else just use every instance as is
            iterator = new GenericResourceIteratorAdapter<Instance, FamilyInstance>(source.iterator()) {

                @Override
                protected FamilyInstance convert(Instance next) {
                    return new FamilyInstanceImpl(next);
                }
            };
        }
    }
    progressIndicator.setCurrentTask("Execute type transformations");
    try {
        while (iterator.hasNext()) {
            // break on cancel
            if (progressIndicator.isCanceled()) {
                return;
            }
            function.setSource(iterator.next());
            function.setPropertyTransformer(transformer);
            function.setParameters(parameters);
            function.setTarget(targetTypes);
            function.setExecutionContext(context.getCellContext(typeCell));
            try {
                ((TypeTransformation) function).execute(transformation.getFunctionId(), engine, executionParameters, cellLog, typeCell);
            } catch (TransformationException e) {
                cellLog.error(cellLog.createMessage("Type transformation failed, skipping instance.", e));
            }
        }
    } finally {
        iterator.close();
    }
}
Also used : FamilyInstance(eu.esdihumboldt.hale.common.instance.model.FamilyInstance) Instance(eu.esdihumboldt.hale.common.instance.model.Instance) TransformationMessageImpl(eu.esdihumboldt.hale.common.align.transformation.report.impl.TransformationMessageImpl) FamilyInstanceImpl(eu.esdihumboldt.hale.common.align.transformation.function.impl.FamilyInstanceImpl) InstanceHandler(eu.esdihumboldt.hale.common.align.transformation.function.InstanceHandler) CellLog(eu.esdihumboldt.hale.common.align.transformation.report.impl.CellLog) 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) TransformationEngine(eu.esdihumboldt.hale.common.align.transformation.engine.TransformationEngine) Type(eu.esdihumboldt.hale.common.align.model.Type) TypeTransformation(eu.esdihumboldt.hale.common.align.transformation.function.TypeTransformation) FamilyInstance(eu.esdihumboldt.hale.common.instance.model.FamilyInstance) TransformationLog(eu.esdihumboldt.hale.common.align.transformation.report.TransformationLog)

Aggregations

TransformationLog (eu.esdihumboldt.hale.common.align.transformation.report.TransformationLog)4 TransformationEngine (eu.esdihumboldt.hale.common.align.transformation.engine.TransformationEngine)3 FamilyInstanceImpl (eu.esdihumboldt.hale.common.align.transformation.function.impl.FamilyInstanceImpl)3 CellLog (eu.esdihumboldt.hale.common.align.transformation.report.impl.CellLog)3 ParameterValue (eu.esdihumboldt.hale.common.align.model.ParameterValue)2 InstanceHandler (eu.esdihumboldt.hale.common.align.transformation.function.InstanceHandler)2 TransformationException (eu.esdihumboldt.hale.common.align.transformation.function.TransformationException)2 FamilyInstance (eu.esdihumboldt.hale.common.instance.model.FamilyInstance)2 Instance (eu.esdihumboldt.hale.common.instance.model.Instance)2 TypeDefinition (eu.esdihumboldt.hale.common.schema.model.TypeDefinition)2 ListMultimap (com.google.common.collect.ListMultimap)1 MultiValue (eu.esdihumboldt.cst.MultiValue)1 EntityDefinition (eu.esdihumboldt.hale.common.align.model.EntityDefinition)1 Type (eu.esdihumboldt.hale.common.align.model.Type)1 MergeFunction (eu.esdihumboldt.hale.common.align.model.functions.MergeFunction)1 MergeUtil (eu.esdihumboldt.hale.common.align.model.functions.merge.MergeUtil)1 PropertyEntityDefinition (eu.esdihumboldt.hale.common.align.model.impl.PropertyEntityDefinition)1 SourceNode (eu.esdihumboldt.hale.common.align.model.transformation.tree.SourceNode)1 TargetNode (eu.esdihumboldt.hale.common.align.model.transformation.tree.TargetNode)1 TransformationTree (eu.esdihumboldt.hale.common.align.model.transformation.tree.TransformationTree)1