Improve creation of StateMachineFactory

- Returning factory from StateMachineBuilder.Builder
- Fixes #845
This commit is contained in:
xJoeWoo
2020-06-14 23:41:02 +08:00
committed by Janne Valkealahti
parent c63a6624e9
commit 61c58cdee0
5 changed files with 68 additions and 61 deletions

View File

@@ -29,10 +29,7 @@ import org.springframework.statemachine.config.builders.StateMachineTransitionBu
import org.springframework.statemachine.config.builders.StateMachineTransitionConfigurer;
import org.springframework.statemachine.config.common.annotation.AnnotationBuilder;
import org.springframework.statemachine.config.common.annotation.ObjectPostProcessor;
import org.springframework.statemachine.config.model.DefaultStateMachineModel;
import org.springframework.statemachine.config.model.ConfigurationData;
import org.springframework.statemachine.config.model.StatesData;
import org.springframework.statemachine.config.model.TransitionsData;
/**
* {@code StateMachineBuilder} provides a builder pattern for
@@ -112,40 +109,40 @@ public class StateMachineBuilder {
}
/**
* Builds a {@link StateMachine}.
* Creates a {@link StateMachineFactory} from builder
*
* @return the state machine
* @return the factory to create a state machine
*/
public StateMachine<S, E> build() {
public StateMachineFactory<S, E> createFactory() {
try {
builder.apply(adapter);
StateMachineConfig<S, E> stateMachineConfig = builder.getOrBuild();
TransitionsData<S, E> stateMachineTransitions = stateMachineConfig.getTransitions();
StatesData<S, E> stateMachineStates = stateMachineConfig.getStates();
ConfigurationData<S, E> stateMachineConfigurationConfig = stateMachineConfig.getStateMachineConfigurationConfig();
ObjectStateMachineFactory<S, E> stateMachineFactory = null;
if (stateMachineConfig.getModel() != null && stateMachineConfig.getModel().getFactory() != null) {
stateMachineFactory = new ObjectStateMachineFactory<S, E>(
new DefaultStateMachineModel<S, E>(stateMachineConfigurationConfig, null, null),
stateMachineConfig.getModel().getFactory());
} else {
stateMachineFactory = new ObjectStateMachineFactory<S, E>(new DefaultStateMachineModel<S, E>(
stateMachineConfigurationConfig, stateMachineStates, stateMachineTransitions), null);
}
ObjectStateMachineFactory<S, E> stateMachineFactory = StateMachineFactory.create(builder);
ConfigurationData<S, E> stateMachineConfigurationConfig = builder.getOrBuild().stateMachineConfigurationConfig;
stateMachineFactory.setHandleAutostartup(stateMachineConfigurationConfig.isAutoStart());
if (stateMachineConfigurationConfig.getBeanFactory() != null) {
stateMachineFactory.setBeanFactory(stateMachineConfigurationConfig.getBeanFactory());
}
return stateMachineFactory.getStateMachine();
return stateMachineFactory;
} catch (Exception e) {
throw new StateMachineException("Error creating state machine factory", e);
}
}
/**
* Builds a {@link StateMachine}.
*
* @return the state machine
*/
public StateMachine<S, E> build() {
try {
return createFactory().getStateMachine();
} catch (Exception e) {
throw new StateMachineException("Error building state machine", e);
}
}
}
private static class BuilderStateMachineConfigurerAdapter<S extends Object, E extends Object>

View File

@@ -16,6 +16,11 @@
package org.springframework.statemachine.config;
import org.springframework.statemachine.StateMachine;
import org.springframework.statemachine.config.builders.StateMachineConfigBuilder;
import org.springframework.statemachine.config.model.ConfigurationData;
import org.springframework.statemachine.config.model.DefaultStateMachineModel;
import org.springframework.statemachine.config.model.StatesData;
import org.springframework.statemachine.config.model.TransitionsData;
import java.util.UUID;
@@ -53,4 +58,23 @@ public interface StateMachineFactory<S, E> {
* @return a new state machine instance.
*/
StateMachine<S, E> getStateMachine(UUID uuid);
static <S, E> ObjectStateMachineFactory<S, E> create(StateMachineConfigBuilder<S, E> builder) {
StateMachineConfig<S, E> stateMachineConfig = builder.getOrBuild();
TransitionsData<S, E> stateMachineTransitions = stateMachineConfig.getTransitions();
StatesData<S, E> stateMachineStates = stateMachineConfig.getStates();
ConfigurationData<S, E> stateMachineConfigurationConfig = stateMachineConfig.getStateMachineConfigurationConfig();
ObjectStateMachineFactory<S, E> stateMachineFactory = null;
if (stateMachineConfig.getModel() != null && stateMachineConfig.getModel().getFactory() != null) {
stateMachineFactory = new ObjectStateMachineFactory<S, E>(
new DefaultStateMachineModel<S, E>(stateMachineConfigurationConfig, null, null),
stateMachineConfig.getModel().getFactory());
} else {
stateMachineFactory = new ObjectStateMachineFactory<S, E>(new DefaultStateMachineModel<S, E>(
stateMachineConfigurationConfig, stateMachineStates, stateMachineTransitions), null);
}
return stateMachineFactory;
}
}

View File

@@ -38,14 +38,11 @@ import org.springframework.statemachine.config.EnableStateMachine;
import org.springframework.statemachine.config.ObjectStateMachineFactory;
import org.springframework.statemachine.config.StateMachineConfig;
import org.springframework.statemachine.config.StateMachineConfigurerAdapter;
import org.springframework.statemachine.config.StateMachineFactory;
import org.springframework.statemachine.config.builders.StateMachineConfigBuilder;
import org.springframework.statemachine.config.builders.StateMachineConfigurer;
import org.springframework.statemachine.config.common.annotation.AbstractImportingAnnotationConfiguration;
import org.springframework.statemachine.config.common.annotation.AnnotationConfigurer;
import org.springframework.statemachine.config.model.ConfigurationData;
import org.springframework.statemachine.config.model.DefaultStateMachineModel;
import org.springframework.statemachine.config.model.StatesData;
import org.springframework.statemachine.config.model.TransitionsData;
import org.springframework.statemachine.monitor.StateMachineMonitor;
import org.springframework.util.ClassUtils;
import org.springframework.util.StringUtils;
@@ -165,27 +162,15 @@ public class StateMachineConfiguration<S, E> extends
AnnotationConfigurer<StateMachineConfig<S, E>, StateMachineConfigBuilder<S, E>> configurer =
(AnnotationConfigurer<StateMachineConfig<S, E>, StateMachineConfigBuilder<S, E>>) getBeanFactory()
.getBean(ClassUtils.forName(clazzName, classLoader));
getBuilder().apply(configurer);
StateMachineConfigBuilder<S, E> builder = getBuilder();
builder.apply(configurer);
StateMachineConfig<S, E> stateMachineConfig = getBuilder().getOrBuild();
TransitionsData<S, E> stateMachineTransitions = stateMachineConfig.getTransitions();
StatesData<S, E> stateMachineStates = stateMachineConfig.getStates();
ConfigurationData<S, E> stateMachineConfigurationConfig = stateMachineConfig.getStateMachineConfigurationConfig();
ObjectStateMachineFactory<S, E> stateMachineFactory = null;
if (stateMachineConfig.getModel() != null && stateMachineConfig.getModel().getFactory() != null) {
stateMachineFactory = new ObjectStateMachineFactory<S, E>(
new DefaultStateMachineModel<S, E>(stateMachineConfigurationConfig, null, null),
stateMachineConfig.getModel().getFactory());
} else {
stateMachineFactory = new ObjectStateMachineFactory<S, E>(new DefaultStateMachineModel<S, E>(
stateMachineConfigurationConfig, stateMachineStates, stateMachineTransitions), null);
}
ObjectStateMachineFactory<S, E> stateMachineFactory = StateMachineFactory.create(builder);
stateMachineFactory.setBeanFactory(getBeanFactory());
stateMachineFactory.setContextEventsEnabled(contextEvents);
stateMachineFactory.setBeanName(beanName);
stateMachineFactory.setHandleAutostartup(stateMachineConfigurationConfig.isAutoStart());
stateMachineFactory.setHandleAutostartup(builder.getOrBuild().stateMachineConfigurationConfig.isAutoStart());
if (stateMachineMonitor != null) {
stateMachineFactory.setStateMachineMonitor(stateMachineMonitor);
}

View File

@@ -41,10 +41,6 @@ import org.springframework.statemachine.config.StateMachineFactory;
import org.springframework.statemachine.config.builders.StateMachineConfigBuilder;
import org.springframework.statemachine.config.common.annotation.AbstractImportingAnnotationConfiguration;
import org.springframework.statemachine.config.common.annotation.AnnotationConfigurer;
import org.springframework.statemachine.config.model.ConfigurationData;
import org.springframework.statemachine.config.model.DefaultStateMachineModel;
import org.springframework.statemachine.config.model.StatesData;
import org.springframework.statemachine.config.model.TransitionsData;
import org.springframework.util.ClassUtils;
/**
@@ -156,21 +152,7 @@ public class StateMachineFactoryConfiguration<S, E> extends
.getBean(ClassUtils.forName(clazzName, classLoader));
builder.apply(configurer);
StateMachineConfig<S, E> stateMachineConfig = builder.getOrBuild();
TransitionsData<S, E> stateMachineTransitions = stateMachineConfig.getTransitions();
StatesData<S, E> stateMachineStates = stateMachineConfig.getStates();
ConfigurationData<S, E> stateMachineConfigurationConfig = stateMachineConfig
.getStateMachineConfigurationConfig();
ObjectStateMachineFactory<S, E> objectStateMachineFactory = null;
if (stateMachineConfig.getModel() != null && stateMachineConfig.getModel().getFactory() != null) {
objectStateMachineFactory = new ObjectStateMachineFactory<S, E>(
new DefaultStateMachineModel<S, E>(stateMachineConfigurationConfig, null, null),
stateMachineConfig.getModel().getFactory());
} else {
objectStateMachineFactory = new ObjectStateMachineFactory<S, E>(new DefaultStateMachineModel<S, E>(
stateMachineConfigurationConfig, stateMachineStates, stateMachineTransitions), null);
}
ObjectStateMachineFactory<S, E> objectStateMachineFactory = StateMachineFactory.create(builder);
objectStateMachineFactory.setBeanFactory(beanFactory);
objectStateMachineFactory.setContextEventsEnabled(contextEvents);

View File

@@ -18,6 +18,7 @@ package org.springframework.statemachine;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
import static org.springframework.statemachine.TestUtils.doSendEventAndConsumeAll;
import static org.springframework.statemachine.TestUtils.doStartAndAssert;
import static org.springframework.statemachine.TestUtils.resolveFactory;
@@ -33,6 +34,7 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.statemachine.config.EnableStateMachineFactory;
import org.springframework.statemachine.config.EnumStateMachineConfigurerAdapter;
import org.springframework.statemachine.config.StateMachineFactory;
import org.springframework.statemachine.config.builders.StateMachineConfigBuilder;
import org.springframework.statemachine.config.builders.StateMachineConfigurationConfigurer;
import org.springframework.statemachine.config.builders.StateMachineStateConfigurer;
import org.springframework.statemachine.config.builders.StateMachineTransitionConfigurer;
@@ -46,6 +48,23 @@ public class StateMachineFactoryTests extends AbstractStateMachineTests {
return new AnnotationConfigApplicationContext();
}
/**
* {@link org.springframework.statemachine.config.ManualBuilderTests#testManualBuildConcept()}
*/
@Test
public void testCreate() throws Exception{
StateMachineConfigBuilder<TestStates, TestEvents> builder = new StateMachineConfigBuilder<>();
Config1 config = new Config1();
builder.apply(config);
StateMachineFactory<TestStates,TestEvents> factory = StateMachineFactory.create(builder);
StateMachine<TestStates,TestEvents> machine = factory.getStateMachine();
doStartAndAssert(machine);
assertThat(machine, notNullValue());
assertThat(machine.getState().getId(), is(TestStates.S1));
}
@Test
public void testMachineFromFactory() {
context.register(Config1.class);