Search in sources :

Example 16 with DefaultCell

use of eu.esdihumboldt.hale.common.align.model.impl.DefaultCell 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 17 with DefaultCell

use of eu.esdihumboldt.hale.common.align.model.impl.DefaultCell 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 18 with DefaultCell

use of eu.esdihumboldt.hale.common.align.model.impl.DefaultCell 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 19 with DefaultCell

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

the class AppSchemaMappingTest method testXrefJoinHandler.

@Test
public void testXrefJoinHandler() {
    DefaultCell joinCell = buildJoinCell(null);
    // create minimal alignment and pass it to JoinHandler
    DefaultCell renameCell = new DefaultCell();
    renameCell.setTransformationIdentifier(RenameFunction.ID);
    renameCell.setSource(getUnitIdSourceProperty(unitDenormType));
    renameCell.setTarget(getNestedUnitHrefTargetProperty());
    DefaultAlignment alignment = new DefaultAlignment();
    alignment.addCell(joinCell);
    alignment.addCell(renameCell);
    processJoinAlignment(alignment, null);
    // logMapping(mappingWrapper.getMainMapping());
    List<FeatureTypeMapping> ftMappings = mappingWrapper.getMainMapping().getTypeMappings().getFeatureTypeMapping();
    assertEquals(2, ftMappings.size());
    FeatureTypeMapping lcdMapping = null, lcuMapping = null;
    for (FeatureTypeMapping ftMapping : ftMappings) {
        if (SOURCE_DATASET.equals(ftMapping.getSourceType()) && "lcv:LandCoverDataset".equals(ftMapping.getTargetElement())) {
            lcdMapping = ftMapping;
        }
        if (SOURCE_UNIT_DENORM.equals(ftMapping.getSourceType()) && "lcv:LandCoverUnit".equals(ftMapping.getTargetElement())) {
            lcuMapping = ftMapping;
        }
    }
    assertNotNull(lcdMapping);
    assertNotNull(lcuMapping);
    // check feature chaining configuration
    List<AttributeMappingType> lcdAttrMappings = lcdMapping.getAttributeMappings().getAttributeMapping();
    List<AttributeMappingType> lcuAttrMappings = lcuMapping.getAttributeMappings().getAttributeMapping();
    assertNotNull(lcdAttrMappings);
    assertNotNull(lcuAttrMappings);
    assertEquals(1, lcdAttrMappings.size());
    assertEquals(1, lcuAttrMappings.size());
    AttributeMappingType containerMapping = lcdAttrMappings.get(0);
    assertEquals("lcv:member", containerMapping.getTargetAttribute());
    assertEquals("lcv:LandCoverUnit", containerMapping.getSourceExpression().getLinkElement());
    assertEquals("FEATURE_LINK[1]", containerMapping.getSourceExpression().getLinkField());
    assertEquals(SOURCE_DATASET_ID, containerMapping.getSourceExpression().getOCQL());
    assertTrue(containerMapping.isIsMultiple());
    assertNotNull(containerMapping.getClientProperty());
    assertEquals(1, containerMapping.getClientProperty().size());
    assertEquals("xlink:href", containerMapping.getClientProperty().get(0).getName());
    assertEquals(SOURCE_UNIT_ID, containerMapping.getClientProperty().get(0).getValue());
    AttributeMappingType nestedMapping = lcuAttrMappings.get(0);
    assertEquals("FEATURE_LINK[1]", nestedMapping.getTargetAttribute());
    assertEquals(SOURCE_DATASET_ID, nestedMapping.getSourceExpression().getOCQL());
    assertNull(nestedMapping.getSourceExpression().getLinkElement());
    assertNull(nestedMapping.getSourceExpression().getLinkField());
}
Also used : AttributeMappingType(eu.esdihumboldt.hale.io.appschema.impl.internal.generated.app_schema.AttributeMappingType) DefaultCell(eu.esdihumboldt.hale.common.align.model.impl.DefaultCell) DefaultAlignment(eu.esdihumboldt.hale.common.align.model.impl.DefaultAlignment) FeatureTypeMapping(eu.esdihumboldt.hale.io.appschema.impl.internal.generated.app_schema.TypeMappingsPropertyType.FeatureTypeMapping) Test(org.junit.Test)

Example 20 with DefaultCell

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

the class AppSchemaMappingTest method testClassificationHandler.

@Test
public void testClassificationHandler() {
    final int FIRST_SOURCE = 1000;
    final String FIRST_TARGET = "http://www.example.com/first";
    final int SECOND_SOURCE = 2000;
    final String SECOND_TARGET = "http://www.example.com/second";
    final int THIRD_SOURCE = 3000;
    final String THIRD_TARGET = "http://www.example.com/third";
    final String FIXED_VALUE = "http://www.example.com/unknown";
    final String OCQL_PATTERN = "if_then_else(in(unit_id,%s), Recode(unit_id,%s), %s)";
    Cell typeCell = getDefaultTypeCell(unitDenormType, landCoverUnitType);
    DefaultCell cell = new DefaultCell();
    cell.setTransformationIdentifier(ClassificationMappingFunction.ID);
    cell.setSource(getUnitIdSourceProperty(unitDenormType));
    cell.setTarget(getMetaDataHrefTargetProperty());
    Map<Value, Value> tableValues = new HashMap<Value, Value>();
    tableValues.put(new StringValue(FIRST_SOURCE), new StringValue(FIRST_TARGET));
    tableValues.put(new StringValue(SECOND_SOURCE), new StringValue(SECOND_TARGET));
    tableValues.put(new StringValue(THIRD_SOURCE), new StringValue(THIRD_TARGET));
    LookupTable lookupTable = new LookupTableImpl(tableValues);
    ListMultimap<String, ParameterValue> parameters = ArrayListMultimap.create();
    parameters.put(ClassificationMappingFunction.PARAMETER_LOOKUPTABLE, new ParameterValue(new ComplexValue(lookupTable)));
    parameters.put(ClassificationMapping.PARAMETER_NOT_CLASSIFIED_ACTION, new ParameterValue(ClassificationMapping.USE_SOURCE_ACTION));
    cell.setTransformationParameters(parameters);
    StringBuilder inArgs = new StringBuilder();
    StringBuilder recodeArgs = new StringBuilder();
    int count = 0;
    for (Value sourceValue : tableValues.keySet()) {
        inArgs.append(sourceValue.as(String.class));
        recodeArgs.append(sourceValue).append(",'").append(tableValues.get(sourceValue)).append("'");
        if (count < tableValues.size() - 1) {
            inArgs.append(",");
            recodeArgs.append(",");
        }
        count++;
    }
    final String OCQL_USE_SOURCE = String.format(OCQL_PATTERN, inArgs.toString(), recodeArgs.toString(), "unit_id");
    final String OCQL_USE_NULL = String.format(OCQL_PATTERN, inArgs.toString(), recodeArgs.toString(), "Expression.NIL");
    final String OCQL_USE_FIXED = String.format(OCQL_PATTERN, inArgs.toString(), recodeArgs.toString(), "'" + FIXED_VALUE + "'");
    ClassificationHandler classificationHandler = new ClassificationHandler();
    AttributeMappingType attrMapping = classificationHandler.handlePropertyTransformation(typeCell, cell, new AppSchemaMappingContext(mappingWrapper));
    assertNotNull(attrMapping.getClientProperty());
    assertEquals(1, attrMapping.getClientProperty().size());
    assertEquals("xlink:href", attrMapping.getClientProperty().get(0).getName());
    assertEquals(OCQL_USE_SOURCE, attrMapping.getClientProperty().get(0).getValue());
    assertEquals(TARGET_METADATA, attrMapping.getTargetAttribute());
    // reset mapping
    initMapping();
    parameters.removeAll(ClassificationMapping.PARAMETER_NOT_CLASSIFIED_ACTION);
    parameters.put(ClassificationMapping.PARAMETER_NOT_CLASSIFIED_ACTION, new ParameterValue(ClassificationMapping.USE_NULL_ACTION));
    cell.setTransformationParameters(parameters);
    attrMapping = classificationHandler.handlePropertyTransformation(typeCell, cell, new AppSchemaMappingContext(mappingWrapper));
    assertNotNull(attrMapping.getClientProperty());
    assertEquals(1, attrMapping.getClientProperty().size());
    assertEquals("xlink:href", attrMapping.getClientProperty().get(0).getName());
    assertEquals(OCQL_USE_NULL, attrMapping.getClientProperty().get(0).getValue());
    assertEquals(TARGET_METADATA, attrMapping.getTargetAttribute());
    // reset mapping
    initMapping();
    parameters.removeAll(ClassificationMapping.PARAMETER_NOT_CLASSIFIED_ACTION);
    parameters.put(ClassificationMapping.PARAMETER_NOT_CLASSIFIED_ACTION, new ParameterValue(ClassificationMapping.USE_FIXED_VALUE_ACTION_PREFIX + FIXED_VALUE));
    cell.setTransformationParameters(parameters);
    attrMapping = classificationHandler.handlePropertyTransformation(typeCell, cell, new AppSchemaMappingContext(mappingWrapper));
    assertNotNull(attrMapping.getClientProperty());
    assertEquals(1, attrMapping.getClientProperty().size());
    assertEquals("xlink:href", attrMapping.getClientProperty().get(0).getName());
    assertEquals(OCQL_USE_FIXED, attrMapping.getClientProperty().get(0).getValue());
    assertEquals(TARGET_METADATA, attrMapping.getTargetAttribute());
}
Also used : ComplexValue(eu.esdihumboldt.hale.common.core.io.impl.ComplexValue) ParameterValue(eu.esdihumboldt.hale.common.align.model.ParameterValue) HashMap(java.util.HashMap) AttributeMappingType(eu.esdihumboldt.hale.io.appschema.impl.internal.generated.app_schema.AttributeMappingType) DefaultCell(eu.esdihumboldt.hale.common.align.model.impl.DefaultCell) ComplexValue(eu.esdihumboldt.hale.common.core.io.impl.ComplexValue) StringValue(eu.esdihumboldt.hale.common.core.io.impl.StringValue) Value(eu.esdihumboldt.hale.common.core.io.Value) ParameterValue(eu.esdihumboldt.hale.common.align.model.ParameterValue) LookupTable(eu.esdihumboldt.hale.common.lookup.LookupTable) AppSchemaMappingContext(eu.esdihumboldt.hale.io.appschema.writer.internal.mapping.AppSchemaMappingContext) StringValue(eu.esdihumboldt.hale.common.core.io.impl.StringValue) DefaultCell(eu.esdihumboldt.hale.common.align.model.impl.DefaultCell) Cell(eu.esdihumboldt.hale.common.align.model.Cell) ClassificationHandler(eu.esdihumboldt.hale.io.appschema.writer.internal.ClassificationHandler) LookupTableImpl(eu.esdihumboldt.hale.common.lookup.impl.LookupTableImpl) Test(org.junit.Test)

Aggregations

DefaultCell (eu.esdihumboldt.hale.common.align.model.impl.DefaultCell)47 Test (org.junit.Test)24 AttributeMappingType (eu.esdihumboldt.hale.io.appschema.impl.internal.generated.app_schema.AttributeMappingType)22 ParameterValue (eu.esdihumboldt.hale.common.align.model.ParameterValue)21 Cell (eu.esdihumboldt.hale.common.align.model.Cell)17 AppSchemaMappingContext (eu.esdihumboldt.hale.io.appschema.writer.internal.mapping.AppSchemaMappingContext)17 MutableCell (eu.esdihumboldt.hale.common.align.model.MutableCell)16 DefaultProperty (eu.esdihumboldt.hale.common.align.model.impl.DefaultProperty)15 TypeEntityDefinition (eu.esdihumboldt.hale.common.align.model.impl.TypeEntityDefinition)13 DefaultType (eu.esdihumboldt.hale.common.align.model.impl.DefaultType)12 Type (eu.esdihumboldt.hale.common.align.model.Type)11 ArrayList (java.util.ArrayList)10 Property (eu.esdihumboldt.hale.common.align.model.Property)9 DefaultAlignment (eu.esdihumboldt.hale.common.align.model.impl.DefaultAlignment)9 ClientProperty (eu.esdihumboldt.hale.io.appschema.impl.internal.generated.app_schema.AttributeMappingType.ClientProperty)9 Entity (eu.esdihumboldt.hale.common.align.model.Entity)8 PropertyEntityDefinition (eu.esdihumboldt.hale.common.align.model.impl.PropertyEntityDefinition)8 MutableAlignment (eu.esdihumboldt.hale.common.align.model.MutableAlignment)7 TypeDefinition (eu.esdihumboldt.hale.common.schema.model.TypeDefinition)7 AssignHandler (eu.esdihumboldt.hale.io.appschema.writer.internal.AssignHandler)6