reshuffle

This commit is contained in:
2026-06-06 07:06:57 +02:00
parent 9b17b71e6d
commit 00161b294c
18 changed files with 264 additions and 35 deletions

View File

@@ -1,4 +1,4 @@
package click.kamil.springstatemachineexporter.statemachine;
package click.kamil.examples.statemachine.complex;
import org.springframework.context.annotation.Configuration;
import org.springframework.statemachine.action.Action;
@@ -156,4 +156,4 @@ public class ComplexStateMachineConfig extends StateMachineConfigurerAdapter<Com
private Action<States, Events> logExitAction(String msg) {
return context -> System.out.println(msg);
}
}
}

View File

@@ -1,4 +1,5 @@
package click.kamil.springstatemachineexporter.statemachine;
package click.kamil.examples.statemachine.dynamic;
import org.springframework.statemachine.StateMachine;
import org.springframework.statemachine.config.StateMachineBuilder;
@@ -45,4 +46,4 @@ public class DynamicStateMachineExample {
sm.sendEvent(Events.APPROVE);
System.out.println("After APPROVE: " + sm.getState().getId());
}
}
}

View File

@@ -1,4 +1,4 @@
package click.kamil.springstatemachineexporter.statemachine;
package click.kamil.examples.statemachine.enumstate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
@@ -35,12 +35,11 @@ class KamilEnumStateMachineConfiguration extends StateMachineConfigurerAdapter<O
.and()
.withExternal().source(OrderStates.PAID).target(OrderStates.CANCELED).event(OrderEvents.CANCEL)
.and()
.withExternal().source(OrderStates.SUBMITTED).event(OrderEvents.ABCD)
// it needs to have target specified to get added to .transitions collections
.and()
.withExternal().source(OrderStates.SUBMITTED).target(OrderStates.PAID1).event(OrderEvents.ABCD)
.withExternal().source(OrderStates.SUBMITTED).target(OrderStates.PAID1).event(OrderEvents.ABCD)
//
.and()
.withChoice().source(OrderStates.PAID1)
@@ -78,6 +77,7 @@ class KamilEnumStateMachineConfiguration extends StateMachineConfigurerAdapter<O
.end(OrderStates.FULFILLED)
.end(OrderStates.CANCELED);
}
@Override
public void configure(StateMachineConfigurationConfigurer<OrderStates, OrderEvents> config) throws Exception {
StateMachineListenerAdapter<OrderStates, OrderEvents> listenerAdapter = new StateMachineListenerAdapter<>() {
@@ -90,4 +90,4 @@ class KamilEnumStateMachineConfiguration extends StateMachineConfigurerAdapter<O
.autoStartup(false)
.listener(listenerAdapter);
}
}
}

View File

@@ -1,4 +1,4 @@
package click.kamil.springstatemachineexporter.statemachine;
package click.kamil.examples.statemachine.enumstate;
public enum OrderEvents {
FULFILL, PAY, CANCEL,

View File

@@ -0,0 +1,5 @@
package click.kamil.examples.statemachine.enumstate;
public enum OrderStates {
SUBMITTED, PAID, FULFILLED, CANCELED, PAID1, PAID2, PAID3, INVALID, HAPPEN, SKIPPED, NEVER
}

View File

@@ -1,4 +1,4 @@
package click.kamil.springstatemachineexporter.statemachine;
package click.kamil.examples.statemachine.forkjoin;
import org.springframework.context.annotation.Configuration;
import org.springframework.statemachine.action.Action;

View File

@@ -0,0 +1,172 @@
package click.kamil.examples.statemachine.inheritancestate;
import org.springframework.statemachine.action.Action;
import org.springframework.statemachine.config.builders.StateMachineConfigurationConfigurer;
import org.springframework.statemachine.config.builders.StateMachineStateConfigurer;
import org.springframework.statemachine.config.builders.StateMachineTransitionConfigurer;
import org.springframework.statemachine.config.configurers.ExternalTransitionConfigurer;
import org.springframework.statemachine.config.configurers.StateConfigurer;
import org.springframework.statemachine.guard.Guard;
abstract class AbstractOneStateMachineConfiguration extends AbstractStateMachineConfiguration<States, Events> {
protected abstract void addAdditionalStates(StateConfigurer<States, Events> states);
protected abstract void addAdditionalTransitions(ExternalTransitionConfigurer<States, Events> transitions) throws Exception;
@Override
public void configure(StateMachineConfigurationConfigurer<States, Events> config) throws Exception {
config
.withConfiguration()
.autoStartup(true);
}
@Override
public void configure(StateMachineStateConfigurer<States, Events> states) throws Exception {
StateConfigurer<States, Events> end = states
.withStates()
.initial(States.STATE1)
.state(States.STATE2)
.state(States.STATE3)
.state(States.STATE4)
.state(States.STATE5)
.state(States.STATE6)
.state(States.STATE7)
.state(States.STATE8)
.state(States.STATE9)
.state(States.STATE10)
.state(States.STATE11)
.state(States.STATE12)
.state(States.STATE13)
.state(States.STATE14)
.state(States.STATE15)
.state(States.STATE16)
.state(States.STATE17)
.state(States.STATE18)
.state(States.STATE19)
.state(States.STATE20)
.state(States.STATEX)
.choice(States.STATEY)
.end(States.STATEZ);
addAdditionalStates(end);
}
@Override
public void configure(StateMachineTransitionConfigurer<States, Events> transitions) throws Exception {
// External transitions
ExternalTransitionConfigurer<States, Events> t1 = transitions
.withExternal().source(States.STATE1).target(States.STATE2).event(Events.EVENT1)
.and()
.withExternal().source(States.STATE2).target(States.STATE3).event(Events.EVENT2)
.and()
.withExternal().source(States.STATE3).target(States.STATE4).event(Events.EVENT3)
.and()
.withExternal().source(States.STATE4).target(States.STATE5).event(Events.EVENT4)
.and()
.withExternal().source(States.STATE5).target(States.STATE6).event(Events.EVENT5)
.and()
.withExternal().source(States.STATE6).target(States.STATE7).event(Events.EVENT6)
.and()
.withExternal().source(States.STATE7).target(States.STATE8).event(Events.EVENT7)
.and()
.withExternal().source(States.STATE8).target(States.STATE9).event(Events.EVENT8)
.and()
.withExternal().source(States.STATE9).target(States.STATE10).event(Events.EVENT9)
.and()
.withExternal().source(States.STATE10).target(States.STATE11).event(Events.EVENT10)
.and()
.withExternal().source(States.STATE11).target(States.STATE12).event(Events.EVENT11)
.and()
.withExternal().source(States.STATE12).target(States.STATE13).event(Events.EVENT12)
.and()
.withExternal().source(States.STATE13).target(States.STATE14).event(Events.EVENT13)
.and()
.withExternal().source(States.STATE14).target(States.STATE15).event(Events.EVENT14)
.and()
.withExternal().source(States.STATE15).target(States.STATE16).event(Events.EVENT15);
// EXTRA CHOICE
transitions.withChoice().source(States.STATEY).first(States.STATEX, guardVarEquals("value1")).last(States.STATEZ);
// Choice transitions (at least 10)
transitions
.withChoice()
.source(States.STATE16)
.first(States.STATE17, guardVarEquals("value1"))
.then(States.STATE18, guardVarEquals("value2"))
.last(States.STATE19)
.and()
.withChoice()
.source(States.STATE17)
.first(States.STATE20, guardEventHeaderEquals("header1", "foo"))
.last(States.STATE16)
.and()
.withChoice()
.source(States.STATE18)
.first(States.STATE19, guardVarEquals("value3"))
.last(States.STATE20)
.and()
.withChoice()
.source(States.STATE19)
.first(States.STATE1, guardVarEquals("reset"))
.last(States.STATE20)
.and()
.withChoice()
.source(States.STATE20)
.first(States.STATE5, guardEventHeaderEquals("header2", "bar"))
.last(States.STATE1)
.and()
.withChoice()
.source(States.STATE11)
.first(States.STATE13, guardVarEquals("goTo13"))
.then(States.STATE14, guardVarEquals("goTo14"))
.last(States.STATE15)
.and()
.withChoice()
.source(States.STATE12)
.first(States.STATE10, guardVarEquals("goBack10"))
.last(States.STATE11)
.and()
.withChoice()
.source(States.STATE14)
.first(States.STATE12, guardVarEquals("loop12"))
.last(States.STATE16)
.and()
.withChoice()
.source(States.STATE9)
.first(States.STATE8, guardVarEquals("stepBack"))
.then(States.STATE7, guardVarEquals("stepBackMore"))
.last(States.STATE6)
.and()
.withChoice()
.source(States.STATE8)
.first(States.STATE9, guardVarEquals("forward9"))
.last(States.STATE10);
addAdditionalTransitions(t1);
}
private Guard<States, Events> guardVarEquals(String expected) {
return context -> {
Object var = context.getExtendedState().getVariables().get("var");
return expected.equals(var);
};
}
private Guard<States, Events> guardEventHeaderEquals(String headerName, String expected) {
return context -> {
Object header = context.getMessageHeader(headerName);
return expected.equals(header);
};
}
private Action<States, Events> logEntryAction(String msg) {
return context -> System.out.println(msg);
}
private Action<States, Events> logExitAction(String msg) {
return context -> System.out.println(msg);
}
}

View File

@@ -0,0 +1,6 @@
package click.kamil.examples.statemachine.inheritancestate;
import org.springframework.statemachine.config.StateMachineConfigurerAdapter;
class AbstractStateMachineConfiguration<S,E> extends StateMachineConfigurerAdapter<S,E> {
}

View File

@@ -0,0 +1,8 @@
package click.kamil.examples.statemachine.inheritancestate;
public enum Events {
EVENT1, EVENT2, EVENT3, EVENT4, EVENT5,
EVENT6, EVENT7, EVENT8, EVENT9, EVENT10,
EVENT11, EVENT12, EVENT13, EVENT14, EVENT15,
EVENTX, EVENTY, EVENTZ
}

View File

@@ -0,0 +1,20 @@
package click.kamil.examples.statemachine.inheritancestate;
import org.springframework.statemachine.config.configurers.ExternalTransitionConfigurer;
import org.springframework.statemachine.config.configurers.StateConfigurer;
class OneStateMachineConfiguration extends AbstractOneStateMachineConfiguration {
@Override
protected void addAdditionalStates(StateConfigurer<States, Events> states) {
states.state(States.STATE_EXTRA_1);
}
@Override
protected void addAdditionalTransitions(ExternalTransitionConfigurer<States, Events> transitions) throws Exception {
transitions
.and()
.withExternal().event(Events.EVENTX)
.source(States.STATE5)
.target(States.STATE_EXTRA_1);
}
}

View File

@@ -0,0 +1,9 @@
package click.kamil.examples.statemachine.inheritancestate;
public enum States {
STATE1, STATE2, STATE3, STATE4, STATE5,
STATE6, STATE7, STATE8, STATE9, STATE10,
STATE11, STATE12, STATE13, STATE14, STATE15,
STATE16, STATE17, STATE18, STATE19, STATE20,
STATEX, STATEY, STATEZ, STATE_EXTRA_1
}

View File

@@ -0,0 +1,7 @@
package click.kamil.examples.statemachine.simple;
public enum OrderEvents {
FULFILL, PAY, CANCEL,
ABCD, NOPE, IGNORE
}

View File

@@ -0,0 +1,5 @@
package click.kamil.examples.statemachine.simple;
public enum OrderStates {
SUBMITTED, PAID, FULFILLED, CANCELED, PAID1, PAID2, PAID3, INVALID, HAPPEN, SKIPPED, NEVER
}

View File

@@ -1,4 +1,4 @@
package click.kamil.springstatemachineexporter.statemachine;
package click.kamil.examples.statemachine.simple;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
@@ -35,7 +35,6 @@ class SimpleEnumStateMachineConfiguration extends StateMachineConfigurerAdapter<
.and()
.withExternal().source(OrderStates.PAID).target(OrderStates.CANCELED).event(OrderEvents.CANCEL)
.and()
.withExternal().source(OrderStates.SUBMITTED).event(OrderEvents.ABCD)
// it needs to have target specified to get added to .transitions collections
@@ -88,6 +87,7 @@ class SimpleEnumStateMachineConfiguration extends StateMachineConfigurerAdapter<
.end(OrderStates.FULFILLED)
.end(OrderStates.CANCELED);
}
@Override
public void configure(StateMachineConfigurationConfigurer<OrderStates, OrderEvents> config) throws Exception {
StateMachineListenerAdapter<OrderStates, OrderEvents> listenerAdapter = new StateMachineListenerAdapter<>() {
@@ -100,4 +100,4 @@ class SimpleEnumStateMachineConfiguration extends StateMachineConfigurerAdapter<
.autoStartup(false)
.listener(listenerAdapter);
}
}
}

View File

@@ -1,4 +1,4 @@
package click.kamil.springstatemachineexporter.ast;
package click.kamil.springstatemachineexporter;
import click.kamil.springstatemachineexporter.ast.app.AstTransitionParser;
import click.kamil.springstatemachineexporter.ast.app.StateMachineStateConfigurationMethodFinder;

View File

@@ -1,11 +1,13 @@
package click.kamil.springstatemachineexporter;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class StatemachinedemoApplication {
public static void main(String[] args) {
SpringApplication.run(StatemachinedemoApplication.class, args);
}
}
//package click.kamil.springstatemachineexporter;
//
//import org.springframework.boot.SpringApplication;
//import org.springframework.boot.autoconfigure.SpringBootApplication;
//import org.springframework.context.annotation.ComponentScan;
//
//@SpringBootApplication
//@ComponentScan(basePackages = {"click.kamil.examples"})
//public class StatemachinedemoApplication {
// public static void main(String[] args) {
// SpringApplication.run(StatemachinedemoApplication.class, args);
// }
//}

View File

@@ -1,7 +1,7 @@
package click.kamil.springstatemachineexporter.springbased;
import click.kamil.springstatemachineexporter.statemachine.OrderEvents;
import click.kamil.springstatemachineexporter.statemachine.OrderStates;
import click.kamil.examples.statemachine.simple.OrderEvents;
import click.kamil.examples.statemachine.simple.OrderStates;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Qualifier;
@@ -46,10 +46,10 @@ class Runner implements ApplicationRunner {
}
abstract class BaseExporter {
@Qualifier("simpleEnumStateMachineFactory")
protected final StateMachineFactory<OrderStates, OrderEvents> factory;
protected BaseExporter(StateMachineFactory<OrderStates, OrderEvents> factory) {
protected BaseExporter(@Qualifier("simpleEnumStateMachineFactory") StateMachineFactory<OrderStates, OrderEvents> factory) {
this.factory = factory;
}

View File

@@ -1,6 +0,0 @@
package click.kamil.springstatemachineexporter.statemachine;
public enum OrderStates {
SUBMITTED, PAID, FULFILLED, CANCELED
,PAID1,PAID2, PAID3, INVALID, HAPPEN, SKIPPED, NEVER
}