Search in sources :

Example 66 with ParameterValue

use of eu.esdihumboldt.hale.common.align.model.ParameterValue in project hale by halestudio.

the class DefaultsVisitor method addDefaultCell.

/**
 * Add a cell assigning a default value to the given entity.
 *
 * @param ped the property entity definition
 * @param value the value to assign or <code>null</code> if it should be
 *            auto-detected
 */
private void addDefaultCell(PropertyEntityDefinition ped, String value) {
    String note;
    // determine value to assign
    if (value == null) {
        value = determineDefaultValue(ped.getDefinition().getPropertyType());
        note = "Generated default value based on property type.";
    } else {
        note = "Generated cell with specified default value.";
    }
    if (value == null) {
        return;
    }
    // create cell template
    MutableCell cell = new DefaultCell();
    cell.setPriority(Priority.LOWEST);
    ListMultimap<String, Entity> target = ArrayListMultimap.create();
    cell.setTarget(target);
    ListMultimap<String, ParameterValue> parameters = ArrayListMultimap.create();
    cell.setTransformationParameters(parameters);
    // set transformation identifier (Assign)
    cell.setTransformationIdentifier(AssignFunction.ID);
    // set cell target (Property)
    target.put(null, new DefaultProperty(ped));
    // set cell parameters (Value)
    parameters.put(AssignFunction.PARAMETER_VALUE, new ParameterValue(value));
    BGISAppUtil.appendNote(cell, note);
    cells.add(cell);
}
Also used : Entity(eu.esdihumboldt.hale.common.align.model.Entity) MutableCell(eu.esdihumboldt.hale.common.align.model.MutableCell) ParameterValue(eu.esdihumboldt.hale.common.align.model.ParameterValue) DefaultCell(eu.esdihumboldt.hale.common.align.model.impl.DefaultCell) DefaultProperty(eu.esdihumboldt.hale.common.align.model.impl.DefaultProperty)

Example 67 with ParameterValue

use of eu.esdihumboldt.hale.common.align.model.ParameterValue 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 68 with ParameterValue

use of eu.esdihumboldt.hale.common.align.model.ParameterValue 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)

Example 69 with ParameterValue

use of eu.esdihumboldt.hale.common.align.model.ParameterValue in project hale by halestudio.

the class FormattedStringMigrator method updateCell.

@Override
public MutableCell updateCell(Cell originalCell, AlignmentMigration migration, MigrationOptions options, SimpleLog log) {
    MutableCell result = super.updateCell(originalCell, migration, options, log);
    log = SimpleLog.all(log, new CellLog(result, CELL_LOG_CATEGORY));
    if (options.updateSource()) {
        ListMultimap<String, ParameterValue> modParams = ArrayListMultimap.create(result.getTransformationParameters());
        List<ParameterValue> patternParams = modParams.get(FormattedStringFunction.PARAMETER_PATTERN);
        if (!patternParams.isEmpty()) {
            String pattern = patternParams.get(0).as(String.class);
            if (pattern != null) {
                patternParams.clear();
                patternParams.add(new ParameterValue(Value.of(convertPattern(pattern, originalCell.getSource(), migration, log))));
            }
        }
        result.setTransformationParameters(modParams);
    }
    return result;
}
Also used : MutableCell(eu.esdihumboldt.hale.common.align.model.MutableCell) ParameterValue(eu.esdihumboldt.hale.common.align.model.ParameterValue) CellLog(eu.esdihumboldt.hale.common.align.model.annotations.messages.CellLog)

Example 70 with ParameterValue

use of eu.esdihumboldt.hale.common.align.model.ParameterValue in project hale by halestudio.

the class MergeExplanation method getExplanation.

@Override
protected String getExplanation(Cell cell, boolean html, ServiceProvider services, Locale locale) {
    Entity source = CellUtil.getFirstEntity(cell.getSource());
    Entity target = CellUtil.getFirstEntity(cell.getTarget());
    List<ParameterValue> properties = (cell.getTransformationParameters() == null) ? (null) : (cell.getTransformationParameters().get(PARAMETER_PROPERTY));
    if (source != null && target != null) {
        if (properties != null && !properties.isEmpty()) {
            List<String> props = properties.stream().map(prop -> quoteText(prop.as(String.class), html)).collect(Collectors.toList());
            String propertiesString = enumerateJoin(props, locale);
            return MessageFormat.format(getMessage("main", locale), formatEntity(source, html, true, locale), formatEntity(target, html, true, locale), propertiesString);
        } else {
            return MessageFormat.format(getMessage("all", locale), formatEntity(source, html, true, locale), formatEntity(target, html, true, locale));
        }
    }
    return null;
}
Also used : List(java.util.List) Cell(eu.esdihumboldt.hale.common.align.model.Cell) MergeFunction(eu.esdihumboldt.hale.common.align.model.functions.MergeFunction) Locale(java.util.Locale) ServiceProvider(eu.esdihumboldt.hale.common.core.service.ServiceProvider) Entity(eu.esdihumboldt.hale.common.align.model.Entity) ParameterValue(eu.esdihumboldt.hale.common.align.model.ParameterValue) AbstractCellExplanation(eu.esdihumboldt.hale.common.align.model.impl.AbstractCellExplanation) Collectors(java.util.stream.Collectors) CellUtil(eu.esdihumboldt.hale.common.align.model.CellUtil) MessageFormat(java.text.MessageFormat) Entity(eu.esdihumboldt.hale.common.align.model.Entity) ParameterValue(eu.esdihumboldt.hale.common.align.model.ParameterValue)

Aggregations

ParameterValue (eu.esdihumboldt.hale.common.align.model.ParameterValue)80 Test (org.junit.Test)29 Cell (eu.esdihumboldt.hale.common.align.model.Cell)28 DefaultCell (eu.esdihumboldt.hale.common.align.model.impl.DefaultCell)21 AttributeMappingType (eu.esdihumboldt.hale.io.appschema.impl.internal.generated.app_schema.AttributeMappingType)14 AppSchemaMappingContext (eu.esdihumboldt.hale.io.appschema.writer.internal.mapping.AppSchemaMappingContext)13 ArrayList (java.util.ArrayList)13 MutableCell (eu.esdihumboldt.hale.common.align.model.MutableCell)9 Property (eu.esdihumboldt.hale.common.align.model.Property)9 ClientProperty (eu.esdihumboldt.hale.io.appschema.impl.internal.generated.app_schema.AttributeMappingType.ClientProperty)9 Entity (eu.esdihumboldt.hale.common.align.model.Entity)8 DefaultProperty (eu.esdihumboldt.hale.common.align.model.impl.DefaultProperty)8 Value (eu.esdihumboldt.hale.common.core.io.Value)8 TypeDefinition (eu.esdihumboldt.hale.common.schema.model.TypeDefinition)8 TransformationException (eu.esdihumboldt.hale.common.align.transformation.function.TransformationException)7 Type (eu.esdihumboldt.hale.common.align.model.Type)6 AssignHandler (eu.esdihumboldt.hale.io.appschema.writer.internal.AssignHandler)6 JoinParameter (eu.esdihumboldt.hale.common.align.model.functions.join.JoinParameter)5 HashSet (java.util.HashSet)5 EntityDefinition (eu.esdihumboldt.hale.common.align.model.EntityDefinition)4