v1 extended analysis render
This commit is contained in:
@@ -472,4 +472,27 @@ public class CodebaseContext {
|
||||
public Collection<CompilationUnit> getCompilationUnits() {
|
||||
return Collections.unmodifiableCollection(allCus);
|
||||
}
|
||||
|
||||
public Collection<TypeDeclaration> getTypeDeclarations() {
|
||||
List<TypeDeclaration> types = new ArrayList<>();
|
||||
for (CompilationUnit cu : allCus) {
|
||||
for (Object typeObj : cu.types()) {
|
||||
if (typeObj instanceof TypeDeclaration td) {
|
||||
types.add(td);
|
||||
// Handle nested types
|
||||
types.addAll(getNestedTypes(td));
|
||||
}
|
||||
}
|
||||
}
|
||||
return types;
|
||||
}
|
||||
|
||||
private List<TypeDeclaration> getNestedTypes(TypeDeclaration td) {
|
||||
List<TypeDeclaration> nested = new ArrayList<>();
|
||||
for (TypeDeclaration inner : td.getTypes()) {
|
||||
nested.add(inner);
|
||||
nested.addAll(getNestedTypes(inner));
|
||||
}
|
||||
return nested;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,4 +10,6 @@ public class ExportOptions {
|
||||
boolean useLambdaGuards = true;
|
||||
@Builder.Default
|
||||
boolean renderChoicesAsDiamonds = true;
|
||||
@Builder.Default
|
||||
boolean embedIdentifiers = false;
|
||||
}
|
||||
|
||||
@@ -3,37 +3,17 @@ package click.kamil.springstatemachineexporter.exporter;
|
||||
import click.kamil.springstatemachineexporter.model.State;
|
||||
import click.kamil.springstatemachineexporter.model.Transition;
|
||||
import click.kamil.springstatemachineexporter.model.TransitionType;
|
||||
import click.kamil.springstatemachineexporter.ast.common.StringUtils;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class PlantUml implements StateMachineExporter {
|
||||
private final List<String> choiceColors = List.of("FF6347", "4682B4", "32CD32", "FFD700", "6A5ACD", "FF69B4");
|
||||
|
||||
private final String LAMBDA = "λ";
|
||||
|
||||
private String loadBaseStyle() {
|
||||
try (InputStream is = getClass().getResourceAsStream("/plantuml/default-style.puml")) {
|
||||
if (is == null) return "";
|
||||
return new BufferedReader(new InputStreamReader(is))
|
||||
.lines()
|
||||
.collect(Collectors.joining("\n"));
|
||||
} catch (Exception e) {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String export(click.kamil.springstatemachineexporter.analysis.model.AnalysisResult result, ExportOptions options) {
|
||||
return export(result.getName(), result.getTransitions(), result.getStartStates(), result.getEndStates(), options);
|
||||
@@ -51,7 +31,26 @@ public class PlantUml implements StateMachineExporter {
|
||||
sb.append("!pragma layout smetana\n");
|
||||
sb.append("hide empty description\n");
|
||||
sb.append("hide stereotype\n");
|
||||
|
||||
|
||||
// --- Premium Modern Styles ---
|
||||
sb.append("skinparam state {\n");
|
||||
sb.append(" BackgroundColor white\n");
|
||||
sb.append(" BorderColor #94a3b8\n");
|
||||
sb.append(" BorderThickness 1\n");
|
||||
sb.append(" FontName Inter\n");
|
||||
sb.append(" FontSize 9\n"); // Sleek but readable
|
||||
sb.append(" FontStyle bold\n");
|
||||
sb.append(" RoundCorner 20\n");
|
||||
sb.append(" Padding 1\n");
|
||||
sb.append("}\n");
|
||||
sb.append("skinparam shadowing false\n");
|
||||
sb.append("skinparam ArrowFontName JetBrains Mono\n");
|
||||
sb.append("skinparam ArrowFontSize 8\n");
|
||||
sb.append("skinparam ArrowColor #cbd5e1\n");
|
||||
sb.append("skinparam ArrowThickness 1\n");
|
||||
sb.append("skinparam dpi 110\n");
|
||||
sb.append("skinparam svgLinkTarget _self\n");
|
||||
|
||||
// Pre-declare all unique states for layout stability
|
||||
Set<String> allUniqueStates = new LinkedHashSet<>();
|
||||
transitions.forEach(t -> {
|
||||
@@ -61,21 +60,6 @@ public class PlantUml implements StateMachineExporter {
|
||||
allUniqueStates.forEach(s -> sb.append("state ").append(s).append("\n"));
|
||||
sb.append("\n");
|
||||
|
||||
String style = loadBaseStyle();
|
||||
|
||||
StringBuilder choiceColorStyles = new StringBuilder();
|
||||
StringBuilder hideChoiceColors = new StringBuilder();
|
||||
for (int i = 0; i < choiceColors.size(); i++) {
|
||||
choiceColorStyles.append(" .choice_color_").append(i)
|
||||
.append(" { LineColor #").append(choiceColors.get(i)).append(" }\n");
|
||||
hideChoiceColors.append("hide <<choice_color_").append(i).append(">> stereotype\n");
|
||||
}
|
||||
|
||||
style = style.replace("/* CHOICE_COLORS_PLACEHOLDER */", choiceColorStyles.toString());
|
||||
style = style.replace("/* HIDE_CHOICE_COLORS_PLACEHOLDER */", hideChoiceColors.toString());
|
||||
|
||||
sb.append(style).append("\n");
|
||||
|
||||
for (String start : startStates) {
|
||||
sb.append("[*] --> ").append(simplify(start)).append("\n");
|
||||
}
|
||||
@@ -90,16 +74,10 @@ public class PlantUml implements StateMachineExporter {
|
||||
}
|
||||
}
|
||||
|
||||
Map<String, String> choiceStateClassMap = new HashMap<>();
|
||||
int colorIndex = 0;
|
||||
|
||||
for (String state : statesWithChoice) {
|
||||
String className = "choice_color_" + (colorIndex % choiceColors.size());
|
||||
if (options.isRenderChoicesAsDiamonds()) {
|
||||
sb.append("state ").append(state).append(" <<choice>>\n");
|
||||
}
|
||||
choiceStateClassMap.put(state, className);
|
||||
colorIndex++;
|
||||
}
|
||||
|
||||
Set<String> junctionStates = transitions.stream()
|
||||
@@ -115,7 +93,6 @@ public class PlantUml implements StateMachineExporter {
|
||||
|
||||
sb.append("\n");
|
||||
|
||||
Set<String> usedEventStereotypes = new java.util.LinkedHashSet<>();
|
||||
for (Transition t : transitions) {
|
||||
if (t.getSourceStates() == null || t.getSourceStates().isEmpty())
|
||||
continue;
|
||||
@@ -139,29 +116,31 @@ public class PlantUml implements StateMachineExporter {
|
||||
for (String rawTarget : targets) {
|
||||
String target = simplify(rawTarget);
|
||||
|
||||
String styleClass = getStyleClass(t, source, choiceStateClassMap);
|
||||
String color = getLegacyColor(t, source, choiceStateClassMap);
|
||||
sb.append(source).append(" -[#").append(color).append(",bold]-> ").append(target).append(" <<").append(styleClass).append(">>");
|
||||
String styleClass = getStyleClass(t, source, Collections.emptyMap());
|
||||
String color = getLegacyColor(t, source, Collections.emptyMap());
|
||||
sb.append(source).append(" -[#").append(color).append(",bold]-> ").append(target);
|
||||
|
||||
sb.append(" <<").append(styleClass).append(">>");
|
||||
|
||||
if (t.getEvent() != null && !t.getEvent().isBlank()) {
|
||||
String eventClass = "e_" + normalize(t.getEvent());
|
||||
sb.append(" <<").append(eventClass).append(">>");
|
||||
usedEventStereotypes.add(eventClass);
|
||||
sb.append(" <<e_").append(normalize(t.getEvent())).append(">>");
|
||||
}
|
||||
|
||||
String label = buildLabel(t, options.isUseLambdaGuards());
|
||||
if (!label.isEmpty()) {
|
||||
sb.append(" : ").append(label);
|
||||
sb.append(" : ");
|
||||
if (options.isEmbedIdentifiers() && t.getEvent() != null && !t.getEvent().isBlank()) {
|
||||
// Link the label text - this forces PlantUML to create an <a> tag
|
||||
sb.append("[[#link_").append(normalize(t.getEvent())).append(" ").append(label).append("]]");
|
||||
} else {
|
||||
sb.append(label);
|
||||
}
|
||||
}
|
||||
sb.append("\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (String eventClass : usedEventStereotypes) {
|
||||
sb.append("hide <<").append(eventClass).append(">> stereotype\n");
|
||||
}
|
||||
|
||||
sb.append("\n");
|
||||
for (String end : endStates) {
|
||||
sb.append(simplify(end)).append(" --> [*]\n");
|
||||
@@ -178,16 +157,9 @@ public class PlantUml implements StateMachineExporter {
|
||||
|
||||
private String getLegacyColor(Transition t, String source, Map<String, String> choiceStateClassMap) {
|
||||
if (t.getType() == null)
|
||||
return "000000";
|
||||
return "94a3b8";
|
||||
return switch (t.getType()) {
|
||||
case CHOICE -> {
|
||||
String className = choiceStateClassMap.get(source);
|
||||
if (className != null && className.startsWith("choice_color_")) {
|
||||
int index = Integer.parseInt(className.substring("choice_color_".length()));
|
||||
yield choiceColors.get(index % choiceColors.size());
|
||||
}
|
||||
yield "000000";
|
||||
}
|
||||
case CHOICE -> "FF6347";
|
||||
case EXTERNAL -> "1E90FF";
|
||||
case INTERNAL -> "32CD32";
|
||||
case LOCAL -> "FFA500";
|
||||
@@ -201,7 +173,7 @@ public class PlantUml implements StateMachineExporter {
|
||||
if (t.getType() == null)
|
||||
return "default";
|
||||
return switch (t.getType()) {
|
||||
case CHOICE -> choiceStateClassMap.getOrDefault(source, "choice_type");
|
||||
case CHOICE -> "choice_type";
|
||||
case EXTERNAL -> "external";
|
||||
case INTERNAL -> "internal";
|
||||
case LOCAL -> "local";
|
||||
@@ -216,35 +188,30 @@ public class PlantUml implements StateMachineExporter {
|
||||
return ".puml";
|
||||
}
|
||||
|
||||
private String getGuardText(Transition t, boolean useLambdaGuards) {
|
||||
if (t.getGuard() == null || StringUtils.isBlank(t.getGuard().expression()))
|
||||
return "";
|
||||
if (useLambdaGuards && t.getGuard().isLambda())
|
||||
return LAMBDA;
|
||||
return t.getGuard().expression().replaceAll("[\\n\\r]+", " ").replaceAll("\\s{2,}", " ").trim();
|
||||
}
|
||||
|
||||
private String getActionsText(Transition t, boolean useLambdaGuards) {
|
||||
if (t.getActions() == null || t.getActions().isEmpty())
|
||||
return "";
|
||||
return t.getActions().stream()
|
||||
.map(action -> (useLambdaGuards && action.isLambda()) ? LAMBDA : action.expression())
|
||||
.collect(Collectors.joining(", "));
|
||||
@Override
|
||||
public String simplify(String state) {
|
||||
if (state == null) return "";
|
||||
if (state.contains("\"")) return state;
|
||||
return state.replaceAll("[^a-zA-Z0-9_]", "");
|
||||
}
|
||||
|
||||
private String buildLabel(Transition t, boolean useLambdaGuards) {
|
||||
List<String> parts = new ArrayList<>();
|
||||
if (t.getEvent() != null && !t.getEvent().isBlank())
|
||||
parts.add(simplify(t.getEvent()));
|
||||
String guard = getGuardText(t, useLambdaGuards);
|
||||
if (!guard.isEmpty())
|
||||
parts.add("[" + guard + "]");
|
||||
String actions = getActionsText(t, useLambdaGuards);
|
||||
if (!actions.isEmpty()) {
|
||||
if (!parts.isEmpty())
|
||||
parts.add("/ " + actions);
|
||||
else
|
||||
parts.add(actions);
|
||||
if (t.getEvent() != null && !t.getEvent().isBlank()) {
|
||||
parts.add(t.getEvent());
|
||||
}
|
||||
if (t.getGuard() != null) {
|
||||
String g = t.getGuard().expression();
|
||||
if (useLambdaGuards && g.contains("->")) {
|
||||
parts.add("[" + LAMBDA + "]");
|
||||
} else {
|
||||
parts.add("[" + g + "]");
|
||||
}
|
||||
}
|
||||
if (t.getActions() != null && !t.getActions().isEmpty()) {
|
||||
parts.add("/ " + t.getActions().stream()
|
||||
.map(a -> a.expression().contains("->") ? LAMBDA : a.expression())
|
||||
.collect(Collectors.joining(", ")));
|
||||
}
|
||||
Optional.ofNullable(t.getOrder())
|
||||
.filter(order -> t.getType() == TransitionType.CHOICE || t.getType() == TransitionType.JUNCTION)
|
||||
|
||||
Reference in New Issue
Block a user