use of eu.esdihumboldt.hale.common.instance.model.MutableInstance 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.instance.model.MutableInstance in project hale by halestudio.
the class Retype method execute.
@Override
public void execute(String transformationIdentifier, TransformationEngine engine, Map<String, String> executionParameters, TransformationLog log, Cell cell) {
// for each source instance create a target instance
TypeDefinition targetType = getTarget().values().iterator().next().getDefinition().getDefinition();
MutableInstance target = null;
// structural rename
boolean structuralRename = getOptionalParameter(RenameFunction.PARAMETER_STRUCTURAL_RENAME, Value.of(false)).as(Boolean.class, false);
if (structuralRename) {
boolean ignoreNamespaces = getOptionalParameter(RenameFunction.PARAMETER_IGNORE_NAMESPACES, Value.of(false)).as(Boolean.class, false);
boolean copyGeometries = getOptionalParameter(RenameFunction.PARAMETER_COPY_GEOMETRIES, Value.of(true)).as(Boolean.class);
target = doStructuralRename(getSource(), targetType, ignoreNamespaces, copyGeometries, log);
}
if (target == null) {
target = getInstanceFactory().createInstance(targetType);
}
getPropertyTransformer().publish(getSource(), target, log, cell);
}
use of eu.esdihumboldt.hale.common.instance.model.MutableInstance in project hale by halestudio.
the class IndexMergeHandler method merge.
private Instance merge(InstanceCollection instances, IndexMergeConfig mergeConfig) {
TypeDefinition type;
try (ResourceIterator<Instance> it = instances.iterator()) {
if (instances.hasSize() && instances.size() == 1) {
// early exit if only one instance to merge
return it.next();
} else {
type = it.next().getDefinition();
}
}
MutableInstance result = getInstanceFactory().createInstance(type);
/*
* FIXME This a first VERY basic implementation, where only the first
* item in each property path is regarded, and that whole tree is added
* only once (from the first instance). XXX This especially will be a
* problem, if a path contains a choice. XXX For more advanced stuff we
* need more advanced test cases.
*/
Set<QName> rootNames = new HashSet<QName>();
Set<QName> nonKeyRootNames = new HashSet<QName>();
// collect path roots
for (List<QName> path : mergeConfig.keyProperties) {
rootNames.add(path.get(0));
}
for (List<QName> path : mergeConfig.additionalProperties) {
nonKeyRootNames.add(path.get(0));
}
// XXX what about metadata?!
// XXX for now only retain IDs
Set<Object> ids = new HashSet<Object>();
try (ResourceIterator<Instance> it = instances.iterator()) {
while (it.hasNext()) {
Instance instance = it.next();
for (QName name : instance.getPropertyNames()) {
if (rootNames.contains(name)) {
/*
* Property is merge key -> only use first occurrence
* (as all entries need to be the same)
*
* TODO adapt if multiple keys are possible per instance
*/
addFirstOccurrence(result, instance, name);
} else if (nonKeyRootNames.contains(name)) {
/*
* Property is additional merge property.
*
* Traditional behavior: Only keep unique values.
*
* XXX should this be configurable?
*/
addUnique(result, instance, name);
} else if (mergeConfig.autoDetect) {
/*
* Auto-detection is enabled.
*
* Only keep unique values.
*
* XXX This differs from the traditional behavior in
* that there only the first value would be used, but
* only if all values were equal. That cannot be easily
* checked in an iterative approach.
*/
addUnique(result, instance, name);
} else {
/*
* Property is not to be merged.
*
* XXX but we could do some kind of aggregation
*
* XXX for now just add all values
*/
addValues(result, instance, name);
}
}
List<Object> instanceIDs = instance.getMetaData(InstanceMetadata.METADATA_ID);
for (Object id : instanceIDs) {
ids.add(id);
}
}
}
// store metadata IDs
result.setMetaData(InstanceMetadata.METADATA_ID, ids.toArray());
return result;
}
use of eu.esdihumboldt.hale.common.instance.model.MutableInstance in project hale by halestudio.
the class GroovyRetype method execute.
@Override
public void execute(String transformationIdentifier, TransformationEngine engine, Map<String, String> executionParameters, TransformationLog log, Cell cell) throws TransformationException {
// for each source instance create a target instance
TypeDefinition targetType = getTarget().values().iterator().next().getDefinition().getDefinition();
InstanceBuilder builder = new InstanceBuilder(false);
Binding binding = createBinding(getSource(), cell, builder, log, getExecutionContext(), targetType);
try {
GroovyService service = getExecutionContext().getService(GroovyService.class);
Script script = GroovyUtil.getScript(this, binding, service);
Iterable<MutableInstance> targets = GroovyUtil.evaluateAll(script, builder, targetType, service, log);
for (MutableInstance target : targets) {
getPropertyTransformer().publish(getSource(), target, log, cell);
}
} catch (TransformationException e) {
throw e;
} catch (NoResultException e) {
log.info(log.createMessage("Skipping target instance because received NoResultException from script", null));
} catch (Exception e) {
throw new TransformationException(e.getMessage(), e);
}
}
use of eu.esdihumboldt.hale.common.instance.model.MutableInstance in project hale by halestudio.
the class GroovyUtil method evaluate.
/**
* Evaluate a Groovy type script.
*
* @param script the script
* @param builder the instance builder
* @param type the type of the instance to create
* @param service the Groovy service
* @param log the log
* @return the created instance
* @throws TransformationException if the target binding does not contain
* exactly one result after script evaluation or an internal
* error occurs
* @throws NoResultException if the script implies that no result should be
* created
*/
public static MutableInstance evaluate(Script script, final InstanceBuilder builder, final TypeDefinition type, GroovyService service, SimpleLog log) throws TransformationException, NoResultException {
Iterable<MutableInstance> results = evaluateAll(script, builder, type, service, log);
Iterator<MutableInstance> it = results.iterator();
MutableInstance result;
if (it.hasNext()) {
result = it.next();
if (it.hasNext()) {
throw new TransformationException("Cell script does not produce exactly one result.");
}
} else {
throw new NoResultException();
}
return result;
}
Aggregations