SS refactor (#1555)

This commit is contained in:
Grzegorz Piwowarek 2017-04-01 11:00:44 +02:00 committed by GitHub
parent 09d4c6f873
commit 2c50b4f1b7
6 changed files with 91 additions and 54 deletions

View File

@ -1,9 +1,5 @@
package com.baeldung.spring.statemachine.config;
import java.util.Arrays;
import java.util.HashSet;
import java.util.logging.Logger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.statemachine.action.Action;
@ -14,6 +10,10 @@ import org.springframework.statemachine.config.builders.StateMachineStateConfigu
import org.springframework.statemachine.config.builders.StateMachineTransitionConfigurer;
import org.springframework.statemachine.guard.Guard;
import java.util.Arrays;
import java.util.HashSet;
import java.util.logging.Logger;
@Configuration
@EnableStateMachine
public class SimpleStateMachineConfiguration extends StateMachineConfigurerAdapter<String, String> {
@ -21,94 +21,126 @@ public class SimpleStateMachineConfiguration extends StateMachineConfigurerAdapt
public static final Logger LOGGER = Logger.getLogger(SimpleStateMachineConfiguration.class.getName());
@Override
public void configure(StateMachineConfigurationConfigurer<String, String> config)
throws Exception {
public void configure(StateMachineConfigurationConfigurer<String, String> config) throws Exception {
config
.withConfiguration()
.autoStartup(true)
.listener(new StateMachineListener());
.withConfiguration()
.autoStartup(true)
.listener(new StateMachineListener());
}
@Override
public void configure(StateMachineStateConfigurer<String, String> states) throws Exception {
states
.withStates()
.initial("SI")
.end("SF")
.states(new HashSet<>(Arrays.asList("S1", "S2")))
.stateEntry("S3", entryAction())
.stateExit("S3", exitAction())
.state("S4", executeAction(), errorAction())
.stateDo("S5", executeAction());
.withStates()
.initial("SI")
.end("SF")
.states(new HashSet<>(Arrays.asList("S1", "S2")))
.stateEntry("S3", entryAction())
.stateExit("S3", exitAction())
.state("S4", executeAction(), errorAction())
.stateDo("S5", executeAction());
}
@Override
public void configure(StateMachineTransitionConfigurer<String, String> transitions) throws Exception {
transitions.withExternal()
.source("SI").target("S1").event("E1").action(initAction())
.and().withExternal()
.source("S1").target("S2").event("E2")
.and().withExternal()
.source("SI").target("S3").event("E3")
.and().withExternal()
.source("S3").target("S4").event("E4")
.and().withExternal()
.source("S4").target("S5").event("E5")
.and().withExternal()
.source("S5").target("SF").event("end").guard(simpleGuard());
transitions
.withExternal()
.source("SI")
.target("S1")
.event("E1")
.action(initAction())
.and()
.withExternal()
.source("S1")
.target("S2")
.event("E2")
.and()
.withExternal()
.source("SI")
.target("S3")
.event("E3")
.and()
.withExternal()
.source("S3")
.target("S4")
.event("E4")
.and()
.withExternal()
.source("S4")
.target("S5")
.event("E5")
.and()
.withExternal()
.source("S5")
.target("SF")
.event("end")
.guard(simpleGuard());
}
@Bean
public Guard<String, String> simpleGuard() {
return (ctx) -> {
int approvalCount = (int) ctx.getExtendedState().getVariables().getOrDefault("approvalCount", 0);
int approvalCount = (int) ctx
.getExtendedState()
.getVariables()
.getOrDefault("approvalCount", 0);
return approvalCount > 0;
};
}
@Bean
public Action<String, String> entryAction() {
return (ctx) -> {
LOGGER.info("Entry " + ctx.getTarget().getId());
};
return ctx -> LOGGER.info("Entry " + ctx
.getTarget()
.getId());
}
@Bean
public Action<String, String> doAction() {
return (ctx) -> {
LOGGER.info("Do " + ctx.getTarget().getId());
};
return ctx -> LOGGER.info("Do " + ctx
.getTarget()
.getId());
}
@Bean
public Action<String, String> executeAction() {
return (ctx) -> {
LOGGER.info("Execute " + ctx.getTarget().getId());
int approvals = (int) ctx.getExtendedState().getVariables().getOrDefault("approvalCount", 0);
return ctx -> {
LOGGER.info("Execute " + ctx
.getTarget()
.getId());
int approvals = (int) ctx
.getExtendedState()
.getVariables()
.getOrDefault("approvalCount", 0);
approvals++;
ctx.getExtendedState().getVariables().put("approvalCount", approvals);
ctx
.getExtendedState()
.getVariables()
.put("approvalCount", approvals);
};
}
@Bean
public Action<String, String> exitAction() {
return (ctx) -> {
LOGGER.info("Exit " + ctx.getSource().getId() + " -> " + ctx.getTarget().getId());
};
return ctx -> LOGGER.info("Exit " + ctx
.getSource()
.getId() + " -> " + ctx
.getTarget()
.getId());
}
@Bean
public Action<String, String> errorAction() {
return (ctx) -> {
LOGGER.info("Error " + ctx.getSource().getId() + ctx.getException());
};
return ctx -> LOGGER.info("Error " + ctx
.getSource()
.getId() + ctx.getException());
}
@Bean
public Action<String, String> initAction() {
return (ctx) -> {
LOGGER.info(ctx.getTarget().getId());
};
return ctx -> LOGGER.info(ctx
.getTarget()
.getId());
}
}

View File

@ -6,6 +6,7 @@ import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.statemachine.StateMachine;
import org.springframework.test.context.ContextConfiguration;
@ -21,7 +22,7 @@ import static org.junit.Assert.assertTrue;
@ContextConfiguration(classes = ForkJoinStateMachineConfiguration.class)
public class ForkJoinStateMachineTest {
@Resource
@Autowired
private StateMachine<String, String> stateMachine;
@Before

View File

@ -6,6 +6,7 @@ import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.statemachine.StateMachine;
import org.springframework.test.context.ContextConfiguration;
@ -20,7 +21,7 @@ import static org.junit.Assert.assertEquals;
@ContextConfiguration(classes = HierarchicalStateMachineConfiguration.class)
public class HierarchicalStateMachineTest {
@Resource
@Autowired
private StateMachine<String, String> stateMachine;
@Before

View File

@ -7,6 +7,7 @@ import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.statemachine.StateMachine;
import org.springframework.test.context.ContextConfiguration;
@ -18,7 +19,7 @@ import javax.annotation.Resource;
@ContextConfiguration(classes = JunctionStateMachineConfiguration.class)
public class JunctionStateMachineTest {
@Resource
@Autowired
private StateMachine<String, String> stateMachine;
@Before

View File

@ -8,6 +8,7 @@ import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.statemachine.StateMachine;
import org.springframework.test.context.ContextConfiguration;
@ -22,7 +23,7 @@ import static org.junit.Assert.assertTrue;
@ContextConfiguration(classes = SimpleEnumStateMachineConfiguration.class)
public class StateEnumMachineTest {
@Resource
@Autowired
private StateMachine<ApplicationReviewStates, ApplicationReviewEvents> stateMachine;
@Before

View File

@ -7,6 +7,7 @@ import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.statemachine.StateMachine;
@ -21,7 +22,7 @@ import javax.annotation.Resource;
@ContextConfiguration(classes = SimpleStateMachineConfiguration.class)
public class StateMachineIntegrationTest {
@Resource
@Autowired
private StateMachine<String, String> stateMachine;
@Before