Skip to content

Commit b77ee89

Browse files
author
Pascal Dal Farra
committed
feat: add PlantUmlWriterParameters.note()
1 parent 7245eba commit b77ee89

21 files changed

+405
-247
lines changed

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

+40-29
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
1716
package org.springframework.statemachine.plantuml;
1817

1918
import jakarta.validation.constraints.NotNull;
@@ -52,14 +51,7 @@
5251
* To display *.puml diagram, install PlantUml plugin<BR/>
5352
* <a href="https://plugins.jetbrains.com/plugin/7017-plantuml-integration">https://plugins.jetbrains.com/plugin/7017-plantuml-integration</a><BR/>
5453
* <BR/>
55-
* For PNG support, install GraphViz<BR/>
56-
* <a href="https://graphviz.org/download/#windows">https://graphviz.org/download/#windows</a><BR/>
57-
* <BR/>
58-
* Install then define GRAPHVIZ_DOT environment variable: <BR/>
59-
* <BR/>
60-
* GRAPHVIZ_DOT=C:\Program Files\Graphviz\bin\dot.exe <BR/>
61-
* <BR/>
62-
* To obtain better results, {@link Action} s and {@link Guard} s should:
54+
* To get better results, {@link Action} s and {@link Guard} s should:
6355
* <UL>
6456
* <LI>be {@link org.springframework.context.annotation.Bean} s</LI>
6557
* <LI>implement {@link BeanNameAware}</LI>
@@ -92,7 +84,9 @@ public class PlantUmlWriter<S, E> {
9284
private Map<State<S, E>, String> historyStatesToHistoryId;
9385
private List<Transition<S, E>> historyTransitions;
9486

95-
// Comparators. Used to keep order of region, states and transitions stable in generated puml
87+
private Map<State<S, E>, String> stateToStateNotes;
88+
89+
// Comparators. Used to maintain order of regions, states and transitions stable in generated puml
9690

9791
private final StateComparator<S, E> stateComparator = new StateComparator<>();
9892

@@ -168,6 +162,7 @@ public String toPlantUml(
168162
// 1st pass: Collecting history states
169163
historyStatesToHistoryId = StateMachineHelper.collectHistoryStates(stateMachine);
170164
historyTransitions = new ArrayList<>();
165+
stateToStateNotes = new HashMap<>();
171166

172167
StringBuilder sb = new StringBuilder("@startuml\n")
173168
.append(PlantUmlWriterParameters.getStateDiagramSettings(plantUmlWriterParameters))
@@ -193,6 +188,23 @@ public String toPlantUml(
193188

194189
sb.append(plantUmlWriterParameters.getAdditionalHiddenTransitions());
195190

191+
// Let's not forget the notes
192+
PlantUmlWriterParameters<S> finalPlantUmlWriterParameters = plantUmlWriterParameters;
193+
StateMachineHelper
194+
.getAllStates(stateMachine)
195+
.forEach(seState -> {
196+
if (!stateToStateNotes.containsKey(seState)) {
197+
String note = finalPlantUmlWriterParameters.getNote(seState.getId());
198+
if (StringUtils.isNotBlank(note)) {
199+
stateToStateNotes.put(seState,
200+
"note left of %s: %s".formatted(seState.getId(), note).stripIndent()
201+
);
202+
}
203+
}
204+
});
205+
stateToStateNotes.values().stream().sorted()
206+
.forEach(noteLine -> sb.append("%n%s".formatted(noteLine)));
207+
196208
sb.append("\n@enduml");
197209

198210
log.debug("toPlantUml:" + sb);
@@ -436,8 +448,10 @@ private void processSimpleState(
436448
sb.append("""
437449
%s
438450
"""
439-
.formatted(stateToString(indent, state.getId(), currentState, plantUmlWriterParameters))
440-
.stripIndent());
451+
.formatted(
452+
stateToString(indent, state.getId(), currentState, plantUmlWriterParameters)
453+
).stripIndent()
454+
);
441455
}
442456
}
443457

@@ -503,23 +517,20 @@ private void processPseudoState(
503517
state.getId(),
504518
getPseudoStatePlantUmlStereotype(pseudoStateKind)
505519
).stripIndent());
506-
case END, CHOICE, FORK, JOIN, JUNCTION -> sb.append("""
507-
%s'%s <<%s>>
508-
%sstate %s <<%s>>
509-
%snote left of %s %s: %s
510-
"""
511-
.formatted(
512-
indent,
513-
state.getId(),
514-
pseudoStateKind.name(),
515-
indent,
516-
state.getId(),
517-
getPseudoStatePlantUmlStereotype(pseudoStateKind),
518-
indent,
519-
state.getId(),
520-
plantUmlWriterParameters.getStateColor(state.getId(), currentState),
521-
state.getId()
522-
).stripIndent());
520+
case END, CHOICE, FORK, JOIN, JUNCTION -> {
521+
sb.append("""
522+
%s'%s <<%s>>
523+
%sstate %s <<%s>>
524+
"""
525+
.formatted(
526+
indent, state.getId(), pseudoStateKind.name(),
527+
indent, state.getId(), getPseudoStatePlantUmlStereotype(pseudoStateKind)
528+
).stripIndent());
529+
stateToStateNotes.put(
530+
state,
531+
"note left of %s %s: %s".formatted(state.getId(), plantUmlWriterParameters.getStateColor(state.getId(), currentState), state.getId())
532+
);
533+
}
523534
}
524535
}
525536

0 commit comments

Comments
 (0)