different versions of defining a state machine
This commit is contained in:
52
src/TODO.md
Normal file
52
src/TODO.md
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
1. extract state machines from this as well
|
||||||
|
|
||||||
|
```java
|
||||||
|
|
||||||
|
import org.springframework.statemachine.StateMachine;
|
||||||
|
import org.springframework.statemachine.config.builders.StateMachineBuilder;
|
||||||
|
|
||||||
|
import java.util.EnumSet;
|
||||||
|
|
||||||
|
public class DynamicStateMachineExample {
|
||||||
|
|
||||||
|
public enum States {
|
||||||
|
DRAFT, REVIEW, APPROVED
|
||||||
|
}
|
||||||
|
|
||||||
|
public enum Events {
|
||||||
|
SUBMIT, APPROVE
|
||||||
|
}
|
||||||
|
|
||||||
|
public static StateMachine<States, Events> buildStateMachine() throws Exception {
|
||||||
|
StateMachineBuilder.Builder<States, Events> builder = StateMachineBuilder.builder();
|
||||||
|
|
||||||
|
builder.configureStates()
|
||||||
|
.withStates()
|
||||||
|
.initial(States.DRAFT)
|
||||||
|
.states(EnumSet.allOf(States.class));
|
||||||
|
|
||||||
|
builder.configureTransitions()
|
||||||
|
.withExternal()
|
||||||
|
.source(States.DRAFT).target(States.REVIEW).event(Events.SUBMIT)
|
||||||
|
.and()
|
||||||
|
.withExternal()
|
||||||
|
.source(States.REVIEW).target(States.APPROVED).event(Events.APPROVE);
|
||||||
|
|
||||||
|
return builder.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) throws Exception {
|
||||||
|
StateMachine<States, Events> sm = buildStateMachine();
|
||||||
|
|
||||||
|
sm.start();
|
||||||
|
System.out.println("Current State: " + sm.getState().getId());
|
||||||
|
|
||||||
|
sm.sendEvent(Events.SUBMIT);
|
||||||
|
System.out.println("After SUBMIT: " + sm.getState().getId());
|
||||||
|
|
||||||
|
sm.sendEvent(Events.APPROVE);
|
||||||
|
System.out.println("After APPROVE: " + sm.getState().getId());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
```
|
||||||
@@ -26,7 +26,7 @@ import java.util.Set;
|
|||||||
public class Main {
|
public class Main {
|
||||||
private static final String START_DIR = ".";
|
private static final String START_DIR = ".";
|
||||||
private static final String EXTENSION = ".java";
|
private static final String EXTENSION = ".java";
|
||||||
private static final String TARGET_ANNOTATION = "EnableStateMachineFactory";
|
private static final List<String> TARGET_ANNOTATIONS = List.of("EnableStateMachineFactory", "EnableStateMachine");
|
||||||
|
|
||||||
public static void main(String[] args) throws IOException {
|
public static void main(String[] args) throws IOException {
|
||||||
List<Path> javaFiles = FileUtils.findFilesWithExtension(Paths.get(START_DIR), EXTENSION);
|
List<Path> javaFiles = FileUtils.findFilesWithExtension(Paths.get(START_DIR), EXTENSION);
|
||||||
@@ -42,7 +42,7 @@ public class Main {
|
|||||||
String source = Files.readString(javaFile);
|
String source = Files.readString(javaFile);
|
||||||
AstFileFinder finder = new AstFileFinder(source);
|
AstFileFinder finder = new AstFileFinder(source);
|
||||||
|
|
||||||
if (finder.hasClassWithAnnotation(source, TARGET_ANNOTATION)) {
|
if (finder.hasClassWithAnnotation(source, TARGET_ANNOTATIONS)) {
|
||||||
System.out.println("Found annotation in: " + javaFile.toAbsolutePath());
|
System.out.println("Found annotation in: " + javaFile.toAbsolutePath());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package click.kamil.springstatemachineexporter.ast.in;
|
|||||||
|
|
||||||
import org.eclipse.jdt.core.dom.*;
|
import org.eclipse.jdt.core.dom.*;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
import java.util.concurrent.atomic.AtomicBoolean;
|
import java.util.concurrent.atomic.AtomicBoolean;
|
||||||
|
|
||||||
public class AstFileFinder {
|
public class AstFileFinder {
|
||||||
@@ -15,7 +16,7 @@ public class AstFileFinder {
|
|||||||
this.cu = (CompilationUnit) parser.createAST(null);
|
this.cu = (CompilationUnit) parser.createAST(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean hasClassWithAnnotation(String source, String annotationName) {
|
public boolean hasClassWithAnnotation(String source, List<String> annotationNames) {
|
||||||
AtomicBoolean found = new AtomicBoolean(false);
|
AtomicBoolean found = new AtomicBoolean(false);
|
||||||
cu.accept(new ASTVisitor() {
|
cu.accept(new ASTVisitor() {
|
||||||
@Override
|
@Override
|
||||||
@@ -23,7 +24,7 @@ public class AstFileFinder {
|
|||||||
if (node.isInterface()) return true;
|
if (node.isInterface()) return true;
|
||||||
for (Object modifier : node.modifiers()) {
|
for (Object modifier : node.modifiers()) {
|
||||||
if (modifier instanceof Annotation annotation &&
|
if (modifier instanceof Annotation annotation &&
|
||||||
annotation.getTypeName().getFullyQualifiedName().equals(annotationName)) {
|
annotationNames.contains(annotation.getTypeName().getFullyQualifiedName())) {
|
||||||
found.set(true);
|
found.set(true);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user