Search in sources :

Example 66 with Value

use of eu.esdihumboldt.hale.common.core.io.Value 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 67 with Value

use of eu.esdihumboldt.hale.common.core.io.Value in project hale by halestudio.

the class DefaultTransformationVariables method replaceVariables.

@Override
public String replaceVariables(String input, boolean failUnresolved) {
    if (input == null) {
        return null;
    }
    String result = input;
    try {
        String re = "\\{\\{([^}]+)\\}\\}";
        Pattern p = Pattern.compile(re);
        Matcher m = p.matcher(input);
        // Iterate over identifiers and determine values.
        while (m.find()) {
            String reference = m.group(1);
            if (reference != null) {
                try {
                    String varName;
                    TransformationVariableScope scope = TransformationVariableScope.transformation;
                    int sepIndex = reference.indexOf(':');
                    if (sepIndex > 0) {
                        String scopeName = reference.substring(0, sepIndex);
                        scope = TransformationVariableScope.valueOf(scopeName);
                        varName = reference.substring(sepIndex + 1);
                    } else {
                        // default scope
                        varName = reference;
                    }
                    Value value = getVariable(scope, varName);
                    String strValue = value.as(String.class);
                    if (strValue != null) {
                        // only replace if there is a value
                        String rx = "\\{\\{" + Pattern.quote(reference) + "\\}\\}";
                        result = result.replaceAll(rx, strValue);
                    } else if (failUnresolved) {
                        throw new IllegalStateException("Cannot resolve variable reference " + reference);
                    }
                } catch (IllegalStateException e) {
                    throw e;
                } catch (Exception e) {
                    if (failUnresolved) {
                        throw new IllegalStateException("Cannot resolve variable reference " + reference);
                    }
                    log.warn("Cannot resolve variable reference " + reference);
                }
            }
        }
    } catch (IllegalStateException e) {
        throw e;
    } catch (Exception e) {
        log.error("Error replacing transformation variables", e);
    }
    return result;
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) Value(eu.esdihumboldt.hale.common.core.io.Value) TransformationVariableScope(eu.esdihumboldt.hale.common.align.transformation.function.TransformationVariableScope)

Example 68 with Value

use of eu.esdihumboldt.hale.common.core.io.Value in project hale by halestudio.

the class JaxbToAlignment method createUnmigratedCell.

private static UnmigratedCell createUnmigratedCell(CellType cell, LoadAlignmentContext context, IOReporter reporter, EntityResolver resolver, ServiceProvider serviceProvider) {
    // The sourceCell represents the cell as it was imported from the
    // XML alignment. The conversion to the resolved cell must be performed
    // later by migrating the UnmigratedCell returned from this function.
    final DefaultCell sourceCell = new DefaultCell();
    sourceCell.setTransformationIdentifier(cell.getRelation());
    final FunctionDefinition<?> cellFunction = FunctionUtil.getFunction(sourceCell.getTransformationIdentifier(), serviceProvider);
    final CellMigrator migrator;
    if (cellFunction != null) {
        migrator = cellFunction.getCustomMigrator().orElse(new DefaultCellMigrator());
    } else {
        migrator = new DefaultCellMigrator();
    }
    Map<EntityDefinition, EntityDefinition> mappings = new HashMap<>();
    try {
        // The returned Entity pair consists of
        // (1st) a dummy entity representing the entity read from JAXB
        // (2nd) the resolved entity
        ListMultimap<String, Pair<Entity, Entity>> convertedSourceEntities = convertEntities(cell.getSource(), context.getSourceTypes(), SchemaSpaceID.SOURCE, resolver);
        if (convertedSourceEntities == null) {
            sourceCell.setSource(null);
        } else {
            sourceCell.setSource(Multimaps.transformValues(convertedSourceEntities, pair -> pair.getFirst()));
            for (Pair<Entity, Entity> pair : convertedSourceEntities.values()) {
                mappings.put(pair.getFirst().getDefinition(), pair.getSecond().getDefinition());
            }
        }
        ListMultimap<String, Pair<Entity, Entity>> convertedTargetEntities = convertEntities(cell.getTarget(), context.getTargetTypes(), SchemaSpaceID.TARGET, resolver);
        if (convertedTargetEntities == null) {
            sourceCell.setTarget(null);
        } else {
            sourceCell.setTarget(Multimaps.transformValues(convertedTargetEntities, pair -> pair.getFirst()));
            for (Pair<Entity, Entity> pair : convertedTargetEntities.values()) {
                mappings.put(pair.getFirst().getDefinition(), pair.getSecond().getDefinition());
            }
        }
        if (sourceCell.getTarget() == null || sourceCell.getTarget().isEmpty()) {
            // target is mandatory for cells!
            throw new IllegalStateException("Cannot create cell without target");
        }
    } catch (Exception e) {
        if (reporter != null) {
            reporter.error(new IOMessageImpl("Could not create cell", e));
        }
        return null;
    }
    if (!cell.getAbstractParameter().isEmpty()) {
        ListMultimap<String, ParameterValue> parameters = ArrayListMultimap.create();
        for (JAXBElement<? extends AbstractParameterType> param : cell.getAbstractParameter()) {
            AbstractParameterType apt = param.getValue();
            if (apt instanceof ParameterType) {
                // treat string parameters or null parameters
                ParameterType pt = (ParameterType) apt;
                ParameterValue pv = new ParameterValue(pt.getType(), Value.of(pt.getValue()));
                parameters.put(pt.getName(), pv);
            } else if (apt instanceof ComplexParameterType) {
                // complex parameters
                ComplexParameterType cpt = (ComplexParameterType) apt;
                parameters.put(cpt.getName(), new ParameterValue(new ElementValue(cpt.getAny(), context)));
            } else
                throw new IllegalStateException("Illegal parameter type");
        }
        sourceCell.setTransformationParameters(parameters);
    }
    // annotations & documentation
    for (Object element : cell.getDocumentationOrAnnotation()) {
        if (element instanceof AnnotationType) {
            // add annotation to the cell
            AnnotationType annot = (AnnotationType) element;
            // but first load it from the DOM
            AnnotationDescriptor<?> desc = AnnotationExtension.getInstance().get(annot.getType());
            if (desc != null) {
                try {
                    Object value = desc.fromDOM(annot.getAny(), null);
                    sourceCell.addAnnotation(annot.getType(), value);
                } catch (Exception e) {
                    if (reporter != null) {
                        reporter.error(new IOMessageImpl("Error loading cell annotation", e));
                    } else
                        throw new IllegalStateException("Error loading cell annotation", e);
                }
            } else
                reporter.error(new IOMessageImpl("Cell annotation of type {0} unknown, cannot load the annotation object", null, -1, -1, annot.getType()));
        } else if (element instanceof DocumentationType) {
            // add documentation to the cell
            DocumentationType doc = (DocumentationType) element;
            sourceCell.getDocumentation().put(doc.getType(), doc.getValue());
        }
    }
    sourceCell.setId(cell.getId());
    // a default value is assured for priority
    String priorityStr = cell.getPriority().value();
    Priority priority = Priority.fromValue(priorityStr);
    if (priority != null) {
        sourceCell.setPriority(priority);
    } else {
        // used.
        throw new IllegalArgumentException();
    }
    return new UnmigratedCell(sourceCell, migrator, mappings);
}
Also used : ArrayListMultimap(com.google.common.collect.ArrayListMultimap) ListMultimap(com.google.common.collect.ListMultimap) CellMigrator(eu.esdihumboldt.hale.common.align.migrate.CellMigrator) Collections2(com.google.common.collect.Collections2) ModifierType(eu.esdihumboldt.hale.common.align.io.impl.internal.generated.ModifierType) MutableAlignment(eu.esdihumboldt.hale.common.align.model.MutableAlignment) ElementValue(eu.esdihumboldt.hale.common.core.io.impl.ElementValue) HaleIO(eu.esdihumboldt.hale.common.core.io.HaleIO) DefaultCell(eu.esdihumboldt.hale.common.align.model.impl.DefaultCell) Map(java.util.Map) ParameterType(eu.esdihumboldt.hale.common.align.io.impl.internal.generated.ParameterType) Pair(eu.esdihumboldt.util.Pair) AnnotationDescriptor(eu.esdihumboldt.hale.common.align.model.AnnotationDescriptor) URI(java.net.URI) AnnotationExtension(eu.esdihumboldt.hale.common.align.extension.annotation.AnnotationExtension) Value(eu.esdihumboldt.hale.common.core.io.Value) Function(com.google.common.base.Function) Collection(java.util.Collection) UnmigratedCell(eu.esdihumboldt.hale.common.align.migrate.impl.UnmigratedCell) ParameterValue(eu.esdihumboldt.hale.common.align.model.ParameterValue) IOReporter(eu.esdihumboldt.hale.common.core.io.report.IOReporter) JAXBException(javax.xml.bind.JAXBException) LoadAlignmentContext(eu.esdihumboldt.hale.common.align.io.LoadAlignmentContext) PathUpdate(eu.esdihumboldt.hale.common.core.io.PathUpdate) SchemaSpaceID(eu.esdihumboldt.hale.common.schema.SchemaSpaceID) CustomFunctionType(eu.esdihumboldt.hale.common.align.io.impl.internal.generated.CustomFunctionType) DummyEntityResolver(eu.esdihumboldt.hale.common.align.io.impl.dummy.DummyEntityResolver) List(java.util.List) Base(eu.esdihumboldt.hale.common.align.io.impl.internal.generated.AlignmentType.Base) ComplexParameterType(eu.esdihumboldt.hale.common.align.io.impl.internal.generated.ComplexParameterType) TypeIndex(eu.esdihumboldt.hale.common.schema.model.TypeIndex) CellType(eu.esdihumboldt.hale.common.align.io.impl.internal.generated.CellType) Priority(eu.esdihumboldt.hale.common.align.model.Priority) StreamSource(javax.xml.transform.stream.StreamSource) AnnotationType(eu.esdihumboldt.hale.common.align.io.impl.internal.generated.AnnotationType) HashMap(java.util.HashMap) DefaultCellMigrator(eu.esdihumboldt.hale.common.align.migrate.impl.DefaultCellMigrator) ArrayList(java.util.ArrayList) Multimaps(com.google.common.collect.Multimaps) JaxbAlignmentIO(eu.esdihumboldt.hale.common.align.io.impl.JaxbAlignmentIO) EntityResolver(eu.esdihumboldt.hale.common.align.io.EntityResolver) IOMessageImpl(eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl) MutableCell(eu.esdihumboldt.hale.common.align.model.MutableCell) DisableFor(eu.esdihumboldt.hale.common.align.io.impl.internal.generated.ModifierType.DisableFor) JAXBContext(javax.xml.bind.JAXBContext) Unmarshaller(javax.xml.bind.Unmarshaller) CustomPropertyFunction(eu.esdihumboldt.hale.common.align.extension.function.custom.CustomPropertyFunction) DocumentationType(eu.esdihumboldt.hale.common.align.io.impl.internal.generated.DocumentationType) JAXBElement(javax.xml.bind.JAXBElement) IOException(java.io.IOException) ServiceProvider(eu.esdihumboldt.hale.common.core.service.ServiceProvider) DefaultEntityResolver(eu.esdihumboldt.hale.common.align.io.impl.DefaultEntityResolver) Entity(eu.esdihumboldt.hale.common.align.model.Entity) FunctionUtil(eu.esdihumboldt.hale.common.align.extension.function.FunctionUtil) AbstractParameterType(eu.esdihumboldt.hale.common.align.io.impl.internal.generated.AbstractParameterType) TransformationMode(eu.esdihumboldt.hale.common.align.model.TransformationMode) FunctionDefinition(eu.esdihumboldt.hale.common.align.extension.function.FunctionDefinition) Element(org.w3c.dom.Element) NamedEntityType(eu.esdihumboldt.hale.common.align.io.impl.internal.generated.NamedEntityType) AlignmentType(eu.esdihumboldt.hale.common.align.io.impl.internal.generated.AlignmentType) EntityDefinition(eu.esdihumboldt.hale.common.align.model.EntityDefinition) InputStream(java.io.InputStream) Entity(eu.esdihumboldt.hale.common.align.model.Entity) HashMap(java.util.HashMap) IOMessageImpl(eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl) CellMigrator(eu.esdihumboldt.hale.common.align.migrate.CellMigrator) DefaultCellMigrator(eu.esdihumboldt.hale.common.align.migrate.impl.DefaultCellMigrator) DefaultCellMigrator(eu.esdihumboldt.hale.common.align.migrate.impl.DefaultCellMigrator) AnnotationType(eu.esdihumboldt.hale.common.align.io.impl.internal.generated.AnnotationType) UnmigratedCell(eu.esdihumboldt.hale.common.align.migrate.impl.UnmigratedCell) AbstractParameterType(eu.esdihumboldt.hale.common.align.io.impl.internal.generated.AbstractParameterType) Pair(eu.esdihumboldt.util.Pair) ParameterType(eu.esdihumboldt.hale.common.align.io.impl.internal.generated.ParameterType) ComplexParameterType(eu.esdihumboldt.hale.common.align.io.impl.internal.generated.ComplexParameterType) AbstractParameterType(eu.esdihumboldt.hale.common.align.io.impl.internal.generated.AbstractParameterType) ParameterValue(eu.esdihumboldt.hale.common.align.model.ParameterValue) Priority(eu.esdihumboldt.hale.common.align.model.Priority) ElementValue(eu.esdihumboldt.hale.common.core.io.impl.ElementValue) JAXBException(javax.xml.bind.JAXBException) IOException(java.io.IOException) ComplexParameterType(eu.esdihumboldt.hale.common.align.io.impl.internal.generated.ComplexParameterType) EntityDefinition(eu.esdihumboldt.hale.common.align.model.EntityDefinition) DocumentationType(eu.esdihumboldt.hale.common.align.io.impl.internal.generated.DocumentationType) DefaultCell(eu.esdihumboldt.hale.common.align.model.impl.DefaultCell)

Example 69 with Value

use of eu.esdihumboldt.hale.common.core.io.Value in project hale by halestudio.

the class LookupTableTypeTest method testComplexLookupJson.

/**
 * Test if a lookup table containing only complex values is the same when
 * converted to JSON and back again.
 */
@Test
public void testComplexLookupJson() {
    Map<Value, Value> values2 = createComplexLookup();
    LookupTable org2 = new LookupTableImpl(values2);
    // converter
    LookupTableType ltt = new LookupTableType();
    // convert to Json
    StringWriter writer = new StringWriter();
    ltt.toJson(org2, writer);
    System.out.println(writer.toString());
    // convert back
    LookupTable conv = ltt.fromJson(new StringReader(writer.toString()), null);
    checkTable(conv, values2);
}
Also used : StringWriter(java.io.StringWriter) Value(eu.esdihumboldt.hale.common.core.io.Value) LookupTable(eu.esdihumboldt.hale.common.lookup.LookupTable) StringReader(java.io.StringReader) LookupTableImpl(eu.esdihumboldt.hale.common.lookup.impl.LookupTableImpl) Test(org.junit.Test)

Example 70 with Value

use of eu.esdihumboldt.hale.common.core.io.Value in project hale by halestudio.

the class LookupTableTypeTest method testStringLookupJson.

/**
 * Test if a simple lookup table containing only string values is the same
 * when converted to JSON and back again.
 */
@Test
public void testStringLookupJson() {
    Map<Value, Value> values = createStringLookup();
    LookupTable org = new LookupTableImpl(values);
    // converter
    LookupTableType ltt = new LookupTableType();
    // convert to Json
    StringWriter writer = new StringWriter();
    ltt.toJson(org, writer);
    System.out.println(writer.toString());
    // convert back
    LookupTable conv = ltt.fromJson(new StringReader(writer.toString()), null);
    checkTable(conv, values);
}
Also used : StringWriter(java.io.StringWriter) Value(eu.esdihumboldt.hale.common.core.io.Value) LookupTable(eu.esdihumboldt.hale.common.lookup.LookupTable) StringReader(java.io.StringReader) LookupTableImpl(eu.esdihumboldt.hale.common.lookup.impl.LookupTableImpl) Test(org.junit.Test)

Aggregations

Value (eu.esdihumboldt.hale.common.core.io.Value)81 ValueList (eu.esdihumboldt.hale.common.core.io.ValueList)12 LookupTable (eu.esdihumboldt.hale.common.lookup.LookupTable)12 HashMap (java.util.HashMap)12 ValueProperties (eu.esdihumboldt.hale.common.core.io.ValueProperties)11 LookupTableImpl (eu.esdihumboldt.hale.common.lookup.impl.LookupTableImpl)11 IOConfiguration (eu.esdihumboldt.hale.common.core.io.project.model.IOConfiguration)10 ParameterValue (eu.esdihumboldt.hale.common.align.model.ParameterValue)9 ArrayList (java.util.ArrayList)9 URI (java.net.URI)8 Test (org.junit.Test)6 StyledString (org.eclipse.jface.viewers.StyledString)5 Composite (org.eclipse.swt.widgets.Composite)5 DefaultCustomPropertyFunction (eu.esdihumboldt.hale.common.align.custom.DefaultCustomPropertyFunction)4 PropertyValue (eu.esdihumboldt.hale.common.align.transformation.function.PropertyValue)4 IOMessageImpl (eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl)4 TypeDefinition (eu.esdihumboldt.hale.common.schema.model.TypeDefinition)4 ProjectService (eu.esdihumboldt.hale.ui.service.project.ProjectService)4 IOException (java.io.IOException)4 Locale (java.util.Locale)4