Skip to content

Commit

Permalink
feat: add arrow length parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
Pascal Dal Farra committed Nov 6, 2024
1 parent ceecb4e commit 42ccaa4
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -665,7 +665,7 @@ private void processPseudoStatesTransition(
}
sb.append("""
%s%s
%s%s -%s%s-> %s %s
%s%s -%s%s%s> %s %s
"""
.formatted(
indent,
Expand All @@ -681,6 +681,7 @@ private void processPseudoStatesTransition(
&& currentContextTransition.getTarget() == target
)
),
plantUmlWriterParameters.getArrowLength(source, target),
historyIdGetter == null ? targetState.getId() : historyIdGetter.getId(targetState),
TransactionHelper.getTransitionDescription(transition, plantUmlWriterParameters)
)
Expand Down Expand Up @@ -761,13 +762,14 @@ private void addTransition(
) {
if (!plantUmlWriterParameters.isTransitionIgnored(source, target)) {
sb.append("""
%s%s -%s%s-> %s %s
%s%s -%s%s%s> %s %s
"""
.formatted(
indent,
sourceLabel,
source == null ? "" : plantUmlWriterParameters.getDirection(source, target),
arrowColor,
plantUmlWriterParameters.getArrowLength(source, target),
target,
TransactionHelper.getTransitionDescription(transition, plantUmlWriterParameters)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,34 @@ private String getStateDiagramSettings() {
: stateDiagramSettings;
}

@Value
static class Arrow {

/**
* The 'default' arrow:
* <UL>
* <LI>Direction is {@link Direction#DOWN}</LI>
* <LI>Lenght is 1</LI>
* </UL>
*/
static final Arrow DEFAULT = Arrow.of(Direction.DOWN);

Direction direction;
int length;

public static Arrow of(Direction direction) {
return new Arrow(direction, 1);
}

public static Arrow of(Direction direction, int length) {
return new Arrow(direction, length);
}

public String getLengthAsString() {
return "-".repeat(length);
}
}

/**
* Direction of an arrow connecting 2 States
*/
Expand Down Expand Up @@ -107,10 +135,15 @@ public int compareTo(Connection<S> o) {
/**
* Map of ( (sourceSate, targetState) -> Direction )
*/
private final Map<Connection<S>, Direction> arrows = new HashMap<>();
private final Map<Connection<S>, Arrow> arrows = new HashMap<>();

public PlantUmlWriterParameters<S> arrow(S source, Direction direction, S target) {
arrows.put(new Connection<>(source, target), Arrow.of(direction));
return this;
}

public PlantUmlWriterParameters<S> arrowDirection(S source, Direction direction, S target) {
arrows.put(new Connection<>(source, target), direction);
public PlantUmlWriterParameters<S> arrow(S source, Direction direction, S target, int length) {
arrows.put(new Connection<>(source, target), Arrow.of(direction, length));
return this;
}

Expand All @@ -122,13 +155,13 @@ public PlantUmlWriterParameters<S> arrowDirection(S source, Direction direction,
* @param target target State
* @return Direction.name()
*/
String getDirection(S source, S target) {
private Arrow getArrow(S source, S target) {
if (source == null && target == null) {
throw new IllegalArgumentException("source and target state cannot both be null!");
}
Connection<S> sourceAndTarget = new Connection<>(source, target);
if (arrows.containsKey(sourceAndTarget)) {
return arrows.get(sourceAndTarget).name().toLowerCase();
return arrows.get(sourceAndTarget);
}
Connection<S> sourceOnly = new Connection<>(source, null);
Connection<S> targetOnly = new Connection<>(null, target);
Expand All @@ -140,14 +173,25 @@ String getDirection(S source, S target) {
"Two 'unary' 'arrowDirection' rules found for (%s, %s) with DIFFERENT values! Using 'target' rule!",
source, target
));
return arrows.get(targetOnly).name().toLowerCase();
return arrows.get(targetOnly);
} else if (arrows.containsKey(sourceOnly)) {
return arrows.get(sourceOnly).name().toLowerCase();
return arrows.get(sourceOnly);
} else if (arrows.containsKey(targetOnly)) {
return arrows.get(targetOnly).name().toLowerCase();
return arrows.get(targetOnly);
} else {
return Direction.DOWN.name().toLowerCase();
return Arrow.DEFAULT;
}
}

String getDirection(S source, S target) {
return getArrow(source, target).getDirection().name().toLowerCase();
}

String getArrowLength(S source, S target) {
if(source == null) {
return Arrow.DEFAULT.getLengthAsString();
}
return getArrow(source, target).getLengthAsString();
}

/**
Expand Down Expand Up @@ -211,7 +255,9 @@ public boolean isTransitionIgnored(S source, S target) {
@Getter
@EqualsAndHashCode
static class LabelDecorator {
@Builder.Default
private String prefix = "";
@Builder.Default
private String suffix = "";

String decorate(String label) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,10 @@ public static Stream<Arguments> plantUmlTestMethodSource() {
Arguments.of("org/springframework/statemachine/uml/simple-history-shallow", null),
Arguments.of("org/springframework/statemachine/uml/simple-junction", null),
Arguments.of("org/springframework/statemachine/uml/simple-localtransition",
// add some parameters to make diagram more readable
// add some parameters to make the diagram more readable
new PlantUmlWriterParameters<String>()
.arrowDirection("S22", UP, "S2")
.arrowDirection("S21", UP, "S2")
.arrowDirection("S22", UP, "S2")
.arrowDirection("S21", UP, "S2")
.arrow("S22", UP, "S2", 2)
.arrow("S21", UP, "S2", 2)
),
// It seems Spring statemachine UML parser creates duplicated transitions!
// Arguments.of("org/springframework/statemachine/uml/simple-root-regions", null),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ state S2 {

[*] --> S1
S1 -down-> S2 : E1
S21 -up-> S2 : E22
S21 -up-> S2 : E32
S22 -up-> S2 : E23
S22 -up-> S2 : E33
S21 -up--> S2 : E22
S21 -up--> S2 : E32
S22 -up--> S2 : E23
S22 -up--> S2 : E33
S2 -down-> S21 : E20
S2 -down-> S21 : E30
S2 -down-> S22 : E21
Expand Down

0 comments on commit 42ccaa4

Please sign in to comment.