Search in sources :

Example 21 with Pair

use of eu.esdihumboldt.util.Pair in project hale by halestudio.

the class MappingView method getDefinitionsFromSelection.

private Pair<Set<EntityDefinition>, Set<EntityDefinition>> getDefinitionsFromSelection(SchemaSelection selection) {
    Set<EntityDefinition> sourceItems;
    Set<EntityDefinition> targetItems;
    if (selection instanceof IStructuredSelection) {
        // prefer getting information from the IStructuredSelection, which
        // from the Schema Explorer only contains the recently selected
        // elements on one side
        sourceItems = new HashSet<EntityDefinition>();
        targetItems = new HashSet<EntityDefinition>();
        for (Object object : ((IStructuredSelection) selection).toArray()) {
            if (object instanceof EntityDefinition) {
                EntityDefinition def = (EntityDefinition) object;
                switch(def.getSchemaSpace()) {
                    case TARGET:
                        targetItems.add(def);
                        break;
                    case SOURCE:
                    default:
                        sourceItems.add(def);
                }
            }
        }
    } else {
        sourceItems = selection.getSourceItems();
        targetItems = selection.getTargetItems();
    }
    return new Pair<Set<EntityDefinition>, Set<EntityDefinition>>(sourceItems, targetItems);
}
Also used : EntityDefinition(eu.esdihumboldt.hale.common.align.model.EntityDefinition) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) Pair(eu.esdihumboldt.util.Pair)

Example 22 with Pair

use of eu.esdihumboldt.util.Pair in project hale by halestudio.

the class FunctionExecutor method executeTransformation.

/**
 * Execute a property transformation.
 *
 * @param transformation the transformation factory
 * @param cell the alignment cell
 * @param sources the named source entities and nodes
 * @param targets the named target entities and nodes
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
protected void executeTransformation(PropertyTransformationFactory transformation, Cell cell, ListMultimap<String, Pair<SourceNode, Entity>> sources, ListMultimap<String, Pair<TargetNode, Entity>> targets) {
    TransformationLog cellLog = new CellLog(reporter, cell);
    PropertyTransformation<?> function;
    try {
        // TODO cache function objects?
        function = transformation.createExtensionObject();
    } catch (Exception e) {
        cellLog.error(cellLog.createMessage("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 property transformation: No matching transformation engine found", null));
        return;
    }
    // configure function
    // set expected result
    ListMultimap<String, PropertyEntityDefinition> expectedResult = ArrayListMultimap.create(targets.keySet().size(), 1);
    for (Entry<String, Pair<TargetNode, Entity>> targetEntry : targets.entries()) {
        EntityDefinition def = targetEntry.getValue().getSecond().getDefinition();
        expectedResult.put(targetEntry.getKey(), toPropertyEntityDefinition(def));
    }
    function.setExpectedResult(expectedResult);
    // set source variables
    ListMultimap<String, PropertyValue> variables = ArrayListMultimap.create();
    for (Entry<String, Pair<SourceNode, Entity>> sourceEntry : sources.entries()) {
        EntityDefinition def = sourceEntry.getValue().getSecond().getDefinition();
        SourceNode sourceNode = sourceEntry.getValue().getFirst();
        if (TransformationTreeUtil.isEager(cell, sourceNode, cellLog, context.getServiceProvider())) {
            // eager source - all values
            Object[] values = sourceNode.getAllValues();
            if (values != null) {
                for (int i = 0; i < values.length; i++) {
                    PropertyValue propertyValue = new PropertyValueImpl(values[i], toPropertyEntityDefinition(def));
                    variables.put(sourceEntry.getKey(), propertyValue);
                }
            }
        } else {
            // non-eager source - one value
            Object value = sourceNode.getValue();
            PropertyValue propertyValue = new PropertyValueImpl(value, toPropertyEntityDefinition(def));
            variables.put(sourceEntry.getKey(), propertyValue);
        }
    }
    function.setVariables(variables);
    // set parameters
    function.setParameters(cell.getTransformationParameters());
    // set context
    function.setExecutionContext(context.getCellContext(cell));
    // set target type
    TypeDefinition targetType = null;
    if (!targets.isEmpty()) {
        TargetNode target = targets.values().iterator().next().getFirst();
        targetType = target.getEntityDefinition().getType();
    }
    function.setTargetType(targetType);
    function.setTypeCell(typeCell.get());
    // execute function
    try {
        ((PropertyTransformation) function).execute(transformation.getIdentifier(), engine, transformation.getExecutionParameters(), cellLog, cell);
    } catch (Throwable e) {
        // TODO instead try another transformation?
        cellLog.error(cellLog.createMessage("Skipping property transformation: Executing property transformation failed.", e));
        return;
    }
    // apply function results
    ListMultimap<String, Object> results = function.getResults();
    if (results != null) {
        for (String name : results.keySet()) {
            List<Object> values = results.get(name);
            List<Pair<TargetNode, Entity>> nodes = targets.get(name);
            if (nodes.size() > values.size()) {
                cellLog.warn(cellLog.createMessage(MessageFormat.format("Transformation result misses values for result with name {0}", name), null));
            }
            if (values.size() > nodes.size()) {
                cellLog.warn(cellLog.createMessage(MessageFormat.format("More transformation results than target nodes for result with name {0}", name), null));
            }
            int count = Math.min(values.size(), nodes.size());
            // node...
            for (int i = 0; i < count; i++) {
                Object value = values.get(i);
                TargetNode node = nodes.get(i).getFirst();
                if (value instanceof MultiValue) {
                    MultiValue originalValue = (MultiValue) value;
                    MultiValue processedValue = new MultiValue(originalValue.size());
                    for (Object o : originalValue) {
                        processedValue.add(processValue(cellLog, function, o, node));
                    }
                    value = processedValue;
                } else {
                    value = processValue(cellLog, function, value, node);
                }
                /*
					 * TODO
					 * 
					 * set node value only if no result has already been set. If
					 * a value is already there and we are in a lower priority
					 * executor, we do not overwrite.
					 */
                if (!node.isDefined()) {
                    node.setResult(value);
                }
            }
        }
    }
}
Also used : TargetNode(eu.esdihumboldt.hale.common.align.model.transformation.tree.TargetNode) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition) SourceNode(eu.esdihumboldt.hale.common.align.model.transformation.tree.SourceNode) PropertyTransformation(eu.esdihumboldt.hale.common.align.transformation.function.PropertyTransformation) CellLog(eu.esdihumboldt.hale.common.align.transformation.report.impl.CellLog) MultiValue(eu.esdihumboldt.cst.MultiValue) Pair(eu.esdihumboldt.util.Pair) PropertyValueImpl(eu.esdihumboldt.hale.common.align.transformation.function.impl.PropertyValueImpl) PropertyValue(eu.esdihumboldt.hale.common.align.transformation.function.PropertyValue) ConversionException(org.springframework.core.convert.ConversionException) TransformationEngine(eu.esdihumboldt.hale.common.align.transformation.engine.TransformationEngine) 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) TransformationLog(eu.esdihumboldt.hale.common.align.transformation.report.TransformationLog)

Example 23 with Pair

use of eu.esdihumboldt.util.Pair in project hale by halestudio.

the class HaleIO method findIOProviderAndId.

/**
 * Find an I/O provider instance for the given input
 *
 * @param <T> the provider interface type
 *
 * @param providerType the provider type, usually an interface
 * @param in the input supplier to use for testing, may be <code>null</code>
 *            if the file name is not <code>null</code>
 * @param filename the file name, may be <code>null</code> if the input
 *            supplier is not <code>null</code>
 * @return a pair with the I/O provider and the corresponding identifier,
 *         both are <code>null</code> if no matching I/O provider was found
 */
@SuppressWarnings("unchecked")
public static <T extends IOProvider> Pair<T, String> findIOProviderAndId(Class<T> providerType, InputSupplier<? extends InputStream> in, String filename) {
    T reader = null;
    String providerId = null;
    IContentType contentType = HaleIO.findContentType(providerType, in, filename);
    if (contentType != null) {
        IOProviderDescriptor factory = HaleIO.findIOProviderFactory(providerType, contentType, null);
        try {
            reader = (T) factory.createExtensionObject();
            providerId = factory.getIdentifier();
        } catch (Exception e) {
            throw new RuntimeException("Could not create I/O provider", e);
        }
        if (reader != null) {
            reader.setContentType(contentType);
        }
    }
    return new Pair<T, String>(reader, providerId);
}
Also used : IOProviderDescriptor(eu.esdihumboldt.hale.common.core.io.extension.IOProviderDescriptor) IContentType(org.eclipse.core.runtime.content.IContentType) IOException(java.io.IOException) Pair(eu.esdihumboldt.util.Pair)

Example 24 with Pair

use of eu.esdihumboldt.util.Pair in project hale by halestudio.

the class CellNodeValidator method visit.

/**
 * @see AbstractTargetToSourceVisitor#visit(CellNode)
 */
@Override
public boolean visit(CellNode node) {
    // evaluate if for the cell all needed inputs are set
    Cell cell = node.getCell();
    // collect source and target nodes per entity name
    ListMultimap<String, Pair<SourceNode, Entity>> sources = ArrayListMultimap.create();
    if (cell.getSource() != null) {
        for (Entry<String, ? extends Entity> sourceEntry : cell.getSource().entries()) {
            String name = sourceEntry.getKey();
            Entity entity = sourceEntry.getValue();
            SourceNode sourceNode = findSourceNode(node, entity);
            if (sourceNode != null) {
                if (sourceNode.isDefined()) {
                    sources.put(name, new Pair<SourceNode, Entity>(sourceNode, entity));
                }
            } else {
                log.error("Source node for entity not found.");
            }
        }
    }
    ListMultimap<String, Pair<TargetNode, Entity>> targets = ArrayListMultimap.create();
    for (Entry<String, ? extends Entity> targetEntry : cell.getTarget().entries()) {
        String name = targetEntry.getKey();
        Entity entity = targetEntry.getValue();
        TargetNode targetNode = findTargetNode(node, entity);
        if (targetNode != null) {
            targets.put(name, new Pair<TargetNode, Entity>(targetNode, entity));
        } else {
            log.error("Target node for entity not found.");
        }
    }
    boolean valid = validate(node, sources, targets);
    if (valid) {
        processValid(cell, sources, targets);
    }
    node.setValid(valid);
    // don't visit source nodes
    return false;
}
Also used : Entity(eu.esdihumboldt.hale.common.align.model.Entity) SourceNode(eu.esdihumboldt.hale.common.align.model.transformation.tree.SourceNode) TargetNode(eu.esdihumboldt.hale.common.align.model.transformation.tree.TargetNode) Cell(eu.esdihumboldt.hale.common.align.model.Cell) Pair(eu.esdihumboldt.util.Pair)

Example 25 with Pair

use of eu.esdihumboldt.util.Pair 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)

Aggregations

Pair (eu.esdihumboldt.util.Pair)33 ArrayList (java.util.ArrayList)11 TypeDefinition (eu.esdihumboldt.hale.common.schema.model.TypeDefinition)8 List (java.util.List)7 Instance (eu.esdihumboldt.hale.common.instance.model.Instance)6 Cell (eu.esdihumboldt.hale.common.align.model.Cell)4 Entity (eu.esdihumboldt.hale.common.align.model.Entity)4 EntityDefinition (eu.esdihumboldt.hale.common.align.model.EntityDefinition)4 Group (eu.esdihumboldt.hale.common.instance.model.Group)4 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)4 Point (org.eclipse.swt.graphics.Point)4 ParameterValue (eu.esdihumboldt.hale.common.align.model.ParameterValue)3 TypeEntityDefinition (eu.esdihumboldt.hale.common.align.model.impl.TypeEntityDefinition)3 IOMessageImpl (eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl)3 GroupPropertyDefinition (eu.esdihumboldt.hale.common.schema.model.GroupPropertyDefinition)3 IOException (java.io.IOException)3 URI (java.net.URI)3 Collection (java.util.Collection)3 QName (javax.xml.namespace.QName)3 SelectionAdapter (org.eclipse.swt.events.SelectionAdapter)3