use of eu.esdihumboldt.hale.common.align.transformation.function.InstanceHandler in project hale by halestudio.
the class ConceptualSchemaTransformer method doTypeTransformation.
/**
* Execute a type transformation based on single type cell
*
* @param transformation the transformation to use
* @param typeCell the type cell
* @param target the target instance sink
* @param source the source instances
* @param alignment the alignment
* @param engines the engine manager
* @param transformer the property transformer
* @param context the transformation execution context
* @param reporter the reporter
* @param progressIndicator the progress indicator
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
protected void doTypeTransformation(TypeTransformationFactory transformation, Cell typeCell, InstanceCollection source, InstanceSink target, Alignment alignment, EngineManager engines, PropertyTransformer transformer, TransformationContext context, TransformationReporter reporter, ProgressIndicator progressIndicator) {
TransformationLog cellLog = new CellLog(reporter, typeCell);
TypeTransformation<?> function;
try {
function = transformation.createExtensionObject();
} catch (Exception e) {
reporter.error(new TransformationMessageImpl(typeCell, "Error creating transformation function.", e));
return;
}
TransformationEngine engine = engines.get(transformation.getEngineId(), cellLog);
if (engine == null) {
// TODO instead try another transformation
cellLog.error(cellLog.createMessage("Skipping type transformation: No matching transformation engine found", null));
return;
}
// prepare transformation configuration
ListMultimap<String, Type> targetTypes = ArrayListMultimap.create();
for (Entry<String, ? extends Entity> entry : typeCell.getTarget().entries()) {
targetTypes.put(entry.getKey(), (Type) entry.getValue());
}
ListMultimap<String, ParameterValue> parameters = typeCell.getTransformationParameters();
if (parameters != null) {
parameters = Multimaps.unmodifiableListMultimap(parameters);
}
Map<String, String> executionParameters = transformation.getExecutionParameters();
// break on cancel
if (progressIndicator.isCanceled()) {
return;
}
ResourceIterator<FamilyInstance> iterator;
if (typeCell.getSource() == null || typeCell.getSource().isEmpty()) {
// type cell w/o source
// -> execute exactly once w/ null source
source = null;
iterator = new GenericResourceIteratorAdapter<Object, FamilyInstance>(Collections.singleton(null).iterator()) {
@Override
protected FamilyInstance convert(Object next) {
return null;
}
};
} else {
// Step 1: selection
// Select only instances that are relevant for the transformation.
source = source.select(new TypeCellFilter(typeCell));
// Step 2: partition
// use InstanceHandler if available - for example merge or join
function.setExecutionContext(context.getCellContext(typeCell));
InstanceHandler instanceHandler = function.getInstanceHandler();
if (instanceHandler != null) {
injectTransformationContext(instanceHandler, context);
progressIndicator.setCurrentTask("Perform instance partitioning");
try {
iterator = instanceHandler.partitionInstances(source, transformation.getFunctionId(), engine, parameters, executionParameters, cellLog);
} catch (TransformationException e) {
cellLog.error(cellLog.createMessage("Type transformation: partitioning failed", e));
return;
}
} else {
// else just use every instance as is
iterator = new GenericResourceIteratorAdapter<Instance, FamilyInstance>(source.iterator()) {
@Override
protected FamilyInstance convert(Instance next) {
return new FamilyInstanceImpl(next);
}
};
}
}
progressIndicator.setCurrentTask("Execute type transformations");
try {
while (iterator.hasNext()) {
// break on cancel
if (progressIndicator.isCanceled()) {
return;
}
function.setSource(iterator.next());
function.setPropertyTransformer(transformer);
function.setParameters(parameters);
function.setTarget(targetTypes);
function.setExecutionContext(context.getCellContext(typeCell));
try {
((TypeTransformation) function).execute(transformation.getFunctionId(), engine, executionParameters, cellLog, typeCell);
} catch (TransformationException e) {
cellLog.error(cellLog.createMessage("Type transformation failed, skipping instance.", e));
}
}
} finally {
iterator.close();
}
}
Aggregations