Search in sources :

Example 6 with MutableAlignment

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

the class GenerateDefaults method generateMapping.

private void generateMapping() {
    System.out.println("Generating default value mapping cells for");
    // collect all ADE feature types
    List<TypeDefinition> featureTypes = BGISAppUtil.getADEFeatureTypes(schema);
    // visit ADE properties and create cells
    DefaultsVisitor defs = new DefaultsVisitor(defaultValues);
    for (TypeDefinition type : featureTypes) {
        System.out.println(type.getDisplayName() + "...");
        defs.accept(new TypeEntityDefinition(type, SchemaSpaceID.TARGET, null));
    }
    if (defs.getCells().isEmpty()) {
        System.out.println("WARNING: no cells were created");
    } else {
        System.out.println(defs.getCells().size() + " cells were created.");
    }
    // create alignment
    MutableAlignment align = new DefaultAlignment();
    for (MutableCell cell : defs.getCells()) {
        align.addCell(cell);
    }
    this.alignment = align;
}
Also used : TypeEntityDefinition(eu.esdihumboldt.hale.common.align.model.impl.TypeEntityDefinition) MutableCell(eu.esdihumboldt.hale.common.align.model.MutableCell) MutableAlignment(eu.esdihumboldt.hale.common.align.model.MutableAlignment) DefaultAlignment(eu.esdihumboldt.hale.common.align.model.impl.DefaultAlignment) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition)

Example 7 with MutableAlignment

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

the class GenerateDuplicates method generateMapping.

private void generateMapping() {
    System.out.println("Indexing example cells...");
    // index all cells based on the target property name
    SetMultimap<String, Cell> exampleCells = HashMultimap.create();
    for (Cell cell : examples.getCells()) {
        if (cell.getTarget().size() == 1) {
            // only supports cells with one target
            EntityDefinition entityDef = CellUtil.getFirstEntity(cell.getTarget()).getDefinition();
            if (entityDef.getDefinition() instanceof PropertyDefinition) {
                if (ADE_NS.equals(entityDef.getDefinition().getName().getNamespaceURI())) {
                    exampleCells.put(entityDef.getDefinition().getName().getLocalPart(), cell);
                } else
                    System.out.println("WARNING: ignoring cell with non-ADE target property");
            } else
                System.out.println("WARNING: ignoring type cell");
        } else
            System.out.println("WARNING: ignoring cell with multiple or no targets");
    }
    // collect all ADE feature types
    List<TypeDefinition> featureTypes = BGISAppUtil.getADEFeatureTypes(targetSchema);
    // visit ADE properties and create cells
    System.out.println("Generating mapping from example cells for");
    String cellNote = MessageFormat.format("Generated through duplication of example cells on BGIS ADE feature types.\n" + "{0,date,medium}", new Date());
    DuplicateVisitor visitor = new DuplicateVisitor(exampleCells, cellNote);
    for (TypeDefinition type : featureTypes) {
        System.out.println(type.getDisplayName() + "...");
        visitor.accept(new TypeEntityDefinition(type, SchemaSpaceID.TARGET, null));
    }
    if (visitor.getCells().isEmpty()) {
        System.out.println("WARNING: no cells were created");
    } else {
        System.out.println(visitor.getCells().size() + " cells were created.");
    }
    // create alignment
    MutableAlignment align = new DefaultAlignment();
    for (MutableCell cell : visitor.getCells()) {
        align.addCell(cell);
    }
    this.alignment = align;
}
Also used : MutableCell(eu.esdihumboldt.hale.common.align.model.MutableCell) MutableAlignment(eu.esdihumboldt.hale.common.align.model.MutableAlignment) PropertyDefinition(eu.esdihumboldt.hale.common.schema.model.PropertyDefinition) Date(java.util.Date) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition) 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) DefaultAlignment(eu.esdihumboldt.hale.common.align.model.impl.DefaultAlignment) Cell(eu.esdihumboldt.hale.common.align.model.Cell) MutableCell(eu.esdihumboldt.hale.common.align.model.MutableCell)

Example 8 with MutableAlignment

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

the class DefaultAlignmentMigrator method updateAligmment.

@Override
public MutableAlignment updateAligmment(Alignment originalAlignment, AlignmentMigration migration, MigrationOptions options, SimpleLog log) {
    MutableAlignment result = new DefaultAlignment(originalAlignment);
    // XXX TODO adapt custom functions?!
    // result.getCustomPropertyFunctions();
    Collection<? extends Cell> cellList = new ArrayList<>(result.getCells());
    for (Cell cell : cellList) {
        // XXX
        if (cell instanceof MutableCell) {
            CellMigrator cm = getCellMigrator(cell.getTransformationIdentifier());
            MutableCell newCell = cm.updateCell(cell, migration, options, log);
            MigrationUtil.removeIdPrefix(newCell, options.transferBase(), options.transferBase());
            result.removeCell(cell);
            if (newCell != null) {
                result.addCell(newCell);
            }
        } else {
            // XXX can we deal with other cases? (Base alignment cells)
            if (options.transferBase()) {
                // include base alignment cell as mutable mapping cell
                CellMigrator cm = getCellMigrator(cell.getTransformationIdentifier());
                MutableCell newCell = cm.updateCell(cell, migration, options, log);
                MigrationUtil.removeIdPrefix(newCell, true, true);
                result.removeCell(cell);
                if (newCell != null) {
                    result.addCell(newCell);
                }
            }
        }
    }
    if (options.transferBase()) {
        MigrationUtil.removeBaseCells(result);
    } else {
    // does something need to be done to correctly retain base
    // alignments?
    }
    return result;
}
Also used : MutableCell(eu.esdihumboldt.hale.common.align.model.MutableCell) ArrayList(java.util.ArrayList) MutableAlignment(eu.esdihumboldt.hale.common.align.model.MutableAlignment) DefaultAlignment(eu.esdihumboldt.hale.common.align.model.impl.DefaultAlignment) CellMigrator(eu.esdihumboldt.hale.common.align.migrate.CellMigrator) Cell(eu.esdihumboldt.hale.common.align.model.Cell) MutableCell(eu.esdihumboldt.hale.common.align.model.MutableCell)

Example 9 with MutableAlignment

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

the class EffectiveMapping method expand.

/**
 * Convert an alignment into an expanded version where all effective
 * mappings are defined explicitly, i.e. mappings effective due to
 * inheritance are converted to use the respective sub-types. Also, base
 * alignment cells are converted into normal cells.
 *
 * @param alignment the alignment to convert
 * @return the expanded copy of the alignment
 */
public static MutableAlignment expand(Alignment alignment) {
    MutableAlignment result = new DefaultAlignment(alignment);
    // remove base alignment cells keeping custom functions
    MigrationUtil.removeBaseCells(result);
    // remove other cells
    result.clearCells();
    // transfer cells based on effective mapping
    // set of all cells used as they are in the resulting alignment
    Set<Cell> usedAsIs = new HashSet<>();
    for (Cell typeCell : alignment.getTypeCells()) {
        // transfer type cell unchanged
        MutableCell typeCellNew = new DefaultCell(typeCell);
        MigrationUtil.removeIdPrefix(typeCellNew, true, true);
        result.addCell(typeCellNew);
        usedAsIs.add(typeCell);
        Collection<? extends Cell> propertyCells = alignment.getPropertyCells(typeCell, true, false);
        for (Cell propertyCell : propertyCells) {
            // FIXME what does this do in case of a join where there are
            // potentially multiple cells to be handled?
            Cell reparented = AlignmentUtil.reparentCell(propertyCell, typeCell, true);
            if (reparented == propertyCell) {
                // use as is
                if (!usedAsIs.contains(propertyCell)) {
                    // only add if not done yet
                    // transfer unchanged
                    MutableCell newCell = new DefaultCell(propertyCell);
                    MigrationUtil.removeIdPrefix(newCell, true, true);
                    result.addCell(newCell);
                    usedAsIs.add(propertyCell);
                }
            } else {
                // inherited cell
                // add the reparented cell
                // TODO check if similar cell has been added already?
                MutableCell rCell = (MutableCell) reparented;
                MigrationUtil.removeIdPrefix(rCell, true, true);
                // avoid ID collision
                // no updates needed in other places because it's a property
                // cell
                rCell.setId(rCell.getId() + '_' + typeCellNew.getId());
                result.addCell(rCell);
            }
        }
    }
    return result;
}
Also used : MutableCell(eu.esdihumboldt.hale.common.align.model.MutableCell) DefaultCell(eu.esdihumboldt.hale.common.align.model.impl.DefaultCell) MutableAlignment(eu.esdihumboldt.hale.common.align.model.MutableAlignment) DefaultAlignment(eu.esdihumboldt.hale.common.align.model.impl.DefaultAlignment) 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) HashSet(java.util.HashSet)

Example 10 with MutableAlignment

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

the class DefaultAlignmentIOTest method testSaveLoad.

/**
 * Test saving and loading an example alignment
 *
 * @throws Exception if an error occurs
 */
@Test
public void testSaveLoad() throws Exception {
    // populate alignment
    MutableAlignment align = new DefaultAlignment();
    DefaultSchema source = new DefaultSchema("", null);
    DefaultSchema target = new DefaultSchema("", null);
    // cell 1
    MutableCell cell1 = new DefaultCell();
    String id1;
    cell1.setTransformationIdentifier(id1 = "trans1");
    ListMultimap<String, ParameterValue> parameters1 = LinkedListMultimap.create();
    parameters1.put("test", new ParameterValue("1"));
    parameters1.put("test", new ParameterValue("2"));
    parameters1.put("t", new ParameterValue("3"));
    cell1.setTransformationParameters(parameters1);
    if (supportsTransformationModes()) {
        cell1.setTransformationMode(TransformationMode.passive);
    }
    ListMultimap<String, Type> source1 = ArrayListMultimap.create();
    QName source1TypeName;
    String source1EntityName;
    TypeDefinition sourceType1 = new DefaultTypeDefinition(source1TypeName = new QName("source1Type"));
    String filterText = "someproperty > 12";
    Filter filter = new FilterGeoCqlImpl(filterText);
    source1.put(source1EntityName = null, new DefaultType(new TypeEntityDefinition(sourceType1, SchemaSpaceID.SOURCE, filter)));
    cell1.setSource(source1);
    source.addType(sourceType1);
    ListMultimap<String, Type> target1 = ArrayListMultimap.create();
    QName target1TypeName;
    String target1EntityName;
    TypeDefinition targetType1 = new DefaultTypeDefinition(target1TypeName = new QName("http://some.name.space/t1", "target1Type"));
    target1.put(target1EntityName = "Some name", new DefaultType(new TypeEntityDefinition(targetType1, SchemaSpaceID.TARGET, null)));
    cell1.setTarget(target1);
    target.addType(targetType1);
    align.addCell(cell1);
    // cell 2
    MutableCell cell2 = new DefaultCell();
    String id2;
    cell2.setTransformationIdentifier(id2 = "trans2");
    ListMultimap<String, ParameterValue> parameters2 = LinkedListMultimap.create();
    parameters2.put("test", new ParameterValue("4"));
    parameters2.put("tx", new ParameterValue("5"));
    parameters2.put("tx", new ParameterValue("6"));
    // complex parameter value
    if (supportsComplexParameters()) {
        TestAnnotation commentParam = new TestAnnotation();
        commentParam.setAuthor("Gerd");
        commentParam.setComment("Should a comment really be used as parameter?");
        parameters2.put("comment", new ParameterValue(Value.complex(commentParam)));
    }
    cell2.setTransformationParameters(parameters2);
    ListMultimap<String, Type> target2 = ArrayListMultimap.create();
    TypeDefinition targetType2 = new DefaultTypeDefinition(new QName("target2Type"));
    target2.put("Some other name", new DefaultType(new TypeEntityDefinition(targetType2, SchemaSpaceID.TARGET, null)));
    cell2.setTarget(target2);
    target.addType(targetType2);
    align.addCell(cell2);
    TestAnnotation ann1 = null;
    TestAnnotation ann2 = null;
    if (supportsAnnotations()) {
        // add some annotations
        ann1 = (TestAnnotation) cell2.addAnnotation("test");
        ann1.setAuthor("Simon");
        ann1.setComment("I have really no idea what I did here");
        ann2 = (TestAnnotation) cell2.addAnnotation("test");
        ann2.setAuthor("Hans");
        ann2.setComment("Me neither");
    }
    String doc1 = "This cell was created in memory of...\nSorry, forgotten.";
    String tag1 = "This is a tag";
    String tag2 = "awesome";
    if (supportsDocumentation()) {
        cell1.getDocumentation().put(null, doc1);
        cell1.getDocumentation().put("tag", tag1);
        cell1.getDocumentation().put("tag", tag2);
    }
    // write alignment
    File alignmentFile = tmp.newFile("alignment.xml");
    System.out.println(alignmentFile.getAbsolutePath());
    saveAlignment(align, new BufferedOutputStream(new FileOutputStream(alignmentFile)));
    // load alignment
    // TODO use and check reporter?
    MutableAlignment align2 = loadAlignment(new FileInputStream(alignmentFile), source, target);
    // compare loaded alignment
    Collection<? extends Cell> cells = align2.getCells();
    assertFalse(cells.isEmpty());
    Iterator<? extends Cell> it = cells.iterator();
    // cell 1
    Cell ncell1 = it.next();
    assertNotNull(ncell1);
    assertEquals(id1, ncell1.getTransformationIdentifier());
    // documentation
    if (supportsDocumentation()) {
        assertEquals(3, ncell1.getDocumentation().size());
        assertEquals(doc1, ncell1.getDocumentation().get(null).get(0));
        assertEquals(tag1, ncell1.getDocumentation().get("tag").get(0));
        assertEquals(tag2, ncell1.getDocumentation().get("tag").get(1));
    }
    if (supportsTransformationModes()) {
        assertEquals(TransformationMode.passive, ncell1.getTransformationMode());
    }
    // source 1
    ListMultimap<String, ? extends Entity> source1Entities = ncell1.getSource();
    assertEquals(1, source1Entities.size());
    List<? extends Entity> s1list = source1Entities.get(source1EntityName);
    assertFalse(s1list.isEmpty());
    assertEquals(source1TypeName, s1list.get(0).getDefinition().getDefinition().getName());
    // filter
    assertEquals(filter, s1list.get(0).getDefinition().getFilter());
    // target 1
    ListMultimap<String, ? extends Entity> target1Entities = ncell1.getTarget();
    assertEquals(1, target1Entities.size());
    List<? extends Entity> t1list = target1Entities.get(target1EntityName);
    assertFalse(t1list.isEmpty());
    assertEquals(target1TypeName, t1list.get(0).getDefinition().getDefinition().getName());
    // cell 2
    Cell ncell2 = it.next();
    assertNotNull(ncell2);
    assertEquals(id2, ncell2.getTransformationIdentifier());
    // parameters
    ListMultimap<String, ParameterValue> param2 = ncell2.getTransformationParameters();
    if (!supportsComplexParameters()) {
        assertEquals(2, param2.keySet().size());
        assertEquals(3, param2.values().size());
    } else {
        assertEquals(3, param2.keySet().size());
        assertEquals(4, param2.values().size());
        ParameterValue complexParam = param2.get("comment").get(0);
        assertTrue(complexParam.getValue() instanceof TestAnnotation);
    }
    // annotations
    if (supportsAnnotations()) {
        List<?> annotations = ncell2.getAnnotations("test");
        assertEquals(2, annotations.size());
        TestAnnotation nann1 = (TestAnnotation) annotations.get(0);
        assertEquals(ann1, nann1);
        TestAnnotation nann2 = (TestAnnotation) annotations.get(1);
        assertEquals(ann2, nann2);
    }
}
Also used : MutableCell(eu.esdihumboldt.hale.common.align.model.MutableCell) FilterGeoCqlImpl(eu.esdihumboldt.hale.common.filter.FilterGeoCqlImpl) DefaultTypeDefinition(eu.esdihumboldt.hale.common.schema.model.impl.DefaultTypeDefinition) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition) DefaultTypeDefinition(eu.esdihumboldt.hale.common.schema.model.impl.DefaultTypeDefinition) TypeEntityDefinition(eu.esdihumboldt.hale.common.align.model.impl.TypeEntityDefinition) DefaultSchema(eu.esdihumboldt.hale.common.schema.model.impl.DefaultSchema) DefaultAlignment(eu.esdihumboldt.hale.common.align.model.impl.DefaultAlignment) 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) ParameterValue(eu.esdihumboldt.hale.common.align.model.ParameterValue) DefaultType(eu.esdihumboldt.hale.common.align.model.impl.DefaultType) QName(javax.xml.namespace.QName) MutableAlignment(eu.esdihumboldt.hale.common.align.model.MutableAlignment) FileInputStream(java.io.FileInputStream) 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) Filter(eu.esdihumboldt.hale.common.instance.model.Filter) FileOutputStream(java.io.FileOutputStream) File(java.io.File) Test(org.junit.Test)

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