Search in sources :

Example 41 with TypeEntityDefinition

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);
}
Also used : Type(eu.esdihumboldt.hale.common.align.model.Type) TypeEntityDefinition(eu.esdihumboldt.hale.common.align.model.impl.TypeEntityDefinition)

Example 42 with TypeEntityDefinition

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);
}
Also used : TransformationException(eu.esdihumboldt.hale.common.align.transformation.function.TransformationException) FamilyInstance(eu.esdihumboldt.hale.common.instance.model.FamilyInstance) Instance(eu.esdihumboldt.hale.common.instance.model.Instance) ArrayList(java.util.ArrayList) JoinParameter(eu.esdihumboldt.hale.common.align.model.functions.join.JoinParameter) LinkedList(java.util.LinkedList) TypeEntityDefinition(eu.esdihumboldt.hale.common.align.model.impl.TypeEntityDefinition) JoinDefinition(eu.esdihumboldt.cst.functions.core.join.JoinUtil.JoinDefinition) ResolvableInstanceReference(eu.esdihumboldt.hale.common.instance.model.ResolvableInstanceReference) InstanceIndexService(eu.esdihumboldt.hale.common.instance.index.InstanceIndexService)

Example 43 with TypeEntityDefinition

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);
}
Also used : TransformationException(eu.esdihumboldt.hale.common.align.transformation.function.TransformationException) HashMap(java.util.HashMap) FamilyInstance(eu.esdihumboldt.hale.common.instance.model.FamilyInstance) Instance(eu.esdihumboldt.hale.common.instance.model.Instance) JoinParameter(eu.esdihumboldt.hale.common.align.model.functions.join.JoinParameter) LinkedList(java.util.LinkedList) ArrayListMultimap(com.google.common.collect.ArrayListMultimap) ListMultimap(com.google.common.collect.ListMultimap) Multimap(com.google.common.collect.Multimap) TypeEntityDefinition(eu.esdihumboldt.hale.common.align.model.impl.TypeEntityDefinition) PropertyEntityDefinition(eu.esdihumboldt.hale.common.align.model.impl.PropertyEntityDefinition) JoinDefinition(eu.esdihumboldt.cst.functions.core.join.JoinUtil.JoinDefinition) InstanceReference(eu.esdihumboldt.hale.common.instance.model.InstanceReference)

Example 44 with TypeEntityDefinition

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;
}
Also used : Binding(groovy.lang.Binding) HelperContext(eu.esdihumboldt.cst.functions.groovy.helper.HelperContext) DefaultHelperContext(eu.esdihumboldt.cst.functions.groovy.helper.DefaultHelperContext) DefaultHelperContext(eu.esdihumboldt.cst.functions.groovy.helper.DefaultHelperContext) TypeEntityDefinition(eu.esdihumboldt.hale.common.align.model.impl.TypeEntityDefinition) SpatialIndexService(eu.esdihumboldt.hale.common.instance.index.spatial.SpatialIndexService) InstanceIndexService(eu.esdihumboldt.hale.common.instance.index.InstanceIndexService)

Example 45 with TypeEntityDefinition

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;
}
Also used : SimpleLog(eu.esdihumboldt.hale.common.core.report.SimpleLog) Entity(eu.esdihumboldt.hale.common.align.model.Entity) MutableCell(eu.esdihumboldt.hale.common.align.model.MutableCell) EntryTransformer(com.google.common.collect.Maps.EntryTransformer) DefaultType(eu.esdihumboldt.hale.common.align.model.impl.DefaultType) DefaultProperty(eu.esdihumboldt.hale.common.align.model.impl.DefaultProperty) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) PropertyEntityDefinition(eu.esdihumboldt.hale.common.align.model.impl.PropertyEntityDefinition) TypeEntityDefinition(eu.esdihumboldt.hale.common.align.model.impl.TypeEntityDefinition) EntityDefinition(eu.esdihumboldt.hale.common.align.model.EntityDefinition) TypeEntityDefinition(eu.esdihumboldt.hale.common.align.model.impl.TypeEntityDefinition) PropertyEntityDefinition(eu.esdihumboldt.hale.common.align.model.impl.PropertyEntityDefinition) DefaultCell(eu.esdihumboldt.hale.common.align.model.impl.DefaultCell) CellLog(eu.esdihumboldt.hale.common.align.model.annotations.messages.CellLog)

Aggregations

TypeEntityDefinition (eu.esdihumboldt.hale.common.align.model.impl.TypeEntityDefinition)64 TypeDefinition (eu.esdihumboldt.hale.common.schema.model.TypeDefinition)23 ArrayList (java.util.ArrayList)19 EntityDefinition (eu.esdihumboldt.hale.common.align.model.EntityDefinition)16 Type (eu.esdihumboldt.hale.common.align.model.Type)16 PropertyEntityDefinition (eu.esdihumboldt.hale.common.align.model.impl.PropertyEntityDefinition)16 DefaultType (eu.esdihumboldt.hale.common.align.model.impl.DefaultType)15 Cell (eu.esdihumboldt.hale.common.align.model.Cell)14 DefaultCell (eu.esdihumboldt.hale.common.align.model.impl.DefaultCell)12 MutableCell (eu.esdihumboldt.hale.common.align.model.MutableCell)11 JoinParameter (eu.esdihumboldt.hale.common.align.model.functions.join.JoinParameter)10 QName (javax.xml.namespace.QName)9 MutableAlignment (eu.esdihumboldt.hale.common.align.model.MutableAlignment)8 DefaultAlignment (eu.esdihumboldt.hale.common.align.model.impl.DefaultAlignment)8 JoinCondition (eu.esdihumboldt.hale.common.align.model.functions.join.JoinParameter.JoinCondition)6 Instance (eu.esdihumboldt.hale.common.instance.model.Instance)6 PropertyDefinition (eu.esdihumboldt.hale.common.schema.model.PropertyDefinition)6 LinkedList (java.util.LinkedList)6 Entity (eu.esdihumboldt.hale.common.align.model.Entity)5 ParameterValue (eu.esdihumboldt.hale.common.align.model.ParameterValue)5