Search in sources :

Example 6 with Text

use of eu.esdihumboldt.hale.common.core.io.Text in project hale by halestudio.

the class GroovyRetypeMergeMigrator method mergeSource.

@Override
protected void mergeSource(MutableCell cell, String sourceName, EntityDefinition source, Cell match, Cell originalCell, SimpleLog log, Void context, AlignmentMigration migration, MergeIndex mergeIndex) {
    // combination always possible
    // but Groovy script cannot be handled automatically
    String matchFunction = match.getTransformationIdentifier();
    // function that the migrated cell should have
    String translateTo = matchFunction;
    // if the script must be dropped
    boolean dropScript = true;
    // if there is an existing script in the match
    boolean addToScript = false;
    switch(matchFunction) {
        // cases where "normal" match is replaced by Groovy equivalent
        case JoinFunction.ID:
            translateTo = GroovyJoin.ID;
            dropScript = false;
            break;
        case RetypeFunction.ID:
            translateTo = GroovyRetype.ID;
            dropScript = false;
            break;
        case MergeFunction.ID:
            translateTo = GroovyMerge.ID;
            dropScript = false;
            break;
        case CreateFunction.ID:
            translateTo = GroovyCreate.ID;
            dropScript = false;
            break;
        // cases where a groovy match is annotated with the script
        case GroovyJoin.ID:
        case GroovyCreate.ID:
        case GroovyRetype.ID:
        case GroovyMerge.ID:
            dropScript = false;
            addToScript = true;
            break;
        default:
    }
    // cell function
    cell.setTransformationIdentifier(translateTo);
    // source from match
    cell.setSource(ArrayListMultimap.create(match.getSource()));
    if (source.getFilter() != null) {
        // note about dropped filter
        String msg = "The filter on the original source has been dropped because transfering it was not possible automatically.";
        String filterString = FilterDefinitionManager.getInstance().asString(source.getFilter());
        if (filterString != null) {
            msg = msg + " The original filter was: \"" + filterString + "\".";
        }
        log.warn(msg);
    }
    // parameters from match
    ListMultimap<String, ParameterValue> params = ArrayListMultimap.create(match.getTransformationParameters());
    String script = null;
    ParameterValue scriptValue = CellUtil.getFirstParameter(originalCell, GroovyConstants.PARAMETER_SCRIPT);
    if (scriptValue != null) {
        // try retrieving as text
        Text text = scriptValue.as(Text.class);
        if (text != null) {
            script = text.getText();
        } else {
            // fall back to string value
            script = scriptValue.as(String.class);
        }
    }
    if (script != null) {
        if (dropScript) {
            // script is dropped and not added to parameters
            String msg = "Script from Groovy Retype could not be transfered to migrated cell:\n\n" + script;
            log.warn(msg);
        } else {
            if (addToScript) {
                // add to existing script
                String matchScript = null;
                ParameterValue matchScriptValue = CellUtil.getFirstParameter(match, GroovyConstants.PARAMETER_SCRIPT);
                if (matchScriptValue != null) {
                    // try retrieving as text
                    Text text = matchScriptValue.as(Text.class);
                    if (text != null) {
                        matchScript = text.getText();
                    } else {
                        // fall back to string value
                        matchScript = matchScriptValue.as(String.class);
                    }
                }
                if (matchScript != null) {
                    script = matchScript + "\n\n\n// FIXME Script from merged Groovy Retype\n\n" + comment(script);
                }
                log.warn("A script was associated to the merged Groovy Retype this cell is derived from, it was added commented out to this cell's script. Please check how the functionality can be combined.");
            } else {
                // set as new script
                if (!RetypeFunction.ID.equals(match.getTransformationIdentifier())) {
                    script = "// FIXME Script taken from merged Groovy Retype:\n\n" + script;
                    log.warn("A script was associated to the merged Groovy Retype this cell is derived from, it was used as script for this cell's script and needs to be verified/adapted.");
                }
            }
            params.put(GroovyConstants.PARAMETER_SCRIPT, new ParameterValue(Value.of(new Text(script))));
        }
    }
    cell.setTransformationParameters(params);
}
Also used : ParameterValue(eu.esdihumboldt.hale.common.align.model.ParameterValue) Text(eu.esdihumboldt.hale.common.core.io.Text)

Example 7 with Text

use of eu.esdihumboldt.hale.common.core.io.Text 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 8 with Text

use of eu.esdihumboldt.hale.common.core.io.Text in project hale by halestudio.

the class JoinMergeMigrator method addScript.

private void addScript(Cell match, JoinContext context) {
    ParameterValue scriptValue = CellUtil.getFirstParameter(match, GroovyConstants.PARAMETER_SCRIPT);
    if (scriptValue != null) {
        String script;
        // try retrieving as text
        Text text = scriptValue.as(Text.class);
        if (text != null) {
            script = text.getText();
        } else {
            // fall back to string value
            script = scriptValue.as(String.class);
        }
        context.addGroovyScript(match, script);
    }
}
Also used : ParameterValue(eu.esdihumboldt.hale.common.align.model.ParameterValue) Text(eu.esdihumboldt.hale.common.core.io.Text)

Aggregations

Text (eu.esdihumboldt.hale.common.core.io.Text)8 ParameterValue (eu.esdihumboldt.hale.common.align.model.ParameterValue)4 Value (eu.esdihumboldt.hale.common.core.io.Value)3 ArrayList (java.util.ArrayList)2 ArrayListMultimap (com.google.common.collect.ArrayListMultimap)1 ListMultimap (com.google.common.collect.ListMultimap)1 GroovyConstants (eu.esdihumboldt.cst.functions.groovy.GroovyConstants)1 GroovyJoin (eu.esdihumboldt.cst.functions.groovy.GroovyJoin)1 DefaultCustomFunctionExplanation (eu.esdihumboldt.hale.common.align.custom.DefaultCustomFunctionExplanation)1 DefaultCustomPropertyFunction (eu.esdihumboldt.hale.common.align.custom.DefaultCustomPropertyFunction)1 DefaultCustomPropertyFunctionEntity (eu.esdihumboldt.hale.common.align.custom.DefaultCustomPropertyFunctionEntity)1 DefaultCustomPropertyFunctionParameter (eu.esdihumboldt.hale.common.align.custom.DefaultCustomPropertyFunctionParameter)1 MergeUtil (eu.esdihumboldt.hale.common.align.merge.MergeUtil)1 AlignmentMigration (eu.esdihumboldt.hale.common.align.migrate.AlignmentMigration)1 AlignmentUtil (eu.esdihumboldt.hale.common.align.model.AlignmentUtil)1 Cell (eu.esdihumboldt.hale.common.align.model.Cell)1 CellUtil (eu.esdihumboldt.hale.common.align.model.CellUtil)1 EntityDefinition (eu.esdihumboldt.hale.common.align.model.EntityDefinition)1 MutableCell (eu.esdihumboldt.hale.common.align.model.MutableCell)1 JoinFunction (eu.esdihumboldt.hale.common.align.model.functions.JoinFunction)1