Add new washer sample
- This sample demonstrate a use of history state
This commit is contained in:
@@ -0,0 +1,86 @@
|
||||
package demo.washer;
|
||||
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.shell.Bootstrap;
|
||||
import org.springframework.statemachine.config.EnableStateMachine;
|
||||
import org.springframework.statemachine.config.EnumStateMachineConfigurerAdapter;
|
||||
import org.springframework.statemachine.config.builders.StateMachineStateConfigurer;
|
||||
import org.springframework.statemachine.config.builders.StateMachineTransitionConfigurer;
|
||||
import org.springframework.statemachine.config.configurers.StateConfigurer.History;
|
||||
|
||||
@Configuration
|
||||
public class Application {
|
||||
|
||||
@Configuration
|
||||
@EnableStateMachine
|
||||
static class StateMachineConfig
|
||||
extends EnumStateMachineConfigurerAdapter<States, Events> {
|
||||
|
||||
//tag::snippetAA[]
|
||||
@Override
|
||||
public void configure(StateMachineStateConfigurer<States, Events> states)
|
||||
throws Exception {
|
||||
states
|
||||
.withStates()
|
||||
.initial(States.RUNNING)
|
||||
.state(States.POWEROFF)
|
||||
.end(States.END)
|
||||
.and()
|
||||
.withStates()
|
||||
.parent(States.RUNNING)
|
||||
.initial(States.WASHING)
|
||||
.state(States.RINSING)
|
||||
.state(States.DRYING)
|
||||
.history(States.HISTORY, History.SHALLOW);
|
||||
}
|
||||
//end::snippetAA[]
|
||||
|
||||
//tag::snippetAB[]
|
||||
@Override
|
||||
public void configure(StateMachineTransitionConfigurer<States, Events> transitions)
|
||||
throws Exception {
|
||||
transitions
|
||||
.withExternal()
|
||||
.source(States.WASHING)
|
||||
.target(States.RINSING)
|
||||
.event(Events.RINSE)
|
||||
.and()
|
||||
.withExternal()
|
||||
.source(States.RINSING)
|
||||
.target(States.DRYING)
|
||||
.event(Events.DRY)
|
||||
.and()
|
||||
.withExternal()
|
||||
.source(States.RUNNING)
|
||||
.target(States.POWEROFF)
|
||||
.event(Events.CUTPOWER)
|
||||
.and()
|
||||
.withExternal()
|
||||
.source(States.POWEROFF)
|
||||
.target(States.HISTORY)
|
||||
.event(Events.RESTOREPOWER);
|
||||
}
|
||||
//end::snippetAB[]
|
||||
|
||||
}
|
||||
|
||||
//tag::snippetB[]
|
||||
public static enum States {
|
||||
RUNNING, HISTORY, END,
|
||||
WASHING, RINSING, DRYING,
|
||||
POWEROFF
|
||||
}
|
||||
//end::snippetB[]
|
||||
|
||||
//tag::snippetC[]
|
||||
public static enum Events {
|
||||
RINSE, DRY,
|
||||
RESTOREPOWER, CUTPOWER
|
||||
}
|
||||
//end::snippetC[]
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
Bootstrap.main(args);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package demo.washer;
|
||||
|
||||
import org.springframework.shell.core.annotation.CliCommand;
|
||||
import org.springframework.shell.core.annotation.CliOption;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import demo.AbstractStateMachineCommands;
|
||||
import demo.washer.Application.Events;
|
||||
import demo.washer.Application.States;
|
||||
|
||||
@Component
|
||||
public class StateMachineCommands extends AbstractStateMachineCommands<States, Events> {
|
||||
|
||||
@CliCommand(value = "sm event", help = "Sends an event to a state machine")
|
||||
public String event(@CliOption(key = { "", "event" }, mandatory = true, help = "The event") final Events event) {
|
||||
getStateMachine().sendEvent(event);
|
||||
return "Event " + event + " send";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:context="http://www.springframework.org/schema/context"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
|
||||
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
|
||||
<context:component-scan base-package="demo" />
|
||||
</beans>
|
||||
@@ -0,0 +1,27 @@
|
||||
+----------------------------------------------------------------------------------+
|
||||
| |
|
||||
+----------------------------------------------------------------------------------+
|
||||
| |
|
||||
| +--------------------------------------------------------------------+ |
|
||||
| *-->| RUNNING |-->X |
|
||||
| +--------------------------------------------------------------------+ |
|
||||
| | | |
|
||||
| | +-------------+ +-------------+ +-------------+ | |
|
||||
| | *-->| WASHING | RINSE | RINSING | DRY | DRYING | | |
|
||||
| | | |------>| |------>| | | |
|
||||
| | | | | | | | | |
|
||||
| | +-------------+ +-------------+ +-------------+ | |
|
||||
| | | |
|
||||
| | H | |
|
||||
| | ^ | |
|
||||
| +------------|-------------------------------------------------------+ |
|
||||
| | | |
|
||||
| |RESTOREPOWER |CUTPOWER |
|
||||
| | | |
|
||||
| +-------------+ | |
|
||||
| | POWEROFF | | |
|
||||
| | |<----------+ |
|
||||
| | | |
|
||||
| +-------------+ |
|
||||
| |
|
||||
+----------------------------------------------------------------------------------+
|
||||
Reference in New Issue
Block a user