Search in sources :

Example 26 with Pair

use of eu.esdihumboldt.util.Pair in project hale by halestudio.

the class JaxbToAlignment method convertEntities.

private static ListMultimap<String, Pair<Entity, Entity>> convertEntities(List<NamedEntityType> namedEntities, TypeIndex types, SchemaSpaceID schemaSpace, EntityResolver resolver) {
    if (namedEntities == null || namedEntities.isEmpty()) {
        return null;
    }
    ListMultimap<String, Pair<Entity, Entity>> result = ArrayListMultimap.create();
    for (NamedEntityType namedEntity : namedEntities) {
        /**
         * Resolve entity.
         *
         * Possible results:
         * <ul>
         * <li>non-null entity - entity could be resolved</li>
         * <li>null entity - entity could not be resolved, continue</li>
         * <li>IllegalStateException - entity could not be resolved, reject
         * cell</li>
         * </ul>
         */
        // Create a dummy entity from the original XML definition
        DummyEntityResolver dummyResolver = new DummyEntityResolver();
        Entity dummyEntity = dummyResolver.resolve(namedEntity.getAbstractEntity().getValue(), types, schemaSpace);
        // Resolve the real entity
        Entity resolvedEntity = resolver.resolve(namedEntity.getAbstractEntity().getValue(), types, schemaSpace);
        if (resolvedEntity != null) {
            result.put(namedEntity.getName(), new Pair<Entity, Entity>(dummyEntity, resolvedEntity));
        }
    }
    return result;
}
Also used : NamedEntityType(eu.esdihumboldt.hale.common.align.io.impl.internal.generated.NamedEntityType) Entity(eu.esdihumboldt.hale.common.align.model.Entity) DummyEntityResolver(eu.esdihumboldt.hale.common.align.io.impl.dummy.DummyEntityResolver) Pair(eu.esdihumboldt.util.Pair)

Example 27 with Pair

use of eu.esdihumboldt.util.Pair in project hale by halestudio.

the class JaxbToAlignment method createUnmigratedCell.

private static UnmigratedCell createUnmigratedCell(CellType cell, LoadAlignmentContext context, IOReporter reporter, EntityResolver resolver, ServiceProvider serviceProvider) {
    // The sourceCell represents the cell as it was imported from the
    // XML alignment. The conversion to the resolved cell must be performed
    // later by migrating the UnmigratedCell returned from this function.
    final DefaultCell sourceCell = new DefaultCell();
    sourceCell.setTransformationIdentifier(cell.getRelation());
    final FunctionDefinition<?> cellFunction = FunctionUtil.getFunction(sourceCell.getTransformationIdentifier(), serviceProvider);
    final CellMigrator migrator;
    if (cellFunction != null) {
        migrator = cellFunction.getCustomMigrator().orElse(new DefaultCellMigrator());
    } else {
        migrator = new DefaultCellMigrator();
    }
    Map<EntityDefinition, EntityDefinition> mappings = new HashMap<>();
    try {
        // The returned Entity pair consists of
        // (1st) a dummy entity representing the entity read from JAXB
        // (2nd) the resolved entity
        ListMultimap<String, Pair<Entity, Entity>> convertedSourceEntities = convertEntities(cell.getSource(), context.getSourceTypes(), SchemaSpaceID.SOURCE, resolver);
        if (convertedSourceEntities == null) {
            sourceCell.setSource(null);
        } else {
            sourceCell.setSource(Multimaps.transformValues(convertedSourceEntities, pair -> pair.getFirst()));
            for (Pair<Entity, Entity> pair : convertedSourceEntities.values()) {
                mappings.put(pair.getFirst().getDefinition(), pair.getSecond().getDefinition());
            }
        }
        ListMultimap<String, Pair<Entity, Entity>> convertedTargetEntities = convertEntities(cell.getTarget(), context.getTargetTypes(), SchemaSpaceID.TARGET, resolver);
        if (convertedTargetEntities == null) {
            sourceCell.setTarget(null);
        } else {
            sourceCell.setTarget(Multimaps.transformValues(convertedTargetEntities, pair -> pair.getFirst()));
            for (Pair<Entity, Entity> pair : convertedTargetEntities.values()) {
                mappings.put(pair.getFirst().getDefinition(), pair.getSecond().getDefinition());
            }
        }
        if (sourceCell.getTarget() == null || sourceCell.getTarget().isEmpty()) {
            // target is mandatory for cells!
            throw new IllegalStateException("Cannot create cell without target");
        }
    } catch (Exception e) {
        if (reporter != null) {
            reporter.error(new IOMessageImpl("Could not create cell", e));
        }
        return null;
    }
    if (!cell.getAbstractParameter().isEmpty()) {
        ListMultimap<String, ParameterValue> parameters = ArrayListMultimap.create();
        for (JAXBElement<? extends AbstractParameterType> param : cell.getAbstractParameter()) {
            AbstractParameterType apt = param.getValue();
            if (apt instanceof ParameterType) {
                // treat string parameters or null parameters
                ParameterType pt = (ParameterType) apt;
                ParameterValue pv = new ParameterValue(pt.getType(), Value.of(pt.getValue()));
                parameters.put(pt.getName(), pv);
            } else if (apt instanceof ComplexParameterType) {
                // complex parameters
                ComplexParameterType cpt = (ComplexParameterType) apt;
                parameters.put(cpt.getName(), new ParameterValue(new ElementValue(cpt.getAny(), context)));
            } else
                throw new IllegalStateException("Illegal parameter type");
        }
        sourceCell.setTransformationParameters(parameters);
    }
    // annotations & documentation
    for (Object element : cell.getDocumentationOrAnnotation()) {
        if (element instanceof AnnotationType) {
            // add annotation to the cell
            AnnotationType annot = (AnnotationType) element;
            // but first load it from the DOM
            AnnotationDescriptor<?> desc = AnnotationExtension.getInstance().get(annot.getType());
            if (desc != null) {
                try {
                    Object value = desc.fromDOM(annot.getAny(), null);
                    sourceCell.addAnnotation(annot.getType(), value);
                } catch (Exception e) {
                    if (reporter != null) {
                        reporter.error(new IOMessageImpl("Error loading cell annotation", e));
                    } else
                        throw new IllegalStateException("Error loading cell annotation", e);
                }
            } else
                reporter.error(new IOMessageImpl("Cell annotation of type {0} unknown, cannot load the annotation object", null, -1, -1, annot.getType()));
        } else if (element instanceof DocumentationType) {
            // add documentation to the cell
            DocumentationType doc = (DocumentationType) element;
            sourceCell.getDocumentation().put(doc.getType(), doc.getValue());
        }
    }
    sourceCell.setId(cell.getId());
    // a default value is assured for priority
    String priorityStr = cell.getPriority().value();
    Priority priority = Priority.fromValue(priorityStr);
    if (priority != null) {
        sourceCell.setPriority(priority);
    } else {
        // used.
        throw new IllegalArgumentException();
    }
    return new UnmigratedCell(sourceCell, migrator, mappings);
}
Also used : ArrayListMultimap(com.google.common.collect.ArrayListMultimap) ListMultimap(com.google.common.collect.ListMultimap) CellMigrator(eu.esdihumboldt.hale.common.align.migrate.CellMigrator) Collections2(com.google.common.collect.Collections2) ModifierType(eu.esdihumboldt.hale.common.align.io.impl.internal.generated.ModifierType) MutableAlignment(eu.esdihumboldt.hale.common.align.model.MutableAlignment) ElementValue(eu.esdihumboldt.hale.common.core.io.impl.ElementValue) HaleIO(eu.esdihumboldt.hale.common.core.io.HaleIO) DefaultCell(eu.esdihumboldt.hale.common.align.model.impl.DefaultCell) Map(java.util.Map) ParameterType(eu.esdihumboldt.hale.common.align.io.impl.internal.generated.ParameterType) Pair(eu.esdihumboldt.util.Pair) AnnotationDescriptor(eu.esdihumboldt.hale.common.align.model.AnnotationDescriptor) URI(java.net.URI) AnnotationExtension(eu.esdihumboldt.hale.common.align.extension.annotation.AnnotationExtension) Value(eu.esdihumboldt.hale.common.core.io.Value) Function(com.google.common.base.Function) Collection(java.util.Collection) UnmigratedCell(eu.esdihumboldt.hale.common.align.migrate.impl.UnmigratedCell) ParameterValue(eu.esdihumboldt.hale.common.align.model.ParameterValue) IOReporter(eu.esdihumboldt.hale.common.core.io.report.IOReporter) JAXBException(javax.xml.bind.JAXBException) LoadAlignmentContext(eu.esdihumboldt.hale.common.align.io.LoadAlignmentContext) PathUpdate(eu.esdihumboldt.hale.common.core.io.PathUpdate) SchemaSpaceID(eu.esdihumboldt.hale.common.schema.SchemaSpaceID) CustomFunctionType(eu.esdihumboldt.hale.common.align.io.impl.internal.generated.CustomFunctionType) DummyEntityResolver(eu.esdihumboldt.hale.common.align.io.impl.dummy.DummyEntityResolver) List(java.util.List) Base(eu.esdihumboldt.hale.common.align.io.impl.internal.generated.AlignmentType.Base) ComplexParameterType(eu.esdihumboldt.hale.common.align.io.impl.internal.generated.ComplexParameterType) TypeIndex(eu.esdihumboldt.hale.common.schema.model.TypeIndex) CellType(eu.esdihumboldt.hale.common.align.io.impl.internal.generated.CellType) Priority(eu.esdihumboldt.hale.common.align.model.Priority) StreamSource(javax.xml.transform.stream.StreamSource) AnnotationType(eu.esdihumboldt.hale.common.align.io.impl.internal.generated.AnnotationType) HashMap(java.util.HashMap) DefaultCellMigrator(eu.esdihumboldt.hale.common.align.migrate.impl.DefaultCellMigrator) ArrayList(java.util.ArrayList) Multimaps(com.google.common.collect.Multimaps) JaxbAlignmentIO(eu.esdihumboldt.hale.common.align.io.impl.JaxbAlignmentIO) EntityResolver(eu.esdihumboldt.hale.common.align.io.EntityResolver) IOMessageImpl(eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl) MutableCell(eu.esdihumboldt.hale.common.align.model.MutableCell) DisableFor(eu.esdihumboldt.hale.common.align.io.impl.internal.generated.ModifierType.DisableFor) JAXBContext(javax.xml.bind.JAXBContext) Unmarshaller(javax.xml.bind.Unmarshaller) CustomPropertyFunction(eu.esdihumboldt.hale.common.align.extension.function.custom.CustomPropertyFunction) DocumentationType(eu.esdihumboldt.hale.common.align.io.impl.internal.generated.DocumentationType) JAXBElement(javax.xml.bind.JAXBElement) IOException(java.io.IOException) ServiceProvider(eu.esdihumboldt.hale.common.core.service.ServiceProvider) DefaultEntityResolver(eu.esdihumboldt.hale.common.align.io.impl.DefaultEntityResolver) Entity(eu.esdihumboldt.hale.common.align.model.Entity) FunctionUtil(eu.esdihumboldt.hale.common.align.extension.function.FunctionUtil) AbstractParameterType(eu.esdihumboldt.hale.common.align.io.impl.internal.generated.AbstractParameterType) TransformationMode(eu.esdihumboldt.hale.common.align.model.TransformationMode) FunctionDefinition(eu.esdihumboldt.hale.common.align.extension.function.FunctionDefinition) Element(org.w3c.dom.Element) NamedEntityType(eu.esdihumboldt.hale.common.align.io.impl.internal.generated.NamedEntityType) AlignmentType(eu.esdihumboldt.hale.common.align.io.impl.internal.generated.AlignmentType) EntityDefinition(eu.esdihumboldt.hale.common.align.model.EntityDefinition) InputStream(java.io.InputStream) Entity(eu.esdihumboldt.hale.common.align.model.Entity) HashMap(java.util.HashMap) IOMessageImpl(eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl) CellMigrator(eu.esdihumboldt.hale.common.align.migrate.CellMigrator) DefaultCellMigrator(eu.esdihumboldt.hale.common.align.migrate.impl.DefaultCellMigrator) DefaultCellMigrator(eu.esdihumboldt.hale.common.align.migrate.impl.DefaultCellMigrator) AnnotationType(eu.esdihumboldt.hale.common.align.io.impl.internal.generated.AnnotationType) UnmigratedCell(eu.esdihumboldt.hale.common.align.migrate.impl.UnmigratedCell) AbstractParameterType(eu.esdihumboldt.hale.common.align.io.impl.internal.generated.AbstractParameterType) Pair(eu.esdihumboldt.util.Pair) ParameterType(eu.esdihumboldt.hale.common.align.io.impl.internal.generated.ParameterType) ComplexParameterType(eu.esdihumboldt.hale.common.align.io.impl.internal.generated.ComplexParameterType) AbstractParameterType(eu.esdihumboldt.hale.common.align.io.impl.internal.generated.AbstractParameterType) ParameterValue(eu.esdihumboldt.hale.common.align.model.ParameterValue) Priority(eu.esdihumboldt.hale.common.align.model.Priority) ElementValue(eu.esdihumboldt.hale.common.core.io.impl.ElementValue) JAXBException(javax.xml.bind.JAXBException) IOException(java.io.IOException) ComplexParameterType(eu.esdihumboldt.hale.common.align.io.impl.internal.generated.ComplexParameterType) EntityDefinition(eu.esdihumboldt.hale.common.align.model.EntityDefinition) DocumentationType(eu.esdihumboldt.hale.common.align.io.impl.internal.generated.DocumentationType) DefaultCell(eu.esdihumboldt.hale.common.align.model.impl.DefaultCell)

Example 28 with Pair

use of eu.esdihumboldt.util.Pair in project hale by halestudio.

the class PropertyBean method findChild.

/**
 * The function to look for a child as ChildDefinition or as Group
 *
 * @param parent the starting point to traverse from
 * @param childName the name of the parent's child
 * @return a pair of child and a list with the full path from parent to the
 *         child or <code>null</code> if no such child was found
 */
public static Pair<ChildDefinition<?>, List<ChildDefinition<?>>> findChild(DefinitionGroup parent, QName childName) {
    ChildDefinition<?> child = parent.getChild(childName);
    if (child == null) {
        // if the namespace is not null
        if (childName.getNamespaceURI().equals(XMLConstants.NULL_NS_URI)) {
            // get all children and iterate over them
            Collection<? extends ChildDefinition<?>> children = DefinitionUtil.getAllChildren(parent);
            for (ChildDefinition<?> _child : children) {
                // different namespace we overwrite child
                if (_child.getName().getLocalPart().equals(childName.getLocalPart())) {
                    child = _child;
                    break;
                }
            }
        }
    }
    if (child != null) {
        return new Pair<ChildDefinition<?>, List<ChildDefinition<?>>>(child, null);
    }
    Collection<? extends ChildDefinition<?>> children = DefinitionUtil.getAllChildren(parent);
    for (ChildDefinition<?> groupChild : children) {
        if (groupChild.asGroup() != null) {
            GroupPropertyDefinition temp = groupChild.asGroup();
            if (findChild(temp, childName) != null) {
                Pair<ChildDefinition<?>, List<ChildDefinition<?>>> recTemp = findChild(temp, childName);
                if (recTemp.getSecond() == null) {
                    List<ChildDefinition<?>> second = new ArrayList<ChildDefinition<?>>();
                    second.add(temp);
                    ChildDefinition<?> first = recTemp.getFirst();
                    return new Pair<ChildDefinition<?>, List<ChildDefinition<?>>>(first, second);
                } else {
                    recTemp.getSecond().add(0, temp);
                }
            }
        }
    }
    return null;
}
Also used : GroupPropertyDefinition(eu.esdihumboldt.hale.common.schema.model.GroupPropertyDefinition) ChildDefinition(eu.esdihumboldt.hale.common.schema.model.ChildDefinition) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) Pair(eu.esdihumboldt.util.Pair)

Example 29 with Pair

use of eu.esdihumboldt.util.Pair in project hale by halestudio.

the class AbstractGeoInstanceWriter method unifyGeometryPair.

/**
 * Returns a pair of unified geometry of given geometry and associated CRS
 * definition based on Winding order supplied.
 *
 * @param pair A pair of Geometry and CRSDefinition, on which winding
 *            process will get done.
 * @param report the reporter
 * @return Unified Pair .
 */
protected Pair<Geometry, CRSDefinition> unifyGeometryPair(Pair<Geometry, CRSDefinition> pair, IOReporter report) {
    // get Geometry object
    Geometry geom = pair.getFirst();
    if (geom == null) {
        return pair;
    }
    // getting CRS
    CRSDefinition def = pair.getSecond();
    CoordinateReferenceSystem crs = null;
    if (def != null)
        crs = pair.getSecond().getCRS();
    // unify geometry
    geom = unifyGeometry(geom, report, crs);
    return new Pair<>(geom, pair.getSecond());
}
Also used : Geometry(com.vividsolutions.jts.geom.Geometry) CRSDefinition(eu.esdihumboldt.hale.common.schema.geometry.CRSDefinition) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) Pair(eu.esdihumboldt.util.Pair)

Example 30 with Pair

use of eu.esdihumboldt.util.Pair in project hale by halestudio.

the class TransformationView method createViewControl.

/**
 * @see eu.esdihumboldt.hale.ui.views.mapping.AbstractMappingView#createViewControl(org.eclipse.swt.widgets.Composite)
 */
@Override
public void createViewControl(Composite parent) {
    super.createViewControl(parent);
    IActionBars bars = getViewSite().getActionBars();
    bars.getToolBarManager().add(instanceAction = new Action("Apply sample instances", IAction.AS_CHECK_BOX) {

        @Override
        public void run() {
            update();
        }
    });
    instanceAction.setImageDescriptor(TransformationViewPlugin.getImageDescriptor("icons/samples.gif"));
    instanceAction.setChecked(initInstanceAction);
    update();
    AlignmentService as = PlatformUI.getWorkbench().getService(AlignmentService.class);
    as.addListener(alignmentListener = new AlignmentServiceAdapter() {

        @Override
        public void alignmentCleared() {
            update();
        }

        @Override
        public void cellsRemoved(Iterable<Cell> cells) {
            update();
        }

        @Override
        public void cellsReplaced(Map<? extends Cell, ? extends Cell> cells) {
            update();
        }

        @Override
        public void cellsAdded(Iterable<Cell> cells) {
            update();
        }

        @Override
        public void alignmentChanged() {
            update();
        }

        @Override
        public void customFunctionsChanged() {
            update();
        }

        @Override
        public void cellsPropertyChanged(Iterable<Cell> cells, String propertyName) {
            if (Cell.PROPERTY_DISABLE_FOR.equals(propertyName) || Cell.PROPERTY_ENABLE_FOR.equals(propertyName))
                // Could add/remove cells from transformation tree
                update();
            else {
                final Display display = PlatformUI.getWorkbench().getDisplay();
                display.syncExec(new Runnable() {

                    @Override
                    public void run() {
                        // refresh view
                        getViewer().refresh();
                    }
                });
            }
        }
    });
    final InstanceSampleService iss = PlatformUI.getWorkbench().getService(InstanceSampleService.class);
    iss.addObserver(instanceSampleObserver = new Observer() {

        @SuppressWarnings("unchecked")
        @Override
        public void update(Observable o, Object arg) {
            if (!instanceAction.isChecked()) {
                return;
            }
            Object input = getViewer().getInput();
            Collection<Instance> oldInstances = null;
            int sampleCount = 0;
            if (input instanceof Pair<?, ?>) {
                Object second = ((Pair<?, ?>) input).getSecond();
                if (second instanceof Collection<?>) {
                    oldInstances = (Collection<Instance>) second;
                    sampleCount = oldInstances.size();
                }
            }
            Collection<Instance> newSamples = iss.getReferenceInstances();
            if (sampleCount == newSamples.size()) {
                // still to decide if update is necessary
                if (sampleCount == 0) {
                    return;
                }
                // check if instances equal?
                Set<Instance> test = new HashSet<Instance>(oldInstances);
                test.removeAll(newSamples);
                if (test.isEmpty()) {
                    return;
                }
            }
            TransformationView.this.update();
        }
    });
}
Also used : IAction(org.eclipse.jface.action.IAction) Action(org.eclipse.jface.action.Action) Instance(eu.esdihumboldt.hale.common.instance.model.Instance) Observable(java.util.Observable) AlignmentService(eu.esdihumboldt.hale.ui.service.align.AlignmentService) Observer(java.util.Observer) InstanceSampleService(eu.esdihumboldt.hale.ui.service.instance.sample.InstanceSampleService) Collection(java.util.Collection) AlignmentServiceAdapter(eu.esdihumboldt.hale.ui.service.align.AlignmentServiceAdapter) Map(java.util.Map) Cell(eu.esdihumboldt.hale.common.align.model.Cell) IActionBars(org.eclipse.ui.IActionBars) Display(org.eclipse.swt.widgets.Display) Pair(eu.esdihumboldt.util.Pair) HashSet(java.util.HashSet)

Aggregations

Pair (eu.esdihumboldt.util.Pair)33 ArrayList (java.util.ArrayList)11 TypeDefinition (eu.esdihumboldt.hale.common.schema.model.TypeDefinition)8 List (java.util.List)7 Instance (eu.esdihumboldt.hale.common.instance.model.Instance)6 Cell (eu.esdihumboldt.hale.common.align.model.Cell)4 Entity (eu.esdihumboldt.hale.common.align.model.Entity)4 EntityDefinition (eu.esdihumboldt.hale.common.align.model.EntityDefinition)4 Group (eu.esdihumboldt.hale.common.instance.model.Group)4 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)4 Point (org.eclipse.swt.graphics.Point)4 ParameterValue (eu.esdihumboldt.hale.common.align.model.ParameterValue)3 TypeEntityDefinition (eu.esdihumboldt.hale.common.align.model.impl.TypeEntityDefinition)3 IOMessageImpl (eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl)3 GroupPropertyDefinition (eu.esdihumboldt.hale.common.schema.model.GroupPropertyDefinition)3 IOException (java.io.IOException)3 URI (java.net.URI)3 Collection (java.util.Collection)3 QName (javax.xml.namespace.QName)3 SelectionAdapter (org.eclipse.swt.events.SelectionAdapter)3