Skip to content

Commit 42ccaa4

Browse files
author
Pascal Dal Farra
committed
feat: add arrow length parameter
1 parent ceecb4e commit 42ccaa4

File tree

4 files changed

+66
-20
lines changed

4 files changed

+66
-20
lines changed

spring-statemachine-uml/src/main/java/org/springframework/statemachine/plantuml/PlantUmlWriter.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -665,7 +665,7 @@ private void processPseudoStatesTransition(
665665
}
666666
sb.append("""
667667
%s%s
668-
%s%s -%s%s-> %s %s
668+
%s%s -%s%s%s> %s %s
669669
"""
670670
.formatted(
671671
indent,
@@ -681,6 +681,7 @@ private void processPseudoStatesTransition(
681681
&& currentContextTransition.getTarget() == target
682682
)
683683
),
684+
plantUmlWriterParameters.getArrowLength(source, target),
684685
historyIdGetter == null ? targetState.getId() : historyIdGetter.getId(targetState),
685686
TransactionHelper.getTransitionDescription(transition, plantUmlWriterParameters)
686687
)
@@ -761,13 +762,14 @@ private void addTransition(
761762
) {
762763
if (!plantUmlWriterParameters.isTransitionIgnored(source, target)) {
763764
sb.append("""
764-
%s%s -%s%s-> %s %s
765+
%s%s -%s%s%s> %s %s
765766
"""
766767
.formatted(
767768
indent,
768769
sourceLabel,
769770
source == null ? "" : plantUmlWriterParameters.getDirection(source, target),
770771
arrowColor,
772+
plantUmlWriterParameters.getArrowLength(source, target),
771773
target,
772774
TransactionHelper.getTransitionDescription(transition, plantUmlWriterParameters)
773775
)

spring-statemachine-uml/src/main/java/org/springframework/statemachine/plantuml/PlantUmlWriterParameters.java

Lines changed: 55 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,34 @@ private String getStateDiagramSettings() {
7878
: stateDiagramSettings;
7979
}
8080

81+
@Value
82+
static class Arrow {
83+
84+
/**
85+
* The 'default' arrow:
86+
* <UL>
87+
* <LI>Direction is {@link Direction#DOWN}</LI>
88+
* <LI>Lenght is 1</LI>
89+
* </UL>
90+
*/
91+
static final Arrow DEFAULT = Arrow.of(Direction.DOWN);
92+
93+
Direction direction;
94+
int length;
95+
96+
public static Arrow of(Direction direction) {
97+
return new Arrow(direction, 1);
98+
}
99+
100+
public static Arrow of(Direction direction, int length) {
101+
return new Arrow(direction, length);
102+
}
103+
104+
public String getLengthAsString() {
105+
return "-".repeat(length);
106+
}
107+
}
108+
81109
/**
82110
* Direction of an arrow connecting 2 States
83111
*/
@@ -107,10 +135,15 @@ public int compareTo(Connection<S> o) {
107135
/**
108136
* Map of ( (sourceSate, targetState) -> Direction )
109137
*/
110-
private final Map<Connection<S>, Direction> arrows = new HashMap<>();
138+
private final Map<Connection<S>, Arrow> arrows = new HashMap<>();
139+
140+
public PlantUmlWriterParameters<S> arrow(S source, Direction direction, S target) {
141+
arrows.put(new Connection<>(source, target), Arrow.of(direction));
142+
return this;
143+
}
111144

112-
public PlantUmlWriterParameters<S> arrowDirection(S source, Direction direction, S target) {
113-
arrows.put(new Connection<>(source, target), direction);
145+
public PlantUmlWriterParameters<S> arrow(S source, Direction direction, S target, int length) {
146+
arrows.put(new Connection<>(source, target), Arrow.of(direction, length));
114147
return this;
115148
}
116149

@@ -122,13 +155,13 @@ public PlantUmlWriterParameters<S> arrowDirection(S source, Direction direction,
122155
* @param target target State
123156
* @return Direction.name()
124157
*/
125-
String getDirection(S source, S target) {
158+
private Arrow getArrow(S source, S target) {
126159
if (source == null && target == null) {
127160
throw new IllegalArgumentException("source and target state cannot both be null!");
128161
}
129162
Connection<S> sourceAndTarget = new Connection<>(source, target);
130163
if (arrows.containsKey(sourceAndTarget)) {
131-
return arrows.get(sourceAndTarget).name().toLowerCase();
164+
return arrows.get(sourceAndTarget);
132165
}
133166
Connection<S> sourceOnly = new Connection<>(source, null);
134167
Connection<S> targetOnly = new Connection<>(null, target);
@@ -140,14 +173,25 @@ String getDirection(S source, S target) {
140173
"Two 'unary' 'arrowDirection' rules found for (%s, %s) with DIFFERENT values! Using 'target' rule!",
141174
source, target
142175
));
143-
return arrows.get(targetOnly).name().toLowerCase();
176+
return arrows.get(targetOnly);
144177
} else if (arrows.containsKey(sourceOnly)) {
145-
return arrows.get(sourceOnly).name().toLowerCase();
178+
return arrows.get(sourceOnly);
146179
} else if (arrows.containsKey(targetOnly)) {
147-
return arrows.get(targetOnly).name().toLowerCase();
180+
return arrows.get(targetOnly);
148181
} else {
149-
return Direction.DOWN.name().toLowerCase();
182+
return Arrow.DEFAULT;
183+
}
184+
}
185+
186+
String getDirection(S source, S target) {
187+
return getArrow(source, target).getDirection().name().toLowerCase();
188+
}
189+
190+
String getArrowLength(S source, S target) {
191+
if(source == null) {
192+
return Arrow.DEFAULT.getLengthAsString();
150193
}
194+
return getArrow(source, target).getLengthAsString();
151195
}
152196

153197
/**
@@ -211,7 +255,9 @@ public boolean isTransitionIgnored(S source, S target) {
211255
@Getter
212256
@EqualsAndHashCode
213257
static class LabelDecorator {
258+
@Builder.Default
214259
private String prefix = "";
260+
@Builder.Default
215261
private String suffix = "";
216262

217263
String decorate(String label) {

spring-statemachine-uml/src/test/java/org/springframework/statemachine/plantuml/PlantUmlWriterTest.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,10 @@ public static Stream<Arguments> plantUmlTestMethodSource() {
7878
Arguments.of("org/springframework/statemachine/uml/simple-history-shallow", null),
7979
Arguments.of("org/springframework/statemachine/uml/simple-junction", null),
8080
Arguments.of("org/springframework/statemachine/uml/simple-localtransition",
81-
// add some parameters to make diagram more readable
81+
// add some parameters to make the diagram more readable
8282
new PlantUmlWriterParameters<String>()
83-
.arrowDirection("S22", UP, "S2")
84-
.arrowDirection("S21", UP, "S2")
85-
.arrowDirection("S22", UP, "S2")
86-
.arrowDirection("S21", UP, "S2")
83+
.arrow("S22", UP, "S2", 2)
84+
.arrow("S21", UP, "S2", 2)
8785
),
8886
// It seems Spring statemachine UML parser creates duplicated transitions!
8987
// Arguments.of("org/springframework/statemachine/uml/simple-root-regions", null),

spring-statemachine-uml/src/test/resources/org/springframework/statemachine/uml/simple-localtransition.puml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ state S2 {
1616

1717
[*] --> S1
1818
S1 -down-> S2 : E1
19-
S21 -up-> S2 : E22
20-
S21 -up-> S2 : E32
21-
S22 -up-> S2 : E23
22-
S22 -up-> S2 : E33
19+
S21 -up--> S2 : E22
20+
S21 -up--> S2 : E32
21+
S22 -up--> S2 : E23
22+
S22 -up--> S2 : E33
2323
S2 -down-> S21 : E20
2424
S2 -down-> S21 : E30
2525
S2 -down-> S22 : E21

0 commit comments

Comments
 (0)