Search in sources :

Example 6 with SimpleLog

use of eu.esdihumboldt.hale.common.core.report.SimpleLog in project hale by halestudio.

the class FormattedStringMigrator method convertPattern.

private String convertPattern(String pattern, ListMultimap<String, ? extends Entity> oldSource, AlignmentMigration migration, SimpleLog log) {
    List<PropertyEntityDefinition> oldVars = new ArrayList<>();
    if (oldSource != null && oldSource.get(FormattedStringFunction.ENTITY_VARIABLE) != null) {
        oldVars = oldSource.get(FormattedStringFunction.ENTITY_VARIABLE).stream().filter(e -> e.getDefinition() instanceof PropertyEntityDefinition).map(e -> (PropertyEntityDefinition) e.getDefinition()).collect(Collectors.toList());
    }
    Map<String, Object> replacements = new HashMap<>();
    for (PropertyEntityDefinition var : oldVars) {
        Optional<EntityDefinition> replacement = migration.entityReplacement(var, log);
        replacement.ifPresent(repl -> {
            String newName = repl.getDefinition().getName().getLocalPart();
            // XXX there might be name conflicts - check for those or use
            // long names?
            FormattedStringFunction.addValue(replacements, newName, var);
        });
    }
    for (Entry<String, Object> replacement : replacements.entrySet()) {
        // replace variables
        pattern = pattern.replaceAll(Pattern.quote("{" + replacement.getKey() + "}"), "{" + replacement.getValue() + "}");
    }
    return pattern;
}
Also used : ArrayListMultimap(com.google.common.collect.ArrayListMultimap) SimpleLog(eu.esdihumboldt.hale.common.core.report.SimpleLog) Cell(eu.esdihumboldt.hale.common.align.model.Cell) PropertyEntityDefinition(eu.esdihumboldt.hale.common.align.model.impl.PropertyEntityDefinition) ListMultimap(com.google.common.collect.ListMultimap) HashMap(java.util.HashMap) Entity(eu.esdihumboldt.hale.common.align.model.Entity) ParameterValue(eu.esdihumboldt.hale.common.align.model.ParameterValue) AlignmentMigration(eu.esdihumboldt.hale.common.align.migrate.AlignmentMigration) Collectors(java.util.stream.Collectors) DefaultCellMigrator(eu.esdihumboldt.hale.common.align.migrate.impl.DefaultCellMigrator) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map) MutableCell(eu.esdihumboldt.hale.common.align.model.MutableCell) Entry(java.util.Map.Entry) Optional(java.util.Optional) MigrationOptions(eu.esdihumboldt.hale.common.align.migrate.MigrationOptions) Pattern(java.util.regex.Pattern) EntityDefinition(eu.esdihumboldt.hale.common.align.model.EntityDefinition) CellLog(eu.esdihumboldt.hale.common.align.model.annotations.messages.CellLog) Value(eu.esdihumboldt.hale.common.core.io.Value) 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) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList)

Example 7 with SimpleLog

use of eu.esdihumboldt.hale.common.core.report.SimpleLog in project hale by halestudio.

the class JoinMigrator method convertJoinParameter.

/**
 * Migrate a join parameter based on an {@link AlignmentMigration}.
 *
 * @param joinParam the join parameter to migrate
 * @param migration the alignment migration
 * @param options the migration options
 * @param log the migration log
 * @return the migrated join parameter
 */
private JoinParameter convertJoinParameter(JoinParameter joinParam, AlignmentMigration migration, MigrationOptions options, SimpleLog log) {
    List<TypeEntityDefinition> types = joinParam.getTypes().stream().map(type -> {
        return (TypeEntityDefinition) migration.entityReplacement(type, log).orElse(type);
    }).collect(Collectors.toList());
    Set<JoinCondition> conditions = joinParam.getConditions().stream().map(condition -> {
        PropertyEntityDefinition baseProperty = (PropertyEntityDefinition) migration.entityReplacement(condition.baseProperty, log).orElse(condition.baseProperty);
        PropertyEntityDefinition joinProperty = (PropertyEntityDefinition) migration.entityReplacement(condition.joinProperty, log).orElse(condition.joinProperty);
        JoinCondition result = new JoinCondition(baseProperty, joinProperty);
        return result;
    }).collect(Collectors.toSet());
    return new JoinParameter(types, conditions);
}
Also used : ArrayListMultimap(com.google.common.collect.ArrayListMultimap) SimpleLog(eu.esdihumboldt.hale.common.core.report.SimpleLog) Cell(eu.esdihumboldt.hale.common.align.model.Cell) PropertyEntityDefinition(eu.esdihumboldt.hale.common.align.model.impl.PropertyEntityDefinition) ListMultimap(com.google.common.collect.ListMultimap) Set(java.util.Set) ParameterValue(eu.esdihumboldt.hale.common.align.model.ParameterValue) AlignmentMigration(eu.esdihumboldt.hale.common.align.migrate.AlignmentMigration) Collectors(java.util.stream.Collectors) DefaultCellMigrator(eu.esdihumboldt.hale.common.align.migrate.impl.DefaultCellMigrator) List(java.util.List) TypeEntityDefinition(eu.esdihumboldt.hale.common.align.model.impl.TypeEntityDefinition) JoinFunction(eu.esdihumboldt.hale.common.align.model.functions.JoinFunction) JoinCondition(eu.esdihumboldt.hale.common.align.model.functions.join.JoinParameter.JoinCondition) MutableCell(eu.esdihumboldt.hale.common.align.model.MutableCell) MigrationOptions(eu.esdihumboldt.hale.common.align.migrate.MigrationOptions) CellLog(eu.esdihumboldt.hale.common.align.model.annotations.messages.CellLog) Value(eu.esdihumboldt.hale.common.core.io.Value) TypeEntityDefinition(eu.esdihumboldt.hale.common.align.model.impl.TypeEntityDefinition) PropertyEntityDefinition(eu.esdihumboldt.hale.common.align.model.impl.PropertyEntityDefinition) JoinCondition(eu.esdihumboldt.hale.common.align.model.functions.join.JoinParameter.JoinCondition)

Example 8 with SimpleLog

use of eu.esdihumboldt.hale.common.core.report.SimpleLog in project hale by halestudio.

the class JoinMigrator method updateCell.

@Override
public MutableCell updateCell(Cell originalCell, AlignmentMigration migration, MigrationOptions options, SimpleLog log) {
    MutableCell result = super.updateCell(originalCell, migration, options, log);
    SimpleLog cellLog = SimpleLog.all(log, new CellLog(result, CELL_LOG_CATEGORY));
    if (options.updateSource()) {
        ListMultimap<String, ParameterValue> modParams = ArrayListMultimap.create(result.getTransformationParameters());
        List<ParameterValue> joinParams = modParams.get(JoinFunction.PARAMETER_JOIN);
        if (!joinParams.isEmpty()) {
            JoinParameter joinParam = joinParams.get(0).as(JoinParameter.class);
            if (joinParam != null) {
                joinParams.clear();
                joinParams.add(new ParameterValue(Value.complex(convertJoinParameter(joinParam, migration, options, cellLog))));
            }
        }
        result.setTransformationParameters(modParams);
    }
    return result;
}
Also used : SimpleLog(eu.esdihumboldt.hale.common.core.report.SimpleLog) 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 9 with SimpleLog

use of eu.esdihumboldt.hale.common.core.report.SimpleLog in project hale by halestudio.

the class JoinContext method apply.

/**
 * Apply information collected in the context to the cell.
 *
 * @param newCell the merged cell
 * @param log the cell log
 * @param migration the alignment migration
 */
public void apply(MutableCell newCell, AlignmentMigration migration, SimpleLog log) {
    ListMultimap<String, ParameterValue> params = ArrayListMultimap.create();
    /*
		 * Order: Keep original order but replace entities w/ all matches
		 */
    Set<TypeEntityDefinition> types = new LinkedHashSet<>();
    for (TypeEntityDefinition type : orgParameter.getTypes()) {
        List<TypeEntityDefinition> repl = replacements.get(type);
        if (repl.isEmpty()) {
            log.error("Could not find replacement for type {0} in join order", type);
            types.add(type);
        } else {
            types.addAll(repl);
        }
    }
    /*
		 * Conditions: (1) add conditions from matches and (2) add conditions
		 * from original cell translated to new schema (via property mapping),
		 * if they are not duplicates
		 */
    Set<Pair<PropertyEntityDefinition, PropertyEntityDefinition>> cons = new LinkedHashSet<>();
    // add conditions from matches
    for (Cell match : joinMatches) {
        JoinParameter matchParameter = CellUtil.getFirstParameter(match, JoinFunction.PARAMETER_JOIN).as(JoinParameter.class);
        for (JoinCondition condition : matchParameter.getConditions()) {
            cons.add(new Pair<>(condition.baseProperty, condition.joinProperty));
        }
    }
    // migrate original conditions
    Set<JoinCondition> migrated = orgParameter.getConditions().stream().map(condition -> {
        PropertyEntityDefinition baseProperty = processOriginalConditionProperty(condition.baseProperty, migration, log);
        PropertyEntityDefinition joinProperty = processOriginalConditionProperty(condition.joinProperty, migration, log);
        JoinCondition result = new JoinCondition(baseProperty, joinProperty);
        return result;
    }).collect(Collectors.toSet());
    for (JoinCondition condition : migrated) {
        if (!condition.baseProperty.equals(condition.joinProperty)) {
            // migrated condition may contain "loop" condition
            cons.add(new Pair<>(condition.baseProperty, condition.joinProperty));
        }
    }
    // add messages on dropped filter/conditions
    for (EntityDefinition stripped : strippedSources) {
        if (!AlignmentUtil.isDefaultEntity(stripped)) {
            String msg = "Conditions/contexts for an original source could not be transfered and were dropped: " + MergeUtil.getContextInfoString(stripped);
            log.warn(msg);
        }
    }
    // all conditions
    Set<JoinCondition> conditions = new HashSet<>();
    for (Pair<PropertyEntityDefinition, PropertyEntityDefinition> condition : cons) {
        conditions.add(new JoinCondition(condition.getFirst(), condition.getSecond()));
    }
    JoinParameter newParam = new JoinParameter(new ArrayList<>(types), conditions);
    params.replaceValues(JoinFunction.PARAMETER_JOIN, Collections.singleton(new ParameterValue(Value.of(newParam))));
    // Use Groovy Join if original or match uses a script
    if (!scripts.isEmpty()) {
        boolean originalScript = scripts.size() == 1 && GroovyJoin.ID.equals(newCell.getTransformationIdentifier());
        Text script;
        if (originalScript) {
            // use original script
            script = new Text(scripts.get(0).getSecond());
            // create annotation
            log.warn("The Groovy script from the original cell was reused, logic and references to sources are very likely not valid anymore.");
        } else {
            // dummy script with all original scripts
            newCell.setTransformationIdentifier(GroovyJoin.ID);
            script = buildScript(scripts);
            // create annotation
            log.warn("At least one source mapping used a Groovy script, the script could not be combined automatically and was replaced with a dummy script (old scripts are commented out). Please check how you can migrate the old functionality.");
        }
        params.replaceValues(GroovyConstants.PARAMETER_SCRIPT, Collections.singleton(new ParameterValue(Value.of(script))));
    }
    newCell.setTransformationParameters(params);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ArrayListMultimap(com.google.common.collect.ArrayListMultimap) Arrays(java.util.Arrays) Cell(eu.esdihumboldt.hale.common.align.model.Cell) Text(eu.esdihumboldt.hale.common.core.io.Text) ListMultimap(com.google.common.collect.ListMultimap) GroovyConstants(eu.esdihumboldt.cst.functions.groovy.GroovyConstants) AlignmentMigration(eu.esdihumboldt.hale.common.align.migrate.AlignmentMigration) AlignmentUtil(eu.esdihumboldt.hale.common.align.model.AlignmentUtil) MergeUtil(eu.esdihumboldt.hale.common.align.merge.MergeUtil) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) JoinFunction(eu.esdihumboldt.hale.common.align.model.functions.JoinFunction) MutableCell(eu.esdihumboldt.hale.common.align.model.MutableCell) Pair(eu.esdihumboldt.util.Pair) LinkedHashSet(java.util.LinkedHashSet) Value(eu.esdihumboldt.hale.common.core.io.Value) SimpleLog(eu.esdihumboldt.hale.common.core.report.SimpleLog) PropertyEntityDefinition(eu.esdihumboldt.hale.common.align.model.impl.PropertyEntityDefinition) Set(java.util.Set) JoinParameter(eu.esdihumboldt.hale.common.align.model.functions.join.JoinParameter) ParameterValue(eu.esdihumboldt.hale.common.align.model.ParameterValue) Collectors(java.util.stream.Collectors) CellUtil(eu.esdihumboldt.hale.common.align.model.CellUtil) GroovyJoin(eu.esdihumboldt.cst.functions.groovy.GroovyJoin) List(java.util.List) TypeEntityDefinition(eu.esdihumboldt.hale.common.align.model.impl.TypeEntityDefinition) JoinCondition(eu.esdihumboldt.hale.common.align.model.functions.join.JoinParameter.JoinCondition) EntityDefinition(eu.esdihumboldt.hale.common.align.model.EntityDefinition) Collections(java.util.Collections) ParameterValue(eu.esdihumboldt.hale.common.align.model.ParameterValue) Text(eu.esdihumboldt.hale.common.core.io.Text) JoinParameter(eu.esdihumboldt.hale.common.align.model.functions.join.JoinParameter) JoinCondition(eu.esdihumboldt.hale.common.align.model.functions.join.JoinParameter.JoinCondition) PropertyEntityDefinition(eu.esdihumboldt.hale.common.align.model.impl.PropertyEntityDefinition) TypeEntityDefinition(eu.esdihumboldt.hale.common.align.model.impl.TypeEntityDefinition) EntityDefinition(eu.esdihumboldt.hale.common.align.model.EntityDefinition) TypeEntityDefinition(eu.esdihumboldt.hale.common.align.model.impl.TypeEntityDefinition) PropertyEntityDefinition(eu.esdihumboldt.hale.common.align.model.impl.PropertyEntityDefinition) Cell(eu.esdihumboldt.hale.common.align.model.Cell) MutableCell(eu.esdihumboldt.hale.common.align.model.MutableCell) Pair(eu.esdihumboldt.util.Pair) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 10 with SimpleLog

use of eu.esdihumboldt.hale.common.core.report.SimpleLog in project hale by halestudio.

the class AbstractBaseAlignmentLoader method migrateCells.

private List<MutableCell> migrateCells(List<MutableCell> cells, SimpleLog log) {
    List<MutableCell> result = new ArrayList<>();
    // Collect mappings from all UnmigratedCells
    Map<EntityDefinition, EntityDefinition> allMappings = new HashMap<>();
    cells.stream().filter(c -> c instanceof UnmigratedCell).map(c -> (UnmigratedCell) c).forEach(uc -> allMappings.putAll(uc.getEntityMappings()));
    // Add cells to the alignment, migrate UnmigratedCells
    for (MutableCell cell : cells) {
        if (cell instanceof UnmigratedCell) {
            result.add(((UnmigratedCell) cell).migrate(allMappings, log));
        } else {
            result.add(cell);
        }
    }
    return result;
}
Also used : IOUtils(eu.esdihumboldt.util.io.IOUtils) Cell(eu.esdihumboldt.hale.common.align.model.Cell) DefaultInputSupplier(eu.esdihumboldt.hale.common.core.io.supplier.DefaultInputSupplier) ModifiableCell(eu.esdihumboldt.hale.common.align.model.ModifiableCell) HashMap(java.util.HashMap) Alignment(eu.esdihumboldt.hale.common.align.model.Alignment) AlignmentUtil(eu.esdihumboldt.hale.common.align.model.AlignmentUtil) MutableAlignment(eu.esdihumboldt.hale.common.align.model.MutableAlignment) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Strings(com.google.common.base.Strings) IOMessageImpl(eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl) Map(java.util.Map) MutableCell(eu.esdihumboldt.hale.common.align.model.MutableCell) URI(java.net.URI) LinkedList(java.util.LinkedList) BaseAlignmentCell(eu.esdihumboldt.hale.common.align.model.BaseAlignmentCell) SimpleLog(eu.esdihumboldt.hale.common.core.report.SimpleLog) CustomPropertyFunction(eu.esdihumboldt.hale.common.align.extension.function.custom.CustomPropertyFunction) DefaultAlignment(eu.esdihumboldt.hale.common.align.model.impl.DefaultAlignment) Collection(java.util.Collection) UnmigratedCell(eu.esdihumboldt.hale.common.align.migrate.impl.UnmigratedCell) Set(java.util.Set) IOException(java.io.IOException) IOReporter(eu.esdihumboldt.hale.common.core.io.report.IOReporter) PathUpdate(eu.esdihumboldt.hale.common.core.io.PathUpdate) TransformationMode(eu.esdihumboldt.hale.common.align.model.TransformationMode) List(java.util.List) Entry(java.util.Map.Entry) EntityDefinition(eu.esdihumboldt.hale.common.align.model.EntityDefinition) TypeIndex(eu.esdihumboldt.hale.common.schema.model.TypeIndex) InputStream(java.io.InputStream) EntityDefinition(eu.esdihumboldt.hale.common.align.model.EntityDefinition) UnmigratedCell(eu.esdihumboldt.hale.common.align.migrate.impl.UnmigratedCell) MutableCell(eu.esdihumboldt.hale.common.align.model.MutableCell) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList)

Aggregations

SimpleLog (eu.esdihumboldt.hale.common.core.report.SimpleLog)10 MutableCell (eu.esdihumboldt.hale.common.align.model.MutableCell)8 EntityDefinition (eu.esdihumboldt.hale.common.align.model.EntityDefinition)7 AlignmentMigration (eu.esdihumboldt.hale.common.align.migrate.AlignmentMigration)6 CellLog (eu.esdihumboldt.hale.common.align.model.annotations.messages.CellLog)6 Cell (eu.esdihumboldt.hale.common.align.model.Cell)5 ParameterValue (eu.esdihumboldt.hale.common.align.model.ParameterValue)5 PropertyEntityDefinition (eu.esdihumboldt.hale.common.align.model.impl.PropertyEntityDefinition)5 MigrationOptions (eu.esdihumboldt.hale.common.align.migrate.MigrationOptions)4 Entity (eu.esdihumboldt.hale.common.align.model.Entity)4 TypeEntityDefinition (eu.esdihumboldt.hale.common.align.model.impl.TypeEntityDefinition)4 ArrayList (java.util.ArrayList)4 List (java.util.List)4 ArrayListMultimap (com.google.common.collect.ArrayListMultimap)3 ListMultimap (com.google.common.collect.ListMultimap)3 Value (eu.esdihumboldt.hale.common.core.io.Value)3 HashMap (java.util.HashMap)3 Set (java.util.Set)3 Collectors (java.util.stream.Collectors)3 DefaultCellMigrator (eu.esdihumboldt.hale.common.align.migrate.impl.DefaultCellMigrator)2