v2.3 extended analysis render update
This commit is contained in:
@@ -18,6 +18,8 @@ import java.util.Set;
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
public class AnalysisResult {
|
||||
private final String name;
|
||||
@Builder.Default
|
||||
private final Set<click.kamil.springstatemachineexporter.model.State> states = java.util.Collections.emptySet();
|
||||
private final List<Transition> transitions;
|
||||
private final Set<String> startStates;
|
||||
private final Set<String> endStates;
|
||||
|
||||
@@ -11,6 +11,8 @@ import org.eclipse.jdt.core.dom.ASTVisitor;
|
||||
import org.eclipse.jdt.core.dom.ClassInstanceCreation;
|
||||
import org.eclipse.jdt.core.dom.CompilationUnit;
|
||||
import org.eclipse.jdt.core.dom.Expression;
|
||||
import org.eclipse.jdt.core.dom.IMethodBinding;
|
||||
import org.eclipse.jdt.core.dom.ASTNode;
|
||||
import org.eclipse.jdt.core.dom.LambdaExpression;
|
||||
import org.eclipse.jdt.core.dom.MethodDeclaration;
|
||||
import org.eclipse.jdt.core.dom.MethodInvocation;
|
||||
@@ -175,15 +177,15 @@ public class AstTransitionParser {
|
||||
Expression secondArg = resolveArg((Expression) args.get(1), argsMap);
|
||||
if ("last".equals(methodName)) {
|
||||
// For 'last', the second argument is an action, not a guard
|
||||
parseAction(secondArg, t);
|
||||
parseAction(secondArg, t, cu);
|
||||
} else {
|
||||
// For 'first' and 'then', the second argument is a guard
|
||||
parseGuard(secondArg, t);
|
||||
parseGuard(secondArg, t, cu);
|
||||
}
|
||||
}
|
||||
if (args.size() > 2) {
|
||||
Expression thirdArg = resolveArg((Expression) args.get(2), argsMap);
|
||||
parseAction(thirdArg, t);
|
||||
parseAction(thirdArg, t, cu);
|
||||
}
|
||||
t.setOrder(orderCounter++);
|
||||
transitions.add(t);
|
||||
@@ -226,28 +228,43 @@ public class AstTransitionParser {
|
||||
}
|
||||
case "guard" -> {
|
||||
Expression resolved = resolveArg((Expression) args.get(0), argsMap);
|
||||
parseGuard(resolved, t);
|
||||
parseGuard(resolved, t, cu);
|
||||
}
|
||||
case "action" -> {
|
||||
Expression resolved = resolveArg((Expression) args.get(0), argsMap);
|
||||
parseAction(resolved, t);
|
||||
parseAction(resolved, t, cu);
|
||||
}
|
||||
}
|
||||
}
|
||||
return t;
|
||||
}
|
||||
|
||||
private static void parseGuard(Object arg, Transition t) {
|
||||
private static String extractInternalLogic(Expression expr, CompilationUnit cu) {
|
||||
if (expr instanceof MethodInvocation mi) {
|
||||
IMethodBinding binding = mi.resolveMethodBinding();
|
||||
if (binding != null) {
|
||||
ASTNode declNode = cu.findDeclaringNode(binding.getKey());
|
||||
if (declNode instanceof MethodDeclaration md) {
|
||||
return md.toString();
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private static void parseGuard(Object arg, Transition t, CompilationUnit cu) {
|
||||
QuotedExpression quotedExpr = QuotedExpression.of(arg);
|
||||
if (quotedExpr != null) {
|
||||
t.setGuard(Guard.of(quotedExpr.toStringWithoutQuotes(), isLambdaOrAnonymous(quotedExpr.getExpression())));
|
||||
String internalLogic = extractInternalLogic(quotedExpr.getExpression(), cu);
|
||||
t.setGuard(Guard.of(quotedExpr.toStringWithoutQuotes(), isLambdaOrAnonymous(quotedExpr.getExpression()), internalLogic));
|
||||
}
|
||||
}
|
||||
|
||||
private static void parseAction(Object arg, Transition t) {
|
||||
private static void parseAction(Object arg, Transition t, CompilationUnit cu) {
|
||||
QuotedExpression quotedExpr = QuotedExpression.of(arg);
|
||||
if (quotedExpr != null) {
|
||||
t.getActions().add(Action.of(quotedExpr.toStringWithoutQuotes(), isLambdaOrAnonymous(quotedExpr.getExpression())));
|
||||
String internalLogic = extractInternalLogic(quotedExpr.getExpression(), cu);
|
||||
t.getActions().add(Action.of(quotedExpr.toStringWithoutQuotes(), isLambdaOrAnonymous(quotedExpr.getExpression()), internalLogic));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -44,6 +44,23 @@ public class TransitionStateUtils {
|
||||
.collect(Collectors.toSet());
|
||||
}
|
||||
|
||||
public static Set<State> findAllStates(Collection<Transition> transitions, Set<String> initialStates, Set<String> endStates) {
|
||||
Set<State> allStates = new java.util.HashSet<>();
|
||||
if (transitions != null) {
|
||||
transitions.forEach(t -> {
|
||||
if (t.getSourceStates() != null) allStates.addAll(t.getSourceStates());
|
||||
if (t.getTargetStates() != null) allStates.addAll(t.getTargetStates());
|
||||
});
|
||||
}
|
||||
if (initialStates != null) {
|
||||
initialStates.forEach(s -> allStates.add(State.of(s)));
|
||||
}
|
||||
if (endStates != null) {
|
||||
endStates.forEach(s -> allStates.add(State.of(s)));
|
||||
}
|
||||
return allStates;
|
||||
}
|
||||
|
||||
private static Stream<String> normalizeStates(Collection<State> states) {
|
||||
return states.stream()
|
||||
.map(State::toString)
|
||||
|
||||
@@ -198,15 +198,15 @@ public class PlantUml implements StateMachineExporter {
|
||||
}
|
||||
if (t.getGuard() != null) {
|
||||
String g = t.getGuard().expression();
|
||||
if (useLambdaGuards && g.contains("->")) {
|
||||
if (useLambdaGuards && t.getGuard().isLambda()) {
|
||||
parts.add("[" + LAMBDA + "]");
|
||||
} else {
|
||||
parts.add("[" + g.replace("\r", "").replace("\n", "\\n") + "]");
|
||||
parts.add("[" + g.replaceAll("[\\n\\r]+", " ").replaceAll("\\s{2,}", " ").trim() + "]");
|
||||
}
|
||||
}
|
||||
if (t.getActions() != null && !t.getActions().isEmpty()) {
|
||||
parts.add("/ " + t.getActions().stream()
|
||||
.map(a -> a.expression().contains("->") ? LAMBDA : a.expression().replace("\r", "").replace("\n", "\\n"))
|
||||
.map(a -> (useLambdaGuards && a.isLambda()) ? LAMBDA : a.expression().replaceAll("[\\n\\r]+", " ").replaceAll("\\s{2,}", " ").trim())
|
||||
.collect(Collectors.joining(", ")));
|
||||
}
|
||||
Optional.ofNullable(t.getOrder())
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package click.kamil.springstatemachineexporter.model;
|
||||
|
||||
public record Action(String expression, boolean isLambda) {
|
||||
public static Action of(String expression, boolean isLambda) {
|
||||
return expression != null ? new Action(expression, isLambda) : null;
|
||||
public record Action(String expression, boolean isLambda, String internalLogic) {
|
||||
public static Action of(String expression, boolean isLambda, String internalLogic) {
|
||||
return expression != null ? new Action(expression, isLambda, internalLogic) : null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package click.kamil.springstatemachineexporter.model;
|
||||
|
||||
public record Guard(String expression, boolean isLambda) {
|
||||
public static Guard of(String expression, boolean isLambda) {
|
||||
return expression != null ? new Guard(expression, isLambda) : null;
|
||||
public record Guard(String expression, boolean isLambda, String internalLogic) {
|
||||
public static Guard of(String expression, boolean isLambda, String internalLogic) {
|
||||
return expression != null ? new Guard(expression, isLambda, internalLogic) : null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -147,9 +147,11 @@ public class ExportService {
|
||||
|
||||
Set<String> startStates = TransitionStateUtils.findStartStates(transitions, initialStatesAst);
|
||||
Set<String> endStates = TransitionStateUtils.findEndStates(transitions, endStatesAst);
|
||||
Set<click.kamil.springstatemachineexporter.model.State> allStates = TransitionStateUtils.findAllStates(transitions, initialStatesAst, endStatesAst);
|
||||
|
||||
AnalysisResult result = AnalysisResult.builder()
|
||||
.name(className)
|
||||
.states(allStates)
|
||||
.transitions(transitions)
|
||||
.startStates(startStates)
|
||||
.endStates(endStates)
|
||||
@@ -172,11 +174,13 @@ public class ExportService {
|
||||
|
||||
List<Transition> transitions = AstTransitionParser.parseTransitions(m, context);
|
||||
|
||||
Set<String> startStates = TransitionStateUtils.findStartStates(transitions);
|
||||
Set<String> endStates = TransitionStateUtils.findEndStates(transitions);
|
||||
Set<String> startStates = TransitionStateUtils.findStartStates(transitions, null);
|
||||
Set<String> endStates = TransitionStateUtils.findEndStates(transitions, null);
|
||||
Set<click.kamil.springstatemachineexporter.model.State> allStates = TransitionStateUtils.findAllStates(transitions, null, null);
|
||||
|
||||
AnalysisResult result = AnalysisResult.builder()
|
||||
.name(uniqueName)
|
||||
.states(allStates)
|
||||
.transitions(transitions)
|
||||
.startStates(startStates)
|
||||
.endStates(endStates)
|
||||
|
||||
Reference in New Issue
Block a user