colors
This commit is contained in:
@@ -1,15 +1,12 @@
|
||||
package com.example.statemachinedemo;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.*;
|
||||
|
||||
public class ExportUtils {
|
||||
|
||||
public static String generatePlantUML(List<Transition> transitions,
|
||||
Set<String> startStates,
|
||||
Set<String> endStates,
|
||||
boolean useLambdaGuards) {
|
||||
Set<String> startStates,
|
||||
Set<String> endStates,
|
||||
boolean useLambdaGuards) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append("@startuml\n");
|
||||
sb.append("skinparam state {\n");
|
||||
@@ -23,7 +20,6 @@ public class ExportUtils {
|
||||
sb.append("\n");
|
||||
|
||||
// 2. Find all states that have choice transitions originating from them
|
||||
// We'll reroute those transitions through a separate choice pseudostate.
|
||||
Set<String> statesWithChoice = new HashSet<>();
|
||||
for (Transition t : transitions) {
|
||||
if ("withChoice".equals(t.type)) {
|
||||
@@ -39,9 +35,28 @@ public class ExportUtils {
|
||||
}
|
||||
sb.append("\n");
|
||||
|
||||
// Palette of colors for choice states (NO leading '#')
|
||||
List<String> choiceColors = List.of("FF6347", "4682B4", "32CD32", "FFD700", "6A5ACD", "FF69B4");
|
||||
|
||||
// Assign a color per withChoice source state cycling through palette
|
||||
Map<String, String> choiceStateColorMap = new HashMap<>();
|
||||
int colorIndex = 0;
|
||||
for (String state : statesWithChoice) {
|
||||
choiceStateColorMap.put(state, choiceColors.get(colorIndex % choiceColors.size()));
|
||||
colorIndex++;
|
||||
}
|
||||
|
||||
Set<String> transitionLines = new HashSet<>();
|
||||
|
||||
// 4. Output transitions:
|
||||
// Helper to get color for non-choice transitions (NO leading '#')
|
||||
java.util.function.Function<String, String> getTransitionColor = (type) -> {
|
||||
if ("withExternal".equals(type)) return "1E90FF"; // DodgerBlue
|
||||
if ("withInternal".equals(type)) return "32CD32"; // LimeGreen
|
||||
if ("withJunction".equals(type)) return "FF69B4"; // HotPink
|
||||
return "000000"; // Black fallback
|
||||
};
|
||||
|
||||
// 4. Output transitions
|
||||
for (Transition t : transitions) {
|
||||
if (t.sourceState == null || t.targetState == null) continue;
|
||||
|
||||
@@ -58,20 +73,60 @@ public class ExportUtils {
|
||||
// Prepare guard text
|
||||
String guardText = "";
|
||||
if (t.guard != null && !t.guard.isBlank()) {
|
||||
guardText = useLambdaGuards ? "lambda"
|
||||
: t.guard.replaceAll("[\\n\\r]+", " ").replaceAll("\\s{2,}", " ").trim();
|
||||
if (useLambdaGuards && t.isLambdaGuard) {
|
||||
guardText = "lambda";
|
||||
} else {
|
||||
guardText = t.guard.replaceAll("[\\n\\r]+", " ").replaceAll("\\s{2,}", " ").trim();
|
||||
}
|
||||
}
|
||||
|
||||
// Build label with event and guard
|
||||
// Prepare actions text (comma separated)
|
||||
String actionsText = "";
|
||||
if (t.actions != null && !t.actions.isEmpty()) {
|
||||
StringBuilder actionBuilder = new StringBuilder();
|
||||
for (int i = 0; i < t.actions.size(); i++) {
|
||||
if (i > 0) actionBuilder.append(", ");
|
||||
String action = t.actions.get(i);
|
||||
boolean isLambda = t.isLambdaActions.get(i);
|
||||
if (useLambdaGuards && isLambda) {
|
||||
actionBuilder.append("lambda");
|
||||
} else {
|
||||
actionBuilder.append(action);
|
||||
}
|
||||
}
|
||||
actionsText = actionBuilder.toString();
|
||||
}
|
||||
|
||||
// Build label with event, guard, and actions
|
||||
StringBuilder label = new StringBuilder();
|
||||
if (t.event != null && !t.event.isBlank()) {
|
||||
label.append(simplify(t.event));
|
||||
}
|
||||
if (!guardText.isBlank()) {
|
||||
label.append(" [").append(guardText).append("]");
|
||||
if (label.length() > 0) label.append(" ");
|
||||
label.append("[").append(guardText).append("]");
|
||||
}
|
||||
if (!actionsText.isBlank()) {
|
||||
if (label.length() > 0) label.append(" / ");
|
||||
label.append(actionsText);
|
||||
}
|
||||
|
||||
String line = transitionSource + " --> " + target + (label.isEmpty() ? "" : (" : " + label));
|
||||
// Add order info for choice/junction transitions
|
||||
if (("withChoice".equals(t.type) || "withJunction".equals(t.type)) && t.order != null) {
|
||||
if (label.length() > 0) label.append(" ");
|
||||
label.append("(order=").append(t.order).append(")");
|
||||
}
|
||||
|
||||
// Determine color for this transition
|
||||
String color;
|
||||
if ("withChoice".equals(t.type)) {
|
||||
color = choiceStateColorMap.getOrDefault(source, "000000");
|
||||
} else {
|
||||
color = getTransitionColor.apply(t.type);
|
||||
}
|
||||
|
||||
// Add color with single '#' prefix
|
||||
String line = transitionSource + " -[#"+color+",bold]-> " + target + (label.isEmpty() ? "" : (" : " + label));
|
||||
|
||||
// Skip duplicates
|
||||
if (transitionLines.add(line)) {
|
||||
@@ -89,6 +144,7 @@ public class ExportUtils {
|
||||
sb.append("@enduml\n");
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
public static String generateDot(List<Transition> transitions,
|
||||
Set<String> startStates,
|
||||
Set<String> endStates,
|
||||
@@ -159,18 +215,51 @@ public class ExportUtils {
|
||||
edgeSource = source + "_choice";
|
||||
}
|
||||
|
||||
// Prepare guard text
|
||||
String guardText = "";
|
||||
if (t.guard != null && !t.guard.isBlank()) {
|
||||
guardText = useLambdaGuards ? "lambda"
|
||||
: t.guard.replaceAll("[\\n\\r]+", " ").replaceAll("\\s{2,}", " ").trim();
|
||||
if (useLambdaGuards && t.isLambdaGuard) {
|
||||
guardText = "lambda";
|
||||
} else {
|
||||
guardText = t.guard.replaceAll("[\\n\\r]+", " ").replaceAll("\\s{2,}", " ").trim();
|
||||
}
|
||||
}
|
||||
|
||||
// Prepare actions text
|
||||
String actionsText = "";
|
||||
if (t.actions != null && !t.actions.isEmpty()) {
|
||||
StringBuilder actionBuilder = new StringBuilder();
|
||||
for (int i = 0; i < t.actions.size(); i++) {
|
||||
if (i > 0) actionBuilder.append(", ");
|
||||
String action = t.actions.get(i);
|
||||
boolean isLambda = t.isLambdaActions.get(i);
|
||||
if (useLambdaGuards && isLambda) {
|
||||
actionBuilder.append("lambda");
|
||||
} else {
|
||||
actionBuilder.append(action);
|
||||
}
|
||||
}
|
||||
actionsText = actionBuilder.toString();
|
||||
}
|
||||
|
||||
// Build label with event, guard, and actions
|
||||
StringBuilder label = new StringBuilder();
|
||||
if (t.event != null && !t.event.isBlank()) {
|
||||
label.append(simplify(t.event));
|
||||
}
|
||||
if (!guardText.isBlank()) {
|
||||
label.append(" [").append(guardText).append("]");
|
||||
if (label.length() > 0) label.append(" ");
|
||||
label.append("[").append(guardText).append("]");
|
||||
}
|
||||
if (!actionsText.isBlank()) {
|
||||
if (label.length() > 0) label.append(" / ");
|
||||
label.append(actionsText);
|
||||
}
|
||||
|
||||
// Add order info for choice/junction transitions
|
||||
if (("withChoice".equals(t.type) || "withJunction".equals(t.type)) && t.order != null) {
|
||||
if (label.length() > 0) label.append(" ");
|
||||
label.append("(order=").append(t.order).append(")");
|
||||
}
|
||||
|
||||
String edgeLabel = label.length() > 0 ? " [label=\"" + label.toString() + "\"]" : "";
|
||||
@@ -216,42 +305,38 @@ public class ExportUtils {
|
||||
if (target == null || target.isEmpty()) continue;
|
||||
|
||||
String event = t.event != null ? simplify(t.event) : null;
|
||||
String guard = t.guard != null && !t.guard.isBlank()
|
||||
? (useLambdaGuards ? "lambda" : t.guard.replaceAll("[\\n\\r]+", " ").trim())
|
||||
: null;
|
||||
String guard = null;
|
||||
if (t.guard != null && !t.guard.isBlank()) {
|
||||
if (useLambdaGuards && t.isLambdaGuard) {
|
||||
guard = "lambda";
|
||||
} else {
|
||||
guard = t.guard.replaceAll("[\\n\\r]+", " ").trim();
|
||||
}
|
||||
}
|
||||
|
||||
sb.append(" <transition");
|
||||
if (event != null && !event.isBlank()) sb.append(" event=\"").append(event).append("\"");
|
||||
sb.append(" target=\"").append(target).append("\"");
|
||||
if (guard != null && !guard.isBlank()) sb.append(" cond=\"").append(escapeXml(guard)).append("\"");
|
||||
sb.append("/>\n");
|
||||
|
||||
// If choice/junction and order is set, add XML comment inside transition
|
||||
if (("withChoice".equals(t.type) || "withJunction".equals(t.type)) && t.order != null) {
|
||||
sb.append(">\n");
|
||||
sb.append(" <!-- order=").append(t.order).append(" -->\n");
|
||||
sb.append(" </transition>\n");
|
||||
} else {
|
||||
sb.append("/>\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
sb.append(" </state>\n\n");
|
||||
}
|
||||
|
||||
// 3. Add final states (if not already defined as states)
|
||||
for (String end : endStates) {
|
||||
String simpleEnd = simplify(end);
|
||||
if (!allStates.contains(end)) {
|
||||
sb.append(" <final id=\"").append(simpleEnd).append("\"/>\n\n");
|
||||
}
|
||||
sb.append(" </state>\n\n");
|
||||
}
|
||||
|
||||
sb.append("</scxml>\n");
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
private static String escapeXml(String s) {
|
||||
if (s == null) return "";
|
||||
return s.replace("&", "&")
|
||||
.replace("<", "<")
|
||||
.replace(">", ">")
|
||||
.replace("\"", """)
|
||||
.replace("'", "'");
|
||||
}
|
||||
|
||||
|
||||
// Helper: extract last enum part
|
||||
private static String simplify(String full) {
|
||||
if (full == null) return "";
|
||||
@@ -259,4 +344,10 @@ public class ExportUtils {
|
||||
return dot >= 0 ? full.substring(dot + 1) : full;
|
||||
}
|
||||
|
||||
private static String escapeXml(String s) {
|
||||
if (s == null) return "";
|
||||
return s.replace("&", "&").replace("<", "<").replace(">", ">")
|
||||
.replace("\"", """).replace("'", "'");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user