event maching heuristic update
This commit is contained in:
37
state_machines/polymorphic_events_sample/.gitignore
vendored
Normal file
37
state_machines/polymorphic_events_sample/.gitignore
vendored
Normal file
@@ -0,0 +1,37 @@
|
||||
HELP.md
|
||||
.gradle
|
||||
build/
|
||||
!gradle/wrapper/gradle-wrapper.jar
|
||||
!**/src/main/**/build/
|
||||
!**/src/test/**/build/
|
||||
|
||||
### STS ###
|
||||
.apt_generated
|
||||
.classpath
|
||||
.factorypath
|
||||
.project
|
||||
.settings
|
||||
.springBeans
|
||||
.sts4-cache
|
||||
bin/
|
||||
!**/src/main/**/bin/
|
||||
!**/src/test/**/bin/
|
||||
|
||||
### IntelliJ IDEA ###
|
||||
.idea
|
||||
*.iws
|
||||
*.iml
|
||||
*.ipr
|
||||
out/
|
||||
!**/src/main/**/out/
|
||||
!**/src/test/**/out/
|
||||
|
||||
### NetBeans ###
|
||||
/nbproject/private/
|
||||
/nbbuild/
|
||||
/dist/
|
||||
/nbdist/
|
||||
/.nb-gradle/
|
||||
|
||||
### VS Code ###
|
||||
.vscode/
|
||||
45
state_machines/polymorphic_events_sample/build.gradle
Normal file
45
state_machines/polymorphic_events_sample/build.gradle
Normal file
@@ -0,0 +1,45 @@
|
||||
plugins {
|
||||
id 'java'
|
||||
id 'org.springframework.boot' version '3.5.3'
|
||||
id 'io.spring.dependency-management' version '1.1.7'
|
||||
}
|
||||
|
||||
group = 'click.kamil'
|
||||
version = '0.0.1-SNAPSHOT'
|
||||
|
||||
java {
|
||||
toolchain {
|
||||
languageVersion = JavaLanguageVersion.of(21)
|
||||
}
|
||||
}
|
||||
|
||||
repositories {
|
||||
mavenCentral()
|
||||
}
|
||||
|
||||
configurations {
|
||||
compileOnly {
|
||||
extendsFrom annotationProcessor
|
||||
}
|
||||
}
|
||||
|
||||
repositories {
|
||||
mavenCentral()
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation 'org.springframework.boot:spring-boot-starter'
|
||||
implementation 'org.springframework.statemachine:spring-statemachine-starter:3.2.0'
|
||||
|
||||
compileOnly 'org.projectlombok:lombok'
|
||||
annotationProcessor 'org.projectlombok:lombok'
|
||||
testImplementation 'org.springframework.boot:spring-boot-starter-test'
|
||||
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
|
||||
}
|
||||
|
||||
tasks.named('test') {
|
||||
useJUnitPlatform()
|
||||
}
|
||||
|
||||
bootJar { enabled = false }
|
||||
jar { enabled = true }
|
||||
@@ -0,0 +1,8 @@
|
||||
package click.kamil.examples.statemachine.polymorphic;
|
||||
|
||||
public class AbcdEvent implements CustomCodeEventInterface {
|
||||
@Override
|
||||
public OrderEvents resolveEventCode() {
|
||||
return OrderEvents.ABCD;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
package click.kamil.examples.statemachine.polymorphic;
|
||||
public interface BaseEvent { OrderEvents getType(); }
|
||||
@@ -0,0 +1,4 @@
|
||||
package click.kamil.examples.statemachine.polymorphic;
|
||||
public class CancelEvent implements BaseEvent {
|
||||
@Override public OrderEvents getType() { return OrderEvents.CANCEL; }
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package click.kamil.examples.statemachine.polymorphic;
|
||||
|
||||
public interface CustomCodeEventInterface {
|
||||
OrderEvents resolveEventCode();
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package click.kamil.examples.statemachine.polymorphic;
|
||||
|
||||
public class EventBuilder {
|
||||
public static BaseEvent buildEvent() {
|
||||
return new FulfillEvent();
|
||||
}
|
||||
|
||||
public BaseEvent buildInstanceEvent() {
|
||||
return new CancelEvent();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
package click.kamil.examples.statemachine.polymorphic;
|
||||
public class FulfillEvent implements BaseEvent {
|
||||
@Override public OrderEvents getType() { return OrderEvents.FULFILL; }
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package click.kamil.examples.statemachine.polymorphic;
|
||||
|
||||
public class MysteryPayload implements CustomCodeEventInterface {
|
||||
@Override
|
||||
public OrderEvents resolveEventCode() {
|
||||
int a = (int) (Math.random() * 10);
|
||||
return a > 5 ? OrderEvents.ABCD : OrderEvents.PAY;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
package click.kamil.examples.statemachine.polymorphic;
|
||||
public enum OrderEvents { FULFILL, PAY, CANCEL, ABCD, NOPE, IGNORE }
|
||||
@@ -0,0 +1,23 @@
|
||||
package click.kamil.examples.statemachine.polymorphic;
|
||||
import org.springframework.statemachine.StateMachine;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.messaging.support.MessageBuilder;
|
||||
|
||||
@Service
|
||||
public class OrderService {
|
||||
private final StateMachine<OrderStates, OrderEvents> sm;
|
||||
|
||||
public OrderService(StateMachine<OrderStates, OrderEvents> sm) { this.sm = sm; }
|
||||
|
||||
public void processEvent(BaseEvent event) {
|
||||
sm.sendEvent(event.getType());
|
||||
}
|
||||
|
||||
public void processPayloadEvent(Object payload) {
|
||||
sm.sendEvent(MessageBuilder.withPayload(payload).build());
|
||||
}
|
||||
|
||||
public void processCustomEvent(CustomCodeEventInterface event) {
|
||||
sm.sendEvent(event.resolveEventCode());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
package click.kamil.examples.statemachine.polymorphic;
|
||||
public enum OrderStates { SUBMITTED, PAID, FULFILLED, CANCELED, PAID1, PAID2, PAID3, INVALID, HAPPEN, SKIPPED, NEVER }
|
||||
@@ -0,0 +1,4 @@
|
||||
package click.kamil.examples.statemachine.polymorphic;
|
||||
public class PayEvent implements BaseEvent {
|
||||
@Override public OrderEvents getType() { return OrderEvents.PAY; }
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
package click.kamil.examples.statemachine.polymorphic;
|
||||
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
public class PolymorphicController {
|
||||
|
||||
private final OrderService orderService;
|
||||
|
||||
public PolymorphicController(OrderService orderService) {
|
||||
this.orderService = orderService;
|
||||
}
|
||||
|
||||
@PostMapping("/pay")
|
||||
public void pay() {
|
||||
orderService.processEvent(new PayEvent());
|
||||
}
|
||||
|
||||
@PostMapping("/fulfill")
|
||||
public void fulfill() {
|
||||
orderService.processEvent(new FulfillEvent());
|
||||
}
|
||||
|
||||
@PostMapping("/cancel")
|
||||
public void cancel() {
|
||||
orderService.processEvent(new CancelEvent());
|
||||
}
|
||||
|
||||
@PostMapping("/payload-pay")
|
||||
public void payloadPay() {
|
||||
orderService.processPayloadEvent(new PayEvent());
|
||||
}
|
||||
|
||||
@PostMapping("/pay-variable")
|
||||
public void payVariable() {
|
||||
PayEvent event = new PayEvent();
|
||||
orderService.processEvent(event);
|
||||
}
|
||||
|
||||
@PostMapping("/pay-cast")
|
||||
public void payCast() {
|
||||
orderService.processEvent((BaseEvent) new PayEvent());
|
||||
}
|
||||
|
||||
@PostMapping("/pay-ternary")
|
||||
public void payTernary(boolean isPay) {
|
||||
orderService.processEvent(isPay ? new PayEvent() : new CancelEvent());
|
||||
}
|
||||
|
||||
@PostMapping("/pay-list")
|
||||
public void payList() {
|
||||
java.util.List<BaseEvent> events = java.util.Arrays.asList(new PayEvent(), new FulfillEvent());
|
||||
for (BaseEvent event : events) {
|
||||
orderService.processEvent(event);
|
||||
}
|
||||
}
|
||||
|
||||
@PostMapping("/pay-builder-static")
|
||||
public void payBuilderStatic() {
|
||||
orderService.processEvent(EventBuilder.buildEvent());
|
||||
}
|
||||
|
||||
@PostMapping("/pay-builder-instance")
|
||||
public void payBuilderInstance() {
|
||||
EventBuilder builder = new EventBuilder();
|
||||
orderService.processEvent(builder.buildInstanceEvent());
|
||||
}
|
||||
|
||||
@PostMapping("/abcd")
|
||||
public void abcd() {
|
||||
orderService.processCustomEvent(new AbcdEvent());
|
||||
}
|
||||
|
||||
@PostMapping("/mystery")
|
||||
public void mystery() {
|
||||
orderService.processCustomEvent(new MysteryPayload());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package click.kamil.examples.statemachine.polymorphic;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.statemachine.config.EnableStateMachine;
|
||||
import org.springframework.statemachine.config.EnumStateMachineConfigurerAdapter;
|
||||
import org.springframework.statemachine.config.builders.StateMachineStateConfigurer;
|
||||
import org.springframework.statemachine.config.builders.StateMachineTransitionConfigurer;
|
||||
|
||||
@Configuration
|
||||
@EnableStateMachine
|
||||
public class PolymorphicStateMachineConfiguration extends EnumStateMachineConfigurerAdapter<OrderStates, OrderEvents> {
|
||||
@Override
|
||||
public void configure(StateMachineStateConfigurer<OrderStates, OrderEvents> states) throws Exception {
|
||||
states.withStates().initial(OrderStates.SUBMITTED).state(OrderStates.PAID).state(OrderStates.FULFILLED).state(OrderStates.CANCELED);
|
||||
}
|
||||
@Override
|
||||
public void configure(StateMachineTransitionConfigurer<OrderStates, OrderEvents> transitions) throws Exception {
|
||||
transitions
|
||||
.withExternal().source(OrderStates.SUBMITTED).target(OrderStates.PAID).event(OrderEvents.PAY).and()
|
||||
.withExternal().source(OrderStates.PAID).target(OrderStates.FULFILLED).event(OrderEvents.FULFILL).and()
|
||||
.withExternal().source(OrderStates.SUBMITTED).target(OrderStates.CANCELED).event(OrderEvents.CANCEL).and()
|
||||
.withExternal().source(OrderStates.PAID).target(OrderStates.CANCELED).event(OrderEvents.ABCD);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package click.kamil.examples.statemachine.polymorphic.app;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
@SpringBootApplication
|
||||
public class StateMachineApplication {
|
||||
public static void main(String[] args) { SpringApplication.run(StateMachineApplication.class, args); }
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
spring.application.name=statemachinedemo
|
||||
Reference in New Issue
Block a user