Search in sources :

Example 21 with PropertyValue

use of eu.esdihumboldt.hale.common.align.transformation.function.PropertyValue in project hale by halestudio.

the class Identifier method evaluate.

@Override
protected Object evaluate(String transformationIdentifier, TransformationEngine engine, ListMultimap<String, PropertyValue> variables, String resultName, PropertyEntityDefinition resultProperty, Map<String, String> executionParameters, TransformationLog log) throws TransformationException, NoResultException {
    // get input
    PropertyValue input = variables.get(null).get(0);
    // source value as string (will be written into localid)
    String source = input.getValueAs(String.class);
    // get all values for the parameters set by the parameter page
    String countryName = getParameterChecked(COUNTRY_PARAMETER_NAME).as(String.class);
    String providerName = getParameterChecked(DATA_PROVIDER_PARAMETER_NAME).as(String.class);
    String productName = getParameterChecked(PRODUCT_PARAMETER_NAME).as(String.class);
    String version = getParameterChecked(VERSION).as(String.class);
    String versionNilReason = getParameterChecked(VERSION_NIL_REASON).as(String.class);
    // definition of the target property (inspireId in this case)
    TypeDefinition targetType = resultProperty.getDefinition().getPropertyType();
    // instance that can be changed (add property/instance as child)
    DefaultInstance inspireInstance = new DefaultInstance(targetType, null);
    // search for the child named "Identifier"
    PropertyDefinition inspireChildPropDef = Util.getChild("Identifier", targetType);
    // get type definition to create the "Identifier" instance
    TypeDefinition identType = inspireChildPropDef.getPropertyType();
    DefaultInstance identInstance = new DefaultInstance(identType, null);
    PropertyDefinition identChildLocal = Util.getChild("localId", identType);
    PropertyDefinition identChildNamespace = Util.getChild("namespace", identType);
    PropertyDefinition identChildVersion = Util.getChild("versionId", identType);
    TypeDefinition versionType = identChildVersion.getPropertyType();
    // 1.)
    // add the "localId" and "namespace" properties to the identifier
    // instance
    identInstance.addProperty(identChildLocal.getName(), source);
    identInstance.addProperty(identChildNamespace.getName(), getNamespace(countryName, providerName, productName, getTargetType()));
    DefaultInstance versionInstance = null;
    // add the "nilReason" property to the version instance
    if (version == null || version.isEmpty()) {
        if (!versionNilReason.isEmpty()) {
            versionInstance = new DefaultInstance(versionType, null);
            PropertyDefinition versionIdChildVersion = Util.getChild("nilReason", versionType);
            versionInstance.addProperty(versionIdChildVersion.getName(), versionNilReason);
        }
    } else {
        versionInstance = new DefaultInstance(versionType, null);
        versionInstance.setValue(version);
    }
    // add the "versionId" instance to the identifier instance
    if (versionInstance != null) {
        identInstance.addProperty(identChildVersion.getName(), versionInstance);
    }
    // 4.)
    // add the "identifier" instance to the inspireId instance
    inspireInstance.addProperty(inspireChildPropDef.getName(), identInstance);
    return inspireInstance;
}
Also used : DefaultInstance(eu.esdihumboldt.hale.common.instance.model.impl.DefaultInstance) PropertyValue(eu.esdihumboldt.hale.common.align.transformation.function.PropertyValue) PropertyDefinition(eu.esdihumboldt.hale.common.schema.model.PropertyDefinition) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition)

Example 22 with PropertyValue

use of eu.esdihumboldt.hale.common.align.transformation.function.PropertyValue in project hale by halestudio.

the class MathScript method createEnvironment.

private Environment createEnvironment(Iterable<PropertyValue> variables) {
    Environment env = new Environment();
    for (PropertyValue var : variables) {
        // add the variable to the environment
        // determine the variable value
        Object value = var.getValue();
        Number number;
        if (value instanceof Number) {
            number = (Number) value;
        } else {
            // try conversion to Double as default
            try {
                number = var.getValueAs(Double.class);
            } catch (ConversionException ce) {
                // expression for better exception messages
                continue;
            }
        }
        // Floats
        if (!(number instanceof Integer) && !(number instanceof Double)) {
            number = number.doubleValue();
        }
        Constant varValue = new Constant(number);
        // add with short name, if it does not override something
        String name = var.getProperty().getDefinition().getName().getLocalPart();
        if (!env.getVariables().containsKey(name))
            env.addVariable(name, varValue);
        // add with full name
        env.addVariable(getVariableName(var.getProperty()), varValue);
    }
    return env;
}
Also used : ConversionException(org.springframework.core.convert.ConversionException) Constant(com.iabcinc.jmep.hooks.Constant) Environment(com.iabcinc.jmep.Environment) PropertyValue(eu.esdihumboldt.hale.common.align.transformation.function.PropertyValue)

Example 23 with PropertyValue

use of eu.esdihumboldt.hale.common.align.transformation.function.PropertyValue in project hale by halestudio.

the class FormattedString method evaluate.

@Override
protected Object evaluate(String transformationIdentifier, TransformationEngine engine, ListMultimap<String, PropertyValue> variables, String resultName, PropertyEntityDefinition resultProperty, Map<String, String> executionParameters, TransformationLog log) throws TransformationException, NoResultException {
    String pattern = getParameterChecked(PARAMETER_PATTERN).as(String.class);
    // replace transformation variables
    pattern = getExecutionContext().getVariables().replaceVariables(pattern);
    // name/value mapping
    Map<String, Object> values = new LinkedHashMap<String, Object>();
    List<PropertyValue> vars = variables.get(ENTITY_VARIABLE);
    for (PropertyValue var : vars) {
        // determine the variable value
        Object value;
        try {
            value = var.getValueAs(String.class);
        } catch (ConversionException e) {
            value = var.getValue();
        }
        FormattedStringFunction.addValue(values, value, var.getProperty());
    }
    // replace markers in pattern
    // FIXME this is quick and dirty! does not handle escaping
    int i = 0;
    for (Entry<String, Object> entry : values.entrySet()) {
        String name = entry.getKey();
        pattern = pattern.replaceAll(Pattern.quote("{" + name + "}"), "{" + i + "}");
        i++;
    }
    try {
        return MessageFormat.format(pattern, values.values().toArray());
    } catch (IllegalArgumentException e) {
        // FIXME an error should still be reported for invalid patterns
        throw new NoResultException(e);
    }
}
Also used : ConversionException(org.springframework.core.convert.ConversionException) PropertyValue(eu.esdihumboldt.hale.common.align.transformation.function.PropertyValue) NoResultException(eu.esdihumboldt.hale.common.align.transformation.function.impl.NoResultException) LinkedHashMap(java.util.LinkedHashMap)

Example 24 with PropertyValue

use of eu.esdihumboldt.hale.common.align.transformation.function.PropertyValue in project hale by halestudio.

the class InlineTransformation method evaluate.

@Override
protected Object evaluate(String transformationIdentifier, TransformationEngine engine, ListMultimap<String, PropertyValue> variables, String resultName, PropertyEntityDefinition resultProperty, Map<String, String> executionParameters, TransformationLog log) throws TransformationException, NoResultException {
    List<PropertyValue> sources = variables.get(null);
    if (sources.isEmpty()) {
        throw new NoResultException("No source available to transform");
    }
    PropertyValue source = sources.get(0);
    Object sourceValue = source.getValue();
    if (sourceValue == null) {
        throw new NoResultException("Source value is null");
    }
    if (!(sourceValue instanceof Instance)) {
        throw new TransformationException("Sources for inline transformation must be instances");
    }
    Instance sourceInstance = (Instance) sourceValue;
    TypeDefinition sourceType = sourceInstance.getDefinition();
    // get the original alignment
    Alignment orgAlignment = getExecutionContext().getAlignment();
    MutableAlignment alignment = new DefaultAlignment(orgAlignment);
    // identify relevant type cell(s)
    MutableCell queryCell = new DefaultCell();
    ListMultimap<String, Type> sourceEntities = ArrayListMultimap.create();
    sourceEntities.put(null, new DefaultType(new TypeEntityDefinition(sourceType, SchemaSpaceID.SOURCE, null)));
    queryCell.setSource(sourceEntities);
    ListMultimap<String, Type> targetEntities = ArrayListMultimap.create();
    targetEntities.put(null, new DefaultType(new TypeEntityDefinition(resultProperty.getDefinition().getPropertyType(), SchemaSpaceID.TARGET, null)));
    queryCell.setTarget(targetEntities);
    Collection<? extends Cell> candidates = alignment.getTypeCells(queryCell);
    if (candidates.isEmpty()) {
        log.error(log.createMessage("No type transformations found for inline transformation", null));
        throw new NoResultException();
    }
    // filter alignment -> only keep relevant type relations
    List<Cell> allTypeCells = new ArrayList<>(alignment.getTypeCells());
    for (Cell cell : allTypeCells) {
        // remove cell
        alignment.removeCell(cell);
        if (!cell.getTransformationMode().equals(TransformationMode.disabled)) {
            // only readd if not disabled
            MutableCell copy = new DefaultCell(cell);
            if (candidates.contains(cell)) {
                // readd as active
                copy.setTransformationMode(TransformationMode.active);
            } else {
                // readd as passive
                copy.setTransformationMode(TransformationMode.passive);
            }
            alignment.addCell(copy);
        }
    }
    // prepare transformation input/output
    DefaultInstanceCollection sourceInstances = new DefaultInstanceCollection();
    sourceInstances.add(sourceInstance);
    DefaultInstanceSink target = new DefaultInstanceSink();
    // run transformation
    TransformationService ts = getExecutionContext().getService(TransformationService.class);
    if (ts == null) {
        throw new TransformationException("Transformation service not available for inline transformation");
    }
    ProgressIndicator progressIndicator = new LogProgressIndicator();
    TransformationReport report = ts.transform(alignment, sourceInstances, new ThreadSafeInstanceSink<InstanceSink>(target), getExecutionContext(), progressIndicator);
    // copy report messages
    log.importMessages(report);
    if (!report.isSuccess()) {
        // copy report messages
        log.importMessages(report);
        throw new TransformationException("Inline transformation failed");
    }
    // extract result
    List<Instance> targetList = target.getInstances();
    if (targetList.isEmpty()) {
        log.error(log.createMessage("Inline transformation yielded no result", null));
        throw new NoResultException("No result from inline transformation");
    }
    if (targetList.size() > 1) {
        log.error(log.createMessage("Inline transformation yielded multiple results, only first result is used", null));
    }
    return targetList.get(0);
}
Also used : MutableCell(eu.esdihumboldt.hale.common.align.model.MutableCell) Instance(eu.esdihumboldt.hale.common.instance.model.Instance) DefaultInstanceSink(eu.esdihumboldt.hale.common.align.transformation.service.impl.DefaultInstanceSink) ArrayList(java.util.ArrayList) NoResultException(eu.esdihumboldt.hale.common.align.transformation.function.impl.NoResultException) DefaultInstanceCollection(eu.esdihumboldt.hale.common.instance.model.impl.DefaultInstanceCollection) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition) Alignment(eu.esdihumboldt.hale.common.align.model.Alignment) MutableAlignment(eu.esdihumboldt.hale.common.align.model.MutableAlignment) DefaultAlignment(eu.esdihumboldt.hale.common.align.model.impl.DefaultAlignment) TypeEntityDefinition(eu.esdihumboldt.hale.common.align.model.impl.TypeEntityDefinition) LogProgressIndicator(eu.esdihumboldt.hale.common.core.io.impl.LogProgressIndicator) ProgressIndicator(eu.esdihumboldt.hale.common.core.io.ProgressIndicator) DefaultAlignment(eu.esdihumboldt.hale.common.align.model.impl.DefaultAlignment) TransformationService(eu.esdihumboldt.hale.common.align.transformation.service.TransformationService) 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) TransformationException(eu.esdihumboldt.hale.common.align.transformation.function.TransformationException) TransformationReport(eu.esdihumboldt.hale.common.align.transformation.report.TransformationReport) DefaultType(eu.esdihumboldt.hale.common.align.model.impl.DefaultType) PropertyValue(eu.esdihumboldt.hale.common.align.transformation.function.PropertyValue) MutableAlignment(eu.esdihumboldt.hale.common.align.model.MutableAlignment) LogProgressIndicator(eu.esdihumboldt.hale.common.core.io.impl.LogProgressIndicator) 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) ThreadSafeInstanceSink(eu.esdihumboldt.hale.common.align.transformation.service.impl.ThreadSafeInstanceSink) DefaultInstanceSink(eu.esdihumboldt.hale.common.align.transformation.service.impl.DefaultInstanceSink) InstanceSink(eu.esdihumboldt.hale.common.align.transformation.service.InstanceSink)

Aggregations

PropertyValue (eu.esdihumboldt.hale.common.align.transformation.function.PropertyValue)24 TransformationException (eu.esdihumboldt.hale.common.align.transformation.function.TransformationException)9 NoResultException (eu.esdihumboldt.hale.common.align.transformation.function.impl.NoResultException)7 TypeDefinition (eu.esdihumboldt.hale.common.schema.model.TypeDefinition)7 Geometry (com.vividsolutions.jts.geom.Geometry)6 PropertyEntityDefinition (eu.esdihumboldt.hale.common.align.model.impl.PropertyEntityDefinition)5 ConversionException (org.springframework.core.convert.ConversionException)5 EntityDefinition (eu.esdihumboldt.hale.common.align.model.EntityDefinition)4 PropertyValueImpl (eu.esdihumboldt.hale.common.align.transformation.function.impl.PropertyValueImpl)4 Binding (groovy.lang.Binding)4 ArrayList (java.util.ArrayList)4 ParameterValue (eu.esdihumboldt.hale.common.align.model.ParameterValue)3 Value (eu.esdihumboldt.hale.common.core.io.Value)3 GeometryFinder (eu.esdihumboldt.hale.common.instance.geometry.GeometryFinder)3 DepthFirstInstanceTraverser (eu.esdihumboldt.hale.common.instance.helper.DepthFirstInstanceTraverser)3 InstanceTraverser (eu.esdihumboldt.hale.common.instance.helper.InstanceTraverser)3 GeometryProperty (eu.esdihumboldt.hale.common.schema.geometry.GeometryProperty)3 Environment (com.iabcinc.jmep.Environment)2 XExpression (com.iabcinc.jmep.XExpression)2 Constant (com.iabcinc.jmep.hooks.Constant)2