-
Notifications
You must be signed in to change notification settings - Fork 4
/
AlgoToDisplayModelConverter.java
336 lines (288 loc) · 15.5 KB
/
AlgoToDisplayModelConverter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/*
* Copyright 2020 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.converter;
import java.util.ArrayList;
import java.util.List;
import io.process.analytics.tools.bpmn.generator.model.*;
import lombok.Builder;
import lombok.RequiredArgsConstructor;
import lombok.Singular;
import lombok.ToString;
public class AlgoToDisplayModelConverter {
// TODO this should be fields of the class and configured by the client code
private static final int CELL_WIDTH = 200;
private static final int CELL_HEIGHT = 100;
public DisplayModel convert(Grid grid, Diagram diagram) {
DisplayModel.DisplayModelBuilder model = DisplayModel.builder();
model.width(grid.width() * CELL_WIDTH).height(grid.height() * CELL_HEIGHT);
for (Position position : grid.getPositions()) {
int xOffset = position.getX() * CELL_WIDTH;
int yOffset = position.getY() * CELL_HEIGHT;
int nodeWidth = x(60);
int nodeHeight = y(60);
// TODO manage when not found (should not occur)
Shape shape = diagram.getShapes().stream()
.filter(s -> s.getId().equals(position.getShape()))
.findFirst().get();
String name = shape.getName();
// ensure to have a square shape (i.e. same width and height) for non activity elements
ShapeType shapeType = shape.getType();
if (shapeType == ShapeType.EVENT || shapeType == ShapeType.GATEWAY) {
int nodeDimension = Math.min(nodeWidth, nodeHeight);
if (shapeType == ShapeType.EVENT) {
nodeDimension /= 2;
}
nodeWidth = nodeDimension;
nodeHeight = nodeDimension;
}
int x = xOffset + (CELL_WIDTH - nodeWidth) / 2;
int y = yOffset + (CELL_HEIGHT - nodeHeight) / 2;
DisplayDimension flowNodeDimension = new DisplayDimension(x, y, nodeWidth, nodeHeight);
// Labels positions better work with the SVG export
// BPMN label positions are adjusted in BPMNDiagramRichBuilder
int labelX = xOffset + x(50);
int labelY = yOffset + y(50);
if (shapeType == ShapeType.EVENT) { // put the label under the shape
labelY = (int) (y + nodeHeight * 1.5);
} else if (shapeType == ShapeType.GATEWAY) { // put the label on the top left of the shape
labelX = (int) (x - nodeWidth * 0.5);
labelY = (int) (y - nodeHeight * 0.5);
}
DisplayDimension labelDimension = new DisplayDimension(labelX, labelY, nodeWidth, nodeHeight);
DisplayLabel label = new DisplayLabel(name, y(16), labelDimension);
model.flowNode(DisplayFlowNode.builder().bpmnElementId(shape.getId())
.dimension(flowNodeDimension)
.label(label)
.type(shapeType)
.rx(y(10)).strokeWidth(y(5)).build());
}
diagram.getEdges()
.stream()
.map(edge -> new DisplayEdge(edge.getId(), inferWayPoints(edge, grid, model.flowNodes)))
.forEach(model::edge);
return model.build();
}
private List<DisplayPoint> inferWayPoints(Edge edge, Grid grid, List<DisplayFlowNode> flowNodes) {
Position positionFrom = getPositionOfShape(grid, edge.getFrom());
Position positionTo = getPositionOfShape(grid, edge.getTo());
EdgeDirection edgeDirection = computeEdgeDirection(positionFrom, positionTo, grid);
DisplayFlowNode flowNodeFrom = getFlowNode(flowNodes, positionFrom.getShape());
DisplayFlowNode flowNodeTo = getFlowNode(flowNodes, positionTo.getShape());
return computeWayPoints(edgeDirection, flowNodeFrom, flowNodeTo);
}
private List<DisplayPoint> computeWayPoints(EdgeDirection edgeDirection, DisplayFlowNode flowNodeFrom, DisplayFlowNode flowNodeTo) {
DisplayDimension dimensionFrom = flowNodeFrom.dimension;
DisplayDimension dimensionTo = flowNodeTo.dimension;
List<DisplayPoint> wayPoints = new ArrayList<>();
switch (edgeDirection) {
case HorizontalLeftToRight:
wayPoints.add(new DisplayPoint(dimensionFrom.x + dimensionFrom.width, dimensionFrom.y + dimensionFrom.height / 2));
wayPoints.add(new DisplayPoint(dimensionTo.x, dimensionTo.y + dimensionTo.height / 2));
break;
case HorizontalRightToLeft:
wayPoints.add(new DisplayPoint(dimensionFrom.x, dimensionFrom.y + dimensionFrom.height / 2));
wayPoints.add(new DisplayPoint(dimensionTo.x + dimensionTo.width, dimensionTo.y + dimensionTo.height / 2));
break;
case BottomLeftToTopRight_FirstHorizontal: // horizontal then vertical
wayPoints.add(new DisplayPoint(dimensionFrom.x + dimensionFrom.width, dimensionFrom.y + dimensionFrom.height / 2));
wayPoints.add(new DisplayPoint(dimensionTo.x + dimensionTo.width / 2, dimensionFrom.y + dimensionFrom.height / 2));
wayPoints.add(new DisplayPoint(dimensionTo.x + dimensionTo.width / 2, dimensionTo.y + dimensionTo.height));
break;
case BottomLeftToTopRight_FirstVertical: // vertical then horizontal
wayPoints.add(new DisplayPoint(dimensionFrom.x + dimensionFrom.width / 2, dimensionFrom.y));
wayPoints.add(new DisplayPoint(dimensionFrom.x + dimensionFrom.width / 2, dimensionTo.y + dimensionTo.height / 2));
wayPoints.add(new DisplayPoint(dimensionTo.x, dimensionTo.y + dimensionTo.height / 2));
break;
case BottomRightToTopLeft_FirstHorizontal: // horizontal then vertical
wayPoints.add(new DisplayPoint(dimensionFrom.x, dimensionFrom.y + dimensionFrom.height / 2));
wayPoints.add(new DisplayPoint(dimensionTo.x + dimensionTo.width / 2,
dimensionFrom.y + dimensionFrom.height / 2));
wayPoints.add(new DisplayPoint(dimensionTo.x + dimensionTo.width / 2, dimensionTo.y + dimensionTo.height));
break;
case BottomRightToTopLeft_FirstVertical: // vertical then horizontal
wayPoints.add(new DisplayPoint(dimensionFrom.x + dimensionFrom.width / 2, dimensionFrom.y));
wayPoints.add(new DisplayPoint(dimensionFrom.x + dimensionFrom.width / 2, dimensionTo.y + dimensionTo.height / 2));
wayPoints.add(new DisplayPoint(dimensionTo.x + dimensionTo.width / 2, dimensionTo.y + dimensionTo.height / 2));
break;
case TopLeftToBottomRight_FirstHorizontal: // horizontal then vertical
wayPoints.add(new DisplayPoint(dimensionFrom.x + dimensionFrom.width, dimensionFrom.y + dimensionFrom.height / 2));
wayPoints.add(new DisplayPoint(dimensionTo.x + dimensionTo.width / 2, dimensionFrom.y + dimensionFrom.height / 2));
wayPoints.add(new DisplayPoint(dimensionTo.x + dimensionTo.width / 2, dimensionTo.y));
break;
case TopLeftToBottomRight_FirstVertical: // vertical then horizontal
wayPoints.add(new DisplayPoint(dimensionFrom.x + dimensionFrom.width / 2, dimensionFrom.y + dimensionTo.height));
wayPoints.add(new DisplayPoint(dimensionFrom.x + dimensionFrom.width / 2, dimensionTo.y + dimensionTo.height / 2));
wayPoints.add(new DisplayPoint(dimensionTo.x, dimensionTo.y + dimensionTo.height / 2));
break;
case TopRightToBottomLeft_FirstHorizontal: // horizontal then vertical
wayPoints.add(new DisplayPoint(dimensionFrom.x, dimensionFrom.y + dimensionFrom.height / 2));
wayPoints.add(new DisplayPoint(dimensionTo.x + dimensionTo.width / 2, dimensionFrom.y + dimensionFrom.height / 2));
wayPoints.add(new DisplayPoint(dimensionTo.x + dimensionTo.width / 2, dimensionTo.y));
break;
case TopRightToBottomLeft_FirstVertical: // vertical then horizontal
wayPoints.add(new DisplayPoint(dimensionFrom.x + dimensionFrom.width / 2, dimensionFrom.y + dimensionTo.height));
wayPoints.add(new DisplayPoint(dimensionFrom.x + dimensionFrom.width / 2, dimensionTo.y + dimensionTo.height / 2));
wayPoints.add(new DisplayPoint(dimensionTo.x + dimensionTo.width / 2, dimensionTo.y + dimensionTo.height / 2));
break;
case VerticalBottomToTop:
wayPoints.add(new DisplayPoint(dimensionFrom.x + dimensionFrom.width / 2, dimensionFrom.y));
wayPoints.add(new DisplayPoint(dimensionTo.x + dimensionTo.width / 2, dimensionTo.y + dimensionTo.height));
break;
case VerticalTopToBottom:
wayPoints.add(new DisplayPoint(dimensionFrom.x + dimensionFrom.width / 2, dimensionFrom.y + dimensionFrom.height));
wayPoints.add(new DisplayPoint(dimensionTo.x + dimensionTo.width / 2, dimensionTo.y));
break;
default:
// do nothing
}
return wayPoints;
}
private DisplayFlowNode getFlowNode(List<DisplayFlowNode> flowNodes, String flowNodeId) {
return flowNodes.stream()
.filter(f -> flowNodeId.equals(f.bpmnElementId))
.findFirst()
.get(); // always exist, otherwise error occur on flow node generation
}
private Position getPositionOfShape(Grid grid, String shapeId) {
return grid.getPositions()
.stream()
.filter(p -> shapeId.equals(p.getShape()))
.findFirst()
.get(); // always exist, otherwise error occur on flow node generation
}
// visible for testing
static EdgeDirection computeEdgeDirection(Position positionFrom, Position positionTo, Grid grid) {
EdgeDirection edgeDirection = null;
if (positionFrom.getX() == positionTo.getX()) {
if (positionFrom.getY() < positionTo.getY()) {
edgeDirection = EdgeDirection.VerticalTopToBottom;
}
else {
edgeDirection = EdgeDirection.VerticalBottomToTop;
}
}
else if (positionFrom.getY() == positionTo.getY()) {
if (positionFrom.getX() < positionTo.getX()) {
edgeDirection = EdgeDirection.HorizontalLeftToRight;
}
else {
edgeDirection = EdgeDirection.HorizontalRightToLeft;
}
} else if (positionFrom.getX() < positionTo.getX()) {
if (positionFrom.getY() < positionTo.getY()) {
// check if there is a shape at the right of 'positionFrom'
boolean shapeExistAtRightPositionFrom = grid.getPositions()
.stream()
.filter(p -> p.getY() == positionFrom.getY())
.anyMatch(p -> p.getX() == positionFrom.getX() + 1);
edgeDirection = shapeExistAtRightPositionFrom ? EdgeDirection.TopLeftToBottomRight_FirstVertical : EdgeDirection.TopLeftToBottomRight_FirstHorizontal;
}
else {
// check if there is a shape above 'positionFrom'
boolean shapeExistAbovePositionFrom = grid.getPositions()
.stream()
.filter(p -> p.getX() == positionFrom.getX())
.anyMatch(p -> p.getY() == positionFrom.getY() - 1);
edgeDirection = shapeExistAbovePositionFrom ? EdgeDirection.BottomLeftToTopRight_FirstHorizontal : EdgeDirection.BottomLeftToTopRight_FirstVertical;
}
} else {
if (positionFrom.getY() < positionTo.getY()) {
// check if there is a shape at the left of 'positionFrom'
boolean shapeExistAtLeftPositionFrom = grid.getPositions()
.stream()
.filter(p -> p.getY() == positionFrom.getY())
.anyMatch(p -> p.getX() == positionFrom.getX() - 1);
edgeDirection = shapeExistAtLeftPositionFrom ? EdgeDirection.TopRightToBottomLeft_FirstVertical : EdgeDirection.TopRightToBottomLeft_FirstHorizontal;
} else {
edgeDirection = EdgeDirection.BottomRightToTopLeft_FirstHorizontal;
// check if there is a shape above 'positionFrom'
boolean shapeExistAbovePositionFrom = grid.getPositions()
.stream()
.filter(p -> p.getX() == positionFrom.getX())
.anyMatch(p -> p.getY() == positionFrom.getY() - 1);
edgeDirection = shapeExistAbovePositionFrom ? EdgeDirection.BottomRightToTopLeft_FirstHorizontal : EdgeDirection.BottomRightToTopLeft_FirstVertical;
}
}
return edgeDirection;
}
// visible for testing
static enum EdgeDirection {
HorizontalLeftToRight,
HorizontalRightToLeft,
VerticalBottomToTop,
VerticalTopToBottom,
BottomLeftToTopRight_FirstHorizontal,
BottomLeftToTopRight_FirstVertical,
BottomRightToTopLeft_FirstHorizontal,
BottomRightToTopLeft_FirstVertical,
TopLeftToBottomRight_FirstHorizontal,
TopLeftToBottomRight_FirstVertical,
TopRightToBottomLeft_FirstHorizontal,
TopRightToBottomLeft_FirstVertical,
}
private int x(int percentage) {
return CELL_WIDTH * percentage / 100;
}
private int y(int percentage) {
return CELL_HEIGHT * percentage / 100;
}
@RequiredArgsConstructor
@Builder
public static class DisplayModel {
public final int width;
public final int height;
@Singular
public final List<DisplayFlowNode> flowNodes;
@Singular
public final List<DisplayEdge> edges;
}
@RequiredArgsConstructor
@Builder
public static class DisplayFlowNode {
public final String bpmnElementId;
public final DisplayDimension dimension;
public final DisplayLabel label;
// for non BPMN exporters only
public final ShapeType type;
public final int rx;
public final int strokeWidth;
}
@RequiredArgsConstructor
@Builder
public static class DisplayEdge {
public final String bpmnElementId;
public final List<DisplayPoint> wayPoints;
}
@RequiredArgsConstructor
public static class DisplayDimension {
public final int x;
public final int y;
public final int width;
public final int height;
}
@RequiredArgsConstructor
public static class DisplayLabel {
public final String text; // for non BPMN exporters only
public final int fontSize;
public final DisplayDimension dimension;
}
@RequiredArgsConstructor
@ToString
public static class DisplayPoint {
public final int x;
public final int y;
}
}