Search in sources :

Example 1 with IndexedPropertyValue

use of eu.esdihumboldt.hale.common.instance.index.IndexedPropertyValue in project hale by halestudio.

the class IndexJoinIterator method join.

/**
 * Joins all direct children of the given type to currentInstances.
 */
@SuppressWarnings("javadoc")
private void join(FamilyInstance[] currentInstances, int currentType) {
    // Join all types that are direct children of the last type.
    for (int i = currentType + 1; i < joinDefinition.directParent.length; i++) {
        if (joinDefinition.directParent[i] == currentType) {
            // Get join condition for the direct child type.
            Multimap<Integer, JoinCondition> joinConditions = joinDefinition.joinTable.get(i);
            // Collect intersection of conditions. null marks beginning
            // in contrast to an empty set.
            Set<ResolvableInstanceReference> possibleInstances = null;
            // ParentType -> JoinConditions
            for (Map.Entry<Integer, JoinCondition> joinCondition : joinConditions.entries()) {
                PropertyEntityDefinition baseProp = joinCondition.getValue().baseProperty;
                QName baseTypeName = baseProp.getType().getName();
                List<QName> basePropertyPath = baseProp.getPropertyPath().stream().map(pp -> pp.getChild().getName()).collect(Collectors.toList());
                PropertyEntityDefinition joinProp = joinCondition.getValue().joinProperty;
                QName joinTypeName = joinProp.getType().getName();
                List<QName> joinPropertyPath = joinProp.getPropertyPath().stream().map(pp -> pp.getChild().getName()).collect(Collectors.toList());
                List<IndexedPropertyValue> currentValues = index.getInstancePropertyValues(baseTypeName, basePropertyPath, currentInstances[joinCondition.getKey()].getId());
                if (currentValues == null || currentValues.isEmpty()) {
                    possibleInstances = Collections.emptySet();
                    break;
                }
                HashSet<ResolvableInstanceReference> matches = new HashSet<ResolvableInstanceReference>();
                for (IndexedPropertyValue currentValue : currentValues) {
                    if (currentValue.getValues() == null || currentValue.getValues().isEmpty()) {
                        continue;
                    }
                    // Find instances that have the current property value
                    Collection<ResolvableInstanceReference> instancesWithValues = index.getInstancesByValue(joinTypeName, joinPropertyPath, currentValue.getValues());
                    matches.addAll(instancesWithValues);
                }
                if (possibleInstances == null) {
                    possibleInstances = matches;
                } else {
                    // Remove candidates that don't have the current
                    // property value
                    Iterator<ResolvableInstanceReference> it = possibleInstances.iterator();
                    while (it.hasNext()) {
                        ResolvableInstanceReference cand = it.next();
                        if (!matches.contains(cand)) {
                            it.remove();
                        }
                    }
                }
                if (possibleInstances.isEmpty()) {
                    break;
                }
            }
            if (possibleInstances != null && !possibleInstances.isEmpty()) {
                FamilyInstance parent = currentInstances[currentType];
                for (ResolvableInstanceReference ref : possibleInstances) {
                    FamilyInstance child;
                    child = new FamilyInstanceImpl(ref.resolve());
                    parent.addChild(child);
                    currentInstances[i] = child;
                    join(currentInstances, i);
                }
                currentInstances[i] = null;
            }
        }
    }
}
Also used : Iterator(java.util.Iterator) PropertyEntityDefinition(eu.esdihumboldt.hale.common.align.model.impl.PropertyEntityDefinition) ResolvableInstanceReference(eu.esdihumboldt.hale.common.instance.model.ResolvableInstanceReference) Collection(java.util.Collection) JoinDefinition(eu.esdihumboldt.cst.functions.core.join.JoinUtil.JoinDefinition) Set(java.util.Set) InstanceIndexService(eu.esdihumboldt.hale.common.instance.index.InstanceIndexService) Multimap(com.google.common.collect.Multimap) Collectors(java.util.stream.Collectors) FamilyInstance(eu.esdihumboldt.hale.common.instance.model.FamilyInstance) HashSet(java.util.HashSet) FamilyInstanceImpl(eu.esdihumboldt.hale.common.align.transformation.function.impl.FamilyInstanceImpl) GenericResourceIteratorAdapter(eu.esdihumboldt.hale.common.instance.model.impl.GenericResourceIteratorAdapter) List(java.util.List) JoinCondition(eu.esdihumboldt.hale.common.align.model.functions.join.JoinParameter.JoinCondition) Map(java.util.Map) QName(javax.xml.namespace.QName) IndexedPropertyValue(eu.esdihumboldt.hale.common.instance.index.IndexedPropertyValue) Collections(java.util.Collections) QName(javax.xml.namespace.QName) IndexedPropertyValue(eu.esdihumboldt.hale.common.instance.index.IndexedPropertyValue) JoinCondition(eu.esdihumboldt.hale.common.align.model.functions.join.JoinParameter.JoinCondition) FamilyInstanceImpl(eu.esdihumboldt.hale.common.align.transformation.function.impl.FamilyInstanceImpl) PropertyEntityDefinition(eu.esdihumboldt.hale.common.align.model.impl.PropertyEntityDefinition) FamilyInstance(eu.esdihumboldt.hale.common.instance.model.FamilyInstance) ResolvableInstanceReference(eu.esdihumboldt.hale.common.instance.model.ResolvableInstanceReference) Map(java.util.Map) HashSet(java.util.HashSet)

Aggregations

Multimap (com.google.common.collect.Multimap)1 JoinDefinition (eu.esdihumboldt.cst.functions.core.join.JoinUtil.JoinDefinition)1 JoinCondition (eu.esdihumboldt.hale.common.align.model.functions.join.JoinParameter.JoinCondition)1 PropertyEntityDefinition (eu.esdihumboldt.hale.common.align.model.impl.PropertyEntityDefinition)1 FamilyInstanceImpl (eu.esdihumboldt.hale.common.align.transformation.function.impl.FamilyInstanceImpl)1 IndexedPropertyValue (eu.esdihumboldt.hale.common.instance.index.IndexedPropertyValue)1 InstanceIndexService (eu.esdihumboldt.hale.common.instance.index.InstanceIndexService)1 FamilyInstance (eu.esdihumboldt.hale.common.instance.model.FamilyInstance)1 ResolvableInstanceReference (eu.esdihumboldt.hale.common.instance.model.ResolvableInstanceReference)1 GenericResourceIteratorAdapter (eu.esdihumboldt.hale.common.instance.model.impl.GenericResourceIteratorAdapter)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 HashSet (java.util.HashSet)1 Iterator (java.util.Iterator)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1 QName (javax.xml.namespace.QName)1