Search in sources :

Example 1 with ThreadSafeInstanceSink

use of eu.esdihumboldt.hale.common.align.transformation.service.impl.ThreadSafeInstanceSink in project hale by halestudio.

the class ConceptualSchemaTransformerTest method transformData.

@Override
protected List<Instance> transformData(TransformationExample example) throws Exception {
    ConceptualSchemaTransformer transformer = new ConceptualSchemaTransformer();
    ThreadSafeInstanceSink<DefaultInstanceSink> sink = new ThreadSafeInstanceSink<>(new DefaultInstanceSink());
    final Map<Class<?>, Object> customServices = new HashMap<>();
    customServices.put(FunctionService.class, new AlignmentFunctionService(example.getAlignment()));
    customServices.put(TransformationFunctionService.class, new AlignmentTransformationFunctionService(example.getAlignment()));
    InstanceIndexServiceImpl indexService = new InstanceIndexServiceImpl();
    customServices.put(InstanceIndexService.class, indexService);
    final ServiceProvider serviceProvider = new ServiceProvider() {

        private final ServiceProvider projectScope = new ServiceManager(ServiceManager.SCOPE_PROJECT);

        @SuppressWarnings("unchecked")
        @Override
        public <T> T getService(Class<T> serviceInterface) {
            if (customServices.containsKey(serviceInterface)) {
                return (T) customServices.get(serviceInterface);
            }
            // FIXME global scope not supported yet
            return projectScope.getService(serviceInterface);
        }
    };
    indexService.addPropertyMappings(example.getAlignment().getActiveTypeCells(), serviceProvider);
    InstanceCollection source = example.getSourceInstances();
    try (ResourceIterator<Instance> it = source.iterator()) {
        while (it.hasNext()) {
            indexService.add(it.next(), source);
        }
    }
    transformer.transform(example.getAlignment(), source, sink, serviceProvider, new NullProgressIndicator());
    return sink.getDecoratee().getInstances();
}
Also used : AlignmentTransformationFunctionService(eu.esdihumboldt.hale.common.align.service.impl.AlignmentTransformationFunctionService) HashMap(java.util.HashMap) Instance(eu.esdihumboldt.hale.common.instance.model.Instance) DefaultInstanceSink(eu.esdihumboldt.hale.common.align.transformation.service.impl.DefaultInstanceSink) InstanceCollection(eu.esdihumboldt.hale.common.instance.model.InstanceCollection) NullProgressIndicator(eu.esdihumboldt.hale.common.core.io.impl.NullProgressIndicator) AlignmentFunctionService(eu.esdihumboldt.hale.common.align.service.impl.AlignmentFunctionService) InstanceIndexServiceImpl(eu.esdihumboldt.hale.common.instance.index.InstanceIndexServiceImpl) ConceptualSchemaTransformer(eu.esdihumboldt.cst.ConceptualSchemaTransformer) ServiceManager(eu.esdihumboldt.hale.common.core.service.ServiceManager) ServiceProvider(eu.esdihumboldt.hale.common.core.service.ServiceProvider) ThreadSafeInstanceSink(eu.esdihumboldt.hale.common.align.transformation.service.impl.ThreadSafeInstanceSink)

Example 2 with ThreadSafeInstanceSink

use of eu.esdihumboldt.hale.common.align.transformation.service.impl.ThreadSafeInstanceSink in project hale by halestudio.

the class InlineTransformation method evaluate.

@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 {
    List<PropertyValue> sources = variables.get(null);
    if (sources.isEmpty()) {
        throw new NoResultException("No source available to transform");
    }
    PropertyValue source = sources.get(0);
    Object sourceValue = source.getValue();
    if (sourceValue == null) {
        throw new NoResultException("Source value is null");
    }
    if (!(sourceValue instanceof Instance)) {
        throw new TransformationException("Sources for inline transformation must be instances");
    }
    Instance sourceInstance = (Instance) sourceValue;
    TypeDefinition sourceType = sourceInstance.getDefinition();
    // get the original alignment
    Alignment orgAlignment = getExecutionContext().getAlignment();
    MutableAlignment alignment = new DefaultAlignment(orgAlignment);
    // identify relevant type cell(s)
    MutableCell queryCell = new DefaultCell();
    ListMultimap<String, Type> sourceEntities = ArrayListMultimap.create();
    sourceEntities.put(null, new DefaultType(new TypeEntityDefinition(sourceType, SchemaSpaceID.SOURCE, null)));
    queryCell.setSource(sourceEntities);
    ListMultimap<String, Type> targetEntities = ArrayListMultimap.create();
    targetEntities.put(null, new DefaultType(new TypeEntityDefinition(resultProperty.getDefinition().getPropertyType(), SchemaSpaceID.TARGET, null)));
    queryCell.setTarget(targetEntities);
    Collection<? extends Cell> candidates = alignment.getTypeCells(queryCell);
    if (candidates.isEmpty()) {
        log.error(log.createMessage("No type transformations found for inline transformation", null));
        throw new NoResultException();
    }
    // filter alignment -> only keep relevant type relations
    List<Cell> allTypeCells = new ArrayList<>(alignment.getTypeCells());
    for (Cell cell : allTypeCells) {
        // remove cell
        alignment.removeCell(cell);
        if (!cell.getTransformationMode().equals(TransformationMode.disabled)) {
            // only readd if not disabled
            MutableCell copy = new DefaultCell(cell);
            if (candidates.contains(cell)) {
                // readd as active
                copy.setTransformationMode(TransformationMode.active);
            } else {
                // readd as passive
                copy.setTransformationMode(TransformationMode.passive);
            }
            alignment.addCell(copy);
        }
    }
    // prepare transformation input/output
    DefaultInstanceCollection sourceInstances = new DefaultInstanceCollection();
    sourceInstances.add(sourceInstance);
    DefaultInstanceSink target = new DefaultInstanceSink();
    // run transformation
    TransformationService ts = getExecutionContext().getService(TransformationService.class);
    if (ts == null) {
        throw new TransformationException("Transformation service not available for inline transformation");
    }
    ProgressIndicator progressIndicator = new LogProgressIndicator();
    TransformationReport report = ts.transform(alignment, sourceInstances, new ThreadSafeInstanceSink<InstanceSink>(target), getExecutionContext(), progressIndicator);
    // copy report messages
    log.importMessages(report);
    if (!report.isSuccess()) {
        // copy report messages
        log.importMessages(report);
        throw new TransformationException("Inline transformation failed");
    }
    // extract result
    List<Instance> targetList = target.getInstances();
    if (targetList.isEmpty()) {
        log.error(log.createMessage("Inline transformation yielded no result", null));
        throw new NoResultException("No result from inline transformation");
    }
    if (targetList.size() > 1) {
        log.error(log.createMessage("Inline transformation yielded multiple results, only first result is used", null));
    }
    return targetList.get(0);
}
Also used : MutableCell(eu.esdihumboldt.hale.common.align.model.MutableCell) Instance(eu.esdihumboldt.hale.common.instance.model.Instance) DefaultInstanceSink(eu.esdihumboldt.hale.common.align.transformation.service.impl.DefaultInstanceSink) ArrayList(java.util.ArrayList) NoResultException(eu.esdihumboldt.hale.common.align.transformation.function.impl.NoResultException) DefaultInstanceCollection(eu.esdihumboldt.hale.common.instance.model.impl.DefaultInstanceCollection) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition) Alignment(eu.esdihumboldt.hale.common.align.model.Alignment) MutableAlignment(eu.esdihumboldt.hale.common.align.model.MutableAlignment) DefaultAlignment(eu.esdihumboldt.hale.common.align.model.impl.DefaultAlignment) TypeEntityDefinition(eu.esdihumboldt.hale.common.align.model.impl.TypeEntityDefinition) LogProgressIndicator(eu.esdihumboldt.hale.common.core.io.impl.LogProgressIndicator) ProgressIndicator(eu.esdihumboldt.hale.common.core.io.ProgressIndicator) DefaultAlignment(eu.esdihumboldt.hale.common.align.model.impl.DefaultAlignment) TransformationService(eu.esdihumboldt.hale.common.align.transformation.service.TransformationService) Cell(eu.esdihumboldt.hale.common.align.model.Cell) DefaultCell(eu.esdihumboldt.hale.common.align.model.impl.DefaultCell) MutableCell(eu.esdihumboldt.hale.common.align.model.MutableCell) TransformationException(eu.esdihumboldt.hale.common.align.transformation.function.TransformationException) TransformationReport(eu.esdihumboldt.hale.common.align.transformation.report.TransformationReport) DefaultType(eu.esdihumboldt.hale.common.align.model.impl.DefaultType) PropertyValue(eu.esdihumboldt.hale.common.align.transformation.function.PropertyValue) MutableAlignment(eu.esdihumboldt.hale.common.align.model.MutableAlignment) LogProgressIndicator(eu.esdihumboldt.hale.common.core.io.impl.LogProgressIndicator) Type(eu.esdihumboldt.hale.common.align.model.Type) DefaultType(eu.esdihumboldt.hale.common.align.model.impl.DefaultType) DefaultCell(eu.esdihumboldt.hale.common.align.model.impl.DefaultCell) ThreadSafeInstanceSink(eu.esdihumboldt.hale.common.align.transformation.service.impl.ThreadSafeInstanceSink) DefaultInstanceSink(eu.esdihumboldt.hale.common.align.transformation.service.impl.DefaultInstanceSink) InstanceSink(eu.esdihumboldt.hale.common.align.transformation.service.InstanceSink)

Aggregations

DefaultInstanceSink (eu.esdihumboldt.hale.common.align.transformation.service.impl.DefaultInstanceSink)2 ThreadSafeInstanceSink (eu.esdihumboldt.hale.common.align.transformation.service.impl.ThreadSafeInstanceSink)2 Instance (eu.esdihumboldt.hale.common.instance.model.Instance)2 ConceptualSchemaTransformer (eu.esdihumboldt.cst.ConceptualSchemaTransformer)1 Alignment (eu.esdihumboldt.hale.common.align.model.Alignment)1 Cell (eu.esdihumboldt.hale.common.align.model.Cell)1 MutableAlignment (eu.esdihumboldt.hale.common.align.model.MutableAlignment)1 MutableCell (eu.esdihumboldt.hale.common.align.model.MutableCell)1 Type (eu.esdihumboldt.hale.common.align.model.Type)1 DefaultAlignment (eu.esdihumboldt.hale.common.align.model.impl.DefaultAlignment)1 DefaultCell (eu.esdihumboldt.hale.common.align.model.impl.DefaultCell)1 DefaultType (eu.esdihumboldt.hale.common.align.model.impl.DefaultType)1 TypeEntityDefinition (eu.esdihumboldt.hale.common.align.model.impl.TypeEntityDefinition)1 AlignmentFunctionService (eu.esdihumboldt.hale.common.align.service.impl.AlignmentFunctionService)1 AlignmentTransformationFunctionService (eu.esdihumboldt.hale.common.align.service.impl.AlignmentTransformationFunctionService)1 PropertyValue (eu.esdihumboldt.hale.common.align.transformation.function.PropertyValue)1 TransformationException (eu.esdihumboldt.hale.common.align.transformation.function.TransformationException)1 NoResultException (eu.esdihumboldt.hale.common.align.transformation.function.impl.NoResultException)1 TransformationReport (eu.esdihumboldt.hale.common.align.transformation.report.TransformationReport)1 InstanceSink (eu.esdihumboldt.hale.common.align.transformation.service.InstanceSink)1