Search in sources :

Example 26 with Entity

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

the class DefaultAlignment method getTypeCells.

/**
 * @see Alignment#getTypeCells(Cell)
 */
@Override
public Collection<? extends Cell> getTypeCells(Cell queryCell) {
    Set<TypeEntityDefinition> sources = new HashSet<TypeEntityDefinition>();
    if (queryCell.getSource() != null) {
        Iterator<? extends Entity> it = queryCell.getSource().values().iterator();
        while (it.hasNext()) sources.add(AlignmentUtil.getTypeEntity(it.next().getDefinition()));
    }
    Entity targetEntity = CellUtil.getFirstEntity(queryCell.getTarget());
    TypeDefinition target = targetEntity == null ? null : targetEntity.getDefinition().getType();
    if (sources.isEmpty() && target == null)
        return getTypeCells();
    List<Cell> result = new ArrayList<Cell>();
    for (Cell typeCell : typeCells) {
        TypeDefinition typeCellTarget = CellUtil.getFirstEntity(typeCell.getTarget()).getDefinition().getType();
        if (target == null || DefinitionUtil.isSuperType(target, typeCellTarget)) {
            // target matches
            if (sources.isEmpty() || matchesSources(typeCell.getSource(), sources)) {
                // source matches, too
                result.add(typeCell);
            }
        }
    }
    return result;
}
Also used : Entity(eu.esdihumboldt.hale.common.align.model.Entity) ArrayList(java.util.ArrayList) Cell(eu.esdihumboldt.hale.common.align.model.Cell) MutableCell(eu.esdihumboldt.hale.common.align.model.MutableCell) BaseAlignmentCell(eu.esdihumboldt.hale.common.align.model.BaseAlignmentCell) HashSet(java.util.HashSet) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition)

Example 27 with Entity

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

the class DefaultAlignment method internalRemoveFromMaps.

/**
 * Removes a cell from the internal indexes, based on the given associated
 * entities.
 *
 * @param entities the cell entities (usually either source or target)
 * @param cell the cell to remove
 */
private void internalRemoveFromMaps(ListMultimap<String, ? extends Entity> entities, Cell cell) {
    if (entities == null) {
        return;
    }
    for (Entity entity : entities.values()) {
        EntityDefinition entityDef = entity.getDefinition();
        cellsPerEntity.remove(entityDef, cell);
        switch(entityDef.getSchemaSpace()) {
            case TARGET:
                cellsPerTargetType.remove(entityDef.getType(), cell);
                break;
            case SOURCE:
                cellsPerSourceType.remove(entityDef.getType(), cell);
                break;
            default:
                throw new IllegalStateException("Entity definition with illegal schema space encountered");
        }
    }
}
Also used : Entity(eu.esdihumboldt.hale.common.align.model.Entity) EntityDefinition(eu.esdihumboldt.hale.common.align.model.EntityDefinition)

Example 28 with Entity

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

the class AbstractMergeCellMigratorTest method assertCellTargetEquals.

// test helpers
/**
 * Check if the given cell's target matches the expected target.
 *
 * @param cell the cell to check
 * @param targetDef the expected target entity (simple definition as name
 *            list)
 */
protected void assertCellTargetEquals(Cell cell, List<String> targetDef) {
    Entity entity = CellUtil.getFirstEntity(cell.getTarget());
    assertNotNull("Target entity", entity);
    EntityDefinition def = entity.getDefinition();
    assertDefEquals(targetDef, def);
}
Also used : Entity(eu.esdihumboldt.hale.common.align.model.Entity) EntityDefinition(eu.esdihumboldt.hale.common.align.model.EntityDefinition)

Example 29 with Entity

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

the class JoinMergeMigrator method addSources.

private void addSources(MutableCell cell, EntityDefinition source, Cell match, SimpleLog log, boolean transferContext) {
    if (match.getSource() != null) {
        ListMultimap<String, Entity> sources = ArrayListMultimap.create(cell.getSource());
        for (Entry<String, ? extends Entity> entry : match.getSource().entries()) {
            Entity entity = entry.getValue();
            if (transferContext) {
                // transfer filter and contexts if possible
                EntityDefinition withContexts = AbstractMigration.translateContexts(source, entity.getDefinition(), log);
                entity = AlignmentUtil.createEntity(withContexts);
            }
            if (!sources.containsEntry(entry.getKey(), entity)) {
                // add if not already present
                sources.put(entry.getKey(), entity);
            }
        }
        cell.setSource(sources);
    } else {
        log.error("Match for source {0} is invalid and does not have a source", source.getDefinition());
        return;
    }
}
Also used : Entity(eu.esdihumboldt.hale.common.align.model.Entity) TypeEntityDefinition(eu.esdihumboldt.hale.common.align.model.impl.TypeEntityDefinition) EntityDefinition(eu.esdihumboldt.hale.common.align.model.EntityDefinition)

Example 30 with Entity

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

the class AbstractMergeCellMigrator method mergeSources.

/**
 * Update the cell sources.
 *
 * @param sources the old sources
 * @param mergeIndex the merge index
 * @param originalCell the original cell
 * @param migration the alignment migration (may be useful for cases where
 *            only entity replacement needs to be done)
 * @param getCellMigrator functions that yields a cell migrator for a
 *            function (may be useful for cases where only entity
 *            replacement needs to be done)
 * @param log the migration process log
 * @return the merged cell or cells
 */
protected Iterable<MutableCell> mergeSources(ListMultimap<String, ? extends Entity> sources, MergeIndex mergeIndex, Cell originalCell, AlignmentMigration migration, Function<String, CellMigrator> getCellMigrator, SimpleLog log) {
    // XXX relevant here at all?
    boolean transferBase = true;
    if (sources.size() == 1) {
        EntityDefinition source = sources.values().iterator().next().getDefinition();
        List<Cell> matches = mergeIndex.getCellsForTarget(source);
        List<String> storedMessages = new ArrayList<>();
        boolean inaccurateMatch = false;
        if (matches.isEmpty()) {
            // try to find match via parent (in specific cases)
            matches = findParentMatch(source, mergeIndex);
            if (!matches.isEmpty()) {
                inaccurateMatch = true;
            // message may not be added in every case, because it may be
            // a duplicate
            // storedMessages.add(MessageFormat
            // .format("Inaccurate match of {0} via parent entity", source));
            }
        }
        if (!matches.isEmpty()) {
            List<MutableCell> cells = new ArrayList<>();
            for (Cell match : matches) {
                // the original cell
                if (isDirectMatch(match)) {
                    MigrationOptions replaceSource = new MigrationOptionsImpl(true, false, transferBase);
                    cells.add(getCellMigrator.apply(originalCell.getTransformationIdentifier()).updateCell(originalCell, migration, replaceSource, log));
                } else // matching cell
                if (isDirectMatch(originalCell)) {
                    MigrationOptions replaceTarget = new MigrationOptionsImpl(false, true, transferBase);
                    AlignmentMigration cellMigration = new AbstractMigration() {

                        @Override
                        protected Optional<EntityDefinition> findMatch(EntityDefinition entity) {
                            Entity target = CellUtil.getFirstEntity(originalCell.getTarget());
                            if (target != null) {
                                return Optional.ofNullable(target.getDefinition());
                            }
                            return Optional.empty();
                        }
                    };
                    MutableCell newCell = getCellMigrator.apply(match.getTransformationIdentifier()).updateCell(match, cellMigration, replaceTarget, log);
                    SimpleLog cellLog = SimpleLog.all(log, new CellLog(newCell, CELL_LOG_CATEGORY));
                    // source of original cell may have
                    // filters/conditions/contexts that are not applied by
                    // changing the target
                    // try to apply source contexts
                    Entity originalSource = CellUtil.getFirstEntity(originalCell.getSource());
                    applySourceContexts(newCell, originalSource, cellLog);
                    cells.add(newCell);
                } else {
                    // otherwise, use custom logic to try to combine cells
                    MutableCell newCell = new DefaultCell(originalCell);
                    SimpleLog cellLog = SimpleLog.all(log, new CellLog(newCell, CELL_LOG_CATEGORY));
                    C context = newContext(originalCell);
                    // reset source
                    newCell.setSource(ArrayListMultimap.create());
                    if (inaccurateMatch) {
                        cellLog.warn(MessageFormat.format("Inaccurate match of {0} via parent entity", source));
                    }
                    mergeSource(newCell, sources.keys().iterator().next(), source, match, originalCell, cellLog, context, migration, mergeIndex);
                    finalize(newCell, migration, context, cellLog);
                    cells.add(newCell);
                }
            }
            if (!cells.isEmpty() && !storedMessages.isEmpty()) {
                // add stored messages
                cells.forEach(cell -> {
                    CellLog cLog = new CellLog(cell, CELL_LOG_CATEGORY);
                    storedMessages.forEach(msg -> cLog.warn(msg));
                });
            }
            return cells;
        } else {
            // no match -> remove?
            // rather add original + documentation
            MutableCell newCell = new DefaultCell(originalCell);
            SimpleLog cellLog = SimpleLog.all(log, new CellLog(newCell, CELL_LOG_CATEGORY));
            cellLog.warn("No match for source {0} found, unable to associate to new source schema", source);
            return Collections.singleton(newCell);
        }
    } else {
        // handle each source
        // collects messages in case all matches are direct matches
        List<String> directMessages = new ArrayList<>();
        // determine if all matches are direct
        boolean allDirect = sources.entries().stream().allMatch(source -> {
            List<Cell> matches = mergeIndex.getCellsForTarget(source.getValue().getDefinition());
            if (matches.isEmpty()) {
                directMessages.add(MessageFormat.format("No match was found for source {0}, please check how this can be compensated.", source.getValue().getDefinition()));
                // if there is no match, treat it as direct match
                return true;
            } else {
                if (matches.size() > 1) {
                    directMessages.add(MessageFormat.format("Multiple matches for source {0}, only one was taken into account", source.getValue().getDefinition()));
                }
                return isDirectMatch(matches.get(0));
            }
        });
        MutableCell newCell;
        if (allDirect) {
            // if the matching are all Retype/Rename, replace sources of
            // the original cell
            MigrationOptions replaceSource = new MigrationOptionsImpl(true, false, transferBase);
            newCell = getCellMigrator.apply(originalCell.getTransformationIdentifier()).updateCell(originalCell, migration, replaceSource, log);
            // add messages from match check
            SimpleLog cellLog = SimpleLog.all(log, new CellLog(newCell, CELL_LOG_CATEGORY));
            directMessages.forEach(msg -> cellLog.warn(msg));
        } else {
            // handle each source separately
            newCell = new DefaultCell(originalCell);
            SimpleLog cellLog = SimpleLog.all(log, new CellLog(newCell, CELL_LOG_CATEGORY));
            C context = newContext(originalCell);
            // reset source
            newCell.setSource(ArrayListMultimap.create());
            for (Entry<String, ? extends Entity> source : sources.entries()) {
                List<Cell> matches = mergeIndex.getCellsForTarget(source.getValue().getDefinition());
                if (!matches.isEmpty()) {
                    Cell match = matches.get(0);
                    mergeSource(newCell, source.getKey(), source.getValue().getDefinition(), match, originalCell, cellLog, context, migration, mergeIndex);
                    if (matches.size() > 1) {
                        // FIXME how can we deal w/ multiple matches?
                        cellLog.warn("Multiple matches for source {0}, only one was handled", source.getValue().getDefinition());
                    }
                } else {
                    // no match, just not add source?
                    cellLog.warn("No match was found for source {0}, please check how this can be compensated.", source.getValue().getDefinition());
                }
            }
            finalize(newCell, migration, context, cellLog);
        }
        return Collections.singleton(newCell);
    }
}
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) Optional(java.util.Optional) ArrayList(java.util.ArrayList) EntityDefinition(eu.esdihumboldt.hale.common.align.model.EntityDefinition) DefaultCell(eu.esdihumboldt.hale.common.align.model.impl.DefaultCell) MigrationOptionsImpl(eu.esdihumboldt.hale.common.align.migrate.impl.MigrationOptionsImpl) AlignmentMigration(eu.esdihumboldt.hale.common.align.migrate.AlignmentMigration) Cell(eu.esdihumboldt.hale.common.align.model.Cell) DefaultCell(eu.esdihumboldt.hale.common.align.model.impl.DefaultCell) MutableCell(eu.esdihumboldt.hale.common.align.model.MutableCell) CellLog(eu.esdihumboldt.hale.common.align.model.annotations.messages.CellLog) MigrationOptions(eu.esdihumboldt.hale.common.align.migrate.MigrationOptions)

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