Search in sources :

Example 11 with MutableAlignment

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

the class DefaultAlignmentIOTest method testCellDisableSaveLoad.

/**
 * Tests cell disable save and load.
 *
 * @throws Exception if an error occurs
 */
@Test
public void testCellDisableSaveLoad() 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();
    }
    // generate base alignment
    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> childContext2 = new ArrayList<ChildContext>();
    PropertyDefinition child2 = DefinitionUtil.getChild(t, new QName("a1")).asProperty();
    childContext2.add(new ChildContext(child2));
    ListMultimap<String, Property> source2 = ArrayListMultimap.create();
    source2.put(null, new DefaultProperty(new PropertyEntityDefinition(t, childContext2, SchemaSpaceID.SOURCE, null)));
    cell2.setSource(source2);
    ListMultimap<String, Property> target2 = ArrayListMultimap.create();
    target2.put(null, new DefaultProperty(new PropertyEntityDefinition(t, childContext2, SchemaSpaceID.TARGET, null)));
    cell2.setTarget(target2);
    DefaultCell cell3 = new DefaultCell();
    cell3.setTransformationIdentifier("trans3");
    List<ChildContext> childContext3 = new ArrayList<ChildContext>();
    PropertyDefinition child3 = DefinitionUtil.getChild(t, new QName("b1")).asProperty();
    childContext3.add(new ChildContext(child3));
    ListMultimap<String, Property> source3 = ArrayListMultimap.create();
    source3.put(null, new DefaultProperty(new PropertyEntityDefinition(t, childContext3, SchemaSpaceID.SOURCE, null)));
    cell3.setSource(source3);
    ListMultimap<String, Property> target3 = ArrayListMultimap.create();
    target3.put(null, new DefaultProperty(new PropertyEntityDefinition(t, childContext3, SchemaSpaceID.TARGET, null)));
    cell3.setTarget(target3);
    baseAlignment.addCell(cell1);
    baseAlignment.addCell(cell2);
    String baseDisableCellId = cell2.getId();
    baseAlignment.addCell(cell3);
    String extendedDisableCellId = cell3.getId();
    assertEquals(3, baseAlignment.getCells().size());
    Cell typeCell = baseAlignment.getTypeCells().iterator().next();
    assertEquals(2, baseAlignment.getPropertyCells(typeCell).size());
    // test disable, it should not be with the related property cells
    cell2.setDisabledFor(cell1, true);
    assertEquals(1, baseAlignment.getPropertyCells(typeCell).size());
    assertTrue(cell2.getDisabledFor().contains(cell1.getId()));
    cell2.setDisabledFor(cell1, false);
    assertFalse(cell2.getDisabledFor().contains(cell1.getId()));
    cell2.setDisabledFor(cell1, true);
    assertEquals(1, baseAlignment.getPropertyCells(typeCell).size());
    // save base alignment
    File baseAlignmentFile = tmp.newFile("alignment_base.xml");
    System.out.println(baseAlignmentFile.getAbsolutePath());
    saveAlignment(baseAlignment, new BufferedOutputStream(new FileOutputStream(baseAlignmentFile)));
    // load base alignment
    MutableAlignment baseAlignment2 = loadAlignment(new FileInputStream(baseAlignmentFile), schema, schema);
    typeCell = baseAlignment2.getTypeCells().iterator().next();
    assertEquals(3, baseAlignment2.getCells().size());
    // test again that it is still disabled
    assertEquals(1, baseAlignment2.getPropertyCells(typeCell).size());
    // disable the remaining enabled cell in extended alignment
    addBaseAlignment(alignment, baseAlignmentFile.toURI(), schema, schema);
    assertEquals(1, alignment.getBaseAlignments().size());
    String usedPrefix = alignment.getBaseAlignments().keySet().iterator().next();
    File alignmentFile = tmp.newFile("alignment_extended.xml");
    // check cells
    typeCell = alignment.getTypeCells().iterator().next();
    assertEquals(3, alignment.getCells().size());
    assertEquals(1, alignment.getPropertyCells(typeCell).size());
    // disable remaining cell
    ((ModifiableCell) alignment.getPropertyCells(typeCell, false, false).iterator().next()).setDisabledFor(typeCell, true);
    assertEquals(0, alignment.getPropertyCells(typeCell).size());
    // save / load extended alignment
    System.out.println(alignmentFile.getAbsolutePath());
    saveAlignment(alignment, new BufferedOutputStream(new FileOutputStream(alignmentFile)));
    // load extended
    MutableAlignment alignment2 = loadAlignment(new FileInputStream(alignmentFile), schema, schema);
    typeCell = alignment2.getTypeCells().iterator().next();
    // test disabled again
    assertEquals(3, alignment2.getCells().size());
    // test again that it is still disabled
    assertEquals(0, alignment2.getPropertyCells(typeCell).size());
    // more specifically test whether the disables come from base alignment
    // or extended alignment
    Cell baseDisableCell = alignment2.getCell(usedPrefix + ":" + baseDisableCellId);
    Cell extendedDisableCell = alignment2.getCell(usedPrefix + ":" + extendedDisableCellId);
    assertTrue(baseDisableCell instanceof BaseAlignmentCell);
    assertEquals(1, baseDisableCell.getDisabledFor().size());
    assertEquals(1, ((BaseAlignmentCell) baseDisableCell).getBaseDisabledFor().size());
    assertEquals(0, ((BaseAlignmentCell) baseDisableCell).getAdditionalDisabledFor().size());
    assertTrue(extendedDisableCell instanceof BaseAlignmentCell);
    assertEquals(1, extendedDisableCell.getDisabledFor().size());
    assertEquals(0, ((BaseAlignmentCell) extendedDisableCell).getBaseDisabledFor().size());
    assertEquals(1, ((BaseAlignmentCell) extendedDisableCell).getAdditionalDisabledFor().size());
}
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) 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) BufferedOutputStream(java.io.BufferedOutputStream) ModifiableCell(eu.esdihumboldt.hale.common.align.model.ModifiableCell) 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 12 with MutableAlignment

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

the class CastorAlignmentReader method loadAlignment.

/**
 * @see AbstractIOProvider#execute(ProgressIndicator, IOReporter)
 */
@Override
protected MutableAlignment loadAlignment(ProgressIndicator progress, IOReporter reporter) throws IOProviderConfigurationException, IOException {
    progress.begin("Load hale alignment", ProgressIndicator.UNKNOWN);
    InputStream in = getSource().getInput();
    MutableAlignment alignment = null;
    try {
        alignment = CastorAlignmentIO.load(in, reporter, getSourceSchema(), getTargetSchema(), getPathUpdater());
    } catch (Exception e) {
        reporter.error(new IOMessageImpl(e.getMessage(), e));
        reporter.setSuccess(false);
        return alignment;
    } finally {
        in.close();
    }
    progress.end();
    reporter.setSuccess(true);
    return alignment;
}
Also used : InputStream(java.io.InputStream) IOMessageImpl(eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl) MutableAlignment(eu.esdihumboldt.hale.common.align.model.MutableAlignment) IOProviderConfigurationException(eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException) IOException(java.io.IOException)

Example 13 with MutableAlignment

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

the class InlineTransformation method evaluate.

@Override
protected Object evaluate(String transformationIdentifier, TransformationEngine engine, ListMultimap<String, PropertyValue> variables, String resultName, PropertyEntityDefinition resultProperty, Map<String, String> executionParameters, TransformationLog log) throws TransformationException, NoResultException {
    List<PropertyValue> sources = variables.get(null);
    if (sources.isEmpty()) {
        throw new NoResultException("No source available to transform");
    }
    PropertyValue source = sources.get(0);
    Object sourceValue = source.getValue();
    if (sourceValue == null) {
        throw new NoResultException("Source value is null");
    }
    if (!(sourceValue instanceof Instance)) {
        throw new TransformationException("Sources for inline transformation must be instances");
    }
    Instance sourceInstance = (Instance) sourceValue;
    TypeDefinition sourceType = sourceInstance.getDefinition();
    // get the original alignment
    Alignment orgAlignment = getExecutionContext().getAlignment();
    MutableAlignment alignment = new DefaultAlignment(orgAlignment);
    // identify relevant type cell(s)
    MutableCell queryCell = new DefaultCell();
    ListMultimap<String, Type> sourceEntities = ArrayListMultimap.create();
    sourceEntities.put(null, new DefaultType(new TypeEntityDefinition(sourceType, SchemaSpaceID.SOURCE, null)));
    queryCell.setSource(sourceEntities);
    ListMultimap<String, Type> targetEntities = ArrayListMultimap.create();
    targetEntities.put(null, new DefaultType(new TypeEntityDefinition(resultProperty.getDefinition().getPropertyType(), SchemaSpaceID.TARGET, null)));
    queryCell.setTarget(targetEntities);
    Collection<? extends Cell> candidates = alignment.getTypeCells(queryCell);
    if (candidates.isEmpty()) {
        log.error(log.createMessage("No type transformations found for inline transformation", null));
        throw new NoResultException();
    }
    // filter alignment -> only keep relevant type relations
    List<Cell> allTypeCells = new ArrayList<>(alignment.getTypeCells());
    for (Cell cell : allTypeCells) {
        // remove cell
        alignment.removeCell(cell);
        if (!cell.getTransformationMode().equals(TransformationMode.disabled)) {
            // only readd if not disabled
            MutableCell copy = new DefaultCell(cell);
            if (candidates.contains(cell)) {
                // readd as active
                copy.setTransformationMode(TransformationMode.active);
            } else {
                // readd as passive
                copy.setTransformationMode(TransformationMode.passive);
            }
            alignment.addCell(copy);
        }
    }
    // prepare transformation input/output
    DefaultInstanceCollection sourceInstances = new DefaultInstanceCollection();
    sourceInstances.add(sourceInstance);
    DefaultInstanceSink target = new DefaultInstanceSink();
    // run transformation
    TransformationService ts = getExecutionContext().getService(TransformationService.class);
    if (ts == null) {
        throw new TransformationException("Transformation service not available for inline transformation");
    }
    ProgressIndicator progressIndicator = new LogProgressIndicator();
    TransformationReport report = ts.transform(alignment, sourceInstances, new ThreadSafeInstanceSink<InstanceSink>(target), getExecutionContext(), progressIndicator);
    // copy report messages
    log.importMessages(report);
    if (!report.isSuccess()) {
        // copy report messages
        log.importMessages(report);
        throw new TransformationException("Inline transformation failed");
    }
    // extract result
    List<Instance> targetList = target.getInstances();
    if (targetList.isEmpty()) {
        log.error(log.createMessage("Inline transformation yielded no result", null));
        throw new NoResultException("No result from inline transformation");
    }
    if (targetList.size() > 1) {
        log.error(log.createMessage("Inline transformation yielded multiple results, only first result is used", null));
    }
    return targetList.get(0);
}
Also used : MutableCell(eu.esdihumboldt.hale.common.align.model.MutableCell) Instance(eu.esdihumboldt.hale.common.instance.model.Instance) DefaultInstanceSink(eu.esdihumboldt.hale.common.align.transformation.service.impl.DefaultInstanceSink) ArrayList(java.util.ArrayList) NoResultException(eu.esdihumboldt.hale.common.align.transformation.function.impl.NoResultException) DefaultInstanceCollection(eu.esdihumboldt.hale.common.instance.model.impl.DefaultInstanceCollection) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition) Alignment(eu.esdihumboldt.hale.common.align.model.Alignment) MutableAlignment(eu.esdihumboldt.hale.common.align.model.MutableAlignment) DefaultAlignment(eu.esdihumboldt.hale.common.align.model.impl.DefaultAlignment) TypeEntityDefinition(eu.esdihumboldt.hale.common.align.model.impl.TypeEntityDefinition) LogProgressIndicator(eu.esdihumboldt.hale.common.core.io.impl.LogProgressIndicator) ProgressIndicator(eu.esdihumboldt.hale.common.core.io.ProgressIndicator) DefaultAlignment(eu.esdihumboldt.hale.common.align.model.impl.DefaultAlignment) TransformationService(eu.esdihumboldt.hale.common.align.transformation.service.TransformationService) 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) TransformationException(eu.esdihumboldt.hale.common.align.transformation.function.TransformationException) TransformationReport(eu.esdihumboldt.hale.common.align.transformation.report.TransformationReport) DefaultType(eu.esdihumboldt.hale.common.align.model.impl.DefaultType) PropertyValue(eu.esdihumboldt.hale.common.align.transformation.function.PropertyValue) MutableAlignment(eu.esdihumboldt.hale.common.align.model.MutableAlignment) LogProgressIndicator(eu.esdihumboldt.hale.common.core.io.impl.LogProgressIndicator) Type(eu.esdihumboldt.hale.common.align.model.Type) DefaultType(eu.esdihumboldt.hale.common.align.model.impl.DefaultType) DefaultCell(eu.esdihumboldt.hale.common.align.model.impl.DefaultCell) ThreadSafeInstanceSink(eu.esdihumboldt.hale.common.align.transformation.service.impl.ThreadSafeInstanceSink) DefaultInstanceSink(eu.esdihumboldt.hale.common.align.transformation.service.impl.DefaultInstanceSink) InstanceSink(eu.esdihumboldt.hale.common.align.transformation.service.InstanceSink)

Example 14 with MutableAlignment

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

the class AlignmentServiceUndoSupport method clean.

/**
 * @see AlignmentServiceDecorator#clean()
 */
@Override
public synchronized void clean() {
    // XXX problem: what about cleans that should not be undone? e.g. when
    // the schemas have changed
    // XXX -> currently on project clean the workbench history is reset
    Alignment alignment = getAlignment();
    if (alignment.getCells().isEmpty()) {
        return;
    }
    if (alignment instanceof MutableAlignment) {
        /*
			 * As long as there is no copy constructor in DefaultAlignment, undo
			 * only supported if the current alignment is a MutableAlignment.
			 */
        IUndoableOperation operation = new CleanOperation((MutableAlignment) alignment);
        executeOperation(operation);
    } else {
        super.clean();
    }
}
Also used : Alignment(eu.esdihumboldt.hale.common.align.model.Alignment) MutableAlignment(eu.esdihumboldt.hale.common.align.model.MutableAlignment) DefaultAlignment(eu.esdihumboldt.hale.common.align.model.impl.DefaultAlignment) IUndoableOperation(org.eclipse.core.commands.operations.IUndoableOperation) MutableAlignment(eu.esdihumboldt.hale.common.align.model.MutableAlignment)

Aggregations

MutableAlignment (eu.esdihumboldt.hale.common.align.model.MutableAlignment)14 DefaultAlignment (eu.esdihumboldt.hale.common.align.model.impl.DefaultAlignment)11 MutableCell (eu.esdihumboldt.hale.common.align.model.MutableCell)9 Cell (eu.esdihumboldt.hale.common.align.model.Cell)8 TypeEntityDefinition (eu.esdihumboldt.hale.common.align.model.impl.TypeEntityDefinition)8 TypeDefinition (eu.esdihumboldt.hale.common.schema.model.TypeDefinition)7 DefaultCell (eu.esdihumboldt.hale.common.align.model.impl.DefaultCell)6 Type (eu.esdihumboldt.hale.common.align.model.Type)5 DefaultType (eu.esdihumboldt.hale.common.align.model.impl.DefaultType)5 ArrayList (java.util.ArrayList)5 PropertyDefinition (eu.esdihumboldt.hale.common.schema.model.PropertyDefinition)4 DefaultSchema (eu.esdihumboldt.hale.common.schema.model.impl.DefaultSchema)4 BufferedOutputStream (java.io.BufferedOutputStream)4 File (java.io.File)4 FileInputStream (java.io.FileInputStream)4 FileOutputStream (java.io.FileOutputStream)4 QName (javax.xml.namespace.QName)4 Test (org.junit.Test)4 BaseAlignmentCell (eu.esdihumboldt.hale.common.align.model.BaseAlignmentCell)3 ModifiableCell (eu.esdihumboldt.hale.common.align.model.ModifiableCell)3