ai tests + bug fix on last with 2args + strip quotes feature
This commit is contained in:
@@ -77,7 +77,7 @@ public class AstTransitionParser {
|
||||
// Extract source state once
|
||||
for (MethodInvocation call : segment) {
|
||||
if ("source".equals(call.getName().getIdentifier()) && !call.arguments().isEmpty()) {
|
||||
sourceState = call.arguments().getFirst().toString();
|
||||
sourceState = QuotedExpression.of(call.arguments().getFirst()).toStringWithoutQuotes();
|
||||
break; // only take the first occurrence
|
||||
}
|
||||
}
|
||||
@@ -93,11 +93,17 @@ public class AstTransitionParser {
|
||||
if (sourceState != null) {
|
||||
t.getSourceStates().add(sourceState);
|
||||
}
|
||||
t.getTargetStates().add(args.get(0).toString());
|
||||
t.getTargetStates().add(QuotedExpression.of(args.get(0)).toStringWithoutQuotes());
|
||||
if (args.size() > 1) {
|
||||
parseGuard(args.get(1), t);
|
||||
if ("last".equals(methodName)) {
|
||||
// For 'last', the second argument is an action, not a guard
|
||||
parseAction(args.get(1), t);
|
||||
} else {
|
||||
// For 'first' and 'then', the second argument is a guard
|
||||
parseGuard(args.get(1), t);
|
||||
}
|
||||
}
|
||||
if ("then".equals(methodName) && args.size() > 2) {
|
||||
if (args.size() > 2) {
|
||||
parseAction(args.get(2), t);
|
||||
}
|
||||
t.setOrder(orderCounter++);
|
||||
@@ -122,9 +128,9 @@ public class AstTransitionParser {
|
||||
}
|
||||
|
||||
switch (methodName) {
|
||||
case "source" -> args.forEach(arg -> t.getSourceStates().add(arg.toString()));
|
||||
case "target" -> args.forEach(arg -> t.getTargetStates().add(arg.toString()));
|
||||
case "event" -> t.setEvent(args.getFirst().toString());
|
||||
case "source" -> args.forEach(arg -> t.getSourceStates().add(QuotedExpression.of(arg).toStringWithoutQuotes()));
|
||||
case "target" -> args.forEach(arg -> t.getTargetStates().add(QuotedExpression.of(arg).toStringWithoutQuotes()));
|
||||
case "event" -> t.setEvent(QuotedExpression.of(args.getFirst()).toStringWithoutQuotes());
|
||||
case "guard" -> parseGuard(args.getFirst(), t);
|
||||
case "action" -> parseAction(args.getFirst(), t);
|
||||
}
|
||||
@@ -133,16 +139,18 @@ public class AstTransitionParser {
|
||||
}
|
||||
|
||||
private static void parseGuard(Object arg, Transition t) {
|
||||
if (arg instanceof Expression expr) {
|
||||
t.setGuard(expr.toString());
|
||||
t.setLambdaGuard(isLambdaOrAnonymous(expr));
|
||||
QuotedExpression quotedExpr = QuotedExpression.of(arg);
|
||||
if (quotedExpr != null) {
|
||||
t.setGuard(quotedExpr.toStringWithoutQuotes());
|
||||
t.setLambdaGuard(isLambdaOrAnonymous(quotedExpr.getExpression()));
|
||||
}
|
||||
}
|
||||
|
||||
private static void parseAction(Object arg, Transition t) {
|
||||
if (arg instanceof Expression expr) {
|
||||
t.getActions().add(expr.toString());
|
||||
t.getIsLambdaActions().add(isLambdaOrAnonymous(expr));
|
||||
QuotedExpression quotedExpr = QuotedExpression.of(arg);
|
||||
if (quotedExpr != null) {
|
||||
t.getActions().add(quotedExpr.toStringWithoutQuotes());
|
||||
t.getIsLambdaActions().add(isLambdaOrAnonymous(quotedExpr.getExpression()));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
package click.kamil.springstatemachineexporter.ast.app;
|
||||
|
||||
import lombok.Getter;
|
||||
import org.eclipse.jdt.core.dom.Expression;
|
||||
|
||||
@Getter
|
||||
public class QuotedExpression {
|
||||
private final Expression expression;
|
||||
|
||||
private QuotedExpression(Expression expression) {
|
||||
this.expression = expression;
|
||||
}
|
||||
|
||||
public String toStringWithoutQuotes() {
|
||||
if (expression == null) return null;
|
||||
return stripQuotes(expression.toString());
|
||||
}
|
||||
|
||||
public static String stripQuotes(String s) {
|
||||
if (s == null) return null;
|
||||
return s.replaceAll("^\"|\"$", "");
|
||||
}
|
||||
|
||||
public static QuotedExpression of(Expression expression) {
|
||||
return new QuotedExpression(expression);
|
||||
}
|
||||
|
||||
public static QuotedExpression of(Object obj) {
|
||||
if (obj instanceof Expression expr) {
|
||||
return new QuotedExpression(expr);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -94,10 +94,10 @@ public class StateMachineStateConfigurationMethodFinder {
|
||||
switch (methodName) {
|
||||
case "initial" -> {
|
||||
if (!isInsideRegionOrFork(call)) {
|
||||
initialStates.add(arg.toString());
|
||||
initialStates.add(QuotedExpression.of(arg).toStringWithoutQuotes());
|
||||
}
|
||||
}
|
||||
case "end" -> endStates.add(arg.toString());
|
||||
case "end" -> endStates.add(QuotedExpression.of(arg).toStringWithoutQuotes());
|
||||
// ignore others
|
||||
}
|
||||
}
|
||||
|
||||
@@ -31,12 +31,7 @@ public class TransitionStateUtils {
|
||||
|
||||
private static Stream<String> normalizeStates(Collection<String> states) {
|
||||
return states.stream()
|
||||
.map(TransitionStateUtils::stripQuotes)
|
||||
.map(QuotedExpression::stripQuotes)
|
||||
.filter(Objects::nonNull);
|
||||
}
|
||||
|
||||
private static String stripQuotes(String s) {
|
||||
if (s == null) return null;
|
||||
return s.replaceAll("^\"|\"$", "");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user