Skip to content

Commit

Permalink
[639] Display properties keywords for Usage compartment element
Browse files Browse the repository at this point in the history
Bug: eclipse-syson#639
Signed-off-by: Jessy Mallet <jessy.mallet@obeo.fr>
  • Loading branch information
jessymallet authored and gdaniel committed Aug 14, 2024
1 parent 3abcd13 commit 291099a
Show file tree
Hide file tree
Showing 7 changed files with 324 additions and 17 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ The second one called "New ExhibitState with referenced State" shows a dialog al
- https://github.com/eclipse-syson/syson/issues/624[#624] [diagrams] Allow to select existing _Action_ on Perform tool.
- https://github.com/eclipse-syson/syson/issues/628[#628] [diagrams] Allow to set measurement units via direct edit.
- https://github.com/eclipse-syson/syson/issues/634[#634] [diagrams] Allow to select existing _Type_ on Subject tool.
- https://github.com/eclipse-syson/syson/issues/639[#639] [diagrams] Handle properties keywords in label of Usage element.


=== New features

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,37 +19,57 @@
*/
public class LabelConstants {

public static final String CR = "\n";
public static final String ABSTRACT = "abstract";

public static final String CLOSE_BRACKET = "]";

public static final String CLOSE_PARENTHESIS = ")";

public static final String CLOSE_QUOTE = "\u00BB";

public static final String COLON = ":";

public static final String COMMA = ",";

public static final String CONJUGATED = "\u007E";

public static final String CLOSE_BRACKET = "]";
public static final String CR = "\n";

public static final String CLOSE_PARENTHESIS = ")";
public static final String DERIVED = "derived";

public static final String CLOSE_QUOTE = "\u00BB";
public static final String END = "end";

public static final String EQUAL = "=";

public static final String IN = "in";

public static final String INOUT = "inout";

public static final String NON_UNIQUE = "nonunique";

public static final String OPEN_BRACKET = "[";

public static final String OPEN_PARENTHESIS = "(";

public static final String OPEN_QUOTE = "\u00AB";

public static final String ORDERED = "ordered";

public static final String OUT = "out";

public static final String READ_ONLY = "readonly";

public static final String REDEFINITION = ":>>";

public static final String REF = "ref";

public static final String REFERENCES = "::>";

public static final String SPACE = " ";

public static final String SUBCLASSIFICATION = ":>";

public static final String SUBSETTING = ":>";

public static final String REFERENCES = "::>";

public static final String COMMA = ",";
public static final String VARIATION = "variation";
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,11 @@
import org.eclipse.syson.services.grammars.DirectEditLexer;
import org.eclipse.syson.services.grammars.DirectEditListener;
import org.eclipse.syson.services.grammars.DirectEditParser;
import org.eclipse.syson.sysml.AttributeUsage;
import org.eclipse.syson.sysml.Element;
import org.eclipse.syson.sysml.Expression;
import org.eclipse.syson.sysml.Feature;
import org.eclipse.syson.sysml.FeatureDirectionKind;
import org.eclipse.syson.sysml.FeatureReferenceExpression;
import org.eclipse.syson.sysml.FeatureTyping;
import org.eclipse.syson.sysml.FeatureValue;
Expand Down Expand Up @@ -142,9 +145,8 @@ public Element directEdit(Element element, String newLabel, String... options) {
*/
public String getDefaultInitialDirectEditLabel(Element element) {
StringBuilder builder = new StringBuilder();
if (element instanceof Usage usage && usage.isIsReference()) {
builder.append(LabelConstants.REF);
builder.append(LabelConstants.SPACE);
if (element instanceof Usage usage) {
builder.append(this.getUsagePrefix(usage));
}
builder.append(element.getDeclaredName());
builder.append(this.getMultiplicityLabel(element));
Expand All @@ -161,7 +163,7 @@ public String getDefaultInitialDirectEditLabel(Element element) {
/**
* Return the label of the multiplicity part of the given {@link Element}.
*
* @param usage
* @param element
* the given {@link Element}.
* @return the label of the multiplicity part of the given {@link Element} if there is one, an empty string
* otherwise.
Expand All @@ -175,6 +177,7 @@ public String getMultiplicityLabel(Element element) {
.filter(MultiplicityRange.class::isInstance)
.map(MultiplicityRange.class::cast)
.findFirst();
label.append(LabelConstants.SPACE);
if (optMultiplicityRange.isPresent()) {
var range = optMultiplicityRange.get();
String firstBound = null;
Expand All @@ -200,11 +203,74 @@ public String getMultiplicityLabel(Element element) {
label.append("..");
label.append(secondBound);
}
label.append(LabelConstants.CLOSE_BRACKET);
label.append(LabelConstants.CLOSE_BRACKET + LabelConstants.SPACE);
}
if (element instanceof Feature feature) {
if (feature.isIsOrdered()) {
label.append(LabelConstants.ORDERED + LabelConstants.SPACE);
}
if (!feature.isIsUnique()) {
label.append(LabelConstants.NON_UNIQUE + LabelConstants.SPACE);
}
}
String labelAsString = label.toString().trim();
if (!labelAsString.isEmpty()) {
return LabelConstants.SPACE + labelAsString;
} else {
return labelAsString;
}
}

/**
* Return the label of the prefix part of the given {@link Usage}.
*
* @param usage
* the given {@link Usage}.
* @return the label of the prefix part of the given {@link Usage} if there is one, an empty string otherwise.
*/
public String getUsagePrefix(Usage usage) {
StringBuilder label = new StringBuilder();
if (usage.getDirection() == FeatureDirectionKind.IN) {
label.append(LabelConstants.IN + LabelConstants.SPACE);
} else if (usage.getDirection() == FeatureDirectionKind.OUT) {
label.append(LabelConstants.OUT + LabelConstants.SPACE);
} else if (usage.getDirection() == FeatureDirectionKind.INOUT) {
label.append(LabelConstants.INOUT + LabelConstants.SPACE);
}
if (usage.isIsAbstract()) {
label.append(LabelConstants.ABSTRACT + LabelConstants.SPACE);
}
if (usage.isIsVariation()) {
label.append(LabelConstants.VARIATION + LabelConstants.SPACE);
}
if (usage.isIsReadOnly()) {
label.append(LabelConstants.READ_ONLY + LabelConstants.SPACE);
}
if (usage.isIsDerived()) {
label.append(LabelConstants.DERIVED + LabelConstants.SPACE);
}
if (usage.isIsEnd()) {
label.append(LabelConstants.END + LabelConstants.SPACE);
}
this.getReferenceUsagePrefix(usage, label);
return label.toString();
}

/**
* Return the label of the reference prefix part of the given {@link Usage}.
*
* @param usage
* the given {@link Usage}.
* @return the label of the reference prefix part of the given {@link Usage} if there is one, an empty string
* otherwise.
*/
private void getReferenceUsagePrefix(Usage usage, StringBuilder label) {
if (usage.isIsReference() && !(usage instanceof AttributeUsage)) {
// AttributeUsage are always referential, so no need to add the ref keyword
label.append(LabelConstants.REF + LabelConstants.SPACE);
}
}

/**
* Return the label of the typing part of the given {@link Element}.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
/*******************************************************************************
* Copyright (c) 2024 Obeo.
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.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.syson.services;

import static org.junit.jupiter.api.Assertions.assertEquals;

import org.eclipse.sirius.components.core.api.IFeedbackMessageService;
import org.eclipse.syson.sysml.AttributeUsage;
import org.eclipse.syson.sysml.FeatureDirectionKind;
import org.eclipse.syson.sysml.SysmlFactory;
import org.eclipse.syson.sysml.Usage;
import org.eclipse.syson.sysml.helper.LabelConstants;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

/**
* Label services tests.
*
* @author jmallet
*/
public class LabelServiceTest extends AbstractServiceTest {

private LabelService labelService;

@BeforeEach
void beforeEach() {
this.labelService = new LabelService(new IFeedbackMessageService.NoOp());
}

@DisplayName("Check Usage prefix label with default properties")
@Test
void testUsagePrefixLabelWithDefaultProperties() {
Usage usageWithNoDirection = SysmlFactory.eINSTANCE.createUsage();

assertEquals(LabelConstants.REF + LabelConstants.SPACE, this.labelService.getUsagePrefix(usageWithNoDirection));
}

@DisplayName("Check Usage prefix label with abstract property")
@Test
void testUsagePrefixLabelWithAbstractProperty() {
Usage usageWithNoDirection = SysmlFactory.eINSTANCE.createUsage();

usageWithNoDirection.setIsAbstract(true);
assertEquals(LabelConstants.ABSTRACT + LabelConstants.SPACE + LabelConstants.REF + LabelConstants.SPACE, this.labelService.getUsagePrefix(usageWithNoDirection));
}

@DisplayName("Check Usage prefix label with derived property")
@Test
void testUsagePrefixLabelWithDerivedProperty() {
Usage usageWithNoDirection = SysmlFactory.eINSTANCE.createUsage();

usageWithNoDirection.setIsDerived(true);
assertEquals(LabelConstants.DERIVED + LabelConstants.SPACE + LabelConstants.REF + LabelConstants.SPACE, this.labelService.getUsagePrefix(usageWithNoDirection));
}

@DisplayName("Check Usage prefix label with end property")
@Test
void testUsagePrefixLabelWithEndProperty() {
Usage usageWithNoDirection = SysmlFactory.eINSTANCE.createUsage();

usageWithNoDirection.setIsEnd(true);
assertEquals(LabelConstants.END + LabelConstants.SPACE + LabelConstants.REF + LabelConstants.SPACE, this.labelService.getUsagePrefix(usageWithNoDirection));
}

@DisplayName("Check Usage prefix label with readonly property")
@Test
void testUsagePrefixLabelWithReadOnlyProperty() {
Usage usageWithNoDirection = SysmlFactory.eINSTANCE.createUsage();

usageWithNoDirection.setIsReadOnly(true);
assertEquals(LabelConstants.READ_ONLY + LabelConstants.SPACE + LabelConstants.REF + LabelConstants.SPACE, this.labelService.getUsagePrefix(usageWithNoDirection));
}

@DisplayName("Check Usage prefix label with readonly property")
@Test
void testUsagePrefixLabelWithVariationProperty() {
Usage usageWithNoDirection = SysmlFactory.eINSTANCE.createUsage();

usageWithNoDirection.setIsVariation(true);
assertEquals(LabelConstants.VARIATION + LabelConstants.SPACE + LabelConstants.REF + LabelConstants.SPACE, this.labelService.getUsagePrefix(usageWithNoDirection));
}

@DisplayName("Check Usage prefix label with many custom properties")
@Test
void testUsagePrefixLabelWithManyCustomProperties() {
Usage usageWithNoDirection = SysmlFactory.eINSTANCE.createUsage();
usageWithNoDirection.setIsAbstract(true);
usageWithNoDirection.setIsDerived(true);
usageWithNoDirection.setIsEnd(true);
usageWithNoDirection.setIsReadOnly(true);
usageWithNoDirection.setIsVariation(true);

assertEquals(LabelConstants.ABSTRACT + LabelConstants.SPACE + LabelConstants.VARIATION + LabelConstants.SPACE + LabelConstants.READ_ONLY + LabelConstants.SPACE
+ LabelConstants.DERIVED + LabelConstants.SPACE
+ LabelConstants.END
+ LabelConstants.SPACE + LabelConstants.REF + LabelConstants.SPACE, this.labelService.getUsagePrefix(usageWithNoDirection));
}

@DisplayName("Check Usage prefix label with direction properties")
@Test
void testUsagePrefixLabelWithDirectionProperties() {
Usage usageWithDirection = SysmlFactory.eINSTANCE.createUsage();

usageWithDirection.setDirection(FeatureDirectionKind.IN);
assertEquals(LabelConstants.IN + LabelConstants.SPACE + LabelConstants.REF + LabelConstants.SPACE, this.labelService.getUsagePrefix(usageWithDirection));

usageWithDirection.setDirection(FeatureDirectionKind.OUT);
assertEquals(LabelConstants.OUT + LabelConstants.SPACE + LabelConstants.REF + LabelConstants.SPACE, this.labelService.getUsagePrefix(usageWithDirection));

usageWithDirection.setDirection(FeatureDirectionKind.INOUT);
assertEquals(LabelConstants.INOUT + LabelConstants.SPACE + LabelConstants.REF + LabelConstants.SPACE, this.labelService.getUsagePrefix(usageWithDirection));
}

@DisplayName("Check AttributeUsage prefix label with default properties")
@Test
void testAttributeUsagePrefixLabelWithDefaultProperties() {
AttributeUsage attributeUsageWithNoDirection = SysmlFactory.eINSTANCE.createAttributeUsage();

assertEquals("", this.labelService.getUsagePrefix(attributeUsageWithNoDirection));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import org.eclipse.syson.services.grammars.DirectEditParser;
import org.eclipse.syson.sysml.AcceptActionUsage;
import org.eclipse.syson.sysml.ActionUsage;
import org.eclipse.syson.sysml.AttributeUsage;
import org.eclipse.syson.sysml.Documentation;
import org.eclipse.syson.sysml.Element;
import org.eclipse.syson.sysml.Expression;
Expand Down Expand Up @@ -90,10 +89,7 @@ public boolean showIcon(Object object) {
*/
public String getCompartmentItemLabel(Usage usage) {
StringBuilder label = new StringBuilder();
if (usage.isIsReference() && !(usage instanceof AttributeUsage)) {
// AttributeUsage are always referential, so no need to add the ref keyword
label.append("ref" + LabelConstants.SPACE);
}
label.append(this.getUsagePrefix(usage));
String declaredName = usage.getDeclaredName();
if (declaredName != null) {
label.append(declaredName);
Expand Down
Loading

0 comments on commit 291099a

Please sign in to comment.