use of eu.esdihumboldt.hale.common.align.model.impl.TypeEntityDefinition in project hale by halestudio.
the class XslRetype method configureTemplate.
@Override
protected void configureTemplate(final VelocityContext context, final Cell typeCell) throws TransformationException {
Type source = (Type) CellUtil.getFirstEntity(typeCell.getSource());
TypeEntityDefinition ted = source.getDefinition();
context.put(CONTEXT_PARAM_SELECT_INSTANCES, XslTransformationUtil.selectInstances(ted, context().getSourceContext(ted.getDefinition()), context().getNamespaceContext()));
context.put(CONTEXT_PARAM_INLINE_TEMPLATE, context().getInlineTemplateName(typeCell));
String properties = createPropertiesFragment(typeCell);
context.put(CONTEXT_PARAM_PROPERTIES, properties);
}
use of eu.esdihumboldt.hale.common.align.model.impl.TypeEntityDefinition 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.model.impl.TypeEntityDefinition 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.model.impl.TypeEntityDefinition in project hale by halestudio.
the class GroovyUtil method createBinding.
/**
* Creates a basic binding used by all Groovy functions.
*
* @param builder the instance builder, may be <code>null</code>
* @param cell the cell of the function
* @param typeCell the type cell the function works on, may be
* <code>null</code>
* @param log the transformation log
* @param executionContext the execution context
* @param targetInstanceType the type of the target instance to create
* @return a basic binding
*/
public static Binding createBinding(InstanceBuilder builder, Cell cell, Cell typeCell, TransformationLog log, ExecutionContext executionContext, TypeDefinition targetInstanceType) {
Binding binding = new Binding();
HelperContext helperContext = new DefaultHelperContext(executionContext, executionContext, cell, typeCell);
binding.setVariable(BINDING_HELPER_FUNCTIONS, HelperFunctions.createDefault(helperContext));
binding.setVariable(BINDING_BUILDER, builder);
binding.setVariable(BINDING_CELL, cell);
TransformationLogWrapper cellLog = new TransformationLogWrapper(log);
binding.setVariable(BINDING_LOG, cellLog);
binding.setVariable(BINDING_CELL_CONTEXT, SynchronizedContextProvider.getContextClosure(executionContext.getCellContext()));
binding.setVariable(BINDING_FUNCTION_CONTEXT, SynchronizedContextProvider.getContextClosure(executionContext.getFunctionContext()));
binding.setVariable(BINDING_TRANSFORMATION_CONTEXT, SynchronizedContextProvider.getContextClosure(executionContext.getTransformationContext()));
// init type cell types
ArrayList<TypeEntityDefinition> sourceTypes = null;
TypeEntityDefinition targetType = null;
if (typeCell != null) {
targetType = ((Type) CellUtil.getFirstEntity(typeCell.getTarget())).getDefinition();
if (typeCell.getSource() != null) {
Collection<? extends Entity> sources = typeCell.getSource().values();
sourceTypes = new ArrayList<>(sources.size());
for (Object entity : sources) {
sourceTypes.add(((Type) entity).getDefinition());
}
}
}
binding.setVariable(BINDING_SOURCE_TYPES, sourceTypes);
binding.setVariable(BINDING_TARGET_TYPE, targetType);
binding.setVariable(BINDING_TARGET, new TargetCollector(builder, targetInstanceType));
binding.setVariable(BINDING_PROJECT, new ProjectAccessor(executionContext.getService(ProjectInfoService.class), cellLog, executionContext));
binding.setVariable(BINDING_SPATIAL_INDEX, executionContext.getService(SpatialIndexService.class));
binding.setVariable(BINDING_INSTANCE_INDEX, executionContext.getService(InstanceIndexService.class));
return binding;
}
use of eu.esdihumboldt.hale.common.align.model.impl.TypeEntityDefinition in project hale by halestudio.
the class DefaultCellMigrator method updateCell.
@Override
public MutableCell updateCell(final Cell originalCell, final AlignmentMigration migration, final MigrationOptions options, SimpleLog log) {
MutableCell result = new DefaultCell(originalCell);
SimpleLog cellLog = SimpleLog.all(log, new CellLog(result, CELL_LOG_CATEGORY));
final AtomicBoolean replacedEntities = new AtomicBoolean(false);
EntryTransformer<String, Entity, Entity> entityTransformer = new EntryTransformer<String, Entity, Entity>() {
@Override
public Entity transformEntry(String key, Entity value) {
EntityDefinition org = value.getDefinition();
Optional<EntityDefinition> replace = migration.entityReplacement(org, cellLog);
EntityDefinition entity = replace.orElse(org);
if (!Objects.equal(entity, org)) {
replacedEntities.set(true);
}
if (entity instanceof PropertyEntityDefinition) {
return new DefaultProperty((PropertyEntityDefinition) entity);
} else if (entity instanceof TypeEntityDefinition) {
return new DefaultType((TypeEntityDefinition) entity);
} else {
throw new IllegalStateException("Invalid entity definition for creating entity");
}
}
};
// update source entities
if (options.updateSource() && result.getSource() != null && !result.getSource().isEmpty()) {
result.setSource(ArrayListMultimap.create(Multimaps.transformEntries(result.getSource(), entityTransformer)));
}
// update target entities
if (options.updateTarget() && result.getTarget() != null && !result.getTarget().isEmpty()) {
result.setTarget(ArrayListMultimap.create(Multimaps.transformEntries(result.getTarget(), entityTransformer)));
}
// TODO anything else?
postUpdateCell(result, options, replacedEntities.get(), cellLog);
return result;
}
Aggregations