Search in sources :

Example 1 with MutableAlignment

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

the class CityGMLPropagate method generateMapping.

private void generateMapping() {
    System.out.println("Indexing example cells...");
    // index all cells based on the target property name
    SetMultimap<String, Cell> bgisExamples = HashMultimap.create();
    SetMultimap<QName, Cell> cityGMLExamples = 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();
            // XXX check source?!
            if (entityDef.getDefinition() instanceof PropertyDefinition) {
                QName name = entityDef.getDefinition().getName();
                if (ADE_NS.equals(name.getNamespaceURI())) {
                    bgisExamples.put(name.getLocalPart(), cell);
                } else if (name.getNamespaceURI().startsWith(CITYGML_NAMESPACE_CORE)) {
                    // XXX only support level 1 properties?
                    cityGMLExamples.put(name, cell);
                } else
                    System.out.println("WARNING: ignoring cell with target property neither from CityGML nor from BGIS ADE");
            } 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);
    // collect ADE display names
    Set<String> adeTypeNames = new HashSet<String>();
    for (TypeDefinition type : featureTypes) {
        adeTypeNames.add(type.getDisplayName());
    }
    // collect possibly relevant target CityGML feature types
    for (TypeDefinition type : targetSchema.getTypes()) {
        if (type.getName().getNamespaceURI().startsWith(CITYGML_NAMESPACE_CORE) && BGISAppUtil.isFeatureType(type)) {
            if (!adeTypeNames.contains(type.getDisplayName())) {
                /*
					 * But ensure to only add those that do not share the
					 * display name with an ADE type, as in the feature map the
					 * type identification is only done on based on the display
					 * name, and ADE types take precedent.
					 */
                featureTypes.add(type);
            }
        }
    }
    // visit ADE properties and create cells
    System.out.println("Generating mapping from example cells for");
    String cellNote = MessageFormat.format("Generated through propagation of example cells on CityGML and BGIS ADE feature types.\n" + "{0,date,medium}", new Date());
    CityGMLPropagateVisitor visitor = new CityGMLPropagateVisitor(cityGMLSource, bgisExamples, cityGMLExamples, config, 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) QName(javax.xml.namespace.QName) 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) HashSet(java.util.HashSet)

Example 2 with MutableAlignment

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

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

the class DefaultAlignmentIOTest method testIDSaveLoad.

/**
 * Tests id generation, save and load.
 *
 * @throws Exception if an error occurs
 */
@Test
public void testIDSaveLoad() throws Exception {
    DefaultAlignment alignment = new DefaultAlignment();
    Schema schema = TestUtil.loadSchema(getClass().getResource("/testdata/simple/t1.xsd").toURI());
    DefaultCell cell = new DefaultCell();
    cell.setTransformationIdentifier("trans1");
    ListMultimap<String, Type> source = ArrayListMultimap.create();
    source.put(null, new DefaultType(new TypeEntityDefinition(schema.getMappingRelevantTypes().iterator().next(), SchemaSpaceID.SOURCE, null)));
    cell.setSource(source);
    ListMultimap<String, Type> target = ArrayListMultimap.create();
    target.put(null, new DefaultType(new TypeEntityDefinition(schema.getMappingRelevantTypes().iterator().next(), SchemaSpaceID.TARGET, null)));
    cell.setTarget(target);
    // add cell and check id generation
    assertNull(cell.getId());
    alignment.addCell(cell);
    assertNotNull(cell.getId());
    assertNotNull(alignment.getCell(cell.getId()));
    // save / load
    File alignmentFile = tmp.newFile("alignment.xml");
    System.out.println(alignmentFile.getAbsolutePath());
    saveAlignment(alignment, new BufferedOutputStream(new FileOutputStream(alignmentFile)));
    MutableAlignment alignment2 = loadAlignment(new FileInputStream(alignmentFile), schema, schema);
    // check cell id
    assertEquals(cell.getId(), alignment2.getCells().iterator().next().getId());
}
Also used : DefaultType(eu.esdihumboldt.hale.common.align.model.impl.DefaultType) Schema(eu.esdihumboldt.hale.common.schema.model.Schema) DefaultSchema(eu.esdihumboldt.hale.common.schema.model.impl.DefaultSchema) 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) TypeEntityDefinition(eu.esdihumboldt.hale.common.align.model.impl.TypeEntityDefinition) DefaultCell(eu.esdihumboldt.hale.common.align.model.impl.DefaultCell) FileOutputStream(java.io.FileOutputStream) DefaultAlignment(eu.esdihumboldt.hale.common.align.model.impl.DefaultAlignment) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream) Test(org.junit.Test)

Example 4 with MutableAlignment

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

the class JaxbAlignmentReader 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 {
        EntityResolver entityResolver = null;
        if (getServiceProvider() != null) {
            entityResolver = getServiceProvider().getService(EntityResolver.class);
        }
        alignment = JaxbAlignmentIO.load(in, reporter, getSourceSchema(), getTargetSchema(), getPathUpdater(), entityResolver, getServiceProvider());
    } 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) EntityResolver(eu.esdihumboldt.hale.common.align.io.EntityResolver) IOProviderConfigurationException(eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException) IOException(java.io.IOException)

Example 5 with MutableAlignment

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

the class OmlReader method loadAlignment.

@Override
protected MutableAlignment loadAlignment(ProgressIndicator progress, IOReporter reporter) throws IOProviderConfigurationException, IOException {
    try {
        progress.begin("Load ontology mapping file", ProgressIndicator.UNKNOWN);
        OmlRdfReader reader = new OmlRdfReader();
        Alignment alignment = reader.read(getSource().getLocation().toURL());
        AlignmentBean align = new AlignmentBean();
        List<CellBean> cells = new ArrayList<CellBean>();
        List<ICell> map = alignment.getMap();
        for (ICell cell : map) {
            // create a new CellBean for each ICell
            CellBean cellBean = new CellBean();
            IEntity entity = cell.getEntity1();
            IEntity entity2 = cell.getEntity2();
            // temporary list to be copied into the CellBean
            List<NamedEntityBean> temp_source = new ArrayList<NamedEntityBean>();
            List<NamedEntityBean> temp_target = new ArrayList<NamedEntityBean>();
            setBeanLists(entity, temp_source, getSourceSchema());
            setBeanLists(entity2, temp_target, getTargetSchema());
            // set source/target lists of the CellBean
            cellBean.setSource(temp_source);
            cellBean.setTarget(temp_target);
            // check if one of the entities has a transformation
            if (entity.getTransformation() != null || entity2.getTransformation() != null) {
                // entity 1 if it has the transformation
                if (entity.getTransformation() != null) {
                    setParameters(cellBean, entity, reporter, cell);
                    setTransformationId(cellBean, entity);
                } else {
                    // else set parameters and transformation id from entity
                    // 2
                    setParameters(cellBean, entity2, reporter, cell);
                    setTransformationId(cellBean, entity2);
                }
            }
            // add the CellBean to a list of CellBeans
            cells.add(cellBean);
        }
        // set the cells for the alignment after the all iterations
        align.setCells(cells);
        MutableAlignment mutableAlignment = align.createAlignment(reporter, getSourceSchema(), getTargetSchema(), new PathUpdate(null, null));
        reporter.setSuccess(true);
        return mutableAlignment;
    } finally {
        progress.end();
    }
}
Also used : IEntity(eu.esdihumboldt.hale.io.oml.internal.model.align.IEntity) ArrayList(java.util.ArrayList) MutableAlignment(eu.esdihumboldt.hale.common.align.model.MutableAlignment) ICell(eu.esdihumboldt.hale.io.oml.internal.model.align.ICell) MutableAlignment(eu.esdihumboldt.hale.common.align.model.MutableAlignment) Alignment(eu.esdihumboldt.hale.io.oml.internal.goml.align.Alignment) CellBean(eu.esdihumboldt.hale.common.align.io.impl.internal.CellBean) NamedEntityBean(eu.esdihumboldt.hale.common.align.io.impl.internal.NamedEntityBean) AlignmentBean(eu.esdihumboldt.hale.common.align.io.impl.internal.AlignmentBean) PathUpdate(eu.esdihumboldt.hale.common.core.io.PathUpdate) OmlRdfReader(eu.esdihumboldt.hale.io.oml.internal.goml.oml.io.OmlRdfReader)

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