Search in sources :

Example 11 with Alignment

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

the class JoinHandler method handleTypeTransformation.

/**
 * @see eu.esdihumboldt.hale.io.appschema.writer.internal.TypeTransformationHandler#handleTypeTransformation(eu.esdihumboldt.hale.common.align.model.Cell,
 *      eu.esdihumboldt.hale.io.appschema.writer.internal.mapping.AppSchemaMappingContext)
 */
@Override
public FeatureTypeMapping handleTypeTransformation(Cell typeCell, AppSchemaMappingContext context) {
    AppSchemaMappingWrapper mapping = context.getMappingWrapper();
    Alignment alignment = context.getAlignment();
    FeatureChaining featureChaining = context.getFeatureChaining();
    final JoinParameter joinParameter = typeCell.getTransformationParameters().get(PARAMETER_JOIN).get(0).as(JoinParameter.class);
    String validation = joinParameter.validate();
    if (validation != null)
        throw new IllegalArgumentException("Join parameter invalid: " + validation);
    // check only single predicate conditions have been used
    int[] conditionCount = new int[joinParameter.getTypes().size()];
    List<JoinCondition> joinConditions = getSortedJoinConditions(joinParameter);
    for (JoinCondition joinCondition : joinConditions) {
        TypeEntityDefinition joinType = AlignmentUtil.getTypeEntity(joinCondition.joinProperty);
        int typeIdx = joinParameter.getTypes().indexOf(joinType);
        conditionCount[typeIdx]++;
        if (conditionCount[typeIdx] > 1) {
            throw new IllegalArgumentException("Only single condition joins are supported so far");
        }
    }
    FeatureTypeMapping topMostMapping = null;
    for (int chainIdx = 0; chainIdx < joinParameter.getTypes().size() - 1; chainIdx++) {
        ChainConfiguration previousChainConf = null;
        ChainConfiguration chainConf = null;
        if (featureChaining != null) {
            chainConf = featureChaining.getChain(typeCell.getId(), chainIdx);
            if (chainConf != null && chainConf.getPrevChainIndex() >= 0) {
                previousChainConf = featureChaining.getChain(typeCell.getId(), chainConf.getPrevChainIndex());
            }
        }
        // join is done pair-wise: I assume the first type is the container
        // type, whereas the second type is nested in the first
        JoinCondition joinCondition = joinConditions.get(chainIdx);
        baseProperty = joinCondition.baseProperty;
        joinProperty = joinCondition.joinProperty;
        TypeEntityDefinition containerType = AlignmentUtil.getTypeEntity(baseProperty);
        TypeEntityDefinition nestedType = AlignmentUtil.getTypeEntity(joinProperty);
        // build FeatureTypeMapping for container type
        // Entity containerTypeTarget = typeCell.getTarget().values().iterator().next();
        // TypeDefinition containerTypeTargetType = containerTypeTarget.getDefinition().getType();
        EntityDefinition containerTypeTarget = null;
        TypeDefinition containerTypeTargetType = null;
        String containerTypeTargetMappingName = null;
        if (previousChainConf == null) {
            containerTypeTarget = getTargetType(typeCell).getDefinition();
            containerTypeTargetType = containerTypeTarget.getType();
        } else {
            containerTypeTarget = previousChainConf.getNestedTypeTarget();
            containerTypeTargetType = previousChainConf.getNestedTypeTarget().getDefinition().getPropertyType();
            containerTypeTargetMappingName = previousChainConf.getMappingName();
        }
        String containerMappingName = null;
        if (previousChainConf != null) {
            containerMappingName = previousChainConf.getMappingName();
        }
        FeatureTypeMapping containerFTMapping = context.getOrCreateFeatureTypeMapping(containerTypeTargetType, containerMappingName);
        containerFTMapping.setSourceType(containerType.getDefinition().getName().getLocalPart());
        // build FeatureTypeMapping for nested type
        TypeDefinition nestedFT = null;
        List<ChildContext> nestedFTPath = null;
        FeatureTypeMapping nestedFTMapping = null;
        if (chainConf != null) {
            nestedFT = chainConf.getNestedTypeTarget().getDefinition().getPropertyType();
            nestedFTPath = chainConf.getNestedTypeTarget().getPropertyPath();
            // remove last element
            nestedFTPath = nestedFTPath.subList(0, nestedFTPath.size() - 1);
            nestedFTMapping = context.getOrCreateFeatureTypeMapping(nestedFT, chainConf.getMappingName());
            nestedFTMapping.setSourceType(nestedType.getDefinition().getName().getLocalPart());
        } else {
            if (joinParameter.getTypes().size() > 2) {
                throw new IllegalArgumentException("If no feature chaining configuration is provided, only join between 2 types is supported");
            }
            // do your best to figure it out on your own... good luck!
            Collection<? extends Cell> propertyCells = alignment.getPropertyCells(typeCell);
            for (Cell propertyCell : propertyCells) {
                Property sourceProperty = AppSchemaMappingUtils.getSourceProperty(propertyCell);
                if (sourceProperty != null) {
                    TypeDefinition sourceType = sourceProperty.getDefinition().getDefinition().getParentType();
                    if (sourceType.getName().equals(nestedType.getDefinition().getName())) {
                        // source property belongs to nested type: determine
                        // target type
                        Property targetProperty = getTargetProperty(propertyCell);
                        // nestedFT =
                        // findOwningFeatureType(targetProperty.getDefinition());
                        nestedFT = findOwningType(targetProperty.getDefinition(), context.getRelevantTargetTypes());
                        if (nestedFT != null && !nestedFT.getName().equals(containerTypeTargetType.getName())) {
                            // target property belongs to a feature type
                            // different from the already mapped one: build
                            // a new mapping
                            nestedFTPath = findOwningTypePath(targetProperty.getDefinition(), context.getRelevantTargetTypes());
                            nestedFTMapping = context.getOrCreateFeatureTypeMapping(nestedFT);
                            nestedFTMapping.setSourceType(nestedType.getDefinition().getName().getLocalPart());
                            // in the join
                            break;
                        } else if (isHRefAttribute(targetProperty.getDefinition().getDefinition())) {
                            // check if target property is a href attribute
                            Property hrefProperty = targetProperty;
                            List<ChildContext> hrefPropertyPath = hrefProperty.getDefinition().getPropertyPath();
                            List<ChildContext> hrefContainerPath = hrefPropertyPath.subList(0, hrefPropertyPath.size() - 1);
                            TypeDefinition hrefParentType = hrefProperty.getDefinition().getDefinition().getParentType();
                            // TypeDefinition childFT =
                            // findChildFeatureType(hrefParentType);
                            TypeDefinition childFT = AppSchemaMappingUtils.findChildType(hrefParentType, context.getRelevantTargetTypes());
                            if (childFT != null) {
                                nestedFTPath = hrefContainerPath;
                                nestedFTMapping = context.getOrCreateFeatureTypeMapping(childFT);
                                nestedFTMapping.setSourceType(nestedType.getDefinition().getName().getLocalPart());
                                // involved in the join
                                break;
                            }
                        }
                    }
                }
            }
        }
        // build join mapping
        if (nestedFTMapping != null && nestedFTPath != null) {
            AttributeMappingType containerJoinMapping = context.getOrCreateAttributeMapping(containerTypeTargetType, containerTypeTargetMappingName, nestedFTPath);
            containerJoinMapping.setTargetAttribute(mapping.buildAttributeXPath(containerTypeTargetType, nestedFTPath));
            // set isMultiple attribute
            PropertyDefinition targetPropertyDef = nestedFTPath.get(nestedFTPath.size() - 1).getChild().asProperty();
            if (AppSchemaMappingUtils.isMultiple(targetPropertyDef)) {
                containerJoinMapping.setIsMultiple(true);
            }
            AttributeExpressionMappingType containerSourceExpr = new AttributeExpressionMappingType();
            // join column extracted from join condition
            containerSourceExpr.setOCQL(baseProperty.getDefinition().getName().getLocalPart());
            containerSourceExpr.setLinkElement(getLinkElementValue(nestedFTMapping));
            String linkField = context.getUniqueFeatureLinkAttribute(nestedFT, nestedFTMapping.getMappingName());
            containerSourceExpr.setLinkField(linkField);
            containerJoinMapping.setSourceExpression(containerSourceExpr);
            AttributeMappingType nestedJoinMapping = new AttributeMappingType();
            AttributeExpressionMappingType nestedSourceExpr = new AttributeExpressionMappingType();
            // join column extracted from join condition
            nestedSourceExpr.setOCQL(joinProperty.getDefinition().getName().getLocalPart());
            nestedJoinMapping.setSourceExpression(nestedSourceExpr);
            nestedJoinMapping.setTargetAttribute(linkField);
            nestedFTMapping.getAttributeMappings().getAttributeMapping().add(nestedJoinMapping);
        }
        if (chainIdx == 0) {
            topMostMapping = containerFTMapping;
        }
    }
    return topMostMapping;
}
Also used : FeatureChaining(eu.esdihumboldt.hale.io.appschema.model.FeatureChaining) ChainConfiguration(eu.esdihumboldt.hale.io.appschema.model.ChainConfiguration) JoinParameter(eu.esdihumboldt.hale.common.align.model.functions.join.JoinParameter) FeatureTypeMapping(eu.esdihumboldt.hale.io.appschema.impl.internal.generated.app_schema.TypeMappingsPropertyType.FeatureTypeMapping) PropertyDefinition(eu.esdihumboldt.hale.common.schema.model.PropertyDefinition) JoinCondition(eu.esdihumboldt.hale.common.align.model.functions.join.JoinParameter.JoinCondition) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition) 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) Alignment(eu.esdihumboldt.hale.common.align.model.Alignment) TypeEntityDefinition(eu.esdihumboldt.hale.common.align.model.impl.TypeEntityDefinition) AttributeMappingType(eu.esdihumboldt.hale.io.appschema.impl.internal.generated.app_schema.AttributeMappingType) ChildContext(eu.esdihumboldt.hale.common.align.model.ChildContext) List(java.util.List) AppSchemaMappingWrapper(eu.esdihumboldt.hale.io.appschema.writer.internal.mapping.AppSchemaMappingWrapper) AttributeExpressionMappingType(eu.esdihumboldt.hale.io.appschema.impl.internal.generated.app_schema.AttributeExpressionMappingType) Cell(eu.esdihumboldt.hale.common.align.model.Cell) AppSchemaMappingUtils.getTargetProperty(eu.esdihumboldt.hale.io.appschema.writer.AppSchemaMappingUtils.getTargetProperty) Property(eu.esdihumboldt.hale.common.align.model.Property)

Example 12 with Alignment

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

the class ExamplesContent method getMappingContent.

/**
 * Get the mapping documentation content for an example project.
 *
 * @param projectId the project ID
 * @return the mapping documentation content stream or <code>null</code>
 */
private InputStream getMappingContent(String projectId) {
    if (!mappingDocExportInitialized) {
        mappingDocExport = HaleIO.createIOProvider(AlignmentWriter.class, null, ID_MAPPING_EXPORT);
        if (mappingDocExport == null) {
            log.error("Could not create mapping documentation exporter.");
        }
        mappingDocExportInitialized = true;
    }
    if (mappingDocExport == null) {
        // no mapping documentation export possible
        return null;
    }
    if (tempMappingDir == null) {
        tempMappingDir = Files.createTempDir();
        tempMappingDir.deleteOnExit();
    }
    // the file of the mapping documentation
    File mappingDoc = new File(tempMappingDir, projectId + ".html");
    if (!mappingDoc.exists()) {
        ATransaction trans = log.begin("Generate example mapping documentation");
        try {
            // create the mapping documentation
            ExampleProject exampleProject = ExampleProjectExtension.getInstance().get(projectId);
            final Project project = (Project) exampleProject.getInfo();
            // determine alignment location - contained in project file, not
            // a resource
            URI alignmentLoc = exampleProject.getAlignmentLocation();
            if (alignmentLoc == null) {
                // no alignment present
                return null;
            }
            // store configurations per action ID
            Multimap<String, IOConfiguration> confs = HashMultimap.create();
            for (IOConfiguration conf : project.getResources()) {
                confs.put(conf.getActionId(), conf);
            }
            // load schemas
            // source schemas
            LoadSchemaAdvisor source = new LoadSchemaAdvisor(SchemaSpaceID.SOURCE);
            for (IOConfiguration conf : confs.get(SchemaIO.ACTION_LOAD_SOURCE_SCHEMA)) {
                source.setConfiguration(conf);
                executeProvider(source, conf.getProviderId(), null);
            }
            // target schemas
            LoadSchemaAdvisor target = new LoadSchemaAdvisor(SchemaSpaceID.TARGET);
            for (IOConfiguration conf : confs.get(SchemaIO.ACTION_LOAD_TARGET_SCHEMA)) {
                target.setConfiguration(conf);
                executeProvider(target, conf.getProviderId(), null);
            }
            // load alignment
            // manual loading needed, as we can't rely on the environment
            // alignment advisor
            DefaultInputSupplier alignmentIn = new DefaultInputSupplier(alignmentLoc);
            AlignmentReader reader = HaleIO.findIOProvider(AlignmentReader.class, alignmentIn, alignmentLoc.getPath());
            LoadAlignmentAdvisor alignmentAdvisor = new LoadAlignmentAdvisor(null, source.getSchemaSpace(), target.getSchemaSpace(), exampleProject.getUpdater());
            reader.setSource(alignmentIn);
            executeProvider(alignmentAdvisor, null, reader);
            Alignment alignment = alignmentAdvisor.getAlignment();
            if (alignment != null) {
                // save alignment docu
                synchronized (mappingDocExport) {
                    // only a single instance
                    mappingDocExport.setAlignment(alignment);
                    mappingDocExport.setTarget(new FileIOSupplier(mappingDoc));
                    if (mappingDocExport instanceof ProjectInfoAware) {
                        ProjectInfo smallInfo = new ProjectInfo() {

                            @Override
                            public String getName() {
                                return project.getName();
                            }

                            @Override
                            public Date getModified() {
                                return null;
                            }

                            @Override
                            public Version getHaleVersion() {
                                return null;
                            }

                            @Override
                            public String getDescription() {
                                return project.getDescription();
                            }

                            @Override
                            public Date getCreated() {
                                return null;
                            }

                            @Override
                            public String getAuthor() {
                                return project.getAuthor();
                            }
                        };
                        // project);
                        ((ProjectInfoAware) mappingDocExport).setProjectInfo(smallInfo);
                    }
                    mappingDocExport.execute(null);
                }
                mappingDoc.deleteOnExit();
            }
        } catch (Throwable e) {
            log.error("Error generating mapping documentation for example project", e);
            return null;
        } finally {
            trans.end();
        }
    }
    if (mappingDoc.exists()) {
        try {
            return new FileInputStream(mappingDoc);
        } catch (FileNotFoundException e) {
            return null;
        }
    } else
        return null;
}
Also used : AlignmentReader(eu.esdihumboldt.hale.common.align.io.AlignmentReader) DefaultInputSupplier(eu.esdihumboldt.hale.common.core.io.supplier.DefaultInputSupplier) IOConfiguration(eu.esdihumboldt.hale.common.core.io.project.model.IOConfiguration) FileNotFoundException(java.io.FileNotFoundException) ExampleProject(eu.esdihumboldt.hale.doc.user.examples.internal.extension.ExampleProject) URI(java.net.URI) ProjectInfoAware(eu.esdihumboldt.hale.common.core.io.project.ProjectInfoAware) FileInputStream(java.io.FileInputStream) ExampleProject(eu.esdihumboldt.hale.doc.user.examples.internal.extension.ExampleProject) Project(eu.esdihumboldt.hale.common.core.io.project.model.Project) Alignment(eu.esdihumboldt.hale.common.align.model.Alignment) ATransaction(de.fhg.igd.slf4jplus.ATransaction) ProjectInfo(eu.esdihumboldt.hale.common.core.io.project.ProjectInfo) FileIOSupplier(eu.esdihumboldt.hale.common.core.io.supplier.FileIOSupplier) LoadSchemaAdvisor(eu.esdihumboldt.hale.common.schema.io.impl.LoadSchemaAdvisor) LoadAlignmentAdvisor(eu.esdihumboldt.hale.common.align.io.impl.LoadAlignmentAdvisor) File(java.io.File) AlignmentWriter(eu.esdihumboldt.hale.common.align.io.AlignmentWriter)

Example 13 with Alignment

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

the class AbstractDefaultFunctionService method getCustomPropertyFunction.

private PropertyFunctionDefinition getCustomPropertyFunction(String id) {
    Alignment al = getCurrentAlignment();
    if (al != null) {
        String localId = id;
        if (localId.startsWith(PREFIX_ALIGNMENT_FUNCTION)) {
            localId = localId.substring(PREFIX_ALIGNMENT_FUNCTION.length());
        }
        CustomPropertyFunction cf = al.getAllCustomPropertyFunctions().get(localId);
        if (cf != null) {
            return new AlignmentFunctionDescriptor(cf.getDescriptor());
        }
    }
    return null;
}
Also used : Alignment(eu.esdihumboldt.hale.common.align.model.Alignment) CustomPropertyFunction(eu.esdihumboldt.hale.common.align.extension.function.custom.CustomPropertyFunction)

Example 14 with Alignment

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

the class AbstractDefaultTransformationFunctionService method getPropertyTransformations.

@Override
public List<PropertyTransformationFactory> getPropertyTransformations(String functionId) {
    List<PropertyTransformationFactory> functions = super.getPropertyTransformations(functionId);
    Alignment al = getCurrentAlignment();
    if (al != null) {
        List<PropertyTransformationFactory> cfs = new ArrayList<>(functions);
        String localId = functionId;
        if (localId.startsWith(PREFIX_ALIGNMENT_FUNCTION)) {
            localId = localId.substring(PREFIX_ALIGNMENT_FUNCTION.length());
        }
        CustomPropertyFunction cf = al.getAllCustomPropertyFunctions().get(localId);
        if (cf != null) {
            cfs.add(new CustomPropertyFunctionFactory(cf));
        }
        functions = cfs;
    }
    return functions;
}
Also used : Alignment(eu.esdihumboldt.hale.common.align.model.Alignment) ArrayList(java.util.ArrayList) PropertyTransformationFactory(eu.esdihumboldt.hale.common.align.extension.transformation.PropertyTransformationFactory) CustomPropertyFunction(eu.esdihumboldt.hale.common.align.extension.function.custom.CustomPropertyFunction)

Example 15 with Alignment

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

the class AbstractMergeCellMigratorTest method mergeWithMigrator.

/**
 * Perform merging using a specific merge migrator.
 *
 * @param migrator the migrator to test, <code>null</code> if the migrator
 *            configured in the system should be used
 * @param cellId the ID of the cell to migrate
 * @param projectToMigrate the location of the project containing the cell
 *            (Mapping from B to C)
 * @param matchingProject the location of the project providing the matching
 *            (Mapping from A to B) information
 * @return the merge result
 * @throws Exception if preparing or running the test fails
 */
public List<MutableCell> mergeWithMigrator(MergeCellMigrator migrator, String cellId, URL projectToMigrate, URL matchingProject) throws Exception {
    ProjectTransformationEnvironment projectToMigrateEnv = projectCache.get(projectToMigrate.toURI());
    Alignment migrateAlignment = projectToMigrateEnv.getAlignment();
    Cell cellToMigrate = migrateAlignment.getCell(cellId);
    assertNotNull(MessageFormat.format("Cell with ID {0} not found in alignment to migrate", cellId), cellToMigrate);
    ProjectTransformationEnvironment matchingProjectEnv = projectCache.get(matchingProject.toURI());
    return mergeWithMigrator(migrator, cellToMigrate, matchingProjectEnv);
}
Also used : Alignment(eu.esdihumboldt.hale.common.align.model.Alignment) ProjectTransformationEnvironment(eu.esdihumboldt.hale.common.headless.impl.ProjectTransformationEnvironment) Cell(eu.esdihumboldt.hale.common.align.model.Cell) MutableCell(eu.esdihumboldt.hale.common.align.model.MutableCell)

Aggregations

Alignment (eu.esdihumboldt.hale.common.align.model.Alignment)23 Cell (eu.esdihumboldt.hale.common.align.model.Cell)12 AlignmentService (eu.esdihumboldt.hale.ui.service.align.AlignmentService)10 ArrayList (java.util.ArrayList)9 TypeEntityDefinition (eu.esdihumboldt.hale.common.align.model.impl.TypeEntityDefinition)6 CustomPropertyFunction (eu.esdihumboldt.hale.common.align.extension.function.custom.CustomPropertyFunction)5 EntityDefinition (eu.esdihumboldt.hale.common.align.model.EntityDefinition)5 MutableCell (eu.esdihumboldt.hale.common.align.model.MutableCell)5 DefaultAlignment (eu.esdihumboldt.hale.common.align.model.impl.DefaultAlignment)5 DefaultCell (eu.esdihumboldt.hale.common.align.model.impl.DefaultCell)4 MutableAlignment (eu.esdihumboldt.hale.common.align.model.MutableAlignment)3 DefaultInputSupplier (eu.esdihumboldt.hale.common.core.io.supplier.DefaultInputSupplier)3 URI (java.net.URI)3 Collection (java.util.Collection)3 HashMap (java.util.HashMap)3 HashSet (java.util.HashSet)3 ATransaction (de.fhg.igd.slf4jplus.ATransaction)2 PropertyFunctionDefinition (eu.esdihumboldt.hale.common.align.extension.function.PropertyFunctionDefinition)2 BaseAlignmentCell (eu.esdihumboldt.hale.common.align.model.BaseAlignmentCell)2 ChildContext (eu.esdihumboldt.hale.common.align.model.ChildContext)2