Search in sources :

Example 6 with Binding

use of eu.esdihumboldt.hale.common.schema.model.constraint.type.Binding in project hale by halestudio.

the class XLSReaderTest method testPropertyType.

/**
 * Test - check declaration of properties' datatype.
 *
 * @throws Exception , if an error occurs
 */
@Test
public void testPropertyType() throws Exception {
    Schema schema = readXLSSchema("/data/simpleOneSheet.xls", 0, typeName, "java.lang.Integer,java.lang.String,java.lang.String");
    // Test property datatype
    TypeDefinition schemaType = schema.getType(QName.valueOf(typeName));
    Binding binding = schemaType.getChildren().iterator().next().asProperty().getPropertyType().getConstraint(Binding.class);
    assertTrue("The type is not an Integer.", binding.getBinding().equals(Integer.class));
    // ### Instance
    InstanceCollection instances = readXLSInstances("/data/simpleOneSheet.xls", 0, typeName, 1, schema);
    assertTrue(instances.hasSize());
    assertEquals(numberOfInstances, instances.size());
    // Check the values of the first (type) instance
    Instance instance = instances.iterator().next();
    Object[] value = instance.getProperty(QName.valueOf(properties[0]));
    assertEquals(Integer.valueOf(dataFirstColumn[0]), value[0]);
    assertTrue(value[0] instanceof Integer);
}
Also used : Binding(eu.esdihumboldt.hale.common.schema.model.constraint.type.Binding) Instance(eu.esdihumboldt.hale.common.instance.model.Instance) Schema(eu.esdihumboldt.hale.common.schema.model.Schema) InstanceCollection(eu.esdihumboldt.hale.common.instance.model.InstanceCollection) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition) Test(org.junit.Test)

Example 7 with Binding

use of eu.esdihumboldt.hale.common.schema.model.constraint.type.Binding in project hale by halestudio.

the class XLSInstanceReader method createInstanceCollection.

/**
 * create instances, see
 * {@link CSVInstanceReader#execute(ProgressIndicator, IOReporter)}
 *
 * @param row the current row
 * @param reporter the reporter of the writer
 * @param solveNestedProperties true, if schema should not be flat <b>(not
 *            implemented yet)</b>
 */
@SuppressWarnings("javadoc")
private void createInstanceCollection(List<String> row, IOReporter reporter) {
    MutableInstance instance = new DefaultInstance(type, null);
    // int propertyIndex = 0;
    for (int index = 0; index < propAr.length; index++) {
        String part = null;
        if (index < row.size())
            part = row.get(index);
        if (part != null) {
            PropertyDefinition property = propAr[index];
            if (part.isEmpty()) {
                // FIXME make this configurable
                part = null;
            }
            Object value = part;
            if (value != null) {
                Binding binding = property.getPropertyType().getConstraint(Binding.class);
                try {
                    if (!binding.getBinding().equals(String.class)) {
                        ConversionService conversionService = HalePlatform.getService(ConversionService.class);
                        if (conversionService.canConvert(String.class, binding.getBinding())) {
                            value = conversionService.convert(part, binding.getBinding());
                        } else {
                            throw new IllegalStateException("Conversion not possible!");
                        }
                    }
                } catch (Exception e) {
                    reporter.error(new IOMessageImpl("Cannot convert property value to {0}", e, line, -1, binding.getBinding().getSimpleName()));
                }
                instance.addProperty(property.getName(), value);
            }
        // propertyIndex++;
        }
    }
    instances.add(instance);
}
Also used : Binding(eu.esdihumboldt.hale.common.schema.model.constraint.type.Binding) ConversionService(org.springframework.core.convert.ConversionService) DefaultInstance(eu.esdihumboldt.hale.common.instance.model.impl.DefaultInstance) MutableInstance(eu.esdihumboldt.hale.common.instance.model.MutableInstance) IOMessageImpl(eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl) PropertyDefinition(eu.esdihumboldt.hale.common.schema.model.PropertyDefinition) IOProviderConfigurationException(eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException) IOException(java.io.IOException)

Example 8 with Binding

use of eu.esdihumboldt.hale.common.schema.model.constraint.type.Binding in project hale by halestudio.

the class ShapefileInstanceWriter method writePropertiesSchema.

/**
 * Method to write schema definition for all the properties.
 *
 * @param instance instance to write to.
 * @param type type definition.
 * @param geometryBuilderMap SimpleFeatureType to build schema definition
 *            for the shape file.
 */
private void writePropertiesSchema(Instance instance, TypeDefinition type, Map<String, SimpleFeatureTypeBuilder> geometryBuilderMap) {
    Collection<? extends PropertyDefinition> allNonComplexProperties = getNonComplexProperties(type);
    for (PropertyDefinition prop : allNonComplexProperties) {
        Class<?> binding = prop.getPropertyType().getConstraint(Binding.class).getBinding();
        // ignore geometry and filename properties.
        if (!prop.getPropertyType().getConstraint(GeometryType.class).isGeometry() && !prop.getName().getNamespaceURI().equalsIgnoreCase(ShapefileConstants.SHAPEFILE_AUGMENT_NS)) {
            // intentionally removing instance.getProperty(prop.getName())
            // != null, otherwise some properties that are null here are
            // read in step 3. This change will export all the attributes.
            Set<String> keySet = geometryBuilderMap.keySet();
            for (String key : keySet) {
                String propName = truncatePropertyName(prop.getName().getLocalPart());
                geometryBuilderMap.get(key).add(propName, binding);
            }
        }
    }
}
Also used : Binding(eu.esdihumboldt.hale.common.schema.model.constraint.type.Binding) GeometryType(eu.esdihumboldt.hale.common.schema.model.constraint.type.GeometryType) PropertyDefinition(eu.esdihumboldt.hale.common.schema.model.PropertyDefinition)

Example 9 with Binding

use of eu.esdihumboldt.hale.common.schema.model.constraint.type.Binding in project hale by halestudio.

the class NetworkExpansion method evaluate.

/**
 * @see AbstractSingleTargetScriptedPropertyTransformation#evaluate(String,
 *      TransformationEngine, ListMultimap, String,
 *      PropertyEntityDefinition, Map, TransformationLog)
 */
@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 the buffer width parameter
    String bufferWidthString = getTransformedParameterChecked(PARAMETER_BUFFER_WIDTH).as(String.class);
    double bufferWidth;
    try {
        bufferWidth = Double.parseDouble(bufferWidthString);
    } catch (NumberFormatException e) {
        // For backwards compatibility try to run the string as script.
        MathScript mathScript = new MathScript();
        try {
            Object result = mathScript.evaluate(bufferWidthString, variables.get(ENTITY_VARIABLE), getExecutionContext());
            bufferWidth = ConversionUtil.getAs(result, Double.class);
        } catch (ScriptException e1) {
            throw new TransformationException("Failed to evaluate buffer width expression.", e1);
        } catch (ConversionException e2) {
            throw new TransformationException("Failed to convert buffer width expression result to double.", e2);
        }
    }
    // get input geometry
    PropertyValue input = variables.get(null).get(0);
    Object inputValue = input.getValue();
    if (inputValue instanceof Instance) {
        inputValue = ((Instance) inputValue).getValue();
    }
    GeometryProperty<Geometry> result = calculateBuffer(inputValue, bufferWidth, log);
    // try to yield a result compatible to the target
    if (result != null) {
        TypeDefinition targetType = resultProperty.getDefinition().getPropertyType();
        // TODO check element type?
        Class<?> binding = targetType.getConstraint(Binding.class).getBinding();
        if (Geometry.class.isAssignableFrom(binding) && binding.isAssignableFrom(result.getGeometry().getClass())) {
            return result.getGeometry();
        } else {
            return result;
        }
    }
    throw new TransformationException("Geometry for network expansion could not be retrieved.");
}
Also used : ConversionException(org.springframework.core.convert.ConversionException) Binding(eu.esdihumboldt.hale.common.schema.model.constraint.type.Binding) TransformationException(eu.esdihumboldt.hale.common.align.transformation.function.TransformationException) Instance(eu.esdihumboldt.hale.common.instance.model.Instance) PropertyValue(eu.esdihumboldt.hale.common.align.transformation.function.PropertyValue) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition) Geometry(org.locationtech.jts.geom.Geometry) ScriptException(javax.script.ScriptException) MathScript(eu.esdihumboldt.hale.common.scripting.scripts.mathematical.MathScript)

Example 10 with Binding

use of eu.esdihumboldt.hale.common.schema.model.constraint.type.Binding in project hale by halestudio.

the class AppSchemaMappingUtils method asCqlLiteral.

/**
 * Convert the provided value to a CQL literal, based on the property
 * definition.
 *
 * <p>
 * In practice, this means that for properties whose binding type is a
 * {@link Number}, the value is returned as is; otherwise, value is
 * translated to a string literal and wrapped in single quotes.
 * </p>
 *
 * @param propertyDef the property definition
 * @param value the value to convert
 * @return the value as CQL literal
 */
public static String asCqlLiteral(PropertyDefinition propertyDef, String value) {
    if (propertyDef != null && value != null) {
        TypeDefinition typeDef = propertyDef.getPropertyType();
        HasValueFlag hasValue = typeDef.getConstraint(HasValueFlag.class);
        if (hasValue != null && hasValue.equals(HasValueFlag.get(true))) {
            Binding binding = typeDef.getConstraint(Binding.class);
            if (binding != null && Number.class.isAssignableFrom(binding.getBinding())) {
                return value;
            } else {
                // treat value as a string literal and hope for the best
                return "'" + value + "'";
            }
        }
    }
    return value;
}
Also used : Binding(eu.esdihumboldt.hale.common.schema.model.constraint.type.Binding) HasValueFlag(eu.esdihumboldt.hale.common.schema.model.constraint.type.HasValueFlag) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition)

Aggregations

Binding (eu.esdihumboldt.hale.common.schema.model.constraint.type.Binding)17 TypeDefinition (eu.esdihumboldt.hale.common.schema.model.TypeDefinition)12 PropertyDefinition (eu.esdihumboldt.hale.common.schema.model.PropertyDefinition)6 PropertyValue (eu.esdihumboldt.hale.common.align.transformation.function.PropertyValue)4 ElementType (eu.esdihumboldt.hale.common.schema.model.constraint.type.ElementType)4 Geometry (org.locationtech.jts.geom.Geometry)4 Instance (eu.esdihumboldt.hale.common.instance.model.Instance)3 HasValueFlag (eu.esdihumboldt.hale.common.schema.model.constraint.type.HasValueFlag)3 Test (org.junit.Test)3 InstanceCollection (eu.esdihumboldt.hale.common.instance.model.InstanceCollection)2 GroupPropertyDefinition (eu.esdihumboldt.hale.common.schema.model.GroupPropertyDefinition)2 Schema (eu.esdihumboldt.hale.common.schema.model.Schema)2 Cardinality (eu.esdihumboldt.hale.common.schema.model.constraint.property.Cardinality)2 AugmentedValueFlag (eu.esdihumboldt.hale.common.schema.model.constraint.type.AugmentedValueFlag)2 GeometryType (eu.esdihumboldt.hale.common.schema.model.constraint.type.GeometryType)2 ArrayList (java.util.ArrayList)2 QName (javax.xml.namespace.QName)2 MultiValue (eu.esdihumboldt.cst.MultiValue)1 TransformationException (eu.esdihumboldt.hale.common.align.transformation.function.TransformationException)1 IOProviderConfigurationException (eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException)1