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);
}
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);
}
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);
}
}
}
}
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.");
}
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;
}
Aggregations