Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: display arrow in SVG export #129

Merged
merged 1 commit into from
Aug 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions java/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@

import static io.process.analytics.tools.bpmn.generator.internal.StringUtils.defaultIfNull;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import io.process.analytics.tools.bpmn.generator.converter.AlgoToDisplayModelConverter;
import io.process.analytics.tools.bpmn.generator.converter.waypoint.Direction;
import io.process.analytics.tools.bpmn.generator.model.Diagram;
import io.process.analytics.tools.bpmn.generator.model.Grid;
import io.process.analytics.tools.bpmn.generator.model.ShapeType;
Expand Down Expand Up @@ -51,6 +54,7 @@ public String export(Grid grid, Diagram diagram) {
final String colorEgeStroke = "Black";
final int edgeStrokeWidth = 2;
final double edgeStrokeOpacity = 0.5;
final int edgeArrowHeight = 10;

for (DisplayFlowNode flowNode : model.flowNodes) {
DisplayDimension flowNodeDimension = flowNode.dimension;
Expand Down Expand Up @@ -158,16 +162,41 @@ else if (flowNode.type == ShapeType.GATEWAY) {
.append(" fill=\"none\"")
.append(" />\n");

// highlight target point
DisplayPoint targetPoint = edge.wayPoints.get(edge.wayPoints.size() - 1);
content.append("<circle")
.append(" cx=\"").append(targetPoint.x).append("\"")
.append(" cy=\"").append(targetPoint.y).append("\"")
.append(" r=\"").append("5").append("\"")
// edge arrow
List<DisplayPoint> wayPoints = edge.wayPoints;
Direction lastSegmentDirection = detectLastSegmentDirection(wayPoints);
DisplayPoint last = wayPoints.get(wayPoints.size() - 1);

List<DisplayPoint> arrowPoints = new ArrayList<>();

if (lastSegmentDirection == Direction.LeftToRight) {
arrowPoints.add(new DisplayPoint(last.x, last.y)); // top of the arrow
arrowPoints.add(new DisplayPoint(last.x - edgeArrowHeight, last.y + edgeArrowHeight / 2));
arrowPoints.add(new DisplayPoint(last.x - edgeArrowHeight, last.y - edgeArrowHeight / 2));
} else if (lastSegmentDirection == Direction.RightToLeft) {
arrowPoints.add(new DisplayPoint(last.x, last.y)); // top of the arrow
arrowPoints.add(new DisplayPoint(last.x + edgeArrowHeight, last.y + edgeArrowHeight / 2));
arrowPoints.add(new DisplayPoint(last.x + edgeArrowHeight, last.y - edgeArrowHeight / 2));
} else if (lastSegmentDirection == Direction.TopToBottom) {
arrowPoints.add(new DisplayPoint(last.x, last.y)); // top of the arrow
arrowPoints.add(new DisplayPoint(last.x - edgeArrowHeight / 2, last.y - edgeArrowHeight));
arrowPoints.add(new DisplayPoint(last.x + edgeArrowHeight / 2, last.y - edgeArrowHeight));
} else if (lastSegmentDirection == Direction.BottomToTop) {
arrowPoints.add(new DisplayPoint(last.x, last.y)); // top of the arrow
arrowPoints.add(new DisplayPoint(last.x - edgeArrowHeight / 2, last.y + edgeArrowHeight));
arrowPoints.add(new DisplayPoint(last.x + edgeArrowHeight / 2, last.y + edgeArrowHeight));
}

content.append("<polygon")
.append(" stroke=\"").append(colorEgeStroke).append("\"")
.append(" stroke-width=\"").append(edgeStrokeWidth).append("\"")
.append(" stroke-opacity=\"").append(edgeStrokeOpacity).append("\"")
.append(" fill=\"").append(colorEgeStroke).append("\"")
.append(" points=\"")
.append(arrowPoints.stream()
.map(point -> point.x + "," + point.y)
.collect(Collectors.joining(" ")))
.append("\"")
.append(" />\n");
}
}
Expand All @@ -176,4 +205,16 @@ else if (flowNode.type == ShapeType.GATEWAY) {
return content.toString();
}

// here we assume that we have only vertical and horizontal segments, and we have at least 2 points in the list
// visible for testing
static Direction detectLastSegmentDirection(List<DisplayPoint> wayPoints) {
DisplayPoint last = wayPoints.get(wayPoints.size() - 1);
DisplayPoint beforeLast = wayPoints.get(wayPoints.size() - 2);
// horizontal
if (last.x == beforeLast.x) {
return last.y > beforeLast.y ? Direction.TopToBottom : Direction.BottomToTop;
}
// vertical
return last.x > beforeLast.x ? Direction.LeftToRight : Direction.RightToLeft;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright 2024 Bonitasoft S.A.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.process.analytics.tools.bpmn.generator.export;

import io.process.analytics.tools.bpmn.generator.converter.waypoint.Direction;
import io.process.analytics.tools.bpmn.generator.model.display.DisplayPoint;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

import java.util.List;
import java.util.stream.Stream;

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

class SVGExporterTest {

@ParameterizedTest
@MethodSource("provideWaypointsForDirectionDetection")
void detect_last_segment_direction(List<DisplayPoint> waypoints, Direction expectedDirection) {
var direction = SVGExporter.detectLastSegmentDirection(waypoints);
assertThat(direction).isEqualTo(expectedDirection);
}

private static Stream<Arguments> provideWaypointsForDirectionDetection() {
return Stream.of(
Arguments.of(List.of(new DisplayPoint(0, 37), new DisplayPoint(10, 37)), Direction.LeftToRight),
Arguments.of(List.of(new DisplayPoint(40, 58), new DisplayPoint(10, 58)), Direction.RightToLeft),
Arguments.of(List.of(new DisplayPoint(13, 73), new DisplayPoint(13, 137)), Direction.TopToBottom),
Arguments.of(List.of(new DisplayPoint(33, 73), new DisplayPoint(33, 37)), Direction.BottomToTop)
);
}

}

Loading