Add root regions support for uml model

- Now multiple root regions works via uml model.
- Relates to #193
This commit is contained in:
Janne Valkealahti
2016-04-09 17:58:47 +01:00
parent ba1729c928
commit 313fe954cb
2 changed files with 74 additions and 1 deletions

View File

@@ -93,11 +93,15 @@ public class UmlModelParser {
State state = (State)vertex;
// find parent state if submachine state, root states have null parent
String parent = null;
String regionId = null;
if (state.getContainer().getOwner() instanceof State) {
parent = ((State)state.getContainer().getOwner()).getName();
}
if (state.getOwner() instanceof Region) {
regionId = ((Region)state.getOwner()).getName();
}
StateData<String, String> stateData = handleActions(
new StateData<String, String>(parent, null, state.getName(), UmlUtils.isInitialState(state)), state);
new StateData<String, String>(parent, regionId, state.getName(), UmlUtils.isInitialState(state)), state);
stateDatas.add(stateData);
// do recursive handling of regions
for (Region sub : state.getRegions()) {

View File

@@ -16,6 +16,7 @@
package org.springframework.statemachine.uml;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.nullValue;
@@ -101,6 +102,36 @@ public class UmlStateMachineModelFactoryTests extends AbstractUmlTests {
}
}
@Test
public void testSimpleRootRegions() {
context.refresh();
Resource model1 = new ClassPathResource("org/springframework/statemachine/uml/simple-root-regions.uml");
UmlStateMachineModelFactory builder = new UmlStateMachineModelFactory(model1);
builder.setBeanFactory(context);
assertThat(model1.exists(), is(true));
StateMachineModel<String, String> stateMachineModel = builder.build();
assertThat(stateMachineModel, notNullValue());
Collection<StateData<String, String>> stateDatas = stateMachineModel.getStatesData().getStateData();
assertThat(stateDatas.size(), is(4));
for (StateData<String, String> stateData : stateDatas) {
if (stateData.getState().equals("S1")) {
assertThat(stateData.isInitial(), is(true));
assertThat(stateData.getRegion(), notNullValue());
} else if (stateData.getState().equals("S2")) {
assertThat(stateData.isInitial(), is(false));
assertThat(stateData.getRegion(), notNullValue());
} else if (stateData.getState().equals("S3")) {
assertThat(stateData.isInitial(), is(true));
assertThat(stateData.getRegion(), notNullValue());
} else if (stateData.getState().equals("S4")) {
assertThat(stateData.isInitial(), is(false));
assertThat(stateData.getRegion(), notNullValue());
} else {
throw new IllegalArgumentException();
}
}
}
@Test
@SuppressWarnings("unchecked")
public void testSimpleFlatMachine() throws Exception {
@@ -131,6 +162,21 @@ public class UmlStateMachineModelFactoryTests extends AbstractUmlTests {
assertThat(stateMachine.getState().getIds(), contains("S2"));
}
@Test
@SuppressWarnings("unchecked")
public void testSimpleRootRegionsMachine() throws Exception {
context.register(Config4.class);
context.refresh();
StateMachine<String, String> stateMachine = context.getBean(StateMachine.class);
stateMachine.start();
assertThat(stateMachine.getState().getIds(), containsInAnyOrder("S1", "S3"));
stateMachine.sendEvent("E1");
assertThat(stateMachine.getState().getIds(), containsInAnyOrder("S2", "S3"));
stateMachine.sendEvent("E2");
assertThat(stateMachine.getState().getIds(), containsInAnyOrder("S2", "S4"));
}
@Configuration
@EnableStateMachine
public static class Config2 extends StateMachineConfigurerAdapter<String, String> {
@@ -177,6 +223,29 @@ public class UmlStateMachineModelFactoryTests extends AbstractUmlTests {
}
}
@Configuration
@EnableStateMachine
public static class Config4 extends StateMachineConfigurerAdapter<String, String> {
@Override
public void configure(StateMachineModelConfigurer<String, String> model) throws Exception {
model
.withModel()
.factory(modelFactory());
}
@Bean
public StateMachineModelFactory<String, String> modelFactory() {
Resource model = new ClassPathResource("org/springframework/statemachine/uml/simple-root-regions.uml");
return new UmlStateMachineModelFactory(model);
}
@Bean
public Action<String, String> action1() {
return new LatchAction();
}
}
public static class LatchAction implements Action<String, String> {
CountDownLatch latch = new CountDownLatch(1);
@Override