Search in sources :

Example 6 with LookupTable

use of eu.esdihumboldt.hale.common.lookup.LookupTable 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)

Example 7 with LookupTable

use of eu.esdihumboldt.hale.common.lookup.LookupTable in project hale by halestudio.

the class ClassificationHandler method getSourceExpressionAsCQL.

/**
 * @see eu.esdihumboldt.hale.io.appschema.writer.internal.AbstractPropertyTransformationHandler#getSourceExpressionAsCQL()
 */
@Override
protected String getSourceExpressionAsCQL() {
    Property source = AppSchemaMappingUtils.getSourceProperty(propertyCell);
    PropertyDefinition sourceDef = source.getDefinition().getDefinition();
    Property target = AppSchemaMappingUtils.getTargetProperty(propertyCell);
    PropertyDefinition targetDef = target.getDefinition().getDefinition();
    String sourceName = source.getDefinition().getDefinition().getName().getLocalPart();
    ListMultimap<String, ParameterValue> parameters = propertyCell.getTransformationParameters();
    LookupTable lookup = ClassificationMappingUtil.getClassificationLookup(parameters, new ServiceManager(ServiceManager.SCOPE_PROJECT));
    if (lookup == null) {
        log.warn("No classification specified");
        return "''";
    } else {
        String cqlTemplate = "if_then_else(in(%s), Recode(%s,%s), %s)";
        // build args to Recode function
        StringBuilder recodeArgsBuilder = new StringBuilder();
        Map<Value, Value> valueMap = lookup.asMap();
        int counter = 0;
        for (Value sourceValue : valueMap.keySet()) {
            Value targetValue = valueMap.get(sourceValue);
            String sourceLiteral = asCqlLiteral(sourceDef, sourceValue.as(String.class));
            String targetLiteral = asCqlLiteral(targetDef, targetValue.as(String.class));
            recodeArgsBuilder.append(sourceLiteral).append(",").append(targetLiteral);
            if (counter < valueMap.size() - 1) {
                recodeArgsBuilder.append(",");
            }
            counter++;
        }
        String recodeArgs = recodeArgsBuilder.toString();
        // build args for in function
        List<String> values = new ArrayList<String>();
        for (Value v : valueMap.keySet()) {
            String valueLiteral = asCqlLiteral(sourceDef, v.as(String.class));
            values.add(valueLiteral);
        }
        values.add(0, sourceName);
        String inArgs = Joiner.on(",").join(values);
        // determine what to put in the "else" branch, based on
        // transformation parameters
        String elsePart = null;
        List<ParameterValue> notClassifiedParam = parameters.get(PARAMETER_NOT_CLASSIFIED_ACTION);
        String notClassifiedAction = null;
        if (notClassifiedParam != null && notClassifiedParam.size() > 0) {
            notClassifiedAction = notClassifiedParam.get(0).as(String.class);
        } else {
            notClassifiedAction = USE_NULL_ACTION;
        }
        if (USE_SOURCE_ACTION.equals(notClassifiedAction))
            elsePart = sourceName;
        else if (notClassifiedAction.startsWith(USE_FIXED_VALUE_ACTION_PREFIX))
            elsePart = asCqlLiteral(targetDef, notClassifiedAction.substring(notClassifiedAction.indexOf(':') + 1));
        else if (USE_NULL_ACTION.equals(notClassifiedAction))
            elsePart = "Expression.NIL";
        return String.format(cqlTemplate, inArgs, sourceName, recodeArgs, elsePart);
    }
}
Also used : ParameterValue(eu.esdihumboldt.hale.common.align.model.ParameterValue) ArrayList(java.util.ArrayList) PropertyDefinition(eu.esdihumboldt.hale.common.schema.model.PropertyDefinition) ServiceManager(eu.esdihumboldt.hale.common.core.service.ServiceManager) LookupTable(eu.esdihumboldt.hale.common.lookup.LookupTable) ParameterValue(eu.esdihumboldt.hale.common.align.model.ParameterValue) Value(eu.esdihumboldt.hale.common.core.io.Value) Property(eu.esdihumboldt.hale.common.align.model.Property)

Example 8 with LookupTable

use of eu.esdihumboldt.hale.common.lookup.LookupTable in project hale by halestudio.

the class ClassificationMappingExplanation method getExplanation.

@Override
public String getExplanation(Cell cell, ServiceProvider provider, Locale locale) {
    Entity target = CellUtil.getFirstEntity(cell.getTarget());
    Entity source = CellUtil.getFirstEntity(cell.getSource());
    LookupTable lookup = ClassificationMappingUtil.getClassificationLookup(cell.getTransformationParameters(), provider);
    ListMultimap<Value, Value> revLookup = lookup.reverse();
    String notClassifiedAction = CellUtil.getFirstParameter(cell, PARAMETER_NOT_CLASSIFIED_ACTION).as(String.class);
    if (target != null && source != null) {
        StringBuilder mappingString = new StringBuilder();
        for (Value targetValue : revLookup.keySet()) {
            mappingString.append(quoteValue(targetValue.as(String.class), false));
            mappingString.append(' ');
            mappingString.append(getMessage("oneOf", locale));
            mappingString.append(' ');
            int i = 1;
            for (Value sourceValue : revLookup.get(targetValue)) {
                if (i != 1) {
                    mappingString.append(", ");
                }
                mappingString.append(quoteValue(sourceValue.as(String.class), false));
                i++;
            }
            mappingString.append(".\n");
        }
        String notClassifiedResult = "null";
        if (USE_SOURCE_ACTION.equals(notClassifiedAction)) {
            notClassifiedResult = getMessage("useSource", locale);
        } else if (notClassifiedAction != null && notClassifiedAction.startsWith(USE_FIXED_VALUE_ACTION_PREFIX)) {
            notClassifiedResult = quoteText(notClassifiedAction.substring(notClassifiedAction.indexOf(':') + 1), false);
        }
        return MessageFormat.format(getMessage("main", locale), formatEntity(target, false, true, locale), formatEntity(source, false, true, locale), mappingString.toString(), notClassifiedResult);
    }
    return null;
}
Also used : Entity(eu.esdihumboldt.hale.common.align.model.Entity) LookupTable(eu.esdihumboldt.hale.common.lookup.LookupTable) Value(eu.esdihumboldt.hale.common.core.io.Value)

Example 9 with LookupTable

use of eu.esdihumboldt.hale.common.lookup.LookupTable in project hale by halestudio.

the class ClassificationMappingExplanation method getExplanationAsHtml.

@Override
public String getExplanationAsHtml(Cell cell, ServiceProvider provider, Locale locale) {
    Entity target = CellUtil.getFirstEntity(cell.getTarget());
    Entity source = CellUtil.getFirstEntity(cell.getSource());
    LookupTable lookup = ClassificationMappingUtil.getClassificationLookup(cell.getTransformationParameters(), provider);
    ListMultimap<Value, Value> revLookup = lookup.reverse();
    String notClassifiedAction = CellUtil.getFirstParameter(cell, PARAMETER_NOT_CLASSIFIED_ACTION).as(String.class);
    if (target != null && source != null) {
        StringBuilder mappingString = new StringBuilder();
        mappingString.append("<table border=\"1\"><tr><th>");
        mappingString.append(getMessage("captionSource", locale));
        mappingString.append("</th><th>");
        mappingString.append(getMessage("captionTarget", locale));
        mappingString.append("</th></tr>");
        for (Value targetValue : revLookup.keySet()) {
            mappingString.append("<tr><td>");
            int i = 1;
            for (Value sourceValue : revLookup.get(targetValue)) {
                if (i != 1) {
                    mappingString.append(", ");
                }
                mappingString.append(quoteText(sourceValue.as(String.class), true));
                i++;
            }
            mappingString.append("</td><td>");
            mappingString.append(quoteText(targetValue.as(String.class), true));
            mappingString.append("</td></tr>");
        }
        mappingString.append("</table>");
        String notClassifiedResult = "null";
        if (USE_SOURCE_ACTION.equals(notClassifiedAction))
            notClassifiedResult = getMessage("useSource", locale);
        else if (notClassifiedAction != null && notClassifiedAction.startsWith(USE_FIXED_VALUE_ACTION_PREFIX))
            notClassifiedResult = quoteText(notClassifiedAction.substring(notClassifiedAction.indexOf(':') + 1), true);
        return MessageFormat.format(getMessage("main", locale), formatEntity(target, true, true, locale), formatEntity(source, true, true, locale), mappingString.toString(), notClassifiedResult).replaceAll("\n", "<br />");
    }
    return null;
}
Also used : Entity(eu.esdihumboldt.hale.common.align.model.Entity) LookupTable(eu.esdihumboldt.hale.common.lookup.LookupTable) Value(eu.esdihumboldt.hale.common.core.io.Value)

Example 10 with LookupTable

use of eu.esdihumboldt.hale.common.lookup.LookupTable in project hale by halestudio.

the class LookupTableTypeTest method testComplexLookup.

/**
 * Test if a lookup table containing only complex values is the same when
 * converted to DOM and back again.
 */
@Test
public void testComplexLookup() {
    Map<Value, Value> values2 = createComplexLookup();
    LookupTable org2 = new LookupTableImpl(values2);
    // convert to DOM
    Element fragment = HaleIO.getComplexElement(org2);
    // convert back
    LookupTable conv = HaleIO.getComplexValue(fragment, LookupTable.class, null);
    checkTable(conv, values2);
}
Also used : Element(org.w3c.dom.Element) Value(eu.esdihumboldt.hale.common.core.io.Value) LookupTable(eu.esdihumboldt.hale.common.lookup.LookupTable) LookupTableImpl(eu.esdihumboldt.hale.common.lookup.impl.LookupTableImpl) Test(org.junit.Test)

Aggregations

LookupTable (eu.esdihumboldt.hale.common.lookup.LookupTable)13 Value (eu.esdihumboldt.hale.common.core.io.Value)12 LookupTableImpl (eu.esdihumboldt.hale.common.lookup.impl.LookupTableImpl)8 Test (org.junit.Test)6 ParameterValue (eu.esdihumboldt.hale.common.align.model.ParameterValue)4 Entity (eu.esdihumboldt.hale.common.align.model.Entity)2 StringReader (java.io.StringReader)2 StringWriter (java.io.StringWriter)2 HashMap (java.util.HashMap)2 StyledString (org.eclipse.jface.viewers.StyledString)2 Element (org.w3c.dom.Element)2 Cell (eu.esdihumboldt.hale.common.align.model.Cell)1 Property (eu.esdihumboldt.hale.common.align.model.Property)1 DefaultCell (eu.esdihumboldt.hale.common.align.model.impl.DefaultCell)1 PropertyValue (eu.esdihumboldt.hale.common.align.transformation.function.PropertyValue)1 NoResultException (eu.esdihumboldt.hale.common.align.transformation.function.impl.NoResultException)1 ComplexValue (eu.esdihumboldt.hale.common.core.io.impl.ComplexValue)1 StringValue (eu.esdihumboldt.hale.common.core.io.impl.StringValue)1 ServiceManager (eu.esdihumboldt.hale.common.core.service.ServiceManager)1 PropertyDefinition (eu.esdihumboldt.hale.common.schema.model.PropertyDefinition)1