Search in sources :

Example 66 with TypeEntityDefinition

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

the class MarkdownCellExplanationTest method createTestCell.

@SuppressWarnings("unused")
private Cell createTestCell() {
    // cell 1
    MutableCell cell1 = new DefaultCell();
    String id1;
    // must be an existing function
    cell1.setTransformationIdentifier(id1 = "eu.esdihumboldt.hale.align.formattedstring");
    ListMultimap<String, ParameterValue> parameters1 = LinkedListMultimap.create();
    // parameter that does not exist (for parameter list testing)
    parameters1.put("test", new ParameterValue("1"));
    parameters1.put("test", new ParameterValue("2"));
    // existing parameter
    parameters1.put("pattern", new ParameterValue("3"));
    cell1.setTransformationParameters(parameters1);
    ListMultimap<String, Type> source1 = ArrayListMultimap.create();
    QName source1TypeName;
    String source1EntityName;
    TypeDefinition sourceType1 = new DefaultTypeDefinition(source1TypeName = new QName("source1Type"));
    Filter filter = null;
    source1.put(source1EntityName = "var", new DefaultType(new TypeEntityDefinition(sourceType1, SchemaSpaceID.SOURCE, filter)));
    cell1.setSource(source1);
    ListMultimap<String, Type> target1 = ArrayListMultimap.create();
    QName target1TypeName;
    String target1EntityName;
    TypeDefinition targetType1 = new DefaultTypeDefinition(target1TypeName = new QName("http://some.name.space/t1", "target1Type"));
    target1.put(target1EntityName = null, new DefaultType(new TypeEntityDefinition(targetType1, SchemaSpaceID.TARGET, null)));
    cell1.setTarget(target1);
    return cell1;
}
Also used : MutableCell(eu.esdihumboldt.hale.common.align.model.MutableCell) ParameterValue(eu.esdihumboldt.hale.common.align.model.ParameterValue) DefaultType(eu.esdihumboldt.hale.common.align.model.impl.DefaultType) QName(javax.xml.namespace.QName) DefaultTypeDefinition(eu.esdihumboldt.hale.common.schema.model.impl.DefaultTypeDefinition) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition) DefaultTypeDefinition(eu.esdihumboldt.hale.common.schema.model.impl.DefaultTypeDefinition) Type(eu.esdihumboldt.hale.common.align.model.Type) DefaultType(eu.esdihumboldt.hale.common.align.model.impl.DefaultType) TypeEntityDefinition(eu.esdihumboldt.hale.common.align.model.impl.TypeEntityDefinition) DefaultCell(eu.esdihumboldt.hale.common.align.model.impl.DefaultCell) Filter(eu.esdihumboldt.hale.common.instance.model.Filter)

Example 67 with TypeEntityDefinition

use of eu.esdihumboldt.hale.common.align.model.impl.TypeEntityDefinition 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 {
            for (TypeEntityDefinition replacement : repl) {
                Filter filter = typeFilters.get(replacement.getDefinition());
                if (filter != null) {
                    // apply filter
                    types.add(new TypeEntityDefinition(replacement.getDefinition(), replacement.getSchemaSpace(), filter));
                } else {
                    types.add(replacement);
                }
            }
        }
    }
    /*
		 * 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.getType().equals(condition.joinProperty.getType())) {
            // 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(applyFilter(condition.getFirst()), applyFilter(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) HashMap(java.util.HashMap) 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) Map(java.util.Map) 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) Filter(eu.esdihumboldt.hale.common.instance.model.Filter) 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) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition) 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) Filter(eu.esdihumboldt.hale.common.instance.model.Filter) 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 68 with TypeEntityDefinition

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

the class AbstractMergeCellMigrator method applySourceContextsToJoinFocus.

/**
 * Handle special case of applying source contexts to the entity that is the
 * Join focus.
 *
 * @param newCell the new cell to update the sources
 * @param originalSource the original source
 * @param migration the alignment migration
 * @param log the operation log
 * @return if the method handled the context transfer
 */
private boolean applySourceContextsToJoinFocus(MutableCell newCell, Entity originalSource, AlignmentMigration migration, SimpleLog log) {
    if (!MergeSettings.isTransferContextsToJoinFocus()) {
        return false;
    }
    String function = newCell.getTransformationIdentifier();
    switch(function) {
        case GroovyJoin.ID:
        case JoinFunction.ID:
            break;
        default:
            return false;
    }
    JoinParameter joinConfig = CellUtil.getFirstParameter(newCell, JoinFunction.PARAMETER_JOIN).as(JoinParameter.class);
    if (joinConfig == null || joinConfig.getTypes() == null || joinConfig.getTypes().isEmpty()) {
        return false;
    }
    TypeEntityDefinition focus = joinConfig.getTypes().iterator().next();
    AtomicReference<Filter> focusFilter = new AtomicReference<>();
    // transfer context
    newCell.setSource(ArrayListMultimap.create(Multimaps.transformValues(newCell.getSource(), new com.google.common.base.Function<Entity, Entity>() {

        @Override
        public Entity apply(Entity input) {
            if (input.getDefinition().getPropertyPath().isEmpty() && input.getDefinition().getType().equals(focus.getType())) {
                EntityDefinition transferedSource = AbstractMigration.translateContexts(originalSource.getDefinition(), input.getDefinition(), migration, log);
                focusFilter.set(transferedSource.getFilter());
                return AlignmentUtil.createEntity(transferedSource);
            } else {
                return input;
            }
        }
    })));
    if (focusFilter.get() != null) {
        // order
        List<TypeEntityDefinition> types = new ArrayList<>();
        for (int i = 0; i < joinConfig.getTypes().size(); i++) {
            TypeEntityDefinition type = joinConfig.getTypes().get(i);
            if (i == 0) {
                type = new TypeEntityDefinition(type.getDefinition(), type.getSchemaSpace(), focusFilter.get());
            }
            types.add(type);
        }
        // conditions
        Set<JoinCondition> conditions = joinConfig.getConditions().stream().map(c -> {
            if (c.baseProperty.getType().equals(focus.getType())) {
                return new JoinCondition(applyFilter(c.baseProperty, focusFilter.get()), c.joinProperty);
            } else {
                return c;
            }
        }).collect(Collectors.toSet());
        JoinParameter newConfig = new JoinParameter(types, conditions);
        ListMultimap<String, ParameterValue> modParams = ArrayListMultimap.create(newCell.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(newConfig)));
            }
        }
        newCell.setTransformationParameters(modParams);
    }
    return true;
}
Also used : ArrayListMultimap(com.google.common.collect.ArrayListMultimap) MergeCellMigrator(eu.esdihumboldt.hale.common.align.merge.MergeCellMigrator) Cell(eu.esdihumboldt.hale.common.align.model.Cell) ListMultimap(com.google.common.collect.ListMultimap) CellMigrator(eu.esdihumboldt.hale.common.align.migrate.CellMigrator) AlignmentMigration(eu.esdihumboldt.hale.common.align.migrate.AlignmentMigration) AlignmentUtil(eu.esdihumboldt.hale.common.align.model.AlignmentUtil) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) MergeUtil(eu.esdihumboldt.hale.common.align.merge.MergeUtil) DefaultCellMigrator(eu.esdihumboldt.hale.common.align.migrate.impl.DefaultCellMigrator) MessageFormat(java.text.MessageFormat) ArrayList(java.util.ArrayList) Multimaps(com.google.common.collect.Multimaps) DefaultCell(eu.esdihumboldt.hale.common.align.model.impl.DefaultCell) JoinFunction(eu.esdihumboldt.hale.common.align.model.functions.JoinFunction) MigrationOptionsImpl(eu.esdihumboldt.hale.common.align.migrate.impl.MigrationOptionsImpl) MutableCell(eu.esdihumboldt.hale.common.align.model.MutableCell) MigrationOptions(eu.esdihumboldt.hale.common.align.migrate.MigrationOptions) MergeSettings(eu.esdihumboldt.hale.common.align.merge.MergeSettings) GeometryType(eu.esdihumboldt.hale.common.schema.model.constraint.type.GeometryType) 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) MergeIndex(eu.esdihumboldt.hale.common.align.merge.MergeIndex) JoinParameter(eu.esdihumboldt.hale.common.align.model.functions.join.JoinParameter) Entity(eu.esdihumboldt.hale.common.align.model.Entity) 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) Filter(eu.esdihumboldt.hale.common.instance.model.Filter) List(java.util.List) TypeEntityDefinition(eu.esdihumboldt.hale.common.align.model.impl.TypeEntityDefinition) JoinCondition(eu.esdihumboldt.hale.common.align.model.functions.join.JoinParameter.JoinCondition) Entry(java.util.Map.Entry) PropertyDefinition(eu.esdihumboldt.hale.common.schema.model.PropertyDefinition) Optional(java.util.Optional) MigrationUtil.isDirectMatch(eu.esdihumboldt.hale.common.align.migrate.util.MigrationUtil.isDirectMatch) EntityDefinition(eu.esdihumboldt.hale.common.align.model.EntityDefinition) CellLog(eu.esdihumboldt.hale.common.align.model.annotations.messages.CellLog) Collections(java.util.Collections) Entity(eu.esdihumboldt.hale.common.align.model.Entity) ParameterValue(eu.esdihumboldt.hale.common.align.model.ParameterValue) ArrayList(java.util.ArrayList) AtomicReference(java.util.concurrent.atomic.AtomicReference) 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) Filter(eu.esdihumboldt.hale.common.instance.model.Filter)

Aggregations

TypeEntityDefinition (eu.esdihumboldt.hale.common.align.model.impl.TypeEntityDefinition)68 TypeDefinition (eu.esdihumboldt.hale.common.schema.model.TypeDefinition)25 ArrayList (java.util.ArrayList)21 EntityDefinition (eu.esdihumboldt.hale.common.align.model.EntityDefinition)20 PropertyEntityDefinition (eu.esdihumboldt.hale.common.align.model.impl.PropertyEntityDefinition)18 Cell (eu.esdihumboldt.hale.common.align.model.Cell)16 Type (eu.esdihumboldt.hale.common.align.model.Type)16 DefaultType (eu.esdihumboldt.hale.common.align.model.impl.DefaultType)15 MutableCell (eu.esdihumboldt.hale.common.align.model.MutableCell)13 DefaultCell (eu.esdihumboldt.hale.common.align.model.impl.DefaultCell)13 JoinParameter (eu.esdihumboldt.hale.common.align.model.functions.join.JoinParameter)12 QName (javax.xml.namespace.QName)10 Entity (eu.esdihumboldt.hale.common.align.model.Entity)8 MutableAlignment (eu.esdihumboldt.hale.common.align.model.MutableAlignment)8 ParameterValue (eu.esdihumboldt.hale.common.align.model.ParameterValue)8 DefaultAlignment (eu.esdihumboldt.hale.common.align.model.impl.DefaultAlignment)8 PropertyDefinition (eu.esdihumboldt.hale.common.schema.model.PropertyDefinition)8 JoinCondition (eu.esdihumboldt.hale.common.align.model.functions.join.JoinParameter.JoinCondition)7 SimpleLog (eu.esdihumboldt.hale.common.core.report.SimpleLog)7 ArrayListMultimap (com.google.common.collect.ArrayListMultimap)6