Search in sources :

Example 61 with Instance

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

Example 62 with Instance

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

the class DefaultTransformationTest method testGenerateUID.

/**
 * Test for the generateduid. Since the uid is always different, just test
 * for them being unique.
 *
 * @throws Exception if an error occurs executing the test
 */
@Test
public void testGenerateUID() throws Exception {
    TransformationExample example = TransformationExamples.getExample(TransformationExamples.GENERATEUID);
    List<Instance> transformedData = transformData(example);
    TreeSet<String> uniqueIdSet = new TreeSet<String>();
    for (Instance instance : transformedData) {
        Iterable<QName> propertyNames = instance.getPropertyNames();
        for (QName propertyName : propertyNames) {
            Object[] property = instance.getProperty(propertyName);
            for (Object object : property) {
                boolean added = uniqueIdSet.add(object.toString());
                if (!added) {
                    assertTrue("Found duplicated id when should be unique: " + object.toString(), added);
                }
            }
        }
    }
}
Also used : Instance(eu.esdihumboldt.hale.common.instance.model.Instance) TreeSet(java.util.TreeSet) QName(javax.xml.namespace.QName) Test(org.junit.Test)

Example 63 with Instance

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

the class AbstractInstancePainter method update.

/**
 * Do a complete update of the way-points. Existing way-points are
 * discarded.
 *
 * @param selection the current selection
 */
public void update(ISelection selection) {
    clearWaypoints();
    // XXX only mappable type instances for source?!
    InstanceCollection instances = instanceService.getInstances(dataSet);
    if (selection != null) {
        lastSelected = collectReferences(selection);
    }
    if (instances.isEmpty()) {
        return;
    }
    final AtomicBoolean updateFinished = new AtomicBoolean(false);
    IRunnableWithProgress op = new IRunnableWithProgress() {

        @Override
        public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
            String taskName;
            switch(getDataSet()) {
                case SOURCE:
                    taskName = "Update source data in map";
                    break;
                case TRANSFORMED:
                default:
                    taskName = "Update transformed data in map";
                    break;
            }
            monitor.beginTask(taskName, IProgressMonitor.UNKNOWN);
            // add way-points for instances
            InstanceCollection instances = instanceService.getInstances(dataSet);
            ResourceIterator<Instance> it = instances.iterator();
            try {
                while (it.hasNext()) {
                    Instance instance = it.next();
                    InstanceWaypoint wp = createWaypoint(instance, instanceService);
                    if (wp != null) {
                        if (lastSelected.contains(wp.getValue())) {
                            // refresh can be
                            wp.setSelected(true, null);
                        // ignored because
                        // it's done for
                        // addWaypoint
                        }
                        // no refresher, as
                        addWaypoint(wp, null);
                    // refreshAll is executed
                    }
                }
            } finally {
                it.close();
                monitor.done();
                updateFinished.set(true);
            }
        }
    };
    try {
        ThreadProgressMonitor.runWithProgressDialog(op, false);
    } catch (Throwable e) {
        log.error("Error running painter update", e);
    }
    HaleUI.waitFor(updateFinished);
    refreshAll();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) Instance(eu.esdihumboldt.hale.common.instance.model.Instance) InstanceCollection(eu.esdihumboldt.hale.common.instance.model.InstanceCollection) IRunnableWithProgress(org.eclipse.jface.operation.IRunnableWithProgress)

Example 64 with Instance

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

the class XsltTransformationTest method transformData.

@Override
protected List<Instance> transformData(TransformationExample example) throws Exception {
    // export alignment to XSLT
    XsltExport export = new XsltExport();
    export.setAlignment(example.getAlignment());
    export.setSourceSchema(new DefaultSchemaSpace().addSchema(example.getSourceSchema()));
    export.setTargetSchema(new DefaultSchemaSpace().addSchema(example.getTargetSchema()));
    export.setParameter(XsltExport.PARAM_ROOT_ELEMENT_NAMESPACE, Value.of(example.getTargetContainerNamespace()));
    export.setParameter(XsltExport.PARAM_ROOT_ELEMENT_NAME, Value.of(example.getTargetContainerName()));
    File tempXsltFile = File.createTempFile("xsltest", ".xsl");
    export.setTarget(new FileIOSupplier(tempXsltFile));
    IOReport res = export.execute(new LogProgressIndicator());
    assertTrue("XSLT export not successful", res.isSuccess());
    assertTrue("Errors during XSLT export", res.getErrors().isEmpty());
    // invoke XSLT on source file to produce target
    File target = File.createTempFile("xsltest", ".xml");
    executeXslt(example.getSourceDataInput(), tempXsltFile, target);
    // load target and return instances
    InstanceCollection instances = TestUtil.loadInstances(target.toURI(), example.getTargetSchema());
    List<Instance> list = new ArrayList<Instance>();
    ResourceIterator<Instance> it = instances.iterator();
    try {
        while (it.hasNext()) {
            list.add(it.next());
        }
    } finally {
        it.close();
    }
    // clean up
    tempXsltFile.delete();
    target.delete();
    return list;
}
Also used : Instance(eu.esdihumboldt.hale.common.instance.model.Instance) InstanceCollection(eu.esdihumboldt.hale.common.instance.model.InstanceCollection) DefaultSchemaSpace(eu.esdihumboldt.hale.common.schema.model.impl.DefaultSchemaSpace) ArrayList(java.util.ArrayList) IOReport(eu.esdihumboldt.hale.common.core.io.report.IOReport) FileIOSupplier(eu.esdihumboldt.hale.common.core.io.supplier.FileIOSupplier) LogProgressIndicator(eu.esdihumboldt.hale.common.core.io.impl.LogProgressIndicator) File(java.io.File)

Example 65 with Instance

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

the class InstanceBuilder method getValue.

/**
 * Get the value for a target node.
 *
 * @param node the target node
 * @param typeLog the type transformation log
 * @return the value or {@link NoObject#NONE} representing no value
 */
private Object getValue(TargetNode node, TransformationLog typeLog) {
    if (node.getChildren(true).isEmpty()) {
        // simple leaf
        if (node.isDefined()) {
            // XXX this is done in FunctionExecutor
            return node.getResult();
        } else {
            return NoObject.NONE;
        }
    }
    boolean isProperty = node.getDefinition().asProperty() != null;
    boolean isGroup = node.getDefinition().asGroup() != null;
    if (isProperty && node.isDefined()) {
        // it's a property and we have a value/values
        Object nodeValue = node.getResult();
        if (!(nodeValue instanceof MultiValue)) {
            // pack single value into multivalue
            MultiValue nodeMultiValue = new MultiValue();
            nodeMultiValue.add(nodeValue);
            nodeValue = nodeMultiValue;
        }
        MultiValue nodeMultiValue = (MultiValue) nodeValue;
        if (!nodeMultiValue.isEmpty()) {
            // Create n instances
            MultiValue resultMultiValue = new MultiValue(nodeMultiValue.size());
            for (Object value : nodeMultiValue) {
                // the value may have been wrapped in an Instance
                if (value instanceof Instance) {
                    value = ((Instance) value).getValue();
                }
                MutableInstance instance = new DefaultInstance(node.getDefinition().asProperty().getPropertyType(), null);
                instance.setValue(value);
                // XXX since this is the same for all instances maybe do
                // this on a dummy and only copy properties for each?
                // XXX MultiValue w/ target node children => strange results
                populateGroup(instance, node, typeLog);
                resultMultiValue.add(instance);
            }
            return resultMultiValue;
        }
    // if nodeMultiValue is empty fall through to below
    // it the instance could still have children even without a value
    }
    // it's a property or group with no value
    MutableGroup group;
    if (isGroup) {
        group = new DefaultGroup(node.getDefinition().asGroup());
    } else if (isProperty) {
        group = new DefaultInstance(node.getDefinition().asProperty().getPropertyType(), null);
    } else {
        throw new IllegalStateException("Illegal child definition");
    }
    // populate with children
    if (populateGroup(group, node, typeLog)) {
        return group;
    } else {
        return NoObject.NONE;
    }
}
Also used : MutableInstance(eu.esdihumboldt.hale.common.instance.model.MutableInstance) Instance(eu.esdihumboldt.hale.common.instance.model.Instance) DefaultInstance(eu.esdihumboldt.hale.common.instance.model.impl.DefaultInstance) DefaultInstance(eu.esdihumboldt.hale.common.instance.model.impl.DefaultInstance) MutableInstance(eu.esdihumboldt.hale.common.instance.model.MutableInstance) DefaultGroup(eu.esdihumboldt.hale.common.instance.model.impl.DefaultGroup) MutableGroup(eu.esdihumboldt.hale.common.instance.model.MutableGroup) MultiValue(eu.esdihumboldt.cst.MultiValue)

Aggregations

Instance (eu.esdihumboldt.hale.common.instance.model.Instance)203 InstanceCollection (eu.esdihumboldt.hale.common.instance.model.InstanceCollection)131 Test (org.junit.Test)122 AbstractHandlerTest (eu.esdihumboldt.hale.io.gml.geometry.handler.internal.AbstractHandlerTest)97 QName (javax.xml.namespace.QName)29 ArrayList (java.util.ArrayList)26 MutableInstance (eu.esdihumboldt.hale.common.instance.model.MutableInstance)25 DefaultInstance (eu.esdihumboldt.hale.common.instance.model.impl.DefaultInstance)23 TypeDefinition (eu.esdihumboldt.hale.common.schema.model.TypeDefinition)22 Group (eu.esdihumboldt.hale.common.instance.model.Group)15 Schema (eu.esdihumboldt.hale.common.schema.model.Schema)13 Coordinate (com.vividsolutions.jts.geom.Coordinate)12 Geometry (com.vividsolutions.jts.geom.Geometry)12 FamilyInstance (eu.esdihumboldt.hale.common.instance.model.FamilyInstance)10 Polygon (com.vividsolutions.jts.geom.Polygon)9 MultiPolygon (com.vividsolutions.jts.geom.MultiPolygon)8 TransformationException (eu.esdihumboldt.hale.common.align.transformation.function.TransformationException)8 GeometryProperty (eu.esdihumboldt.hale.common.schema.geometry.GeometryProperty)8 HashSet (java.util.HashSet)8 Point (com.vividsolutions.jts.geom.Point)7