Skip to content

Commit

Permalink
[407] Add support for edges' sizeComputationExpression
Browse files Browse the repository at this point in the history
Also adjust some APIs to reduce the number of unneeded calls to
VariablesManager.getVariables() which is not free when repeated in hot
loops like during a render.

Fixes #407.

Bug: #407
Signed-off-by: Pierre-Charles David <pierre-charles.david@obeo.fr>
  • Loading branch information
pcdavid committed Mar 24, 2021
1 parent adb643c commit 94062cb
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import org.eclipse.sirius.viewpoint.description.ComputedColor;
import org.eclipse.sirius.viewpoint.description.FixedColor;
import org.eclipse.sirius.web.interpreter.AQLInterpreter;
import org.eclipse.sirius.web.representations.VariableManager;

/**
* Provides a unified color format from a Sirius {@link ColorDescription}.
Expand All @@ -32,11 +31,11 @@ public class ColorDescriptionConverter {

private final AQLInterpreter interpreter;

private final VariableManager variableManager;
private final Map<String, Object> variables;

public ColorDescriptionConverter(AQLInterpreter interpreter, VariableManager variableManager) {
public ColorDescriptionConverter(AQLInterpreter interpreter, Map<String, Object> variables) {
this.interpreter = Objects.requireNonNull(interpreter);
this.variableManager = Objects.requireNonNull(variableManager);
this.variables = Objects.requireNonNull(variables);
}

public String convert(ColorDescription colorDescription) {
Expand All @@ -46,11 +45,9 @@ public String convert(ColorDescription colorDescription) {
value = this.toHex(fixedColor.getRed(), fixedColor.getGreen(), fixedColor.getBlue());
} else if (colorDescription instanceof ComputedColor) {
ComputedColor computedColor = (ComputedColor) colorDescription;

Map<String, Object> variables = this.variableManager.getVariables();
int red = this.interpreter.evaluateExpression(variables, computedColor.getRed()).asInt().orElse(0);
int green = this.interpreter.evaluateExpression(variables, computedColor.getGreen()).asInt().orElse(0);
int blue = this.interpreter.evaluateExpression(variables, computedColor.getBlue()).asInt().orElse(0);
int red = this.interpreter.evaluateExpression(this.variables, computedColor.getRed()).asInt().orElse(0);
int green = this.interpreter.evaluateExpression(this.variables, computedColor.getGreen()).asInt().orElse(0);
int blue = this.interpreter.evaluateExpression(this.variables, computedColor.getBlue()).asInt().orElse(0);

value = this.toHex(red, green, blue);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
*******************************************************************************/
package org.eclipse.sirius.web.compat.diagrams;

import java.util.Map;
import java.util.Objects;
import java.util.function.Function;

Expand Down Expand Up @@ -64,15 +65,15 @@ private INodeStyle getNodeStyle(VariableManager variableManager, ContainerStyleD
}

private RectangularNodeStyle createRectangularNodeStyle(VariableManager variableManager, FlatContainerStyleDescription flatContainerStyleDescription) {
ColorDescriptionConverter backgroundColorProvider = new ColorDescriptionConverter(this.interpreter, variableManager);
ColorDescriptionConverter borderColorProvider = new ColorDescriptionConverter(this.interpreter, variableManager);
Map<String, Object> variables = variableManager.getVariables();
ColorDescriptionConverter colorProvider = new ColorDescriptionConverter(this.interpreter, variables);

String color = backgroundColorProvider.convert(flatContainerStyleDescription.getBackgroundColor());
String borderColor = borderColorProvider.convert(flatContainerStyleDescription.getBorderColor());
String color = colorProvider.convert(flatContainerStyleDescription.getBackgroundColor());
String borderColor = colorProvider.convert(flatContainerStyleDescription.getBorderColor());

LineStyle borderStyle = new LineStyleConverter().getStyle(flatContainerStyleDescription.getBorderLineStyle());

Result result = this.interpreter.evaluateExpression(variableManager.getVariables(), flatContainerStyleDescription.getBorderSizeComputationExpression());
Result result = this.interpreter.evaluateExpression(variables, flatContainerStyleDescription.getBorderSizeComputationExpression());
int borderSize = result.asInt().getAsInt();

// @formatter:off
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
*******************************************************************************/
package org.eclipse.sirius.web.compat.diagrams;

import java.util.Map;
import java.util.Objects;
import java.util.function.Function;

Expand Down Expand Up @@ -46,11 +47,12 @@ public EdgeStyle apply(VariableManager variableManager) {
}

private EdgeStyle getEdgeStyle(VariableManager variableManager, EdgeStyleDescription style) {
ColorDescriptionConverter colorDescriptionConverter = new ColorDescriptionConverter(this.interpreter, variableManager);
Map<String, Object> variables = variableManager.getVariables();
ColorDescriptionConverter colorDescriptionConverter = new ColorDescriptionConverter(this.interpreter, variables);
LineStyleConverter lineStyleConverter = new LineStyleConverter();
ArrowStyleConverter arrowStyleConverter = new ArrowStyleConverter();

int size = 1;
int size = this.interpreter.evaluateExpression(variables, style.getSizeComputationExpression()).asInt().orElse(1);
LineStyle lineStyle = lineStyleConverter.getStyle(style.getLineStyle());
ArrowStyle sourceArrow = arrowStyleConverter.getStyle(style.getSourceArrow());
ArrowStyle targetArrow = arrowStyleConverter.getStyle(style.getTargetArrow());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public LabelStyleDescription convert(org.eclipse.sirius.viewpoint.description.st
};

Function<VariableManager, String> colorProvider = variableManager -> {
return new ColorDescriptionConverter(this.interpreter, variableManager).convert(labelStyleDescription.getLabelColor());
return new ColorDescriptionConverter(this.interpreter, variableManager.getVariables()).convert(labelStyleDescription.getLabelColor());
};

// @formatter:off
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,10 @@ private INodeStyle getNodeStyle(VariableManager variableManager, NodeStyleDescri
}

private RectangularNodeStyle createRectangularNodeStyle(VariableManager variableManager, SquareDescription squareDescription) {
ColorDescriptionConverter colorProvider = new ColorDescriptionConverter(this.interpreter, variableManager);
ColorDescriptionConverter borderColorProvider = new ColorDescriptionConverter(this.interpreter, variableManager);
ColorDescriptionConverter colorProvider = new ColorDescriptionConverter(this.interpreter, variableManager.getVariables());

String color = colorProvider.convert(squareDescription.getColor());
String borderColor = borderColorProvider.convert(squareDescription.getBorderColor());
String borderColor = colorProvider.convert(squareDescription.getBorderColor());

LineStyle borderStyle = new LineStyleConverter().getStyle(squareDescription.getBorderLineStyle());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@

import static org.assertj.core.api.Assertions.assertThat;

import java.util.Collections;
import java.util.List;

import org.eclipse.emf.ecore.EcorePackage;
import org.eclipse.sirius.viewpoint.description.DescriptionFactory;
import org.eclipse.sirius.viewpoint.description.FixedColor;
import org.eclipse.sirius.web.interpreter.AQLInterpreter;
import org.eclipse.sirius.web.representations.VariableManager;
import org.junit.Test;

/**
Expand All @@ -38,8 +38,7 @@ public void testConvertFixedColor() {
fixedColor.setBlue(16);

AQLInterpreter interpreter = new AQLInterpreter(List.of(), List.of(EcorePackage.eINSTANCE));
VariableManager variableManager = new VariableManager();
ColorDescriptionConverter colorProvider = new ColorDescriptionConverter(interpreter, variableManager);
ColorDescriptionConverter colorProvider = new ColorDescriptionConverter(interpreter, Collections.emptyMap());
String color = colorProvider.convert(fixedColor);
assertThat(color).isEqualTo("#0f0110"); //$NON-NLS-1$
}
Expand Down

0 comments on commit 94062cb

Please sign in to comment.