From 313fe954cb5effb44cfa18dc9c1bb35ff409acdd Mon Sep 17 00:00:00 2001 From: Janne Valkealahti Date: Sat, 9 Apr 2016 17:58:47 +0100 Subject: [PATCH] Add root regions support for uml model - Now multiple root regions works via uml model. - Relates to #193 --- .../statemachine/uml/UmlModelParser.java | 6 +- .../uml/UmlStateMachineModelFactoryTests.java | 69 +++++++++++++++++++ 2 files changed, 74 insertions(+), 1 deletion(-) diff --git a/spring-statemachine-uml/src/main/java/org/springframework/statemachine/uml/UmlModelParser.java b/spring-statemachine-uml/src/main/java/org/springframework/statemachine/uml/UmlModelParser.java index 87a2af22..3ff63c77 100644 --- a/spring-statemachine-uml/src/main/java/org/springframework/statemachine/uml/UmlModelParser.java +++ b/spring-statemachine-uml/src/main/java/org/springframework/statemachine/uml/UmlModelParser.java @@ -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 stateData = handleActions( - new StateData(parent, null, state.getName(), UmlUtils.isInitialState(state)), state); + new StateData(parent, regionId, state.getName(), UmlUtils.isInitialState(state)), state); stateDatas.add(stateData); // do recursive handling of regions for (Region sub : state.getRegions()) { diff --git a/spring-statemachine-uml/src/test/java/org/springframework/statemachine/uml/UmlStateMachineModelFactoryTests.java b/spring-statemachine-uml/src/test/java/org/springframework/statemachine/uml/UmlStateMachineModelFactoryTests.java index 4d0f71dc..f427ed46 100644 --- a/spring-statemachine-uml/src/test/java/org/springframework/statemachine/uml/UmlStateMachineModelFactoryTests.java +++ b/spring-statemachine-uml/src/test/java/org/springframework/statemachine/uml/UmlStateMachineModelFactoryTests.java @@ -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 stateMachineModel = builder.build(); + assertThat(stateMachineModel, notNullValue()); + Collection> stateDatas = stateMachineModel.getStatesData().getStateData(); + assertThat(stateDatas.size(), is(4)); + for (StateData 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 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 { @@ -177,6 +223,29 @@ public class UmlStateMachineModelFactoryTests extends AbstractUmlTests { } } + @Configuration + @EnableStateMachine + public static class Config4 extends StateMachineConfigurerAdapter { + + @Override + public void configure(StateMachineModelConfigurer model) throws Exception { + model + .withModel() + .factory(modelFactory()); + } + + @Bean + public StateMachineModelFactory modelFactory() { + Resource model = new ClassPathResource("org/springframework/statemachine/uml/simple-root-regions.uml"); + return new UmlStateMachineModelFactory(model); + } + + @Bean + public Action action1() { + return new LatchAction(); + } + } + public static class LatchAction implements Action { CountDownLatch latch = new CountDownLatch(1); @Override