Search in sources :

Example 46 with Entity

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

the class GroovyTransformation method createGroovyBinding.

/**
 * Create a Groovy binding from the list of variables.
 *
 * @param vars the variable values
 * @param varDefs definition of the assigned variables, in case some
 *            variable values are not set, may be <code>null</code>
 * @param cell the cell the binding is created for
 * @param typeCell the type cell the binding is created for, may be
 *            <code>null</code>
 * @param builder the instance builder for creating target instances, or
 *            <code>null</code> if not applicable
 * @param useInstanceVariables if instances should be used as variables for
 *            the binding instead of extracting the instance values
 * @param log the transformation log
 * @param context the execution context
 * @param targetInstanceType the type of the target instance
 * @return the binding for use with {@link GroovyShell}
 */
public static Binding createGroovyBinding(List<PropertyValue> vars, List<? extends Entity> varDefs, Cell cell, Cell typeCell, InstanceBuilder builder, boolean useInstanceVariables, TransformationLog log, ExecutionContext context, TypeDefinition targetInstanceType) {
    Binding binding = GroovyUtil.createBinding(builder, cell, typeCell, log, context, targetInstanceType);
    // collect definitions to check if all were provided
    Set<EntityDefinition> notDefined = new HashSet<>();
    if (varDefs != null) {
        for (Entity entity : varDefs) {
            notDefined.add(entity.getDefinition());
        }
    }
    // keep only defs where no value is provided
    if (!notDefined.isEmpty()) {
        for (PropertyValue var : vars) {
            notDefined.remove(var.getProperty());
        }
    }
    // add null value for missing variables
    if (!notDefined.isEmpty()) {
        vars = new ArrayList<>(vars);
        for (EntityDefinition entityDef : notDefined) {
            if (entityDef instanceof PropertyEntityDefinition) {
                vars.add(new PropertyValueImpl(null, (PropertyEntityDefinition) entityDef));
            }
        }
    }
    for (PropertyValue var : vars) {
        // add the variable to the environment
        addToBinding(binding, var.getProperty(), getUseValue(var.getValue(), useInstanceVariables));
    }
    return binding;
}
Also used : Binding(groovy.lang.Binding) PropertyEntityDefinition(eu.esdihumboldt.hale.common.align.model.impl.PropertyEntityDefinition) EntityDefinition(eu.esdihumboldt.hale.common.align.model.EntityDefinition) Entity(eu.esdihumboldt.hale.common.align.model.Entity) PropertyEntityDefinition(eu.esdihumboldt.hale.common.align.model.impl.PropertyEntityDefinition) PropertyValue(eu.esdihumboldt.hale.common.align.transformation.function.PropertyValue) HashSet(java.util.HashSet) PropertyValueImpl(eu.esdihumboldt.hale.common.align.transformation.function.impl.PropertyValueImpl)

Example 47 with Entity

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

the class MathematicalExpressionExplanation method getExplanation.

@Override
protected String getExplanation(Cell cell, boolean html, ServiceProvider services, Locale locale) {
    Entity target = CellUtil.getFirstEntity(cell.getTarget());
    String expression = CellUtil.getFirstParameter(cell, MathematicalExpression.PARAMETER_EXPRESSION).as(String.class);
    List<? extends Entity> sources = cell.getSource().get(MathematicalExpression.ENTITY_VARIABLE);
    if (target != null && expression != null) {
        if (html)
            expression = "<pre>" + expression + "</pre>";
        String explanation = MessageFormat.format(getMessage("main", locale), formatEntity(target, html, true, locale), expression);
        if (html)
            explanation = explanation.replaceAll("\n", "<br />");
        if (html) {
            Map<String, String> varToProperty = sources.stream().collect(Collectors.toMap(entity -> {
                return getEntityNameWithoutCondition(entity);
            }, entity -> {
                return formatEntity(entity, true, false, locale);
            }));
            explanation += buildReplacementTable(varToProperty, locale);
        }
        return explanation;
    }
    return null;
}
Also used : List(java.util.List) Cell(eu.esdihumboldt.hale.common.align.model.Cell) Locale(java.util.Locale) Map(java.util.Map) ServiceProvider(eu.esdihumboldt.hale.common.core.service.ServiceProvider) Entity(eu.esdihumboldt.hale.common.align.model.Entity) AbstractCellExplanation(eu.esdihumboldt.hale.common.align.model.impl.AbstractCellExplanation) Collectors(java.util.stream.Collectors) CellUtil(eu.esdihumboldt.hale.common.align.model.CellUtil) MessageFormat(java.text.MessageFormat) Entity(eu.esdihumboldt.hale.common.align.model.Entity)

Example 48 with Entity

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

the class DefaultCellMigrator method updateCell.

@Override
public MutableCell updateCell(final Cell originalCell, final AlignmentMigration migration, final MigrationOptions options, SimpleLog log) {
    MutableCell result = new DefaultCell(originalCell);
    SimpleLog cellLog = SimpleLog.all(log, new CellLog(result, CELL_LOG_CATEGORY));
    final AtomicBoolean replacedEntities = new AtomicBoolean(false);
    EntryTransformer<String, Entity, Entity> entityTransformer = new EntryTransformer<String, Entity, Entity>() {

        @Override
        public Entity transformEntry(String key, Entity value) {
            EntityDefinition org = value.getDefinition();
            Optional<EntityDefinition> replace = migration.entityReplacement(org, cellLog);
            EntityDefinition entity = replace.orElse(org);
            if (!Objects.equal(entity, org)) {
                replacedEntities.set(true);
            }
            if (entity instanceof PropertyEntityDefinition) {
                return new DefaultProperty((PropertyEntityDefinition) entity);
            } else if (entity instanceof TypeEntityDefinition) {
                return new DefaultType((TypeEntityDefinition) entity);
            } else {
                throw new IllegalStateException("Invalid entity definition for creating entity");
            }
        }
    };
    // update source entities
    if (options.updateSource() && result.getSource() != null && !result.getSource().isEmpty()) {
        result.setSource(ArrayListMultimap.create(Multimaps.transformEntries(result.getSource(), entityTransformer)));
    }
    // update target entities
    if (options.updateTarget() && result.getTarget() != null && !result.getTarget().isEmpty()) {
        result.setTarget(ArrayListMultimap.create(Multimaps.transformEntries(result.getTarget(), entityTransformer)));
    }
    // TODO anything else?
    postUpdateCell(result, options, replacedEntities.get(), cellLog);
    return result;
}
Also used : SimpleLog(eu.esdihumboldt.hale.common.core.report.SimpleLog) Entity(eu.esdihumboldt.hale.common.align.model.Entity) MutableCell(eu.esdihumboldt.hale.common.align.model.MutableCell) EntryTransformer(com.google.common.collect.Maps.EntryTransformer) DefaultType(eu.esdihumboldt.hale.common.align.model.impl.DefaultType) DefaultProperty(eu.esdihumboldt.hale.common.align.model.impl.DefaultProperty) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) 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) DefaultCell(eu.esdihumboldt.hale.common.align.model.impl.DefaultCell) CellLog(eu.esdihumboldt.hale.common.align.model.annotations.messages.CellLog)

Example 49 with Entity

use of eu.esdihumboldt.hale.common.align.model.Entity 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 50 with Entity

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

the class ClassificationMappingExplanation method getExplanation.

@Override
public String getExplanation(Cell cell, ServiceProvider provider, Locale locale) {
    Entity target = CellUtil.getFirstEntity(cell.getTarget());
    Entity source = CellUtil.getFirstEntity(cell.getSource());
    LookupTable lookup = ClassificationMappingUtil.getClassificationLookup(cell.getTransformationParameters(), provider);
    ListMultimap<Value, Value> revLookup = lookup.reverse();
    String notClassifiedAction = CellUtil.getFirstParameter(cell, PARAMETER_NOT_CLASSIFIED_ACTION).as(String.class);
    if (target != null && source != null) {
        StringBuilder mappingString = new StringBuilder();
        for (Value targetValue : revLookup.keySet()) {
            mappingString.append(quoteValue(targetValue.as(String.class), false));
            mappingString.append(' ');
            mappingString.append(getMessage("oneOf", locale));
            mappingString.append(' ');
            int i = 1;
            for (Value sourceValue : revLookup.get(targetValue)) {
                if (i != 1) {
                    mappingString.append(", ");
                }
                mappingString.append(quoteValue(sourceValue.as(String.class), false));
                i++;
            }
            mappingString.append(".\n");
        }
        String notClassifiedResult = "null";
        if (USE_SOURCE_ACTION.equals(notClassifiedAction)) {
            notClassifiedResult = getMessage("useSource", locale);
        } else if (notClassifiedAction != null && notClassifiedAction.startsWith(USE_FIXED_VALUE_ACTION_PREFIX)) {
            notClassifiedResult = quoteText(notClassifiedAction.substring(notClassifiedAction.indexOf(':') + 1), false);
        }
        return MessageFormat.format(getMessage("main", locale), formatEntity(target, false, true, locale), formatEntity(source, false, true, locale), mappingString.toString(), notClassifiedResult);
    }
    return null;
}
Also used : Entity(eu.esdihumboldt.hale.common.align.model.Entity) LookupTable(eu.esdihumboldt.hale.common.lookup.LookupTable) Value(eu.esdihumboldt.hale.common.core.io.Value)

Aggregations

Entity (eu.esdihumboldt.hale.common.align.model.Entity)64 Cell (eu.esdihumboldt.hale.common.align.model.Cell)25 EntityDefinition (eu.esdihumboldt.hale.common.align.model.EntityDefinition)21 PropertyEntityDefinition (eu.esdihumboldt.hale.common.align.model.impl.PropertyEntityDefinition)14 ArrayList (java.util.ArrayList)12 MutableCell (eu.esdihumboldt.hale.common.align.model.MutableCell)9 ParameterValue (eu.esdihumboldt.hale.common.align.model.ParameterValue)9 TypeEntityDefinition (eu.esdihumboldt.hale.common.align.model.impl.TypeEntityDefinition)9 TypeDefinition (eu.esdihumboldt.hale.common.schema.model.TypeDefinition)9 List (java.util.List)9 DefaultCell (eu.esdihumboldt.hale.common.align.model.impl.DefaultCell)8 DefaultProperty (eu.esdihumboldt.hale.common.align.model.impl.DefaultProperty)7 ChildContext (eu.esdihumboldt.hale.common.align.model.ChildContext)6 HashSet (java.util.HashSet)6 Locale (java.util.Locale)6 PropertyDefinition (eu.esdihumboldt.hale.common.schema.model.PropertyDefinition)5 FunctionDefinition (eu.esdihumboldt.hale.common.align.extension.function.FunctionDefinition)4 CellUtil (eu.esdihumboldt.hale.common.align.model.CellUtil)4 CellLog (eu.esdihumboldt.hale.common.align.model.annotations.messages.CellLog)4 AbstractCellExplanation (eu.esdihumboldt.hale.common.align.model.impl.AbstractCellExplanation)4