Skip to content

Commit

Permalink
[333] Add migration participant to remove useless StringValueStyle
Browse files Browse the repository at this point in the history
The representations created before the previous commit, that fixes the
unexpected creation of StringValueStyle, can contain some kind of "empty
StringValueStyle".
The goal of this commit is to remove them.

This commit also adds tests to check migration effect.

Bug: #333
  • Loading branch information
lredor committed Mar 26, 2024
1 parent b126576 commit a4d177b
Show file tree
Hide file tree
Showing 4 changed files with 165 additions and 2 deletions.
4 changes: 3 additions & 1 deletion plugins/org.eclipse.sirius.diagram.elk/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@ Require-Bundle: org.eclipse.ui,
org.eclipse.emf.common.ui,
org.eclipse.elk.alg.layered;bundle-version="0.9.0",
org.eclipse.sirius.ext.base;bundle-version="7.4.1",
org.eclipse.elk.alg.rectpacking;bundle-version="0.9.0"
org.eclipse.elk.alg.rectpacking;bundle-version="0.9.0",
org.eclipse.sirius.ecore.extender;bundle-version="7.4.1"
Bundle-RequiredExecutionEnvironment: JavaSE-17
Bundle-ActivationPolicy: lazy
Import-Package: com.google.common.collect;version="27.0.0",
org.eclipse.sirius.common.tools.api.util;version="3.1.0"
Bundle-Vendor: %providerName
Automatic-Module-Name: org.eclipse.sirius.diagram.elk
Export-Package: org.eclipse.sirius.diagram.elk,
org.eclipse.sirius.diagram.elk.migration,
org.eclipse.sirius.diagram.elk.migration.description
4 changes: 4 additions & 0 deletions plugins/org.eclipse.sirius.diagram.elk/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,9 @@
class="org.eclipse.sirius.diagram.elk.migration.description.ELK090MigrationParticipant"
kind="VSM">
</participant>
<participant
class="org.eclipse.sirius.diagram.elk.migration.EmptyJunctionPointsStringValueStyleMigrationParticipant"
kind="RepresentationsFile">
</participant>
</extension>
</plugin>
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/*******************************************************************************
* Copyright (c) 2024 Obeo
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Obeo - initial API and implementation
*******************************************************************************/
package org.eclipse.sirius.diagram.elk.migration;

import java.util.Optional;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

import org.eclipse.gmf.runtime.notation.Diagram;
import org.eclipse.gmf.runtime.notation.Edge;
import org.eclipse.gmf.runtime.notation.NotationPackage;
import org.eclipse.gmf.runtime.notation.StringValueStyle;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.sirius.business.api.migration.AbstractRepresentationsFileMigrationParticipant;
import org.eclipse.sirius.business.api.query.DViewQuery;
import org.eclipse.sirius.diagram.DDiagram;
import org.eclipse.sirius.diagram.elk.GmfLayoutCommand;
import org.eclipse.sirius.diagram.ui.business.api.query.DDiagramGraphicalQuery;
import org.eclipse.sirius.viewpoint.DAnalysis;
import org.eclipse.sirius.viewpoint.DRepresentationDescriptor;
import org.eclipse.sirius.viewpoint.DView;
import org.osgi.framework.Version;

/**
*
* This migration participant fixes the GMF edges that contain an empty "junctionPoints" StringValueStyle. These empty
* "junctionPoints" StringValueStyle were created due to a bug in ELK./Sirius integration. This migration participant
* simply removes the empty "junctionPoints" StringValueStyle.
*
* @author Laurent Redor
*
*/
public class EmptyJunctionPointsStringValueStyleMigrationParticipant extends AbstractRepresentationsFileMigrationParticipant {

/**
* Migration version.
*/
public static final Version MIGRATION_VERSION = new Version("15.4.1.202403251543"); //$NON-NLS-1$

@Override
public Version getMigrationVersion() {
return MIGRATION_VERSION;
}

/**
* Update the styles of this <code>edge</code> if it contains empty "junctionPoints" StringValueStyle. In this case,
* this style is removed.
*
* @param edge
* the GMF edge
*/
private void updateEdge(View edge) {
StringValueStyle stringValueStyle = (StringValueStyle) edge.getStyle(NotationPackage.Literals.STRING_VALUE_STYLE);
if (stringValueStyle != null && GmfLayoutCommand.JUNCTION_POINTS_STYLE_NAME.equals(stringValueStyle.getName()) && "()".equals(stringValueStyle.getStringValue())) { //$NON-NLS-1$
edge.getStyles().remove(stringValueStyle);
}
}

/**
* Update the GMF diagram to migrate all invalid edges.
*
* @param diagram
* the GMF diagram
*/
@SuppressWarnings("unchecked")
private void migrateDiagram(Diagram diagram) {
StreamSupport.stream(diagram.getEdges().spliterator(), false).filter(Edge.class::isInstance).forEach(edge -> updateEdge((Edge) edge));
}

/**
* This method returns the associated GMF diagram of a Sirius diagram.
*
* @param dDiagram
* the Sirius diagram
* @return the associated GMF diagram if present
*/
private Optional<Diagram> getGMFDiagram(DDiagram dDiagram) {
DDiagramGraphicalQuery query = new DDiagramGraphicalQuery(dDiagram);
return Optional.ofNullable(query.getAssociatedGMFDiagram().get());
}

/**
* This method returns all representations descriptors of a view.
*
* @param dView
* the Sirius viewpoint.
* @return java stream of all representation descriptors of the <code>dView</code>.
*/
private Stream<DRepresentationDescriptor> getRepresentationsDescriptors(DView dView) {
return new DViewQuery(dView).getLoadedRepresentationsDescriptors().stream();
}

/**
* This method is overridden to fix file with inconsistent GMF edges, ie having an empty "junctionPoints"
* StringValueStyle.
*/
@Override
protected void postLoad(DAnalysis dAnalysis, Version loadedVersion) {
if (loadedVersion.compareTo(MIGRATION_VERSION) < 0) { // loadedVersion < MIGRATION_VERSION
// for each diagrams of each viewpoints
dAnalysis.getOwnedViews().stream() //
.flatMap(this::getRepresentationsDescriptors) //
.map(descriptor -> descriptor.getRepresentation()) //
.filter(representation -> representation instanceof DDiagram) //
.map(representation -> (DDiagram) representation) //
.map(this::getGMFDiagram) //
.flatMap(Optional::stream) //
.forEach(this::migrateDiagram);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.PointList;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.emf.common.util.TreeIterator;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EAttribute;
import org.eclipse.emf.ecore.EObject;
Expand Down Expand Up @@ -62,12 +63,14 @@
import org.eclipse.gmf.runtime.draw2d.ui.internal.figures.AnimatableScrollPane;
import org.eclipse.gmf.runtime.notation.Bounds;
import org.eclipse.gmf.runtime.notation.Diagram;
import org.eclipse.gmf.runtime.notation.Edge;
import org.eclipse.gmf.runtime.notation.FontStyle;
import org.eclipse.gmf.runtime.notation.LayoutConstraint;
import org.eclipse.gmf.runtime.notation.Location;
import org.eclipse.gmf.runtime.notation.Node;
import org.eclipse.gmf.runtime.notation.NotationPackage;
import org.eclipse.gmf.runtime.notation.Routing;
import org.eclipse.gmf.runtime.notation.StringValueStyle;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.sirius.diagram.DDiagram;
Expand All @@ -89,6 +92,8 @@
import org.eclipse.sirius.diagram.description.EnumLayoutOption;
import org.eclipse.sirius.diagram.description.EnumLayoutValue;
import org.eclipse.sirius.diagram.description.LayoutOptionTarget;
import org.eclipse.sirius.diagram.elk.GmfLayoutCommand;
import org.eclipse.sirius.diagram.elk.migration.EmptyJunctionPointsStringValueStyleMigrationParticipant;
import org.eclipse.sirius.diagram.tools.api.preferences.SiriusDiagramPreferencesKeys;
import org.eclipse.sirius.diagram.tools.internal.commands.PinElementsCommand;
import org.eclipse.sirius.diagram.ui.business.api.query.ViewQuery;
Expand Down Expand Up @@ -121,6 +126,7 @@
import org.eclipse.sirius.viewpoint.description.Group;
import org.eclipse.sirius.viewpoint.description.RepresentationDescription;
import org.eclipse.ui.IEditorPart;
import org.osgi.framework.Version;

import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
Expand Down Expand Up @@ -1888,6 +1894,18 @@ private void testArrangeAllResultForEdgesRoutingStyle(String diagramName, String
checkRoutingStyle(edgeEditPartWithoutRightAngle, WRONG_ROUTING_AFTER_LAYOUT_MESSAGE, secondEdgeNameToCheck, afterLayoutExpectedEdgeRouting);
}

/**
* Test that the data were not migrated on the repository. It allows to check the effect of the migration of
* EmptyJunctionPointsStringValueStyleMigrationParticipant in other tests.
*/
public void testMigrationIsNeededOnData() {
Version migrationVersion = new EmptyJunctionPointsStringValueStyleMigrationParticipant().getMigrationVersion();

// Check that the migration of the session resource is needed.
Version loadedVersion = checkRepresentationFileMigrationStatus(URI.createPlatformPluginURI("/" + TEMPORARY_PROJECT_NAME + "/" + REPRESENTATIONS_RESOURCE_NAME, true), true);
assertTrue("The migration must be required on test data.", migrationVersion.compareTo(loadedVersion) > 0); //$NON-NLS-1$
}

private void checkRoutingStyle(AbstractDiagramEdgeEditPart edgeEditPart, String message, String edgeName, int expectedRoutingStyle) {
String fullMessage = MessageFormat.format(message, edgeName);
View notationView = edgeEditPart.getNotationView();
Expand Down Expand Up @@ -1915,8 +1933,25 @@ protected void openDiagram(String diagramName) {
diagram = (DDiagram) getRepresentationsByName(diagramName).toArray()[0];
editorPart = (IDiagramWorkbenchPart) DialectUIManager.INSTANCE.openEditor(session, diagram, new NullProgressMonitor());
TestsUtil.synchronizationWithUIThread();
checkStringValueStyle();
}

protected void checkStringValueStyle() {
Diagram gmfDiag = getGmfDiagram(diagram);
if (gmfDiag != null) {
TreeIterator<EObject> childIterator = gmfDiag.eAllContents();
while (childIterator.hasNext()) {
EObject eObject = childIterator.next();
if (eObject instanceof Edge) {
Edge edge = (Edge) eObject;
StringValueStyle stringValueStyle = (StringValueStyle) edge.getStyle(NotationPackage.Literals.STRING_VALUE_STYLE);
if (stringValueStyle != null && GmfLayoutCommand.JUNCTION_POINTS_STYLE_NAME.equals(stringValueStyle.getName()) && "()".equals(stringValueStyle.getStringValue())) {
fail("At least one edge contains an empty junctionPoints StringValueStyle.");
}
}
}
}
}

/**
* Check that the Note is moved or not moved with an arrange using ELK, according to the value of the preference
* "Move unlinked notes during layout".
Expand Down

0 comments on commit a4d177b

Please sign in to comment.