move files into modules
This commit is contained in:
45
state_machines/simple_state_machine/.gitignore
vendored
Normal file
45
state_machines/simple_state_machine/.gitignore
vendored
Normal file
@@ -0,0 +1,45 @@
|
||||
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/
|
||||
|
||||
|
||||
|
||||
#### added
|
||||
*.png
|
||||
*.dot
|
||||
*.scxml.xml
|
||||
Combined.java
|
||||
42
state_machines/simple_state_machine/build.gradle
Normal file
42
state_machines/simple_state_machine/build.gradle
Normal file
@@ -0,0 +1,42 @@
|
||||
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()
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package click.kamil.examples.statemachine.simple;
|
||||
|
||||
public enum OrderEvents {
|
||||
FULFILL, PAY, CANCEL,
|
||||
|
||||
ABCD, NOPE, IGNORE
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package click.kamil.examples.statemachine.simple;
|
||||
|
||||
public enum OrderStates {
|
||||
SUBMITTED, PAID, FULFILLED, CANCELED, PAID1, PAID2, PAID3, INVALID, HAPPEN, SKIPPED, NEVER
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
package click.kamil.examples.statemachine.simple;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.statemachine.StateContext;
|
||||
import org.springframework.statemachine.action.Action;
|
||||
import org.springframework.statemachine.config.EnableStateMachineFactory;
|
||||
import org.springframework.statemachine.config.StateMachineConfigurerAdapter;
|
||||
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.guard.Guard;
|
||||
import org.springframework.statemachine.listener.StateMachineListenerAdapter;
|
||||
import org.springframework.statemachine.state.State;
|
||||
|
||||
@Configuration
|
||||
@EnableStateMachineFactory(name = "simpleEnumStateMachineFactory")
|
||||
@Slf4j
|
||||
class SimpleEnumStateMachineConfiguration extends StateMachineConfigurerAdapter<OrderStates, OrderEvents> {
|
||||
@Override
|
||||
public void configure(StateMachineTransitionConfigurer<OrderStates, OrderEvents> transitions) throws Exception {
|
||||
Guard<OrderStates, OrderEvents> guard1 = null;
|
||||
Action<OrderStates, OrderEvents> action2 = null;
|
||||
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).guard(new Guard<OrderStates, OrderEvents>() {
|
||||
@Override
|
||||
public boolean evaluate(StateContext<OrderStates, OrderEvents> context) {
|
||||
return false;
|
||||
}
|
||||
})
|
||||
.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
|
||||
// .withExternal().source(OrderStates.SUBMITTED).target(OrderStates.SUBMITTED).event(OrderEvents.ABCD)
|
||||
//
|
||||
.and()
|
||||
.withChoice()
|
||||
.source(OrderStates.SUBMITTED)
|
||||
.first(OrderStates.PAID2, new Guard<OrderStates, OrderEvents>() {
|
||||
@Override
|
||||
public boolean evaluate(StateContext<OrderStates, OrderEvents> context) {
|
||||
return false;
|
||||
}
|
||||
})
|
||||
.last(OrderStates.PAID3)
|
||||
.and()
|
||||
.withChoice().source(OrderStates.PAID)
|
||||
.first(OrderStates.PAID1, new Guard<OrderStates, OrderEvents>() {
|
||||
@Override
|
||||
public boolean evaluate(StateContext<OrderStates, OrderEvents> context) {
|
||||
return true;
|
||||
}
|
||||
})
|
||||
.then(OrderStates.PAID2, guard1)
|
||||
.then(OrderStates.HAPPEN, new Guard<OrderStates, OrderEvents>() {
|
||||
@Override
|
||||
public boolean evaluate(StateContext<OrderStates, OrderEvents> context) {
|
||||
return false;
|
||||
}
|
||||
})
|
||||
.last(OrderStates.PAID3)
|
||||
.and().withExternal().source(OrderStates.PAID2).target(OrderStates.CANCELED)
|
||||
.and().withExternal().source(OrderStates.PAID3).target(OrderStates.CANCELED)
|
||||
// .and().withLocal().source(OrderStates.SUBMITTED).target(OrderStates.CANCELED)
|
||||
.and().withExternal().source(OrderStates.PAID1).event(OrderEvents.ABCD).action(new Action<OrderStates, OrderEvents>() {
|
||||
@Override
|
||||
public void execute(StateContext<OrderStates, OrderEvents> context) {
|
||||
;
|
||||
}
|
||||
}).action(action2);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void configure(StateMachineStateConfigurer<OrderStates, OrderEvents> states) throws Exception {
|
||||
states.withStates()
|
||||
.initial(OrderStates.SUBMITTED)
|
||||
.state(OrderStates.PAID)
|
||||
.state(OrderStates.PAID2)
|
||||
.state(OrderStates.PAID3)
|
||||
.end(OrderStates.FULFILLED)
|
||||
.end(OrderStates.CANCELED);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void configure(StateMachineConfigurationConfigurer<OrderStates, OrderEvents> config) throws Exception {
|
||||
StateMachineListenerAdapter<OrderStates, OrderEvents> listenerAdapter = new StateMachineListenerAdapter<>() {
|
||||
@Override
|
||||
public void stateChanged(State<OrderStates, OrderEvents> from, State<OrderStates, OrderEvents> to) {
|
||||
log.info("state changed from {} to {}", from, to);
|
||||
}
|
||||
};
|
||||
config.withConfiguration()
|
||||
.autoStartup(false)
|
||||
.listener(listenerAdapter);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package click.kamil.examples.statemachine.simple.app;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
class StateMachineApplication {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(StateMachineApplication.class, args);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
spring.application.name=statemachinedemo
|
||||
@@ -0,0 +1,11 @@
|
||||
package click.kamil.examples.statemachine;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class MyTest {
|
||||
|
||||
@Test
|
||||
void contextLoads() {
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user