Skip to content

Commit

Permalink
Merge pull request #27 from /issues/26
Browse files Browse the repository at this point in the history
Experiments to fix inconsistent project setup issues
  • Loading branch information
ujhelyiz authored Mar 5, 2019
2 parents ca2562d + 9a82bf5 commit 5a11c5f
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.incquerylabs.v4md;

import com.incquerylabs.v4md.expressions.BinaryVQLExpression;
import com.incquerylabs.v4md.internal.IProjectChangedListener;
import com.nomagic.ci.persistence.IProject;
import com.nomagic.magicdraw.core.Application;
import com.nomagic.magicdraw.core.Project;
Expand All @@ -11,8 +12,9 @@
import com.nomagic.magicdraw.expressions.evaluation.ExpressionEvaluationConfigurator;

public class V4MDPlugin extends com.nomagic.magicdraw.plugins.Plugin {

private final class V4MDProjectListener extends ProjectEventListenerAdapter implements ProjectPartListener {

@Override
public void projectOpened(Project project) {
ViatraQueryAdapter.getOrCreateAdapter(project);
Expand All @@ -22,29 +24,29 @@ public void projectOpened(Project project) {
public void projectPreClosed(Project project) {
ViatraQueryAdapter.disposeAdapter(project);
}

@Override
public void projectPartLoaded(Project project, IProject storage) {
ViatraQueryAdapter.getAdapter(project).ifPresent(ViatraQueryAdapter::projectStructureUpdated);
IProjectChangedListener.MANAGER.notifyProjectListeners(project);
}

@Override
public void projectPartAttached(ModuleUsage usage) {
Project project = ProjectUtilities.getProject(usage.getUsed());
ViatraQueryAdapter.getAdapter(project).ifPresent(ViatraQueryAdapter::projectStructureUpdated);
IProjectChangedListener.MANAGER.notifyProjectListeners(project);
}

@Override
public void projectPartDetached(ModuleUsage usage) {
Project project = ProjectUtilities.getProject(usage.getUsed());
ViatraQueryAdapter.getAdapter(project).ifPresent(ViatraQueryAdapter::projectStructureUpdated);
IProjectChangedListener.MANAGER.notifyProjectListeners(project);

}

@Override
public void projectPartRemoved(IProject project) {
Project modelProject = ProjectUtilities.getProject(project);
ViatraQueryAdapter.getAdapter(modelProject).ifPresent(ViatraQueryAdapter::projectStructureUpdated);
IProjectChangedListener.MANAGER.notifyProjectListeners(modelProject);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.incquerylabs.v4md.internal;

import com.google.common.collect.ArrayListMultimap;
import com.nomagic.magicdraw.core.Project;

public interface IProjectChangedListener {
void modelSetUpdated();

public static final Manager MANAGER = new Manager();

public class Manager {
ArrayListMultimap<Project, IProjectChangedListener> listenerMap = ArrayListMultimap.create();


private Manager() {
// Private constructor for singleton class
}

public void notifyProjectListeners(Project project) {
for (IProjectChangedListener listener : listenerMap.get(project)) {
listener.modelSetUpdated();
}
}

public boolean addProjectChangeListener(Project project, IProjectChangedListener listener) {
return listenerMap.put(project, listener);
}

public boolean removeProjectChangeListener(Project project, IProjectChangedListener listener) {
return listenerMap.remove(project, listener);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.util.stream.Collectors;

import org.apache.log4j.Logger;
import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.notify.Notifier;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
Expand All @@ -18,6 +19,7 @@
import org.eclipse.viatra.query.runtime.base.api.NavigationHelper;
import org.eclipse.viatra.query.runtime.base.api.filters.IBaseIndexObjectFilter;
import org.eclipse.viatra.query.runtime.base.api.filters.IBaseIndexResourceFilter;
import org.eclipse.viatra.query.runtime.base.core.NavigationHelperContentAdapter;
import org.eclipse.viatra.query.runtime.base.core.NavigationHelperImpl;
import org.eclipse.viatra.query.runtime.base.exception.ViatraBaseException;
import org.eclipse.viatra.query.runtime.emf.DynamicEMFQueryRuntimeContext;
Expand All @@ -27,7 +29,6 @@
import org.eclipse.viatra.query.runtime.matchers.context.IQueryRuntimeContext;

import com.google.common.collect.Sets;
import com.incquerylabs.v4md.internal.MagicDrawProjectScope.IProjectChangedListener;

/**
* Provides a specific engine context implementation for MagicDraw projects.
Expand Down Expand Up @@ -77,6 +78,16 @@ private class MagicDrawProjectNavigationHelper extends NavigationHelperImpl {

public MagicDrawProjectNavigationHelper(Notifier emfRoot, BaseIndexOptions options, Logger logger) {
super(emfRoot, options, logger);
this.contentAdapter = new NavigationHelperContentAdapter(this) {

@Override
public void notifyChanged(Notification notification) {
if (scope.getProject().getRepository().getEventSupport().isEnableEventFiring()) {
super.notifyChanged(notification);
}
}

};
}

Set<Notifier> getModelRoots() {
Expand All @@ -101,7 +112,7 @@ public void removeRoot(Notifier root) {
return;

// no veto by filters
modelRoots.add(root);
modelRoots.remove(root);
// TODO contentAdapter.removeAdapter(root); removeAdapter is not visible here
Method method;
try {
Expand All @@ -114,13 +125,13 @@ public void removeRoot(Notifier root) {
notifyBaseIndexChangeListeners();
}
}
public MagicDrawProjectEngineContext(MagicDrawProjectScope scope, ViatraQueryEngine engine, IIndexingErrorListener taintListener, Logger logger) {
this.scope = scope;
this.engine = engine;
this.logger = logger;
this.taintListener = taintListener;
scope.addProjectChangeListener(scopeListener);
IProjectChangedListener.MANAGER.addProjectChangeListener(scope.getProject(), scopeListener);
}

/**
Expand Down Expand Up @@ -169,7 +180,7 @@ public void dispose() {
if (runtimeContext != null) runtimeContext.dispose();
if (navHelper != null) navHelper.dispose();

scope.removeProjectChangeListener(scopeListener);
IProjectChangedListener.MANAGER.removeProjectChangeListener(scope.getProject(), scopeListener);

this.baseIndex = null;
this.engine = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.eclipse.viatra.query.runtime.emf.EMFScope;

import com.nomagic.magicdraw.core.Project;
import com.nomagic.uml2.ext.magicdraw.classes.mdkernel.Package;

public class MagicDrawProjectScope extends EMFScope {

Expand All @@ -29,15 +30,19 @@ public class MagicDrawProjectScope extends EMFScope {
// XXX Omitting references can cause semantic errors (so far we are in the clear though)
// these references are only present in UML profiles, typically their contents are equal to the original references inherited from the UML type hierarchy, however there are some cases when this might not be the case.
private static final BaseIndexOptions BASE_OPTIONS = new BaseIndexOptions()
.withFeatureFilterConfiguration(reference -> reference instanceof EReference
&& ((EReference) reference).isContainment() && reference.getName().contains("_from_"))
.withFeatureFilterConfiguration(reference -> reference instanceof EReference && isReferenceToBeFiltered((EReference) reference))
.withStrictNotificationMode(false);

public interface IProjectChangedListener {
void modelSetUpdated();
private static boolean isReferenceToBeFiltered(EReference reference) {
String name = reference.getName();
return (reference.isContainment() && name.contains("_from_"))
||
name.startsWith("_");
}

static Stream<? extends Notifier> getProjectModels(Project projectModel) {
return projectModel.getModels().stream();
Package primaryModel = projectModel.getPrimaryModel();
return projectModel.getModels().stream().filter(pkg -> pkg == primaryModel || !EcoreUtil.isAncestor(primaryModel, pkg));
}

static Stream<Notifier> getCustomNotifiers(Notifier... notifiers) {
Expand All @@ -60,14 +65,6 @@ protected IEngineContext createEngineContext(ViatraQueryEngine engine, IIndexing
Logger logger) {
return new MagicDrawProjectEngineContext(this, engine, errorListener, logger);
}

boolean addProjectChangeListener(IProjectChangedListener listener) {
return listeners.add(listener);
}

boolean removeProjectChangeListener(IProjectChangedListener listener) {
return listeners.remove(listener);
}

public void projectStructureUpdated() {
for (IProjectChangedListener listener : listeners) {
Expand Down

0 comments on commit 5a11c5f

Please sign in to comment.