Search in sources :

Example 21 with PropertyEntityDefinition

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

the class AlignmentUtil method reparentCell.

/**
 * Returns a cell like the given property cell with all source and target
 * types matching those off the given type cell.<br>
 * If the types already match they are unchanged. If the types are sub types
 * of the types of the type cell they are changed. If no change is necessary
 * the cell itself is returned.
 *
 * @param propertyCell the property cell to update
 * @param typeCell the type cell with the target types
 * @param strict If false and the target type cell has no sources or target,
 *            the property cell is updated to have the sources/target in
 *            their declaring type. If true, said properties are left
 *            unchanged. Does not matter for complete type cells, since they
 *            have sources and a target.
 * @return the updated cell or <code>null</code> if an update isn't possible
 */
public static Cell reparentCell(Cell propertyCell, Cell typeCell, boolean strict) {
    ListMultimap<String, Entity> sources = ArrayListMultimap.create();
    ListMultimap<String, Entity> targets = ArrayListMultimap.create();
    boolean updateNecessary = false;
    // XXX are updates to the property path needed?
    // Currently not, since ChildDefinitions are compared by their names
    // only.
    // TARGETS
    Entity targetEntity = CellUtil.getFirstEntity(typeCell.getTarget());
    if (targetEntity != null) {
        TypeDefinition typeCellTargetType = ((Type) targetEntity).getDefinition().getDefinition();
        for (Entry<String, ? extends Entity> target : propertyCell.getTarget().entries()) {
            TypeDefinition propertyCellTargetType = target.getValue().getDefinition().getType();
            if (propertyCellTargetType.equals(typeCellTargetType))
                targets.put(target.getKey(), target.getValue());
            else if (DefinitionUtil.isSuperType(typeCellTargetType, propertyCellTargetType)) {
                PropertyEntityDefinition oldDef = (PropertyEntityDefinition) target.getValue().getDefinition();
                targets.put(target.getKey(), new DefaultProperty(new PropertyEntityDefinition(typeCellTargetType, oldDef.getPropertyPath(), SchemaSpaceID.TARGET, null)));
                updateNecessary = true;
            } else {
                // a cell with targets in more than one type
                return null;
            }
        }
    } else if (!strict)
        updateNecessary |= reparentToDeclaring(propertyCell.getTarget(), targets);
    else
        targets.putAll(propertyCell.getTarget());
    if (propertyCell.getSource() != null && !propertyCell.getSource().isEmpty()) {
        if (typeCell.getSource() != null && !typeCell.getSource().isEmpty()) {
            // collect source entity definitions
            Collection<TypeEntityDefinition> typeCellSourceTypes = new ArrayList<TypeEntityDefinition>();
            for (Entity entity : typeCell.getSource().values()) typeCellSourceTypes.add((TypeEntityDefinition) entity.getDefinition());
            for (Entry<String, ? extends Entity> source : propertyCell.getSource().entries()) {
                TypeEntityDefinition propertyCellSourceType = getTypeEntity(source.getValue().getDefinition());
                if (typeCellSourceTypes.contains(propertyCellSourceType))
                    sources.put(source.getKey(), source.getValue());
                else {
                    boolean matchFound = false;
                    // maybe the whole cell should be duplicated?
                    for (TypeEntityDefinition typeCellSourceType : typeCellSourceTypes) {
                        if (DefinitionUtil.isSuperType(typeCellSourceType.getDefinition(), propertyCellSourceType.getDefinition()) && (propertyCellSourceType.getFilter() == null || propertyCellSourceType.getFilter().equals(typeCellSourceType.getFilter()))) {
                            if (matchFound)
                                log.warn("Inherited property cell source matches multiple sources of type cell.");
                            matchFound = true;
                            PropertyEntityDefinition oldDef = (PropertyEntityDefinition) source.getValue().getDefinition();
                            sources.put(source.getKey(), new DefaultProperty(new PropertyEntityDefinition(typeCellSourceType.getDefinition(), oldDef.getPropertyPath(), SchemaSpaceID.SOURCE, typeCellSourceType.getFilter())));
                            updateNecessary = true;
                        // XXX break; if only one match should be added
                        }
                    }
                    if (!matchFound) {
                        // cell
                        return null;
                    }
                }
            }
        } else if (!strict)
            updateNecessary |= reparentToDeclaring(propertyCell.getSource(), sources);
        else
            targets.putAll(propertyCell.getTarget());
    }
    if (updateNecessary) {
        MutableCell copy = new DefaultCell(propertyCell);
        copy.setSource(sources);
        copy.setTarget(targets);
        propertyCell = copy;
    }
    return propertyCell;
}
Also used : ArrayList(java.util.ArrayList) DefaultProperty(eu.esdihumboldt.hale.common.align.model.impl.DefaultProperty) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition) 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)

Example 22 with PropertyEntityDefinition

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

the class AlignmentUtil method reparentToDeclaring.

/**
 * Copies the properties from original to modified, modifying properties to
 * their declaring type, if the type has no filter set.
 *
 * @param original the original Multimap of properties
 * @param modified the target Multimap
 * @return true, if a property was changed, false otherwise
 */
private static boolean reparentToDeclaring(ListMultimap<String, ? extends Entity> original, ListMultimap<String, Entity> modified) {
    boolean changed = false;
    for (Entry<String, ? extends Entity> oEntity : original.entries()) {
        PropertyEntityDefinition property = (PropertyEntityDefinition) oEntity.getValue().getDefinition();
        ChildDefinition<?> childDef = property.getPropertyPath().get(0).getChild();
        if (Objects.equals(childDef.getDeclaringGroup(), childDef.getParentType()) || property.getFilter() != null)
            modified.put(oEntity.getKey(), oEntity.getValue());
        else if (childDef.getDeclaringGroup() instanceof TypeDefinition) {
            modified.put(oEntity.getKey(), new DefaultProperty(new PropertyEntityDefinition((TypeDefinition) childDef.getDeclaringGroup(), property.getPropertyPath(), property.getSchemaSpace(), null)));
            changed = true;
        } else {
            // declaring group of first level property no type
            // definition?
            // simply add it without change, shouldn't happen
            modified.put(oEntity.getKey(), oEntity.getValue());
        }
    }
    return changed;
}
Also used : PropertyEntityDefinition(eu.esdihumboldt.hale.common.align.model.impl.PropertyEntityDefinition) DefaultProperty(eu.esdihumboldt.hale.common.align.model.impl.DefaultProperty) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition)

Example 23 with PropertyEntityDefinition

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

the class PropertyOrChildrenTypeCondition method accept.

/**
 * @see EntityCondition#accept(Entity)
 */
@Override
public boolean accept(Property entity) {
    PropertyEntityDefinition ped = entity.getDefinition();
    Set<TypeDefinition> tested = new HashSet<>();
    return accept(ped, tested);
}
Also used : PropertyEntityDefinition(eu.esdihumboldt.hale.common.align.model.impl.PropertyEntityDefinition) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition) HashSet(java.util.HashSet)

Example 24 with PropertyEntityDefinition

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

the class MergeUtil method getKeyPropertyDefinitions.

/**
 * Get the {@link PropertyEntityDefinition} paths for all key properties of
 * a merge.<br>
 * <br>
 * <b>Subproperties are not yet supported to be part of a merge key.
 * Therefore, the inner lists will contain only a single property for the
 * time being.</b>
 *
 * @param cell Mapping cell of the merge
 * @return <code>PropertyEntityDefinition</code> paths for all key
 *         properties of the merge
 */
public static List<PropertyEntityDefinition> getKeyPropertyDefinitions(Cell cell) {
    List<PropertyEntityDefinition> result = new ArrayList<>();
    List<List<QName>> mergeProperties = MergeUtil.getProperties(cell.getTransformationParameters(), MergeFunction.PARAMETER_PROPERTY);
    for (Entity sourceEntity : cell.getSource().values()) {
        PropertyEntityDefinition keyProperty = null;
        for (List<QName> mergePropertyPath : mergeProperties) {
            // TODO Only root property is considered for now
            // If the propertyPath can consist of more than one element,
            // make sure to construct the PropertyEntityDefinition
            // accordingly
            QName root = mergePropertyPath.get(0);
            for (PropertyEntityDefinition property : AlignmentUtil.getChildrenWithoutContexts(sourceEntity)) {
                if (property.getDefinition().getName().equals(root)) {
                    keyProperty = property;
                    break;
                }
            }
            if (keyProperty != null) {
                result.add(keyProperty);
            }
        }
    }
    return result;
}
Also used : Entity(eu.esdihumboldt.hale.common.align.model.Entity) PropertyEntityDefinition(eu.esdihumboldt.hale.common.align.model.impl.PropertyEntityDefinition) QName(javax.xml.namespace.QName) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List)

Example 25 with PropertyEntityDefinition

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

the class DefaultAlignmentIOTest method testBaseAlignmentSaveLoad.

/**
 * Tests base alignment add, save and load.
 *
 * @throws Exception if an error occurs
 */
@Test
public void testBaseAlignmentSaveLoad() throws Exception {
    DefaultAlignment baseAlignment = new DefaultAlignment();
    MutableAlignment alignment = new DefaultAlignment();
    Schema schema = TestUtil.loadSchema(getClass().getResource("/testdata/simple/t1.xsd").toURI());
    Iterator<? extends TypeDefinition> iter = schema.getMappingRelevantTypes().iterator();
    TypeDefinition t = iter.next();
    if (!t.getName().getLocalPart().equals("T1")) {
        t = iter.next();
    }
    DefaultCell cell1 = new DefaultCell();
    cell1.setTransformationIdentifier("trans1");
    ListMultimap<String, Type> source = ArrayListMultimap.create();
    source.put(null, new DefaultType(new TypeEntityDefinition(t, SchemaSpaceID.SOURCE, null)));
    cell1.setSource(source);
    ListMultimap<String, Type> target = ArrayListMultimap.create();
    target.put(null, new DefaultType(new TypeEntityDefinition(t, SchemaSpaceID.TARGET, null)));
    cell1.setTarget(target);
    DefaultCell cell2 = new DefaultCell();
    cell2.setTransformationIdentifier("trans2");
    List<ChildContext> childContext = new ArrayList<ChildContext>();
    PropertyDefinition child = DefinitionUtil.getChild(t, new QName("a1")).asProperty();
    childContext.add(new ChildContext(child));
    ListMultimap<String, Property> source2 = ArrayListMultimap.create();
    source2.put(null, new DefaultProperty(new PropertyEntityDefinition(t, childContext, SchemaSpaceID.SOURCE, null)));
    cell2.setSource(source2);
    ListMultimap<String, Property> target2 = ArrayListMultimap.create();
    target2.put(null, new DefaultProperty(new PropertyEntityDefinition(t, childContext, SchemaSpaceID.TARGET, null)));
    cell2.setTarget(target2);
    // add cell1 to base alignment
    baseAlignment.addCell(cell1);
    // save base alignment
    File baseAlignmentFile = tmp.newFile("alignment_base.xml");
    System.out.println(baseAlignmentFile.getAbsolutePath());
    saveAlignment(baseAlignment, new BufferedOutputStream(new FileOutputStream(baseAlignmentFile)));
    // add as base alignment to extended alignment
    addBaseAlignment(alignment, baseAlignmentFile.toURI(), schema, schema);
    assertEquals(1, alignment.getBaseAlignments().size());
    String usedPrefix = alignment.getBaseAlignments().keySet().iterator().next();
    assertEquals(1, alignment.getCells().size());
    assertEquals(usedPrefix + ":" + cell1.getId(), alignment.getCells().iterator().next().getId());
    // add cell2 to extended alignment
    alignment.addCell(cell2);
    assertEquals(2, alignment.getCells().size());
    assertEquals(1, alignment.getPropertyCells(cell1).size());
    // save extended alignment
    File alignmentFile = tmp.newFile("alignment_extended.xml");
    System.out.println(alignmentFile.getAbsolutePath());
    saveAlignment(alignment, new BufferedOutputStream(new FileOutputStream(alignmentFile)));
    // load extended
    MutableAlignment alignment2 = loadAlignment(new FileInputStream(alignmentFile), schema, schema);
    assertEquals(2, alignment2.getCells().size());
    assertEquals(1, alignment2.getTypeCells().size());
    Cell typeCell = alignment2.getTypeCells().iterator().next();
    assertTrue(typeCell instanceof BaseAlignmentCell);
    assertEquals(usedPrefix + ":" + cell1.getId(), typeCell.getId());
    assertEquals(1, alignment2.getPropertyCells(typeCell).size());
    assertFalse(alignment2.getPropertyCells(typeCell).iterator().next() instanceof BaseAlignmentCell);
}
Also used : Schema(eu.esdihumboldt.hale.common.schema.model.Schema) DefaultSchema(eu.esdihumboldt.hale.common.schema.model.impl.DefaultSchema) ArrayList(java.util.ArrayList) DefaultTypeDefinition(eu.esdihumboldt.hale.common.schema.model.impl.DefaultTypeDefinition) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition) TypeEntityDefinition(eu.esdihumboldt.hale.common.align.model.impl.TypeEntityDefinition) ChildContext(eu.esdihumboldt.hale.common.align.model.ChildContext) DefaultAlignment(eu.esdihumboldt.hale.common.align.model.impl.DefaultAlignment) Property(eu.esdihumboldt.hale.common.align.model.Property) DefaultProperty(eu.esdihumboldt.hale.common.align.model.impl.DefaultProperty) BufferedOutputStream(java.io.BufferedOutputStream) DefaultCell(eu.esdihumboldt.hale.common.align.model.impl.DefaultCell) BaseAlignmentCell(eu.esdihumboldt.hale.common.align.model.BaseAlignmentCell) Cell(eu.esdihumboldt.hale.common.align.model.Cell) ModifiableCell(eu.esdihumboldt.hale.common.align.model.ModifiableCell) MutableCell(eu.esdihumboldt.hale.common.align.model.MutableCell) DefaultType(eu.esdihumboldt.hale.common.align.model.impl.DefaultType) QName(javax.xml.namespace.QName) MutableAlignment(eu.esdihumboldt.hale.common.align.model.MutableAlignment) PropertyDefinition(eu.esdihumboldt.hale.common.schema.model.PropertyDefinition) DefaultProperty(eu.esdihumboldt.hale.common.align.model.impl.DefaultProperty) FileInputStream(java.io.FileInputStream) Type(eu.esdihumboldt.hale.common.align.model.Type) DefaultType(eu.esdihumboldt.hale.common.align.model.impl.DefaultType) BaseAlignmentCell(eu.esdihumboldt.hale.common.align.model.BaseAlignmentCell) PropertyEntityDefinition(eu.esdihumboldt.hale.common.align.model.impl.PropertyEntityDefinition) DefaultCell(eu.esdihumboldt.hale.common.align.model.impl.DefaultCell) FileOutputStream(java.io.FileOutputStream) File(java.io.File) Test(org.junit.Test)

Aggregations

PropertyEntityDefinition (eu.esdihumboldt.hale.common.align.model.impl.PropertyEntityDefinition)55 ArrayList (java.util.ArrayList)26 ChildContext (eu.esdihumboldt.hale.common.align.model.ChildContext)19 EntityDefinition (eu.esdihumboldt.hale.common.align.model.EntityDefinition)19 TypeEntityDefinition (eu.esdihumboldt.hale.common.align.model.impl.TypeEntityDefinition)16 TypeDefinition (eu.esdihumboldt.hale.common.schema.model.TypeDefinition)16 List (java.util.List)15 Entity (eu.esdihumboldt.hale.common.align.model.Entity)12 DefaultProperty (eu.esdihumboldt.hale.common.align.model.impl.DefaultProperty)12 QName (javax.xml.namespace.QName)11 Cell (eu.esdihumboldt.hale.common.align.model.Cell)10 HashSet (java.util.HashSet)10 Property (eu.esdihumboldt.hale.common.align.model.Property)9 DefaultCell (eu.esdihumboldt.hale.common.align.model.impl.DefaultCell)7 MutableCell (eu.esdihumboldt.hale.common.align.model.MutableCell)6 ParameterValue (eu.esdihumboldt.hale.common.align.model.ParameterValue)6 PropertyDefinition (eu.esdihumboldt.hale.common.schema.model.PropertyDefinition)6 Collectors (java.util.stream.Collectors)6 Type (eu.esdihumboldt.hale.common.align.model.Type)5 JoinCondition (eu.esdihumboldt.hale.common.align.model.functions.join.JoinParameter.JoinCondition)5