Search in sources :

Example 11 with DefaultType

use of eu.esdihumboldt.hale.common.align.model.impl.DefaultType in project hale by halestudio.

the class MarkdownCellExplanationTest method createTestCell.

@SuppressWarnings("unused")
private Cell createTestCell() {
    // cell 1
    MutableCell cell1 = new DefaultCell();
    String id1;
    // must be an existing function
    cell1.setTransformationIdentifier(id1 = "eu.esdihumboldt.hale.align.formattedstring");
    ListMultimap<String, ParameterValue> parameters1 = LinkedListMultimap.create();
    // parameter that does not exist (for parameter list testing)
    parameters1.put("test", new ParameterValue("1"));
    parameters1.put("test", new ParameterValue("2"));
    // existing parameter
    parameters1.put("pattern", new ParameterValue("3"));
    cell1.setTransformationParameters(parameters1);
    ListMultimap<String, Type> source1 = ArrayListMultimap.create();
    QName source1TypeName;
    String source1EntityName;
    TypeDefinition sourceType1 = new DefaultTypeDefinition(source1TypeName = new QName("source1Type"));
    Filter filter = null;
    source1.put(source1EntityName = "var", new DefaultType(new TypeEntityDefinition(sourceType1, SchemaSpaceID.SOURCE, filter)));
    cell1.setSource(source1);
    ListMultimap<String, Type> target1 = ArrayListMultimap.create();
    QName target1TypeName;
    String target1EntityName;
    TypeDefinition targetType1 = new DefaultTypeDefinition(target1TypeName = new QName("http://some.name.space/t1", "target1Type"));
    target1.put(target1EntityName = null, new DefaultType(new TypeEntityDefinition(targetType1, SchemaSpaceID.TARGET, null)));
    cell1.setTarget(target1);
    return cell1;
}
Also used : MutableCell(eu.esdihumboldt.hale.common.align.model.MutableCell) ParameterValue(eu.esdihumboldt.hale.common.align.model.ParameterValue) DefaultType(eu.esdihumboldt.hale.common.align.model.impl.DefaultType) QName(javax.xml.namespace.QName) DefaultTypeDefinition(eu.esdihumboldt.hale.common.schema.model.impl.DefaultTypeDefinition) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition) DefaultTypeDefinition(eu.esdihumboldt.hale.common.schema.model.impl.DefaultTypeDefinition) Type(eu.esdihumboldt.hale.common.align.model.Type) DefaultType(eu.esdihumboldt.hale.common.align.model.impl.DefaultType) TypeEntityDefinition(eu.esdihumboldt.hale.common.align.model.impl.TypeEntityDefinition) DefaultCell(eu.esdihumboldt.hale.common.align.model.impl.DefaultCell) Filter(eu.esdihumboldt.hale.common.instance.model.Filter)

Example 12 with DefaultType

use of eu.esdihumboldt.hale.common.align.model.impl.DefaultType 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)

Example 13 with DefaultType

use of eu.esdihumboldt.hale.common.align.model.impl.DefaultType in project hale by halestudio.

the class SourceTargetTypeSelector method getSelectedCell.

/**
 * Returns the selected cell. If no cell is selected a dummy cell with the
 * selected source and target is returned. Both source and target may or may
 * not be empty in that case.
 *
 * @return the selected cell (or a dummy)
 */
public Cell getSelectedCell() {
    if (selectedCell != null)
        return selectedCell;
    DefaultCell cell = new DefaultCell();
    if (sourceTypeSelector.getSelectedObject() != null) {
        ListMultimap<String, Type> sources = ArrayListMultimap.create(1, 1);
        sources.put(null, new DefaultType((TypeEntityDefinition) sourceTypeSelector.getSelectedObject()));
        cell.setSource(sources);
    }
    if (targetTypeSelector.getSelectedObject() != null) {
        ListMultimap<String, Type> targets = ArrayListMultimap.create(1, 1);
        targets.put(null, new DefaultType((TypeEntityDefinition) targetTypeSelector.getSelectedObject()));
        cell.setTarget(targets);
    }
    return cell;
}
Also used : Type(eu.esdihumboldt.hale.common.align.model.Type) DefaultType(eu.esdihumboldt.hale.common.align.model.impl.DefaultType) TypeEntityDefinition(eu.esdihumboldt.hale.common.align.model.impl.TypeEntityDefinition) DefaultCell(eu.esdihumboldt.hale.common.align.model.impl.DefaultCell) DefaultType(eu.esdihumboldt.hale.common.align.model.impl.DefaultType)

Example 14 with DefaultType

use of eu.esdihumboldt.hale.common.align.model.impl.DefaultType in project hale by halestudio.

the class TypeEntitySelector method createFilters.

private static ViewerFilter[] createFilters(TypeParameterDefinition field) {
    if (field == null) {
        return null;
    }
    List<TypeCondition> conditions = field.getConditions();
    if (conditions == null)
        return new ViewerFilter[0];
    ViewerFilter[] filters = new ViewerFilter[conditions.size()];
    int i = 0;
    for (final TypeCondition condition : conditions) {
        filters[i] = new ViewerFilter() {

            @Override
            public boolean select(Viewer viewer, Object parentElement, Object element) {
                if (element instanceof TypeEntityDefinition) {
                    Type property = new DefaultType((TypeEntityDefinition) element);
                    return condition.accept(property);
                } else
                    return false;
            }
        };
    }
    return filters;
}
Also used : TypeEntityDefinition(eu.esdihumboldt.hale.common.align.model.impl.TypeEntityDefinition) DefaultType(eu.esdihumboldt.hale.common.align.model.impl.DefaultType) Type(eu.esdihumboldt.hale.common.align.model.Type) ViewerFilter(org.eclipse.jface.viewers.ViewerFilter) DefaultType(eu.esdihumboldt.hale.common.align.model.impl.DefaultType) TypeCondition(eu.esdihumboldt.hale.common.align.model.condition.TypeCondition) Viewer(org.eclipse.jface.viewers.Viewer)

Example 15 with DefaultType

use of eu.esdihumboldt.hale.common.align.model.impl.DefaultType in project hale by halestudio.

the class UserFallbackEntityResolver method resolveType.

/**
 * Ask the user to select a replacement for a type.
 *
 * @param original the original entity
 * @param candidate a candidate for the replacement
 * @param schemaSpace the schema space
 * @return the resolved type (may be the original)
 */
public static Type resolveType(TypeEntityDefinition original, @Nullable EntityDefinition candidate, SchemaSpaceID schemaSpace) {
    ResolveCache cache = getCache();
    TypeEntityDefinition replacement = cache.getReplacement(original);
    if (replacement != null) {
        // use cached replacement
        return new DefaultType(replacement);
    }
    ProjectService ps = HaleUI.getServiceProvider().getService(ProjectService.class);
    final AtomicBoolean canceled;
    final AtomicBoolean skipped = new AtomicBoolean(false);
    if (ps.getTemporaryProperty(RESOLVE_SKIP_PROPERTY, Value.of(false)).as(Boolean.class)) {
        canceled = new AtomicBoolean(true);
    } else {
        canceled = new AtomicBoolean(false);
    }
    final AtomicReference<EntityDefinition> result = new AtomicReference<>();
    if (!canceled.get()) {
        PlatformUI.getWorkbench().getDisplay().syncExec(new Runnable() {

            @Override
            public void run() {
                TypeEntityResolverDialog dlg = new TypeEntityResolverDialog(Display.getCurrent().getActiveShell(), schemaSpace, "Cell entity could not be resolved", candidate, false) {

                    @Override
                    public void create() {
                        super.create();
                        openTray(new ViewerEntityTray(original));
                    }
                };
                switch(dlg.open()) {
                    case Window.OK:
                        result.set(dlg.getObject());
                        break;
                    case Window.CANCEL:
                        // Don't try to resolve further entities
                        ps.setTemporaryProperty(RESOLVE_SKIP_PROPERTY, Value.of(true));
                        canceled.set(true);
                        break;
                    case TypeEntityResolverDialog.SKIP:
                        // skip this entity
                        skipped.set(true);
                        break;
                    default:
                        canceled.set(true);
                }
            }
        });
    }
    EntityDefinition def = result.get();
    if (canceled.get() || skipped.get()) {
        // return the original so the cell is not lost
        return new DefaultType(original);
    } else if (def == null) {
        // caller must take care about this
        return null;
    } else {
        TypeEntityDefinition ted = (TypeEntityDefinition) def;
        // make sure that the type is classified as mapping relevant
        if (!ted.getType().getConstraint(MappingRelevantFlag.class).isEnabled()) {
            SchemaService ss = PlatformUI.getWorkbench().getService(SchemaService.class);
            ss.toggleMappable(schemaSpace, Collections.singleton(ted.getType()));
        }
        cache.put(original, ted);
        return new DefaultType(ted);
    }
}
Also used : DefaultType(eu.esdihumboldt.hale.common.align.model.impl.DefaultType) TypeEntityResolverDialog(eu.esdihumboldt.hale.ui.service.align.resolver.internal.TypeEntityResolverDialog) ProjectService(eu.esdihumboldt.hale.ui.service.project.ProjectService) ViewerEntityTray(eu.esdihumboldt.hale.ui.service.align.resolver.internal.ViewerEntityTray) AtomicReference(java.util.concurrent.atomic.AtomicReference) 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) SchemaService(eu.esdihumboldt.hale.ui.service.schema.SchemaService) ResolveCache(eu.esdihumboldt.hale.ui.service.align.resolver.internal.ResolveCache) MappingRelevantFlag(eu.esdihumboldt.hale.common.schema.model.constraint.type.MappingRelevantFlag)

Aggregations

DefaultType (eu.esdihumboldt.hale.common.align.model.impl.DefaultType)16 TypeEntityDefinition (eu.esdihumboldt.hale.common.align.model.impl.TypeEntityDefinition)15 Type (eu.esdihumboldt.hale.common.align.model.Type)14 DefaultCell (eu.esdihumboldt.hale.common.align.model.impl.DefaultCell)12 MutableCell (eu.esdihumboldt.hale.common.align.model.MutableCell)6 PropertyEntityDefinition (eu.esdihumboldt.hale.common.align.model.impl.PropertyEntityDefinition)6 TypeDefinition (eu.esdihumboldt.hale.common.schema.model.TypeDefinition)6 Cell (eu.esdihumboldt.hale.common.align.model.Cell)5 MutableAlignment (eu.esdihumboldt.hale.common.align.model.MutableAlignment)5 DefaultAlignment (eu.esdihumboldt.hale.common.align.model.impl.DefaultAlignment)5 ArrayList (java.util.ArrayList)5 Test (org.junit.Test)5 EntityDefinition (eu.esdihumboldt.hale.common.align.model.EntityDefinition)4 DefaultProperty (eu.esdihumboldt.hale.common.align.model.impl.DefaultProperty)4 DefaultSchema (eu.esdihumboldt.hale.common.schema.model.impl.DefaultSchema)4 DefaultTypeDefinition (eu.esdihumboldt.hale.common.schema.model.impl.DefaultTypeDefinition)4 BufferedOutputStream (java.io.BufferedOutputStream)4 File (java.io.File)4 FileInputStream (java.io.FileInputStream)4 FileOutputStream (java.io.FileOutputStream)4