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;
}
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);
}
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;
}
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);
}
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;
}
Aggregations