Search in sources :

Example 61 with TypeDefinition

use of eu.esdihumboldt.hale.common.schema.model.TypeDefinition in project hale by halestudio.

the class EditMappableTypesPage method createControl.

/**
 * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
 */
@Override
public void createControl(Composite parent) {
    parent.setLayout(new GridLayout());
    // create filtered tree
    PatternFilter patternFilter = new PatternFilter();
    patternFilter.setIncludeLeadingWildcard(true);
    FilteredTree tree = new FilteredTree(parent, SWT.BORDER | SWT.CHECK | SWT.H_SCROLL | SWT.V_SCROLL, patternFilter, true) {

        @Override
        protected TreeViewer doCreateTreeViewer(Composite parent, int style) {
            return new CheckboxTreeViewer(parent, style);
        }
    };
    // configure viewer
    viewer = (CheckboxTreeViewer) tree.getViewer();
    contentProvider = new NSTypeTreeContentProvider();
    viewer.setContentProvider(contentProvider);
    viewer.setComparator(new DefinitionComparator());
    viewer.addDoubleClickListener(new IDoubleClickListener() {

        @Override
        public void doubleClick(DoubleClickEvent event) {
            IStructuredSelection selection = (IStructuredSelection) event.getSelection();
            Object doubleClicked = selection.getFirstElement();
            if (doubleClicked instanceof String)
                viewer.setExpandedState(doubleClicked, !viewer.getExpandedState(doubleClicked));
            else {
                boolean newState = !checkStateProvider.isChecked(doubleClicked);
                viewer.setChecked(doubleClicked, newState);
                checkStateOfTypeChanged((TypeDefinition) doubleClicked, newState);
            }
        }
    });
    viewer.setLabelProvider(definitionLabels);
    // because elements filtered by FilteredTree lose their checked state:
    checkStateProvider = new ICheckStateProvider() {

        @Override
        public boolean isGrayed(Object element) {
            if (element instanceof String) {
                Object[] children = contentProvider.getChildren(element);
                boolean containsChecked = false;
                boolean containsUnchecked = false;
                for (Object child : children) {
                    if (isChecked(child))
                        containsChecked = true;
                    else
                        containsUnchecked = true;
                    if (containsChecked && containsUnchecked)
                        return true;
                }
            }
            return false;
        }

        @Override
        public boolean isChecked(Object element) {
            if (element instanceof String) {
                for (Object child : contentProvider.getChildren(element)) if (isChecked(child))
                    return true;
                return false;
            }
            return ((TypeDefinition) element).getConstraint(MappingRelevantFlag.class).isEnabled() != changedTypes.contains(element);
        }
    };
    viewer.setCheckStateProvider(checkStateProvider);
    viewer.addCheckStateListener(new ICheckStateListener() {

        @Override
        public void checkStateChanged(CheckStateChangedEvent event) {
            if (event.getElement() instanceof String) {
                // update children
                viewer.setGrayed(event.getElement(), false);
                for (Object child : contentProvider.getChildren(event.getElement())) {
                    // if (mappedTypeFilter.select(viewer, event.getElement(), child)) {
                    if (checkStateProvider.isChecked(child) != event.getChecked()) {
                        viewer.setChecked(child, event.getChecked());
                        checkStateOfTypeChanged((TypeDefinition) child, event.getChecked());
                    }
                // }
                }
                viewer.setGrayed(event.getElement(), checkStateProvider.isGrayed(event.getElement()));
            // only two levels, no need to update any parents or
            // children's children
            } else {
                // if (mappedTypeFilter.select(viewer, null, event.getElement()))
                checkStateOfTypeChanged((TypeDefinition) event.getElement(), event.getChecked());
            // else if (!event.getChecked())
            // viewer.setChecked(event.getElement(), true);
            }
        }
    });
    // // filter types which are used in the current alignment
    // viewer.addFilter(mappedTypeFilter);
    // set input to all types in the given index
    viewer.setInput(typeIndex.getTypes());
    // expand all except XMLSchema
    viewer.expandAll();
    viewer.setExpandedState("http://www.w3.org/2001/XMLSchema", false);
    // set control
    setControl(viewer.getControl());
}
Also used : PatternFilter(org.eclipse.ui.dialogs.PatternFilter) Composite(org.eclipse.swt.widgets.Composite) ICheckStateProvider(org.eclipse.jface.viewers.ICheckStateProvider) DefinitionComparator(eu.esdihumboldt.hale.ui.common.definition.viewer.DefinitionComparator) ICheckStateListener(org.eclipse.jface.viewers.ICheckStateListener) DoubleClickEvent(org.eclipse.jface.viewers.DoubleClickEvent) FilteredTree(org.eclipse.ui.dialogs.FilteredTree) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition) CheckboxTreeViewer(org.eclipse.jface.viewers.CheckboxTreeViewer) GridLayout(org.eclipse.swt.layout.GridLayout) IDoubleClickListener(org.eclipse.jface.viewers.IDoubleClickListener) NSTypeTreeContentProvider(eu.esdihumboldt.hale.ui.service.schema.util.NSTypeTreeContentProvider) CheckStateChangedEvent(org.eclipse.jface.viewers.CheckStateChangedEvent) MappingRelevantFlag(eu.esdihumboldt.hale.common.schema.model.constraint.type.MappingRelevantFlag)

Example 62 with TypeDefinition

use of eu.esdihumboldt.hale.common.schema.model.TypeDefinition in project hale by halestudio.

the class NSTypeTreeContentProvider method getElements.

/**
 * @see org.eclipse.jface.viewers.ITreeContentProvider#getElements(java.lang.Object)
 */
@Override
public Object[] getElements(Object inputElement) {
    if (inputElement == null) {
        nameSpaces = null;
        return new Object[0];
    }
    @SuppressWarnings("unchecked") Collection<? extends TypeDefinition> types = (Collection<? extends TypeDefinition>) inputElement;
    nameSpaces = ArrayListMultimap.create();
    for (TypeDefinition type : types) {
        // only show types with mappable flag
        if (!type.getConstraint(MappableFlag.class).isEnabled())
            continue;
        String ns = type.getName().getNamespaceURI();
        if (XMLConstants.NULL_NS_URI.equals(ns))
            ns = "(no namespace)";
        nameSpaces.put(ns, type);
    }
    return nameSpaces.keySet().toArray();
}
Also used : Collection(java.util.Collection) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition)

Example 63 with TypeDefinition

use of eu.esdihumboldt.hale.common.schema.model.TypeDefinition in project hale by halestudio.

the class Retype method execute.

@Override
public void execute(String transformationIdentifier, TransformationEngine engine, Map<String, String> executionParameters, TransformationLog log, Cell cell) {
    // for each source instance create a target instance
    TypeDefinition targetType = getTarget().values().iterator().next().getDefinition().getDefinition();
    MutableInstance target = null;
    // structural rename
    boolean structuralRename = getOptionalParameter(RenameFunction.PARAMETER_STRUCTURAL_RENAME, Value.of(false)).as(Boolean.class, false);
    if (structuralRename) {
        boolean ignoreNamespaces = getOptionalParameter(RenameFunction.PARAMETER_IGNORE_NAMESPACES, Value.of(false)).as(Boolean.class, false);
        boolean copyGeometries = getOptionalParameter(RenameFunction.PARAMETER_COPY_GEOMETRIES, Value.of(true)).as(Boolean.class);
        target = doStructuralRename(getSource(), targetType, ignoreNamespaces, copyGeometries, log);
    }
    if (target == null) {
        target = getInstanceFactory().createInstance(targetType);
    }
    getPropertyTransformer().publish(getSource(), target, log, cell);
}
Also used : MutableInstance(eu.esdihumboldt.hale.common.instance.model.MutableInstance) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition) DefaultTypeDefinition(eu.esdihumboldt.hale.common.schema.model.impl.DefaultTypeDefinition)

Example 64 with TypeDefinition

use of eu.esdihumboldt.hale.common.schema.model.TypeDefinition 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 65 with TypeDefinition

use of eu.esdihumboldt.hale.common.schema.model.TypeDefinition 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

TypeDefinition (eu.esdihumboldt.hale.common.schema.model.TypeDefinition)192 QName (javax.xml.namespace.QName)58 PropertyDefinition (eu.esdihumboldt.hale.common.schema.model.PropertyDefinition)38 ArrayList (java.util.ArrayList)32 TypeEntityDefinition (eu.esdihumboldt.hale.common.align.model.impl.TypeEntityDefinition)26 DefaultTypeDefinition (eu.esdihumboldt.hale.common.schema.model.impl.DefaultTypeDefinition)26 Test (org.junit.Test)24 Instance (eu.esdihumboldt.hale.common.instance.model.Instance)22 PropertyEntityDefinition (eu.esdihumboldt.hale.common.align.model.impl.PropertyEntityDefinition)21 HashSet (java.util.HashSet)21 EntityDefinition (eu.esdihumboldt.hale.common.align.model.EntityDefinition)20 Schema (eu.esdihumboldt.hale.common.schema.model.Schema)20 InstanceCollection (eu.esdihumboldt.hale.common.instance.model.InstanceCollection)16 ChildContext (eu.esdihumboldt.hale.common.align.model.ChildContext)15 XmlElement (eu.esdihumboldt.hale.io.xsd.model.XmlElement)15 Cell (eu.esdihumboldt.hale.common.align.model.Cell)14 IOMessageImpl (eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl)14 MutableCell (eu.esdihumboldt.hale.common.align.model.MutableCell)12 Binding (eu.esdihumboldt.hale.common.schema.model.constraint.type.Binding)12 DefaultPropertyDefinition (eu.esdihumboldt.hale.common.schema.model.impl.DefaultPropertyDefinition)12