Skip to main content
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLaurent Delaigue2015-04-02 22:26:58 +0000
committerAxel RICHARD2015-05-13 08:16:19 +0000
commit063db4a41280f9822e0537873ffb4375f88e2195 (patch)
treedaa8b4289702dbf71193144492ae470938ae02a0 /plugins/org.eclipse.emf.compare.ide.ui/src/org/eclipse/emf/compare/ide/ui/internal/logical/resolver/AbstractResolution.java
parent6719de0f862809a32d0ed902ad0eb645bd84e985 (diff)
downloadorg.eclipse.emf.compare-063db4a41280f9822e0537873ffb4375f88e2195.tar.gz
org.eclipse.emf.compare-063db4a41280f9822e0537873ffb4375f88e2195.tar.xz
org.eclipse.emf.compare-063db4a41280f9822e0537873ffb4375f88e2195.zip
Refactoring of ThreadedModelResolver.
Split computations in several smaller classes to ease maintenance. Extracted multi-threaded logic in a separate class. Extracted function AS_URI to ResourceUtil. Extracted IResourceDependencyProviderListener in its own class Factor out Ecore and URI dependency from DependencyFoundEvent Encapsulates decision on whether a parent URI is registered (i.e., whether the reference is containment or not) in a new subclass ResourceDependencyFoundEvent. This also allows to remove dependencies to URI and EcoreUtils. To allow mocking the graph, we also change Graph to not be final anymore. Added GraphResolutionTest to test suite. Extracted multiple interfaces to separate: - dependency obtention; - local resolution; - remote resolution. Change-Id: I7fe11b2cf6f4abc13f64821826855fe6fe205ef2 Also-by: Philip Langer <planger@eclipsesource.com> Signed-off-by: Laurent Delaigue <laurent.delaigue@obeo.fr> Signed-off-by: Axel Richard <axel.richard@obeo.fr>
Diffstat (limited to 'plugins/org.eclipse.emf.compare.ide.ui/src/org/eclipse/emf/compare/ide/ui/internal/logical/resolver/AbstractResolution.java')
-rw-r--r--plugins/org.eclipse.emf.compare.ide.ui/src/org/eclipse/emf/compare/ide/ui/internal/logical/resolver/AbstractResolution.java165
1 files changed, 165 insertions, 0 deletions
diff --git a/plugins/org.eclipse.emf.compare.ide.ui/src/org/eclipse/emf/compare/ide/ui/internal/logical/resolver/AbstractResolution.java b/plugins/org.eclipse.emf.compare.ide.ui/src/org/eclipse/emf/compare/ide/ui/internal/logical/resolver/AbstractResolution.java
new file mode 100644
index 000000000..283e301ef
--- /dev/null
+++ b/plugins/org.eclipse.emf.compare.ide.ui/src/org/eclipse/emf/compare/ide/ui/internal/logical/resolver/AbstractResolution.java
@@ -0,0 +1,165 @@
+/*******************************************************************************
+ * Copyright (c) 2015 Obeo.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Obeo - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.emf.compare.ide.ui.internal.logical.resolver;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+import static org.eclipse.emf.compare.ide.utils.ResourceUtil.asURI;
+
+import com.google.common.collect.Sets;
+
+import java.util.LinkedHashSet;
+import java.util.Set;
+import java.util.concurrent.Callable;
+
+import org.eclipse.core.resources.IFile;
+import org.eclipse.core.resources.IStorage;
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.core.runtime.SubMonitor;
+import org.eclipse.emf.common.util.Diagnostic;
+import org.eclipse.emf.common.util.URI;
+import org.eclipse.emf.compare.ide.utils.ResourceUtil;
+import org.eclipse.emf.ecore.resource.URIConverter;
+
+/**
+ * Abstract super-class of resolving computations.
+ *
+ * @author <a href="mailto:laurent.delaigue@obeo.fr">Laurent Delaigue</a>
+ */
+public abstract class AbstractResolution {
+
+ /** The context. */
+ protected final IResolutionContext context;
+
+ /** The monitor. */
+ protected final SubMonitor monitor;
+
+ /** The diagnostic. */
+ protected DiagnosticSupport diagnostic;
+
+ /**
+ * Constructor.
+ *
+ * @param context
+ * The resolution context, must not be {@code null}
+ * @param monitor
+ * The progress monitor, can be {@code null}
+ */
+ public AbstractResolution(IResolutionContext context, IProgressMonitor monitor) {
+ this.context = checkNotNull(context);
+ this.monitor = SubMonitor.convert(monitor, getTicks());
+ }
+
+ /**
+ * Number of ticks to allocate to the progress monitor used for reporting progress.
+ *
+ * @return The number of ticks to use, 100 by default but can be overridden if necessary.
+ */
+ protected int getTicks() {
+ return 100;
+ }
+
+ /**
+ * Executes the given callable as soon as there is no other computation running, and automatically runs
+ * "finalization" treatment once the computation is over, whatever its outcome (success or failure). A
+ * {@link #diagnostic} is instantiated before the computation and should be used thourghout this whole
+ * computation. It will be set to {@code null} before returning, whatever happens.
+ *
+ * @param <T>
+ * The type of the returned value.
+ * @param callable
+ * Treatment to run
+ * @return The result of the treatment
+ * @throws InterruptedException
+ * If the treatment is interrupted
+ */
+ protected <T> T call(Callable<T> callable) throws InterruptedException {
+ this.diagnostic = new DiagnosticSupport();
+ return context.getScheduler().call(callable, getFinalizeResolvingRunnable());
+ }
+
+ /**
+ * This provides the treatment that is run at the end of the computation, whatever its outcome. It is
+ * guaranteed to run once, in a block "finally", along with other required finalization treatments that
+ * are run systematically. There's no need to acquire a lock, this is guaranteed to have been done before,
+ * and it is released after this treatment ends.
+ *
+ * @return The {@link Runnable} to run after having resolved resources.
+ */
+ protected Runnable getFinalizeResolvingRunnable() {
+ return new Runnable() {
+ public void run() {
+ if (diagnostic.getDiagnostic().getSeverity() >= Diagnostic.ERROR) {
+ // something bad (or a cancel request) happened during resolution, so we invalidate the
+ // dependency graph to avoid weird behavior next time the resolution is called.
+ // TODO Should we really do that?
+ // FIXME dependencyGraph.clear();
+ }
+ diagnostic = null;
+ }
+ };
+ }
+
+ /**
+ * Transforms the given {@link Set} of {@link IStorage}s into a {@link Set} of {@link URI}s.
+ *
+ * @param storages
+ * The storages to transform, must not be {@code null}.
+ * @return A mutable set of {@link URI}s, may be empty but never {@code )null}.
+ */
+ protected Set<URI> asURISet(Set<IStorage> storages) {
+ final Set<URI> uris = new LinkedHashSet<URI>();
+ for (IStorage storage : storages) {
+ uris.add(asURI().apply(storage));
+ }
+ return uris;
+ }
+
+ /**
+ * Computes the traversal of the given file, excluding the given bounds if needed.
+ *
+ * @param file
+ * File for which the traversal is needed
+ * @param bounds
+ * URI to exclude from the logical model computation in case both compared resources are part
+ * of the same logical model
+ * @return A {@link Set} of the file's outgoing and incoming dependencies, never null but possibly empty.
+ */
+ protected Set<IStorage> resolveTraversal(IFile file, Set<URI> bounds) {
+ final Set<IStorage> traversalSet = Sets.newLinkedHashSet();
+ Set<IFile> filesToAdd = Sets.newLinkedHashSet();
+ filesToAdd.add(file);
+ Set<URI> knownURIs = Sets.newLinkedHashSet();
+ while (!filesToAdd.isEmpty()) {
+ Set<IFile> filesToResolve = Sets.newLinkedHashSet();
+ for (IFile newFile : filesToAdd) {
+ URI baseUri = ResourceUtil.createURIFor(newFile);
+ Set<URI> newURIs = context.getImplicitDependencies().of(baseUri, URIConverter.INSTANCE);
+ for (URI uri : newURIs) {
+ if (knownURIs.add(uri)) {
+ IFile toResolve = ResolutionUtil.getFileAt(uri);
+ Iterable<URI> dependencies = context.getDependencyProvider().getDependenciesOf(
+ toResolve, bounds);
+ for (URI dep : dependencies) {
+ IFile dependentFile = ResolutionUtil.getFileAt(dep);
+ if (dependentFile != null && traversalSet.add(dependentFile)
+ && !knownURIs.contains(dep)) {
+ filesToResolve.add(dependentFile);
+ }
+ }
+ }
+ }
+ }
+ filesToAdd.clear();
+ filesToAdd = filesToResolve;
+ }
+ return traversalSet;
+ }
+}

Back to the top