Search in sources :

Example 11 with NoResultException

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

the class ExtentTransformation method calculateExtent.

/**
 * Calculate the extent of a set of geometries.
 *
 * @param geometries the geometries or instances containing geometries
 * @param type the type of extent to calculate
 * @return the calculated extent
 * @throws TransformationException if source geometries don't have a common
 *             CRS
 * @throws NoResultException if the result extent would be <code>null</code>
 */
public static GeometryProperty<?> calculateExtent(Iterable<?> geometries, ExtentType type) throws TransformationException, NoResultException {
    InstanceTraverser traverser = new DepthFirstInstanceTraverser(true);
    GeometryFinder geoFind = new GeometryFinder(null);
    GeometryFactory fact = new GeometryFactory();
    CRSDefinition commonCrs = null;
    Geometry[] geomsCollectingArray = new Geometry[SIMULTAN_PROCESS_GEOMS];
    short geomsCollectedIdx = 0;
    for (Object value : geometries) {
        traverser.traverse(value, geoFind);
        for (GeometryProperty<?> geom : geoFind.getGeometries()) {
            // no CRS or one common CRS is OK
            if (commonCrs == null) {
                commonCrs = geom.getCRSDefinition();
            } else {
                if (geom.getCRSDefinition() != null && !geom.getCRSDefinition().equals(commonCrs)) {
                    // CRS doesn't match
                    throw new TransformationException("Source geometries don't have a common CRS.");
                }
            }
            Geometry g = geom.getGeometry();
            // If geometry collecting array not filled.
            if (geomsCollectedIdx < SIMULTAN_PROCESS_GEOMS - 1) {
                geomsCollectingArray[geomsCollectedIdx++] = g;
            } else // Geometry collecting array filled.
            {
                // add last
                geomsCollectingArray[geomsCollectedIdx] = g;
                // geometry
                GeometryCollection gc = new GeometryCollection(geomsCollectingArray, fact);
                geomsCollectingArray[0] = resolveParam(gc, type);
                geomsCollectedIdx = 1;
            }
        }
        geoFind.reset();
    }
    Geometry extent = resolveParam(new GeometryCollection(Arrays.copyOfRange(geomsCollectingArray, 0, geomsCollectedIdx), fact), type);
    if (extent != null) {
        return new DefaultGeometryProperty<Geometry>(commonCrs, extent);
    }
    throw new NoResultException();
}
Also used : DepthFirstInstanceTraverser(eu.esdihumboldt.hale.common.instance.helper.DepthFirstInstanceTraverser) InstanceTraverser(eu.esdihumboldt.hale.common.instance.helper.InstanceTraverser) TransformationException(eu.esdihumboldt.hale.common.align.transformation.function.TransformationException) GeometryFactory(com.vividsolutions.jts.geom.GeometryFactory) GeometryFinder(eu.esdihumboldt.hale.common.instance.geometry.GeometryFinder) CRSDefinition(eu.esdihumboldt.hale.common.schema.geometry.CRSDefinition) NoResultException(eu.esdihumboldt.hale.common.align.transformation.function.impl.NoResultException) DepthFirstInstanceTraverser(eu.esdihumboldt.hale.common.instance.helper.DepthFirstInstanceTraverser) Geometry(com.vividsolutions.jts.geom.Geometry) GeometryCollection(com.vividsolutions.jts.geom.GeometryCollection) DefaultGeometryProperty(eu.esdihumboldt.hale.common.instance.geometry.DefaultGeometryProperty)

Example 12 with NoResultException

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

the class GroovyTransformation 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 {
    // determine if instances should be used in variables or their values
    boolean useInstanceVariables = getOptionalParameter(PARAM_INSTANCE_VARIABLES, Value.of(false)).as(Boolean.class);
    // instance builder
    InstanceBuilder builder = createBuilder(resultProperty);
    // create the script binding
    List<? extends Entity> varDefs = null;
    if (getCell().getSource() != null) {
        varDefs = getCell().getSource().get(ENTITY_VARIABLE);
    }
    Binding binding = createGroovyBinding(variables.get(ENTITY_VARIABLE), varDefs, getCell(), getTypeCell(), builder, useInstanceVariables, log, getExecutionContext(), resultProperty.getDefinition().getPropertyType());
    Object result;
    try {
        GroovyService service = getExecutionContext().getService(GroovyService.class);
        Script groovyScript = GroovyUtil.getScript(this, binding, service);
        // evaluate the script
        result = evaluate(groovyScript, builder, resultProperty.getDefinition().getPropertyType(), service, log);
    } catch (TransformationException | NoResultException e) {
        throw e;
    } catch (Throwable e) {
        throw new TransformationException("Error evaluating the cell script", e);
    }
    if (result == null) {
        throw new NoResultException();
    }
    return result;
}
Also used : Binding(groovy.lang.Binding) Script(groovy.lang.Script) TransformationException(eu.esdihumboldt.hale.common.align.transformation.function.TransformationException) NoResultException(eu.esdihumboldt.hale.common.align.transformation.function.impl.NoResultException) GroovyService(eu.esdihumboldt.util.groovy.sandbox.GroovyService) InstanceBuilder(eu.esdihumboldt.hale.common.instance.groovy.InstanceBuilder)

Example 13 with NoResultException

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

the class RegexAnalysis method analize.

/**
 * Performs regex analysis.
 *
 * @param regexPattern the regular expression.
 * @param outputFormat the output format to gain.
 * @param sourceString the text to convert.
 * @return the converted text.
 * @throws NoResultException in case of missing pattern matching or errors.
 */
public static String analize(String regexPattern, String outputFormat, String sourceString) throws NoResultException {
    Pattern pattern = Pattern.compile(regexPattern);
    Matcher matcher = pattern.matcher(sourceString);
    StringBuilder result = new StringBuilder();
    int index = 0;
    boolean didMatch = false;
    while (matcher.find()) {
        didMatch = true;
        String tmpOutput = outputFormat;
        int groupCount = matcher.groupCount();
        for (int i = 0; i <= groupCount; i++) {
            String substring = sourceString.substring(matcher.start(i), matcher.end(i));
            tmpOutput = tmpOutput.replaceAll("\\{" + index + "\\}", substring);
            index++;
        }
        result.append(tmpOutput);
        index = 0;
    }
    if (!didMatch) {
        throw new NoResultException("Could not match the pattern.");
    }
    String resultString = result.toString();
    return resultString;
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) NoResultException(eu.esdihumboldt.hale.common.align.transformation.function.impl.NoResultException)

Example 14 with NoResultException

use of eu.esdihumboldt.hale.common.align.transformation.function.impl.NoResultException 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 15 with NoResultException

use of eu.esdihumboldt.hale.common.align.transformation.function.impl.NoResultException 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

NoResultException (eu.esdihumboldt.hale.common.align.transformation.function.impl.NoResultException)15 TransformationException (eu.esdihumboldt.hale.common.align.transformation.function.TransformationException)11 PropertyValue (eu.esdihumboldt.hale.common.align.transformation.function.PropertyValue)7 InstanceBuilder (eu.esdihumboldt.hale.common.instance.groovy.InstanceBuilder)6 GroovyService (eu.esdihumboldt.util.groovy.sandbox.GroovyService)6 Script (groovy.lang.Script)6 Binding (groovy.lang.Binding)4 Geometry (com.vividsolutions.jts.geom.Geometry)3 DefaultGeometryProperty (eu.esdihumboldt.hale.common.instance.geometry.DefaultGeometryProperty)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 CRSDefinition (eu.esdihumboldt.hale.common.schema.geometry.CRSDefinition)3 ArrayList (java.util.ArrayList)3 Cell (eu.esdihumboldt.hale.common.align.model.Cell)2 ParameterValue (eu.esdihumboldt.hale.common.align.model.ParameterValue)2 Value (eu.esdihumboldt.hale.common.core.io.Value)2 MutableInstance (eu.esdihumboldt.hale.common.instance.model.MutableInstance)2 TypeDefinition (eu.esdihumboldt.hale.common.schema.model.TypeDefinition)2 GeometryCollection (com.vividsolutions.jts.geom.GeometryCollection)1