Search in sources :

Example 1 with FamilyInstance

use of eu.esdihumboldt.hale.common.instance.model.FamilyInstance in project hale by halestudio.

the class GroovyJoinPage method validate.

@Override
protected boolean validate(String document) {
    ParameterValue param = CellUtil.getFirstParameter(getWizard().getUnfinishedCell(), JoinFunction.PARAMETER_JOIN);
    JoinParameter joinParameter = param.as(JoinParameter.class);
    // check Join parameter
    if (joinParameter == null) {
        // setValidationError("Missing join configuration");
        return false;
    } else {
        String error = joinParameter.validate();
        if (!setValidationError(error)) {
            return false;
        }
    }
    // target type
    Type targetType = (Type) CellUtil.getFirstEntity(getWizard().getUnfinishedCell().getTarget());
    if (targetType == null) {
        // not yet selected (NewRelationWizard)
        return false;
    }
    /*
		 * FIXME use JoinParameter to fake joined instances!
		 * 
		 * XXX for now just base instance
		 */
    TypeEntityDefinition sourceType = joinParameter.getTypes().get(0);
    InstanceBuilder builder = new InstanceBuilder(false);
    Instance instance = getTestValues().get(sourceType);
    if (instance == null) {
        // use an empty instance as input for the script
        instance = new DefaultInstance(sourceType.getDefinition(), DataSet.SOURCE);
    }
    FamilyInstance source = new FamilyInstanceImpl(instance);
    // prepare binding
    Cell cell = getWizard().getUnfinishedCell();
    CellLog log = new CellLog(new DefaultTransformationReporter("dummy", false), cell);
    ExecutionContext context = new DummyExecutionContext(HaleUI.getServiceProvider());
    Binding binding = GroovyRetype.createBinding(source, cell, builder, log, context, targetType.getDefinition().getDefinition());
    GroovyService service = HaleUI.getServiceProvider().getService(GroovyService.class);
    Script script = null;
    try {
        script = service.parseScript(document, binding);
        GroovyUtil.evaluateAll(script, builder, targetType.getDefinition().getDefinition(), service, log);
    } catch (final Exception e) {
        return handleValidationResult(script, e);
    }
    return handleValidationResult(script, null);
}
Also used : Binding(groovy.lang.Binding) Script(groovy.lang.Script) ParameterValue(eu.esdihumboldt.hale.common.align.model.ParameterValue) FamilyInstance(eu.esdihumboldt.hale.common.instance.model.FamilyInstance) Instance(eu.esdihumboldt.hale.common.instance.model.Instance) DefaultInstance(eu.esdihumboldt.hale.common.instance.model.impl.DefaultInstance) DefaultTransformationReporter(eu.esdihumboldt.hale.common.align.transformation.report.impl.DefaultTransformationReporter) JoinParameter(eu.esdihumboldt.hale.common.align.model.functions.join.JoinParameter) GroovyService(eu.esdihumboldt.util.groovy.sandbox.GroovyService) FamilyInstanceImpl(eu.esdihumboldt.hale.common.align.transformation.function.impl.FamilyInstanceImpl) Type(eu.esdihumboldt.hale.common.align.model.Type) TypeEntityDefinition(eu.esdihumboldt.hale.common.align.model.impl.TypeEntityDefinition) ExecutionContext(eu.esdihumboldt.hale.common.align.transformation.function.ExecutionContext) DefaultInstance(eu.esdihumboldt.hale.common.instance.model.impl.DefaultInstance) FamilyInstance(eu.esdihumboldt.hale.common.instance.model.FamilyInstance) Cell(eu.esdihumboldt.hale.common.align.model.Cell) CellLog(eu.esdihumboldt.hale.common.align.transformation.report.impl.CellLog) InstanceBuilder(eu.esdihumboldt.hale.common.instance.groovy.InstanceBuilder)

Example 2 with FamilyInstance

use of eu.esdihumboldt.hale.common.instance.model.FamilyInstance 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)

Example 3 with FamilyInstance

use of eu.esdihumboldt.hale.common.instance.model.FamilyInstance in project hale by halestudio.

the class JoinIterator method convert.

/**
 * @see eu.esdihumboldt.hale.common.instance.model.impl.GenericResourceIteratorAdapter#convert(java.lang.Object)
 */
@Override
protected FamilyInstance convert(InstanceReference next) {
    FamilyInstance base = new FamilyInstanceImpl(instances.getInstance(next));
    FamilyInstance[] currentInstances = new FamilyInstance[parent.length];
    currentInstances[0] = base;
    join(currentInstances, 0);
    return base;
}
Also used : FamilyInstanceImpl(eu.esdihumboldt.hale.common.align.transformation.function.impl.FamilyInstanceImpl) FamilyInstance(eu.esdihumboldt.hale.common.instance.model.FamilyInstance)

Example 4 with FamilyInstance

use of eu.esdihumboldt.hale.common.instance.model.FamilyInstance in project hale by halestudio.

the class JoinIterator method join.

// Joins all direct children of the given type to currentInstances.
private void join(FamilyInstance[] currentInstances, int currentType) {
    // Join all types that are direct children of the last type.
    for (int i = currentType + 1; i < parent.length; i++) {
        if (parent[i] == currentType) {
            // Get join condition for the direct child type.
            Multimap<Integer, JoinCondition> joinConditions = joinTable.get(i);
            // Collect intersection of conditions. null marks beginning
            // in contrast to an empty set.
            Set<InstanceReference> possibleInstances = null;
            // ParentType -> JoinConditions
            for (Map.Entry<Integer, JoinCondition> joinCondition : joinConditions.entries()) {
                Collection<Object> currentValues = AlignmentUtil.getValues(currentInstances[joinCondition.getKey()], joinCondition.getValue().baseProperty, true);
                if (currentValues == null) {
                    possibleInstances = Collections.emptySet();
                    break;
                }
                // Allow targets with any of the property values.
                HashSet<InstanceReference> matches = new HashSet<InstanceReference>();
                for (Object currentValue : currentValues) {
                    Object keyValue = currentValue;
                    if (valueProcessor != null) {
                        keyValue = valueProcessor.processValue(currentValue, joinCondition.getValue().baseProperty);
                    }
                    matches.addAll(index.get(joinCondition.getValue().joinProperty).get(keyValue));
                }
                if (possibleInstances == null)
                    possibleInstances = matches;
                else {
                    // Intersect!
                    Iterator<InstanceReference> iter = possibleInstances.iterator();
                    while (iter.hasNext()) {
                        InstanceReference ref = iter.next();
                        if (!matches.contains(ref))
                            iter.remove();
                    }
                }
                // Break if set is empty.
                if (possibleInstances.isEmpty())
                    break;
            }
            if (possibleInstances != null && !possibleInstances.isEmpty()) {
                FamilyInstance parent = currentInstances[currentType];
                for (InstanceReference ref : possibleInstances) {
                    FamilyInstance child;
                    if (ref instanceof ResolvableInstanceReference) {
                        child = new FamilyInstanceImpl(((ResolvableInstanceReference) ref).resolve());
                    } else {
                        child = new FamilyInstanceImpl(instances.getInstance(ref));
                    }
                    parent.addChild(child);
                    currentInstances[i] = child;
                    join(currentInstances, i);
                }
                currentInstances[i] = null;
            }
        }
    }
}
Also used : JoinCondition(eu.esdihumboldt.hale.common.align.model.functions.join.JoinParameter.JoinCondition) FamilyInstanceImpl(eu.esdihumboldt.hale.common.align.transformation.function.impl.FamilyInstanceImpl) ResolvableInstanceReference(eu.esdihumboldt.hale.common.instance.model.ResolvableInstanceReference) InstanceReference(eu.esdihumboldt.hale.common.instance.model.InstanceReference) FamilyInstance(eu.esdihumboldt.hale.common.instance.model.FamilyInstance) Map(java.util.Map) ResolvableInstanceReference(eu.esdihumboldt.hale.common.instance.model.ResolvableInstanceReference) HashSet(java.util.HashSet)

Example 5 with FamilyInstance

use of eu.esdihumboldt.hale.common.instance.model.FamilyInstance in project hale by halestudio.

the class SpatialJoinHandler method partitionInstances.

/**
 * @see eu.esdihumboldt.hale.common.align.transformation.function.InstanceHandler#partitionInstances(eu.esdihumboldt.hale.common.instance.model.InstanceCollection,
 *      java.lang.String,
 *      eu.esdihumboldt.hale.common.align.transformation.engine.TransformationEngine,
 *      com.google.common.collect.ListMultimap, java.util.Map,
 *      eu.esdihumboldt.hale.common.align.transformation.report.TransformationLog)
 */
@Override
public ResourceIterator<FamilyInstance> partitionInstances(InstanceCollection instances, String transformationIdentifier, TransformationEngine engine, ListMultimap<String, ParameterValue> transformationParameters, Map<String, String> executionParameters, TransformationLog log) throws TransformationException {
    if (transformationParameters == null || !transformationParameters.containsKey(PARAMETER_SPATIAL_JOIN) || transformationParameters.get(PARAMETER_SPATIAL_JOIN).isEmpty()) {
        throw new TransformationException("No join parameter defined");
    }
    if (services == null) {
        throw new IllegalStateException("ServiceProvider must be set before calling partitionInstances");
    }
    SpatialJoinParameter joinParameter = transformationParameters.get(PARAMETER_SPATIAL_JOIN).get(0).as(SpatialJoinParameter.class);
    String validation = joinParameter.validate();
    if (validation != null) {
        throw new TransformationException("Spatial Join parameter invalid: " + validation);
    }
    List<TypeEntityDefinition> types = joinParameter.types;
    // ChildType -> DirectParentType
    int[] directParent = new int[joinParameter.types.size()];
    // ChildType -> (ParentType -> Collection<JoinCondition>)
    Map<Integer, Multimap<Integer, SpatialJoinCondition>> joinTable = new HashMap<>();
    for (SpatialJoinCondition condition : joinParameter.conditions) {
        int baseTypeIndex = types.indexOf(AlignmentUtil.getTypeEntity(condition.baseProperty));
        int joinTypeIndex = types.indexOf(AlignmentUtil.getTypeEntity(condition.joinProperty));
        Multimap<Integer, SpatialJoinCondition> typeTable = joinTable.get(joinTypeIndex);
        if (typeTable == null) {
            typeTable = ArrayListMultimap.create(2, 2);
            joinTable.put(joinTypeIndex, typeTable);
        }
        typeTable.put(baseTypeIndex, condition);
        // update highest type if necessary
        if (directParent[joinTypeIndex] < baseTypeIndex) {
            directParent[joinTypeIndex] = baseTypeIndex;
        }
    }
    // remember instances of first type to start join afterwards
    Collection<InstanceReference> startInstances = new LinkedList<InstanceReference>();
    // iterate once over all instances
    ResourceIterator<Instance> iterator = instances.iterator();
    try {
        while (iterator.hasNext()) {
            Instance next = iterator.next();
            // remember instances of first type
            if (next.getDefinition().equals(types.get(0).getDefinition())) {
                startInstances.add(instances.getReference(next));
            }
        }
    } finally {
        iterator.close();
    }
    return new SpatialJoinIterator(instances, startInstances, directParent, services, joinTable);
}
Also used : TransformationException(eu.esdihumboldt.hale.common.align.transformation.function.TransformationException) HashMap(java.util.HashMap) SpatialJoinCondition(eu.esdihumboldt.cst.functions.geometric.join.SpatialJoinParameter.SpatialJoinCondition) FamilyInstance(eu.esdihumboldt.hale.common.instance.model.FamilyInstance) Instance(eu.esdihumboldt.hale.common.instance.model.Instance) LinkedList(java.util.LinkedList) ArrayListMultimap(com.google.common.collect.ArrayListMultimap) ListMultimap(com.google.common.collect.ListMultimap) Multimap(com.google.common.collect.Multimap) TypeEntityDefinition(eu.esdihumboldt.hale.common.align.model.impl.TypeEntityDefinition) ResolvableInstanceReference(eu.esdihumboldt.hale.common.instance.model.ResolvableInstanceReference) InstanceReference(eu.esdihumboldt.hale.common.instance.model.InstanceReference)

Aggregations

FamilyInstance (eu.esdihumboldt.hale.common.instance.model.FamilyInstance)14 Instance (eu.esdihumboldt.hale.common.instance.model.Instance)9 FamilyInstanceImpl (eu.esdihumboldt.hale.common.align.transformation.function.impl.FamilyInstanceImpl)8 TypeEntityDefinition (eu.esdihumboldt.hale.common.align.model.impl.TypeEntityDefinition)5 TransformationException (eu.esdihumboldt.hale.common.align.transformation.function.TransformationException)5 ResolvableInstanceReference (eu.esdihumboldt.hale.common.instance.model.ResolvableInstanceReference)5 InstanceReference (eu.esdihumboldt.hale.common.instance.model.InstanceReference)4 ListMultimap (com.google.common.collect.ListMultimap)3 Multimap (com.google.common.collect.Multimap)3 JoinDefinition (eu.esdihumboldt.cst.functions.core.join.JoinUtil.JoinDefinition)3 ParameterValue (eu.esdihumboldt.hale.common.align.model.ParameterValue)3 Type (eu.esdihumboldt.hale.common.align.model.Type)3 JoinParameter (eu.esdihumboldt.hale.common.align.model.functions.join.JoinParameter)3 CellLog (eu.esdihumboldt.hale.common.align.transformation.report.impl.CellLog)3 InstanceIndexService (eu.esdihumboldt.hale.common.instance.index.InstanceIndexService)3 HashSet (java.util.HashSet)3 ArrayListMultimap (com.google.common.collect.ArrayListMultimap)2 Cell (eu.esdihumboldt.hale.common.align.model.Cell)2 JoinCondition (eu.esdihumboldt.hale.common.align.model.functions.join.JoinParameter.JoinCondition)2 PropertyEntityDefinition (eu.esdihumboldt.hale.common.align.model.impl.PropertyEntityDefinition)2