Add support for join/fork states
- Adding join/fork state construction - Relates to #262
This commit is contained in:
@@ -443,6 +443,43 @@ public class JpaRepositoryTests extends AbstractJpaRepositoryTests {
|
||||
plan.test();
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
public void testMachine10() throws Exception {
|
||||
context.register(Config10.class, FactoryConfig.class);
|
||||
context.refresh();
|
||||
StateMachineFactory<String, String> stateMachineFactory = context.getBean(StateMachineFactory.class);
|
||||
StateMachine<String, String> stateMachine = stateMachineFactory.getStateMachine();
|
||||
|
||||
StateMachineTestPlan<String, String> plan =
|
||||
StateMachineTestPlanBuilder.<String, String>builder()
|
||||
.stateMachine(stateMachine)
|
||||
.step().expectStates("SI").and()
|
||||
.step().sendEvent("E1").expectStates("S2", "S21", "S31").and()
|
||||
.build();
|
||||
plan.test();
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
public void testMachine11() throws Exception {
|
||||
context.register(Config11.class, FactoryConfig.class);
|
||||
context.refresh();
|
||||
StateMachineFactory<String, String> stateMachineFactory = context.getBean(StateMachineFactory.class);
|
||||
StateMachine<String, String> stateMachine = stateMachineFactory.getStateMachine();
|
||||
|
||||
StateMachineTestPlan<String, String> plan =
|
||||
StateMachineTestPlanBuilder.<String, String>builder()
|
||||
.stateMachine(stateMachine)
|
||||
.step().expectStates("SI").and()
|
||||
.step().sendEvent("E1").expectStates("S2", "S20", "S30").and()
|
||||
.step().sendEvent("E2").expectStates("S2", "S21", "S30").and()
|
||||
.step().sendEvent("E3").expectStates("S4").and()
|
||||
.step().sendEvent("E4").expectStates("SI").and()
|
||||
.build();
|
||||
plan.test();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPopulate1() {
|
||||
context.register(Config2.class);
|
||||
@@ -624,6 +661,28 @@ public class JpaRepositoryTests extends AbstractJpaRepositoryTests {
|
||||
}
|
||||
}
|
||||
|
||||
@EnableAutoConfiguration
|
||||
static class Config10 {
|
||||
|
||||
@Bean
|
||||
public StateMachineJackson2RepositoryPopulatorFactoryBean jackson2RepositoryPopulatorFactoryBean() {
|
||||
StateMachineJackson2RepositoryPopulatorFactoryBean factoryBean = new StateMachineJackson2RepositoryPopulatorFactoryBean();
|
||||
factoryBean.setResources(new Resource[]{new ClassPathResource("data10.json")});
|
||||
return factoryBean;
|
||||
}
|
||||
}
|
||||
|
||||
@EnableAutoConfiguration
|
||||
static class Config11 {
|
||||
|
||||
@Bean
|
||||
public StateMachineJackson2RepositoryPopulatorFactoryBean jackson2RepositoryPopulatorFactoryBean() {
|
||||
StateMachineJackson2RepositoryPopulatorFactoryBean factoryBean = new StateMachineJackson2RepositoryPopulatorFactoryBean();
|
||||
factoryBean.setResources(new Resource[]{new ClassPathResource("data11.json")});
|
||||
return factoryBean;
|
||||
}
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@EnableStateMachineFactory
|
||||
public static class FactoryConfig extends StateMachineConfigurerAdapter<String, String> {
|
||||
|
||||
71
spring-statemachine-data/jpa/src/test/resources/data10.json
Normal file
71
spring-statemachine-data/jpa/src/test/resources/data10.json
Normal file
@@ -0,0 +1,71 @@
|
||||
[
|
||||
{
|
||||
"@id": "1",
|
||||
"_class": "org.springframework.statemachine.data.jpa.JpaRepositoryState",
|
||||
"initial": true,
|
||||
"state": "SI"
|
||||
},
|
||||
{
|
||||
"@id": "2",
|
||||
"_class": "org.springframework.statemachine.data.jpa.JpaRepositoryState",
|
||||
"state": "S1",
|
||||
"kind": "FORK"
|
||||
},
|
||||
{
|
||||
"@id": "3",
|
||||
"_class": "org.springframework.statemachine.data.jpa.JpaRepositoryState",
|
||||
"state": "S2"
|
||||
},
|
||||
{
|
||||
"@id": "4",
|
||||
"_class": "org.springframework.statemachine.data.jpa.JpaRepositoryState",
|
||||
"state": "SF",
|
||||
"kind": "END"
|
||||
},
|
||||
{
|
||||
"@id": "5",
|
||||
"_class": "org.springframework.statemachine.data.jpa.JpaRepositoryState",
|
||||
"initial": true,
|
||||
"region": "r2",
|
||||
"parentState": "3",
|
||||
"state": "S20"
|
||||
},
|
||||
{
|
||||
"@id": "6",
|
||||
"_class": "org.springframework.statemachine.data.jpa.JpaRepositoryState",
|
||||
"region": "r2",
|
||||
"parentState": "3",
|
||||
"state": "S21"
|
||||
},
|
||||
{
|
||||
"@id": "7",
|
||||
"_class": "org.springframework.statemachine.data.jpa.JpaRepositoryState",
|
||||
"initial": true,
|
||||
"region": "r3",
|
||||
"parentState": "3",
|
||||
"state": "S30"
|
||||
},
|
||||
{
|
||||
"@id": "8",
|
||||
"_class": "org.springframework.statemachine.data.jpa.JpaRepositoryState",
|
||||
"region": "r3",
|
||||
"parentState": "3",
|
||||
"state": "S31"
|
||||
},
|
||||
{
|
||||
"_class": "org.springframework.statemachine.data.jpa.JpaRepositoryTransition",
|
||||
"source": "1",
|
||||
"target": "2",
|
||||
"event": "E1"
|
||||
},
|
||||
{
|
||||
"_class": "org.springframework.statemachine.data.jpa.JpaRepositoryTransition",
|
||||
"source": "2",
|
||||
"target": "6"
|
||||
},
|
||||
{
|
||||
"_class": "org.springframework.statemachine.data.jpa.JpaRepositoryTransition",
|
||||
"source": "2",
|
||||
"target": "8"
|
||||
}
|
||||
]
|
||||
93
spring-statemachine-data/jpa/src/test/resources/data11.json
Normal file
93
spring-statemachine-data/jpa/src/test/resources/data11.json
Normal file
@@ -0,0 +1,93 @@
|
||||
[
|
||||
{
|
||||
"@id": "1",
|
||||
"_class": "org.springframework.statemachine.data.jpa.JpaRepositoryState",
|
||||
"initial": true,
|
||||
"state": "SI"
|
||||
},
|
||||
{
|
||||
"@id": "2",
|
||||
"_class": "org.springframework.statemachine.data.jpa.JpaRepositoryState",
|
||||
"state": "S2"
|
||||
},
|
||||
{
|
||||
"@id": "3",
|
||||
"_class": "org.springframework.statemachine.data.jpa.JpaRepositoryState",
|
||||
"state": "S3",
|
||||
"kind": "JOIN"
|
||||
},
|
||||
{
|
||||
"@id": "4",
|
||||
"_class": "org.springframework.statemachine.data.jpa.JpaRepositoryState",
|
||||
"state": "S4"
|
||||
},
|
||||
{
|
||||
"@id": "5",
|
||||
"_class": "org.springframework.statemachine.data.jpa.JpaRepositoryState",
|
||||
"initial": true,
|
||||
"region": "r2",
|
||||
"parentState": "2",
|
||||
"state": "S20"
|
||||
},
|
||||
{
|
||||
"@id": "6",
|
||||
"_class": "org.springframework.statemachine.data.jpa.JpaRepositoryState",
|
||||
"region": "r2",
|
||||
"parentState": "2",
|
||||
"state": "S21"
|
||||
},
|
||||
{
|
||||
"@id": "7",
|
||||
"_class": "org.springframework.statemachine.data.jpa.JpaRepositoryState",
|
||||
"initial": true,
|
||||
"region": "r3",
|
||||
"parentState": "2",
|
||||
"state": "S30"
|
||||
},
|
||||
{
|
||||
"@id": "8",
|
||||
"_class": "org.springframework.statemachine.data.jpa.JpaRepositoryState",
|
||||
"region": "r3",
|
||||
"parentState": "2",
|
||||
"state": "S31"
|
||||
},
|
||||
{
|
||||
"_class": "org.springframework.statemachine.data.jpa.JpaRepositoryTransition",
|
||||
"source": "1",
|
||||
"target": "2",
|
||||
"event": "E1"
|
||||
},
|
||||
{
|
||||
"_class": "org.springframework.statemachine.data.jpa.JpaRepositoryTransition",
|
||||
"source": "5",
|
||||
"target": "6",
|
||||
"event": "E2"
|
||||
},
|
||||
{
|
||||
"_class": "org.springframework.statemachine.data.jpa.JpaRepositoryTransition",
|
||||
"source": "7",
|
||||
"target": "8",
|
||||
"event": "E3"
|
||||
},
|
||||
{
|
||||
"_class": "org.springframework.statemachine.data.jpa.JpaRepositoryTransition",
|
||||
"source": "7",
|
||||
"target": "3"
|
||||
},
|
||||
{
|
||||
"_class": "org.springframework.statemachine.data.jpa.JpaRepositoryTransition",
|
||||
"source": "8",
|
||||
"target": "3"
|
||||
},
|
||||
{
|
||||
"_class": "org.springframework.statemachine.data.jpa.JpaRepositoryTransition",
|
||||
"source": "3",
|
||||
"target": "4"
|
||||
},
|
||||
{
|
||||
"_class": "org.springframework.statemachine.data.jpa.JpaRepositoryTransition",
|
||||
"source": "4",
|
||||
"target": "1",
|
||||
"event": "E4"
|
||||
}
|
||||
]
|
||||
@@ -161,6 +161,10 @@ public class RepositoryStateMachineModelFactory extends AbstractStateMachineMode
|
||||
Collection<HistoryData<String, String>> historys = new ArrayList<HistoryData<String, String>>();
|
||||
Map<String, LinkedList<ChoiceData<String, String>>> choices = new HashMap<String, LinkedList<ChoiceData<String,String>>>();
|
||||
Map<String, LinkedList<JunctionData<String, String>>> junctions = new HashMap<String, LinkedList<JunctionData<String,String>>>();
|
||||
Map<String, List<String>> forks = new HashMap<String, List<String>>();
|
||||
Map<String, List<String>> joins = new HashMap<String, List<String>>();
|
||||
|
||||
|
||||
for (RepositoryTransition t : transitionRepository.findByMachineId(machineId)) {
|
||||
|
||||
Collection<Action<String, String>> actions = new ArrayList<Action<String, String>>();
|
||||
@@ -217,6 +221,20 @@ public class RepositoryStateMachineModelFactory extends AbstractStateMachineMode
|
||||
} else {
|
||||
list.addFirst(new JunctionData<String, String>(t.getSource().getState(), t.getTarget().getState(), guard));
|
||||
}
|
||||
} else if (t.getSource().getKind() == PseudoStateKind.FORK) {
|
||||
List<String> list = forks.get(t.getSource().getState());
|
||||
if (list == null) {
|
||||
list = new ArrayList<String>();
|
||||
forks.put(t.getSource().getState(), list);
|
||||
}
|
||||
list.add(t.getTarget().getState());
|
||||
} else if (t.getTarget().getKind() == PseudoStateKind.JOIN) {
|
||||
List<String> list = joins.get(t.getTarget().getState());
|
||||
if (list == null) {
|
||||
list = new ArrayList<String>();
|
||||
joins.put(t.getTarget().getState(), list);
|
||||
}
|
||||
list.add(t.getSource().getState());
|
||||
} else if (t.getSource().getKind() == PseudoStateKind.HISTORY_SHALLOW) {
|
||||
historys.add(new HistoryData<String, String>(t.getSource().getState(), t.getTarget().getState()));
|
||||
} else if (t.getSource().getKind() == PseudoStateKind.HISTORY_DEEP) {
|
||||
@@ -229,7 +247,7 @@ public class RepositoryStateMachineModelFactory extends AbstractStateMachineMode
|
||||
HashMap<String, List<JunctionData<String, String>>> junctionsCopy = new HashMap<String, List<JunctionData<String, String>>>();
|
||||
junctionsCopy.putAll(junctions);
|
||||
|
||||
TransitionsData<String, String> transitionsData = new TransitionsData<>(transitionData, choicesCopy, junctionsCopy, null, null,
|
||||
TransitionsData<String, String> transitionsData = new TransitionsData<>(transitionData, choicesCopy, junctionsCopy, forks, joins,
|
||||
entrys, exits, historys);
|
||||
|
||||
StateMachineModel<String, String> stateMachineModel = new DefaultStateMachineModel<>(configurationData, statesData, transitionsData);
|
||||
|
||||
Reference in New Issue
Block a user