13
13
* See the License for the specific language governing permissions and
14
14
* limitations under the License.
15
15
*/
16
-
17
16
package org .springframework .statemachine .plantuml ;
18
17
19
18
import jakarta .validation .constraints .NotNull ;
52
51
* To display *.puml diagram, install PlantUml plugin<BR/>
53
52
* <a href="https://plugins.jetbrains.com/plugin/7017-plantuml-integration">https://plugins.jetbrains.com/plugin/7017-plantuml-integration</a><BR/>
54
53
* <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:
63
55
* <UL>
64
56
* <LI>be {@link org.springframework.context.annotation.Bean} s</LI>
65
57
* <LI>implement {@link BeanNameAware}</LI>
@@ -92,7 +84,9 @@ public class PlantUmlWriter<S, E> {
92
84
private Map <State <S , E >, String > historyStatesToHistoryId ;
93
85
private List <Transition <S , E >> historyTransitions ;
94
86
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
96
90
97
91
private final StateComparator <S , E > stateComparator = new StateComparator <>();
98
92
@@ -168,6 +162,7 @@ public String toPlantUml(
168
162
// 1st pass: Collecting history states
169
163
historyStatesToHistoryId = StateMachineHelper .collectHistoryStates (stateMachine );
170
164
historyTransitions = new ArrayList <>();
165
+ stateToStateNotes = new HashMap <>();
171
166
172
167
StringBuilder sb = new StringBuilder ("@startuml\n " )
173
168
.append (PlantUmlWriterParameters .getStateDiagramSettings (plantUmlWriterParameters ))
@@ -193,6 +188,23 @@ public String toPlantUml(
193
188
194
189
sb .append (plantUmlWriterParameters .getAdditionalHiddenTransitions ());
195
190
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
+
196
208
sb .append ("\n @enduml" );
197
209
198
210
log .debug ("toPlantUml:" + sb );
@@ -436,8 +448,10 @@ private void processSimpleState(
436
448
sb .append ("""
437
449
%s
438
450
"""
439
- .formatted (stateToString (indent , state .getId (), currentState , plantUmlWriterParameters ))
440
- .stripIndent ());
451
+ .formatted (
452
+ stateToString (indent , state .getId (), currentState , plantUmlWriterParameters )
453
+ ).stripIndent ()
454
+ );
441
455
}
442
456
}
443
457
@@ -503,23 +517,20 @@ private void processPseudoState(
503
517
state .getId (),
504
518
getPseudoStatePlantUmlStereotype (pseudoStateKind )
505
519
).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
+ }
523
534
}
524
535
}
525
536
0 commit comments