use of eu.esdihumboldt.hale.common.align.transformation.function.TransformationException in project hale by halestudio.
the class AbstractScriptedPropertyTransformation method evaluate.
@Override
protected final ListMultimap<String, Object> evaluate(String transformationIdentifier, E engine, ListMultimap<String, PropertyValue> variables, ListMultimap<String, PropertyEntityDefinition> resultNames, Map<String, String> executionParameters, TransformationLog log) throws TransformationException {
ListMultimap<String, ParameterValue> originalParameters = getParameters();
ListMultimap<String, Value> transformedParameters = ArrayListMultimap.create();
if (originalParameters != null) {
for (Map.Entry<String, ParameterValue> entry : originalParameters.entries()) {
if (!entry.getValue().needsProcessing()) {
Value value = entry.getValue().intern();
if (!value.isRepresentedAsDOM()) {
value = Value.simple(getExecutionContext().getVariables().replaceVariables(value.getStringRepresentation()));
}
transformedParameters.put(entry.getKey(), value);
} else {
// type is a script
ScriptFactory factory = ScriptExtension.getInstance().getFactory(entry.getValue().getType());
if (factory == null)
throw new TransformationException("Couldn't find factory for script id " + entry.getValue().getType());
Script script;
try {
script = factory.createExtensionObject();
} catch (Exception e) {
throw new TransformationException("Couldn't create script from factory", e);
}
Object result;
try {
String scriptStr = entry.getValue().as(String.class);
if (script.requiresReplacedTransformationVariables()) {
// replace transformation variables
scriptStr = getExecutionContext().getVariables().replaceVariables(scriptStr);
}
result = script.evaluate(scriptStr, variables.values(), getExecutionContext());
} catch (ScriptException e) {
throw new TransformationException("Couldn't evaluate a transformation parameter", e);
}
// XXX use conversion service instead of valueOf?
transformedParameters.put(entry.getKey(), Value.simple(result));
}
}
}
this.transformedParameters = Multimaps.unmodifiableListMultimap(transformedParameters);
return evaluateImpl(transformationIdentifier, engine, variables, resultNames, executionParameters, log);
}
use of eu.esdihumboldt.hale.common.align.transformation.function.TransformationException in project hale by halestudio.
the class IndexJoinHandler method partitionInstances.
/**
* @see eu.esdihumboldt.hale.common.align.transformation.function.InstanceHandler#partitionInstances(eu.esdihumboldt.hale.common.instance.model.InstanceCollection,
* java.lang.String,
* eu.esdihumboldt.hale.common.align.transformation.engine.TransformationEngine,
* com.google.common.collect.ListMultimap, java.util.Map,
* eu.esdihumboldt.hale.common.align.transformation.report.TransformationLog)
*/
@Override
public ResourceIterator<FamilyInstance> partitionInstances(InstanceCollection instances, String transformationIdentifier, TransformationEngine engine, ListMultimap<String, ParameterValue> transformationParameters, Map<String, String> executionParameters, TransformationLog log) throws TransformationException {
if (transformationParameters == null || !transformationParameters.containsKey(PARAMETER_JOIN) || transformationParameters.get(PARAMETER_JOIN).isEmpty()) {
throw new TransformationException("No join parameter defined");
}
JoinHandler fallbackHandler = new JoinHandler();
InstanceIndexService indexService = serviceProvider.getService(InstanceIndexService.class);
if (indexService == null) {
log.warn(MessageFormat.format("Index service not available, falling back to join handler {0}", fallbackHandler.getClass().getCanonicalName()));
return fallbackHandler.partitionInstances(instances, transformationIdentifier, engine, transformationParameters, executionParameters, log);
}
JoinParameter joinParameter = transformationParameters.get(PARAMETER_JOIN).get(0).as(JoinParameter.class);
String validation = joinParameter.validate();
if (validation != null) {
throw new TransformationException("Join parameter invalid: " + validation);
}
List<TypeEntityDefinition> types = joinParameter.getTypes();
JoinDefinition joinDefinition = JoinUtil.getJoinDefinition(joinParameter);
// remember instances of first type to start join afterwards
Collection<ResolvableInstanceReference> startInstances = new LinkedList<ResolvableInstanceReference>();
List<Object> inputInstanceIds = new ArrayList<>();
try (ResourceIterator<Instance> it = instances.iterator()) {
while (it.hasNext()) {
Instance i = InstanceDecorator.getRoot(it.next());
// remember instances of first type
if (i.getDefinition().equals(types.get(0).getDefinition())) {
startInstances.add(new ResolvableInstanceReference(instances.getReference(i), instances));
}
if (!Identifiable.is(i)) {
log.warn(MessageFormat.format("At least one instance does not have an ID, falling back to join handler {0}", fallbackHandler.getClass().getCanonicalName()));
return fallbackHandler.partitionInstances(instances, transformationIdentifier, engine, transformationParameters, executionParameters, log);
}
inputInstanceIds.add(Identifiable.getId(i));
}
}
return new IndexJoinIterator(startInstances, joinDefinition, indexService);
}
use of eu.esdihumboldt.hale.common.align.transformation.function.TransformationException in project hale by halestudio.
the class JoinHandler method partitionInstances.
// For now no support for using the same type more than once in a join.
/**
* @see eu.esdihumboldt.hale.common.align.transformation.function.InstanceHandler#partitionInstances(eu.esdihumboldt.hale.common.instance.model.InstanceCollection,
* java.lang.String,
* eu.esdihumboldt.hale.common.align.transformation.engine.TransformationEngine,
* com.google.common.collect.ListMultimap, java.util.Map,
* eu.esdihumboldt.hale.common.align.transformation.report.TransformationLog)
*/
@Override
public ResourceIterator<FamilyInstance> partitionInstances(InstanceCollection instances, String transformationIdentifier, TransformationEngine engine, ListMultimap<String, ParameterValue> transformationParameters, Map<String, String> executionParameters, TransformationLog log) throws TransformationException {
if (transformationParameters == null || !transformationParameters.containsKey(PARAMETER_JOIN) || transformationParameters.get(PARAMETER_JOIN).isEmpty()) {
throw new TransformationException("No join parameter defined");
}
JoinParameter joinParameter = transformationParameters.get(PARAMETER_JOIN).get(0).as(JoinParameter.class);
String validation = joinParameter.validate();
if (validation != null)
throw new TransformationException("Join parameter invalid: " + validation);
List<TypeEntityDefinition> types = joinParameter.getTypes();
JoinDefinition joinDefinition = JoinUtil.getJoinDefinition(joinParameter);
// JoinProperty -> (Value -> Collection<Reference>)
Map<PropertyEntityDefinition, Multimap<Object, InstanceReference>> index = new HashMap<>();
for (PropertyEntityDefinition property : joinDefinition.properties.values()) index.put(property, ArrayListMultimap.<Object, InstanceReference>create());
// remember instances of first type to start join afterwards
Collection<InstanceReference> startInstances = new LinkedList<InstanceReference>();
// iterate once over all instances
ResourceIterator<Instance> iterator = instances.iterator();
try {
while (iterator.hasNext()) {
Instance next = iterator.next();
// remember instances of first type
if (next.getDefinition().equals(types.get(0).getDefinition())) {
startInstances.add(instances.getReference(next));
}
// fill index over needed properties
for (PropertyEntityDefinition property : joinDefinition.properties.get(next.getDefinition())) {
// XXX what about null? for now ignore null values
// XXX how to treat multiple values? must all be equal (in
// order?) or only one?
Collection<Object> values = AlignmentUtil.getValues(next, property, true);
if (values != null && !values.isEmpty()) {
// XXX take only first value for now
index.get(property).put(valueProcessor.processValue(values.iterator().next(), property), instances.getReference(next));
}
}
}
} finally {
iterator.close();
}
return new JoinIterator(instances, startInstances, joinDefinition.directParent, index, joinDefinition.joinTable, valueProcessor);
}
use of eu.esdihumboldt.hale.common.align.transformation.function.TransformationException in project hale by halestudio.
the class IndexMergeHandler method partitionInstances.
/**
* @see eu.esdihumboldt.cst.functions.core.merge.AbstractMergeHandler#partitionInstances(eu.esdihumboldt.hale.common.instance.model.InstanceCollection,
* java.lang.String,
* eu.esdihumboldt.hale.common.align.transformation.engine.TransformationEngine,
* com.google.common.collect.ListMultimap, java.util.Map,
* eu.esdihumboldt.hale.common.align.transformation.report.TransformationLog)
*/
@Override
public ResourceIterator<FamilyInstance> partitionInstances(InstanceCollection instances, String transformationIdentifier, TransformationEngine engine, ListMultimap<String, ParameterValue> transformationParameters, Map<String, String> executionParameters, TransformationLog log) throws TransformationException {
PropertiesMergeHandler fallbackHandler = new PropertiesMergeHandler();
InstanceIndexService indexService = serviceProvider.getService(InstanceIndexService.class);
if (indexService == null) {
log.warn(MessageFormat.format("Index service not available, falling back to merge handler {0}", fallbackHandler.getClass().getCanonicalName()));
return fallbackHandler.partitionInstances(instances, transformationIdentifier, engine, transformationParameters, executionParameters, log);
}
final IndexMergeConfig mergeConfig = createMergeConfiguration(transformationParameters);
QName typeName;
try (ResourceIterator<Instance> it = instances.iterator()) {
if (it.hasNext()) {
typeName = it.next().getDefinition().getName();
} else {
// Nothing to partition
return new ResourceIterator<FamilyInstance>() {
@Override
public boolean hasNext() {
return false;
}
@Override
public FamilyInstance next() {
return null;
}
@Override
public void close() {
// Do nothing
}
};
}
}
// Querying the index will yield a result over all instances. We must,
// however, be able to operate only on the given input instances instead
// of all instances.
// We must, therefore, be able to uniquely identify every instance in
// the index, so that we can retain from the index query only the
// relevant instances.
List<Object> inputInstanceIds = new ArrayList<>();
try (ResourceIterator<Instance> it = instances.iterator()) {
while (it.hasNext()) {
Instance i = InstanceDecorator.getRoot(it.next());
if (!Identifiable.is(i)) {
log.warn(MessageFormat.format("At least one instance does not have an ID, falling back to merge handler {0}", fallbackHandler.getClass().getCanonicalName()));
return fallbackHandler.partitionInstances(instances, transformationIdentifier, engine, transformationParameters, executionParameters, log);
}
inputInstanceIds.add(Identifiable.getId(i));
}
}
Collection<Collection<ResolvableInstanceReference>> partitionedIndex = indexService.groupBy(typeName, mergeConfig.keyProperties);
// Remove instance groups from the partitioned index where none of the
// instances in the group are in the processed instances.
partitionedIndex.removeIf(part -> !part.stream().map(ref -> ref.getId()).anyMatch(id -> inputInstanceIds.contains(id)));
Iterator<Collection<ResolvableInstanceReference>> it = partitionedIndex.iterator();
return new ResourceIterator<FamilyInstance>() {
@Override
public boolean hasNext() {
return it.hasNext();
}
@Override
public FamilyInstance next() {
Collection<ResolvableInstanceReference> instanceRefs = it.next();
InstanceCollection instancesToBeMerged = new DefaultInstanceCollection(instanceRefs.stream().map(ref -> ref.resolve()).collect(Collectors.toList()));
return new FamilyInstanceImpl(merge(instancesToBeMerged, mergeConfig));
}
@Override
public void close() {
// TODO Auto-generated method stub
}
};
}
use of eu.esdihumboldt.hale.common.align.transformation.function.TransformationException in project hale by halestudio.
the class CalculateLength method evaluate.
/**
* @see eu.esdihumboldt.hale.common.align.transformation.function.impl.AbstractSingleTargetPropertyTransformation#evaluate(java.lang.String,
* eu.esdihumboldt.hale.common.align.transformation.engine.TransformationEngine,
* com.google.common.collect.ListMultimap, java.lang.String,
* eu.esdihumboldt.hale.common.align.model.impl.PropertyEntityDefinition,
* java.util.Map,
* eu.esdihumboldt.hale.common.align.transformation.report.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 input geometry
PropertyValue input = variables.get(null).get(0);
Object inputValue = input.getValue();
// depth first traverser that on cancel continues traversal but w/o the
// children of the current object
InstanceTraverser traverser = new DepthFirstInstanceTraverser(true);
GeometryFinder geoFind = new GeometryFinder(null);
traverser.traverse(inputValue, geoFind);
List<GeometryProperty<?>> geoms = geoFind.getGeometries();
Geometry geom = null;
if (geoms.size() > 1) {
int length = 0;
for (GeometryProperty<?> geoProp : geoms) {
length += geoProp.getGeometry().getLength();
}
return length;
} else {
geom = geoms.get(0).getGeometry();
}
if (geom != null) {
return geom.getLength();
} else {
throw new TransformationException("Geometry for calculate length could not be retrieved.");
}
}
Aggregations