Search in sources :

Example 6 with DefaultInstanceCollection

use of eu.esdihumboldt.hale.common.instance.model.impl.DefaultInstanceCollection 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 7 with DefaultInstanceCollection

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

the class InlineTransformation method evaluate.

@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 {
    List<PropertyValue> sources = variables.get(null);
    if (sources.isEmpty()) {
        throw new NoResultException("No source available to transform");
    }
    PropertyValue source = sources.get(0);
    Object sourceValue = source.getValue();
    if (sourceValue == null) {
        throw new NoResultException("Source value is null");
    }
    if (!(sourceValue instanceof Instance)) {
        throw new TransformationException("Sources for inline transformation must be instances");
    }
    Instance sourceInstance = (Instance) sourceValue;
    TypeDefinition sourceType = sourceInstance.getDefinition();
    // get the original alignment
    Alignment orgAlignment = getExecutionContext().getAlignment();
    MutableAlignment alignment = new DefaultAlignment(orgAlignment);
    // identify relevant type cell(s)
    MutableCell queryCell = new DefaultCell();
    ListMultimap<String, Type> sourceEntities = ArrayListMultimap.create();
    sourceEntities.put(null, new DefaultType(new TypeEntityDefinition(sourceType, SchemaSpaceID.SOURCE, null)));
    queryCell.setSource(sourceEntities);
    ListMultimap<String, Type> targetEntities = ArrayListMultimap.create();
    targetEntities.put(null, new DefaultType(new TypeEntityDefinition(resultProperty.getDefinition().getPropertyType(), SchemaSpaceID.TARGET, null)));
    queryCell.setTarget(targetEntities);
    Collection<? extends Cell> candidates = alignment.getTypeCells(queryCell);
    if (candidates.isEmpty()) {
        log.error(log.createMessage("No type transformations found for inline transformation", null));
        throw new NoResultException();
    }
    // filter alignment -> only keep relevant type relations
    List<Cell> allTypeCells = new ArrayList<>(alignment.getTypeCells());
    for (Cell cell : allTypeCells) {
        // remove cell
        alignment.removeCell(cell);
        if (!cell.getTransformationMode().equals(TransformationMode.disabled)) {
            // only readd if not disabled
            MutableCell copy = new DefaultCell(cell);
            if (candidates.contains(cell)) {
                // readd as active
                copy.setTransformationMode(TransformationMode.active);
            } else {
                // readd as passive
                copy.setTransformationMode(TransformationMode.passive);
            }
            alignment.addCell(copy);
        }
    }
    // prepare transformation input/output
    DefaultInstanceCollection sourceInstances = new DefaultInstanceCollection();
    sourceInstances.add(sourceInstance);
    DefaultInstanceSink target = new DefaultInstanceSink();
    // run transformation
    TransformationService ts = getExecutionContext().getService(TransformationService.class);
    if (ts == null) {
        throw new TransformationException("Transformation service not available for inline transformation");
    }
    ProgressIndicator progressIndicator = new LogProgressIndicator();
    TransformationReport report = ts.transform(alignment, sourceInstances, new ThreadSafeInstanceSink<InstanceSink>(target), getExecutionContext(), progressIndicator);
    // copy report messages
    log.importMessages(report);
    if (!report.isSuccess()) {
        // copy report messages
        log.importMessages(report);
        throw new TransformationException("Inline transformation failed");
    }
    // extract result
    List<Instance> targetList = target.getInstances();
    if (targetList.isEmpty()) {
        log.error(log.createMessage("Inline transformation yielded no result", null));
        throw new NoResultException("No result from inline transformation");
    }
    if (targetList.size() > 1) {
        log.error(log.createMessage("Inline transformation yielded multiple results, only first result is used", null));
    }
    return targetList.get(0);
}
Also used : MutableCell(eu.esdihumboldt.hale.common.align.model.MutableCell) Instance(eu.esdihumboldt.hale.common.instance.model.Instance) DefaultInstanceSink(eu.esdihumboldt.hale.common.align.transformation.service.impl.DefaultInstanceSink) ArrayList(java.util.ArrayList) NoResultException(eu.esdihumboldt.hale.common.align.transformation.function.impl.NoResultException) DefaultInstanceCollection(eu.esdihumboldt.hale.common.instance.model.impl.DefaultInstanceCollection) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition) Alignment(eu.esdihumboldt.hale.common.align.model.Alignment) MutableAlignment(eu.esdihumboldt.hale.common.align.model.MutableAlignment) DefaultAlignment(eu.esdihumboldt.hale.common.align.model.impl.DefaultAlignment) TypeEntityDefinition(eu.esdihumboldt.hale.common.align.model.impl.TypeEntityDefinition) LogProgressIndicator(eu.esdihumboldt.hale.common.core.io.impl.LogProgressIndicator) ProgressIndicator(eu.esdihumboldt.hale.common.core.io.ProgressIndicator) DefaultAlignment(eu.esdihumboldt.hale.common.align.model.impl.DefaultAlignment) TransformationService(eu.esdihumboldt.hale.common.align.transformation.service.TransformationService) Cell(eu.esdihumboldt.hale.common.align.model.Cell) DefaultCell(eu.esdihumboldt.hale.common.align.model.impl.DefaultCell) MutableCell(eu.esdihumboldt.hale.common.align.model.MutableCell) TransformationException(eu.esdihumboldt.hale.common.align.transformation.function.TransformationException) TransformationReport(eu.esdihumboldt.hale.common.align.transformation.report.TransformationReport) DefaultType(eu.esdihumboldt.hale.common.align.model.impl.DefaultType) PropertyValue(eu.esdihumboldt.hale.common.align.transformation.function.PropertyValue) MutableAlignment(eu.esdihumboldt.hale.common.align.model.MutableAlignment) LogProgressIndicator(eu.esdihumboldt.hale.common.core.io.impl.LogProgressIndicator) Type(eu.esdihumboldt.hale.common.align.model.Type) DefaultType(eu.esdihumboldt.hale.common.align.model.impl.DefaultType) DefaultCell(eu.esdihumboldt.hale.common.align.model.impl.DefaultCell) ThreadSafeInstanceSink(eu.esdihumboldt.hale.common.align.transformation.service.impl.ThreadSafeInstanceSink) DefaultInstanceSink(eu.esdihumboldt.hale.common.align.transformation.service.impl.DefaultInstanceSink) InstanceSink(eu.esdihumboldt.hale.common.align.transformation.service.InstanceSink)

Aggregations

DefaultInstanceCollection (eu.esdihumboldt.hale.common.instance.model.impl.DefaultInstanceCollection)7 Instance (eu.esdihumboldt.hale.common.instance.model.Instance)6 InstanceCollection (eu.esdihumboldt.hale.common.instance.model.InstanceCollection)5 ArrayList (java.util.ArrayList)4 List (java.util.List)3 TransformationException (eu.esdihumboldt.hale.common.align.transformation.function.TransformationException)2 IOProviderConfigurationException (eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException)2 MutableInstance (eu.esdihumboldt.hale.common.instance.model.MutableInstance)2 TypeDefinition (eu.esdihumboldt.hale.common.schema.model.TypeDefinition)2 QName (javax.xml.namespace.QName)2 ListMultimap (com.google.common.collect.ListMultimap)1 Geometry (com.vividsolutions.jts.geom.Geometry)1 MultiPoint (com.vividsolutions.jts.geom.MultiPoint)1 MultiPolygon (com.vividsolutions.jts.geom.MultiPolygon)1 Point (com.vividsolutions.jts.geom.Point)1 Polygon (com.vividsolutions.jts.geom.Polygon)1 Alignment (eu.esdihumboldt.hale.common.align.model.Alignment)1 Cell (eu.esdihumboldt.hale.common.align.model.Cell)1 MutableAlignment (eu.esdihumboldt.hale.common.align.model.MutableAlignment)1 MutableCell (eu.esdihumboldt.hale.common.align.model.MutableCell)1