Search in sources :

Example 56 with Instance

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

the class InstanceValueLabelProvider method update.

/**
 * @see CellLabelProvider#update(ViewerCell)
 */
@Override
public void update(ViewerCell cell) {
    TreePath treePath = cell.getViewerRow().getTreePath();
    Object element = treePath.getLastSegment();
    Definition<?> definition = null;
    Object value = ((Pair<?, ?>) element).getSecond();
    if (((Pair<?, ?>) element).getFirst() instanceof Definition)
        definition = (Definition<?>) ((Pair<?, ?>) element).getFirst();
    InstanceValidationReport report = null;
    if (definition instanceof ChildDefinition<?>) {
        report = validator.validate(value, (ChildDefinition<?>) ((Pair<?, ?>) element).getFirst());
    } else if (definition instanceof TypeDefinition) {
        report = validator.validate((Instance) value);
    }
    boolean hasValue = false;
    if (value instanceof Instance) {
        hasValue = ((Instance) value).getValue() != null;
    }
    StyledString styledString;
    if (value == null) {
        styledString = new StyledString("no value", StyledString.DECORATIONS_STYLER);
    } else if (value instanceof Group && !hasValue) {
        styledString = new StyledString("+", StyledString.QUALIFIER_STYLER);
    } else {
        if (value instanceof Instance) {
            value = ((Instance) value).getValue();
        }
        // TODO some kind of conversion?
        String stringValue = value.toString();
        /*
			 * Values that are very large, e.g. string representations of very
			 * complex geometries lead to
			 * StyledCellLabelProvider.updateTextLayout taking a very long time,
			 * rendering the application unresponsive when the data views are
			 * displayed. As such, we reduce the string to a maximum size.
			 */
        if (stringValue.length() > MAX_STRING_LENGTH) {
            stringValue = stringValue.substring(0, MAX_STRING_LENGTH) + "...";
        }
        styledString = new StyledString(stringValue, null);
    }
    cell.setText(styledString.toString());
    cell.setStyleRanges(styledString.getStyleRanges());
    if (report != null && !report.getWarnings().isEmpty()) {
        cell.setImage(PlatformUI.getWorkbench().getSharedImages().getImage(ISharedImages.IMG_OBJS_WARN_TSK));
    }
    super.update(cell);
}
Also used : Group(eu.esdihumboldt.hale.common.instance.model.Group) Instance(eu.esdihumboldt.hale.common.instance.model.Instance) Definition(eu.esdihumboldt.hale.common.schema.model.Definition) ChildDefinition(eu.esdihumboldt.hale.common.schema.model.ChildDefinition) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition) ChildDefinition(eu.esdihumboldt.hale.common.schema.model.ChildDefinition) StyledString(org.eclipse.jface.viewers.StyledString) StyledString(org.eclipse.jface.viewers.StyledString) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition) InstanceValidationReport(eu.esdihumboldt.hale.common.instance.extension.validation.report.InstanceValidationReport) TreePath(org.eclipse.jface.viewers.TreePath) Pair(eu.esdihumboldt.util.Pair)

Example 57 with Instance

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

the class InstanceImportAdvisor method handleResults.

/**
 * @see IOAdvisor#handleResults(IOProvider)
 */
@Override
public void handleResults(InstanceReader provider) {
    // add instances to instance service
    InstanceService is = getService(InstanceService.class);
    InstanceCollection instances = provider.getInstances();
    ResourceIterator<Instance> it = instances.iterator();
    try {
        if (!it.hasNext()) {
            URI loc = provider.getSource().getLocation();
            if (loc != null) {
                log.warn(MessageFormat.format("No instances could be imported with the given configuration from {0}", loc.toString()));
            } else {
                log.warn("No instances could be imported with the given configuration.");
            }
        }
    } finally {
        it.close();
    }
    // apply sampling before adding to the instance service
    InstanceViewService ivs = PlatformUI.getWorkbench().getService(InstanceViewService.class);
    if (ivs != null) {
        instances = ivs.sample(instances);
    }
    is.addSourceInstances(instances);
    super.handleResults(provider);
}
Also used : InstanceViewService(eu.esdihumboldt.hale.ui.service.instance.sample.InstanceViewService) Instance(eu.esdihumboldt.hale.common.instance.model.Instance) InstanceCollection(eu.esdihumboldt.hale.common.instance.model.InstanceCollection) InstanceService(eu.esdihumboldt.hale.ui.service.instance.InstanceService) URI(java.net.URI)

Example 58 with Instance

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

the class TypeStyleHandler method collectTypesFromSelection.

/**
 * Collect all type definitions and data sets from the current selection of
 * {@link TypeDefinition}s, {@link EntityDefinition}s, {@link Instance}s and
 * {@link InstanceReference}s.
 *
 * @param event the handler execution event
 * @return the collected type definitions
 */
public static SetMultimap<DataSet, TypeDefinition> collectTypesFromSelection(ExecutionEvent event) {
    SetMultimap<DataSet, TypeDefinition> types = HashMultimap.create();
    ISelection selection = HandlerUtil.getCurrentSelection(event);
    if (selection instanceof IStructuredSelection && !selection.isEmpty()) {
        for (Object object : ((IStructuredSelection) selection).toArray()) {
            if (object instanceof TypeDefinition) {
                TypeDefinition type = (TypeDefinition) object;
                if (!types.containsValue(type)) {
                    DataSet dataSet = findDataSet(type);
                    types.put(dataSet, type);
                }
            }
            if (object instanceof EntityDefinition) {
                EntityDefinition entityDef = (EntityDefinition) object;
                if (entityDef.getPropertyPath().isEmpty()) {
                    DataSet dataSet = (entityDef.getSchemaSpace() == SchemaSpaceID.SOURCE) ? (DataSet.SOURCE) : (DataSet.TRANSFORMED);
                    types.put(dataSet, entityDef.getType());
                }
            }
            if (object instanceof InstanceReference) {
                InstanceService is = HandlerUtil.getActiveWorkbenchWindow(event).getWorkbench().getService(InstanceService.class);
                object = is.getInstance((InstanceReference) object);
            }
            if (object instanceof Instance) {
                Instance instance = (Instance) object;
                types.put(instance.getDataSet(), instance.getDefinition());
            }
        }
    }
    return types;
}
Also used : EntityDefinition(eu.esdihumboldt.hale.common.align.model.EntityDefinition) DataSet(eu.esdihumboldt.hale.common.instance.model.DataSet) Instance(eu.esdihumboldt.hale.common.instance.model.Instance) InstanceReference(eu.esdihumboldt.hale.common.instance.model.InstanceReference) ISelection(org.eclipse.jface.viewers.ISelection) InstanceService(eu.esdihumboldt.hale.ui.service.instance.InstanceService) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition)

Example 59 with Instance

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

the class IndexMergeHandler method merge.

private Instance merge(InstanceCollection instances, IndexMergeConfig mergeConfig) {
    TypeDefinition type;
    try (ResourceIterator<Instance> it = instances.iterator()) {
        if (instances.hasSize() && instances.size() == 1) {
            // early exit if only one instance to merge
            return it.next();
        } else {
            type = it.next().getDefinition();
        }
    }
    MutableInstance result = getInstanceFactory().createInstance(type);
    /*
		 * FIXME This a first VERY basic implementation, where only the first
		 * item in each property path is regarded, and that whole tree is added
		 * only once (from the first instance). XXX This especially will be a
		 * problem, if a path contains a choice. XXX For more advanced stuff we
		 * need more advanced test cases.
		 */
    Set<QName> rootNames = new HashSet<QName>();
    Set<QName> nonKeyRootNames = new HashSet<QName>();
    // collect path roots
    for (List<QName> path : mergeConfig.keyProperties) {
        rootNames.add(path.get(0));
    }
    for (List<QName> path : mergeConfig.additionalProperties) {
        nonKeyRootNames.add(path.get(0));
    }
    // XXX what about metadata?!
    // XXX for now only retain IDs
    Set<Object> ids = new HashSet<Object>();
    try (ResourceIterator<Instance> it = instances.iterator()) {
        while (it.hasNext()) {
            Instance instance = it.next();
            for (QName name : instance.getPropertyNames()) {
                if (rootNames.contains(name)) {
                    /*
						 * Property is merge key -> only use first occurrence
						 * (as all entries need to be the same)
						 * 
						 * TODO adapt if multiple keys are possible per instance
						 */
                    addFirstOccurrence(result, instance, name);
                } else if (nonKeyRootNames.contains(name)) {
                    /*
						 * Property is additional merge property.
						 * 
						 * Traditional behavior: Only keep unique values.
						 * 
						 * XXX should this be configurable?
						 */
                    addUnique(result, instance, name);
                } else if (mergeConfig.autoDetect) {
                    /*
						 * Auto-detection is enabled.
						 * 
						 * Only keep unique values.
						 * 
						 * XXX This differs from the traditional behavior in
						 * that there only the first value would be used, but
						 * only if all values were equal. That cannot be easily
						 * checked in an iterative approach.
						 */
                    addUnique(result, instance, name);
                } else {
                    /*
						 * Property is not to be merged.
						 * 
						 * XXX but we could do some kind of aggregation
						 * 
						 * XXX for now just add all values
						 */
                    addValues(result, instance, name);
                }
            }
            List<Object> instanceIDs = instance.getMetaData(InstanceMetadata.METADATA_ID);
            for (Object id : instanceIDs) {
                ids.add(id);
            }
        }
    }
    // store metadata IDs
    result.setMetaData(InstanceMetadata.METADATA_ID, ids.toArray());
    return result;
}
Also used : FamilyInstance(eu.esdihumboldt.hale.common.instance.model.FamilyInstance) MutableInstance(eu.esdihumboldt.hale.common.instance.model.MutableInstance) Instance(eu.esdihumboldt.hale.common.instance.model.Instance) QName(javax.xml.namespace.QName) MutableInstance(eu.esdihumboldt.hale.common.instance.model.MutableInstance) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition) HashSet(java.util.HashSet)

Example 60 with Instance

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

the class NetworkExpansion method evaluate.

/**
 * @see AbstractSingleTargetScriptedPropertyTransformation#evaluate(String,
 *      TransformationEngine, ListMultimap, String,
 *      PropertyEntityDefinition, Map, TransformationLog)
 */
@Override
protected Object evaluate(String transformationIdentifier, TransformationEngine engine, ListMultimap<String, PropertyValue> variables, String resultName, PropertyEntityDefinition resultProperty, Map<String, String> executionParameters, TransformationLog log) throws TransformationException, NoResultException {
    // get the buffer width parameter
    String bufferWidthString = getTransformedParameterChecked(PARAMETER_BUFFER_WIDTH).as(String.class);
    double bufferWidth;
    try {
        bufferWidth = Double.parseDouble(bufferWidthString);
    } catch (NumberFormatException e) {
        // For backwards compatibility try to run the string as script.
        MathScript mathScript = new MathScript();
        try {
            Object result = mathScript.evaluate(bufferWidthString, variables.get(ENTITY_VARIABLE), getExecutionContext());
            bufferWidth = ConversionUtil.getAs(result, Double.class);
        } catch (ScriptException e1) {
            throw new TransformationException("Failed to evaluate buffer width expression.", e1);
        } catch (ConversionException e2) {
            throw new TransformationException("Failed to convert buffer width expression result to double.", e2);
        }
    }
    // get input geometry
    PropertyValue input = variables.get(null).get(0);
    Object inputValue = input.getValue();
    if (inputValue instanceof Instance) {
        inputValue = ((Instance) inputValue).getValue();
    }
    GeometryProperty<Geometry> result = calculateBuffer(inputValue, bufferWidth, log);
    // try to yield a result compatible to the target
    if (result != null) {
        TypeDefinition targetType = resultProperty.getDefinition().getPropertyType();
        // TODO check element type?
        Class<?> binding = targetType.getConstraint(Binding.class).getBinding();
        if (Geometry.class.isAssignableFrom(binding) && binding.isAssignableFrom(result.getGeometry().getClass())) {
            return result.getGeometry();
        } else {
            return result;
        }
    }
    throw new TransformationException("Geometry for network expansion could not be retrieved.");
}
Also used : ConversionException(org.springframework.core.convert.ConversionException) Binding(eu.esdihumboldt.hale.common.schema.model.constraint.type.Binding) TransformationException(eu.esdihumboldt.hale.common.align.transformation.function.TransformationException) Instance(eu.esdihumboldt.hale.common.instance.model.Instance) PropertyValue(eu.esdihumboldt.hale.common.align.transformation.function.PropertyValue) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition) Geometry(com.vividsolutions.jts.geom.Geometry) ScriptException(javax.script.ScriptException) MathScript(eu.esdihumboldt.hale.common.scripting.scripts.mathematical.MathScript)

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