Search in sources :

Example 56 with Cell

use of eu.esdihumboldt.hale.common.align.model.Cell 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)

Example 57 with Cell

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

the class DefaultAlignmentIOTest method testBaseAlignmentSaveLoad.

/**
 * Tests base alignment add, save and load.
 *
 * @throws Exception if an error occurs
 */
@Test
public void testBaseAlignmentSaveLoad() throws Exception {
    DefaultAlignment baseAlignment = new DefaultAlignment();
    MutableAlignment alignment = new DefaultAlignment();
    Schema schema = TestUtil.loadSchema(getClass().getResource("/testdata/simple/t1.xsd").toURI());
    Iterator<? extends TypeDefinition> iter = schema.getMappingRelevantTypes().iterator();
    TypeDefinition t = iter.next();
    if (!t.getName().getLocalPart().equals("T1")) {
        t = iter.next();
    }
    DefaultCell cell1 = new DefaultCell();
    cell1.setTransformationIdentifier("trans1");
    ListMultimap<String, Type> source = ArrayListMultimap.create();
    source.put(null, new DefaultType(new TypeEntityDefinition(t, SchemaSpaceID.SOURCE, null)));
    cell1.setSource(source);
    ListMultimap<String, Type> target = ArrayListMultimap.create();
    target.put(null, new DefaultType(new TypeEntityDefinition(t, SchemaSpaceID.TARGET, null)));
    cell1.setTarget(target);
    DefaultCell cell2 = new DefaultCell();
    cell2.setTransformationIdentifier("trans2");
    List<ChildContext> childContext = new ArrayList<ChildContext>();
    PropertyDefinition child = DefinitionUtil.getChild(t, new QName("a1")).asProperty();
    childContext.add(new ChildContext(child));
    ListMultimap<String, Property> source2 = ArrayListMultimap.create();
    source2.put(null, new DefaultProperty(new PropertyEntityDefinition(t, childContext, SchemaSpaceID.SOURCE, null)));
    cell2.setSource(source2);
    ListMultimap<String, Property> target2 = ArrayListMultimap.create();
    target2.put(null, new DefaultProperty(new PropertyEntityDefinition(t, childContext, SchemaSpaceID.TARGET, null)));
    cell2.setTarget(target2);
    // add cell1 to base alignment
    baseAlignment.addCell(cell1);
    // save base alignment
    File baseAlignmentFile = tmp.newFile("alignment_base.xml");
    System.out.println(baseAlignmentFile.getAbsolutePath());
    saveAlignment(baseAlignment, new BufferedOutputStream(new FileOutputStream(baseAlignmentFile)));
    // add as base alignment to extended alignment
    addBaseAlignment(alignment, baseAlignmentFile.toURI(), schema, schema);
    assertEquals(1, alignment.getBaseAlignments().size());
    String usedPrefix = alignment.getBaseAlignments().keySet().iterator().next();
    assertEquals(1, alignment.getCells().size());
    assertEquals(usedPrefix + ":" + cell1.getId(), alignment.getCells().iterator().next().getId());
    // add cell2 to extended alignment
    alignment.addCell(cell2);
    assertEquals(2, alignment.getCells().size());
    assertEquals(1, alignment.getPropertyCells(cell1).size());
    // save extended alignment
    File alignmentFile = tmp.newFile("alignment_extended.xml");
    System.out.println(alignmentFile.getAbsolutePath());
    saveAlignment(alignment, new BufferedOutputStream(new FileOutputStream(alignmentFile)));
    // load extended
    MutableAlignment alignment2 = loadAlignment(new FileInputStream(alignmentFile), schema, schema);
    assertEquals(2, alignment2.getCells().size());
    assertEquals(1, alignment2.getTypeCells().size());
    Cell typeCell = alignment2.getTypeCells().iterator().next();
    assertTrue(typeCell instanceof BaseAlignmentCell);
    assertEquals(usedPrefix + ":" + cell1.getId(), typeCell.getId());
    assertEquals(1, alignment2.getPropertyCells(typeCell).size());
    assertFalse(alignment2.getPropertyCells(typeCell).iterator().next() instanceof BaseAlignmentCell);
}
Also used : Schema(eu.esdihumboldt.hale.common.schema.model.Schema) DefaultSchema(eu.esdihumboldt.hale.common.schema.model.impl.DefaultSchema) ArrayList(java.util.ArrayList) DefaultTypeDefinition(eu.esdihumboldt.hale.common.schema.model.impl.DefaultTypeDefinition) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition) TypeEntityDefinition(eu.esdihumboldt.hale.common.align.model.impl.TypeEntityDefinition) ChildContext(eu.esdihumboldt.hale.common.align.model.ChildContext) DefaultAlignment(eu.esdihumboldt.hale.common.align.model.impl.DefaultAlignment) Property(eu.esdihumboldt.hale.common.align.model.Property) DefaultProperty(eu.esdihumboldt.hale.common.align.model.impl.DefaultProperty) BufferedOutputStream(java.io.BufferedOutputStream) DefaultCell(eu.esdihumboldt.hale.common.align.model.impl.DefaultCell) BaseAlignmentCell(eu.esdihumboldt.hale.common.align.model.BaseAlignmentCell) Cell(eu.esdihumboldt.hale.common.align.model.Cell) ModifiableCell(eu.esdihumboldt.hale.common.align.model.ModifiableCell) MutableCell(eu.esdihumboldt.hale.common.align.model.MutableCell) DefaultType(eu.esdihumboldt.hale.common.align.model.impl.DefaultType) QName(javax.xml.namespace.QName) MutableAlignment(eu.esdihumboldt.hale.common.align.model.MutableAlignment) PropertyDefinition(eu.esdihumboldt.hale.common.schema.model.PropertyDefinition) DefaultProperty(eu.esdihumboldt.hale.common.align.model.impl.DefaultProperty) FileInputStream(java.io.FileInputStream) Type(eu.esdihumboldt.hale.common.align.model.Type) DefaultType(eu.esdihumboldt.hale.common.align.model.impl.DefaultType) BaseAlignmentCell(eu.esdihumboldt.hale.common.align.model.BaseAlignmentCell) PropertyEntityDefinition(eu.esdihumboldt.hale.common.align.model.impl.PropertyEntityDefinition) DefaultCell(eu.esdihumboldt.hale.common.align.model.impl.DefaultCell) FileOutputStream(java.io.FileOutputStream) File(java.io.File) Test(org.junit.Test)

Example 58 with Cell

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

the class MarkdownCellExplanationTest method testExplanation.

@Test
public void testExplanation() {
    TestExplanation2 exp = new TestExplanation2();
    Cell cell = createTestCell();
    String expected = // 
    "Source 'source1Type'\n" + // 
    "Source 'source1Type'\n" + // 
    "Target 'target1Type'\n" + // 
    "test 1\n" + // 
    "test 2\n" + "pattern 3";
    String expl = exp.getExplanation(cell, null, Locale.getDefault());
    org.junit.Assert.assertNotNull(expl);
    assertEquals(expected, expl);
}
Also used : TestExplanation2(eu.esdihumboldt.hale.common.align.model.impl.mdexpl.test.TestExplanation2) 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) Test(org.junit.Test)

Example 59 with Cell

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

the class AbstractAlignmentReader method postProcess.

/**
 * Post process the alignment after loading.
 *
 * @param alignment the alignment to process
 * @return the processed alignment
 */
protected MutableAlignment postProcess(MutableAlignment alignment) {
    /*
		 * Processing of core functions. This should eventually be handled
		 * through an extension point to allow external contributions.
		 */
    Collection<? extends Cell> originalCells = new ArrayList<>(alignment.getCells());
    for (Cell orgCell : originalCells) {
        // for backwards compatibility
        if (AssignFunction.ID.equals(orgCell.getTransformationIdentifier()) && orgCell.getSource() != null && !orgCell.getSource().isEmpty()) {
            // assign with a source assigned
            // -> replace by bound assign
            MutableCell newCell = new DefaultCell(orgCell);
            newCell.setTransformationIdentifier(AssignFunction.ID_BOUND);
            alignment.removeCell(orgCell);
            alignment.addCell(newCell);
        }
    }
    return alignment;
}
Also used : MutableCell(eu.esdihumboldt.hale.common.align.model.MutableCell) DefaultCell(eu.esdihumboldt.hale.common.align.model.impl.DefaultCell) ArrayList(java.util.ArrayList) 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)

Example 60 with Cell

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

the class AbstractBaseAlignmentLoader method getCell.

/**
 * Returns the cell in question or null, if it could not be found in which
 * case a suitable warning was generated.
 *
 * @param alignment the alignment which contains the cell
 * @param cellId the cell id
 * @param defaultPrefix the prefix to use if the cell id does not contain a
 *            prefix, may be <code>null</code>
 * @param prefixMapping the prefix map to transform the prefix of the cell
 *            id with, if it has one
 * @param reporter the I/O reporter to report any errors to, may be
 *            <code>null</code>
 * @return the cell in question or <code>null</code>
 */
private Cell getCell(Alignment alignment, String cellId, String defaultPrefix, Map<String, String> prefixMapping, IOReporter reporter) {
    String prefix = defaultPrefix;
    // check if the cell id references another base alignment
    int prefixSplit = cellId.indexOf(':');
    if (prefixSplit != -1) {
        prefix = prefixMapping.get(cellId.substring(0, prefixSplit));
        if (prefix == null) {
            reporter.warn(new IOMessageImpl("A modifier used an unknown cell prefix", null));
            return null;
        }
        cellId = cellId.substring(prefixSplit + 1);
    }
    if (prefix != null)
        cellId = prefix + ':' + cellId;
    Cell cell = alignment.getCell(cellId);
    if (cell == null)
        reporter.warn(new IOMessageImpl("A cell referenced by a modifier could not be found", null));
    return cell;
}
Also used : IOMessageImpl(eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl) Cell(eu.esdihumboldt.hale.common.align.model.Cell) ModifiableCell(eu.esdihumboldt.hale.common.align.model.ModifiableCell) MutableCell(eu.esdihumboldt.hale.common.align.model.MutableCell) BaseAlignmentCell(eu.esdihumboldt.hale.common.align.model.BaseAlignmentCell) UnmigratedCell(eu.esdihumboldt.hale.common.align.migrate.impl.UnmigratedCell)

Aggregations

Cell (eu.esdihumboldt.hale.common.align.model.Cell)123 ArrayList (java.util.ArrayList)33 MutableCell (eu.esdihumboldt.hale.common.align.model.MutableCell)28 ParameterValue (eu.esdihumboldt.hale.common.align.model.ParameterValue)28 Test (org.junit.Test)27 Entity (eu.esdihumboldt.hale.common.align.model.Entity)24 DefaultCell (eu.esdihumboldt.hale.common.align.model.impl.DefaultCell)24 EntityDefinition (eu.esdihumboldt.hale.common.align.model.EntityDefinition)18 BaseAlignmentCell (eu.esdihumboldt.hale.common.align.model.BaseAlignmentCell)16 TypeEntityDefinition (eu.esdihumboldt.hale.common.align.model.impl.TypeEntityDefinition)16 AlignmentService (eu.esdihumboldt.hale.ui.service.align.AlignmentService)16 TypeDefinition (eu.esdihumboldt.hale.common.schema.model.TypeDefinition)15 Alignment (eu.esdihumboldt.hale.common.align.model.Alignment)13 HashSet (java.util.HashSet)13 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)12 Type (eu.esdihumboldt.hale.common.align.model.Type)11 List (java.util.List)11 ModifiableCell (eu.esdihumboldt.hale.common.align.model.ModifiableCell)9 PropertyEntityDefinition (eu.esdihumboldt.hale.common.align.model.impl.PropertyEntityDefinition)9 Property (eu.esdihumboldt.hale.common.align.model.Property)8