松鼠状态机学习
2020-09-10 本文已影响0人
ccccaixiaohao
1.参考博客
1.https://github.com/hekailiang/squirrel
2.https://www.yangguo.info/2015/02/01/squirrel/
2.DEMO
1.状态枚举类:
public enum StateEnum {
A("状态A"),
B("状态B"),
C("状态C"),
D("状态D");
private String desc;
StateEnum(String desc) {
this.desc = desc;
}
}
2.事件枚举类:
public enum EventEnum {
TO_B("事件到B"),
TO_C("事件到C"),
TO_D("事件到D");
private String desc;
EventEnum(String desc) {
this.desc = desc;
}
}
3.上下文类:
@Data
public class StateContext {
private StateEnum initState;
private EventEnum event;
private String id;
}
4.定义状态机类:
@States({
@State(name = "A",initialState = true),
@State(name = "B"),
@State(name = "C"),
@State(name = "D",isFinal = true)
})
@Transitions({
@Transit(from = "A",to = "B",on = "TO_B",callMethod = "A_TO_B_Method",when = MyStateMachine.AtoBCondition.class),
@Transit(from = "B",to = "C",on = "TO_C",callMethod = "B_TO_C_Method"),
@Transit(from = "C",to = "D",on = "TO_D",callMethod = "C_TO_D_Method")
})
@StateMachineParameters(stateType = StateEnum.class, eventType = EventEnum.class, contextType = StateContext.class)
public class MyStateMachine extends AbstractStateMachine<UntypedStateMachine,Object,Object,Object>
implements UntypedStateMachine{
public static class AtoBCondition extends AnonymousCondition<StateContext> {
@Override
public boolean isSatisfied(StateContext context) {
if(context.getId().equals("111")){
return true;
}
return false;
}
}
public void A_TO_B_Method(StateEnum from,StateEnum to,EventEnum event,StateContext context){
System.out.println("this is a to b method");
System.out.println("context+" + context.toString());
}
public void B_TO_C_Method(StateEnum from,StateEnum to,EventEnum event,StateContext context){
System.out.println("this is b to c method");
System.out.println("context+" + context.toString());
}
public void C_TO_D_Method(StateEnum from,StateEnum to,EventEnum event,StateContext context){
System.out.println("this is c to d method");
System.out.println("context+" + context.toString());
}
}
5.状态机创建类:
public class StateMachineEngineBuilder {
private UntypedStateMachineBuilder machineBuilder;
public UntypedStateMachineBuilder getMyStateMachine(){
if(machineBuilder == null){
machineBuilder = StateMachineBuilderFactory.create(MyStateMachine.class);
}
return this.machineBuilder;
}
}
创建状态机时,需要传入一个初始状态
public class StateFireEngine extends StateMachineEngineBuilder {
public void fire(StateContext context){
//可以用spring注入
StateMachineEngineBuilder engine = new StateMachineEngineBuilder();
UntypedStateMachineBuilder stateMachineBuilder = engine.getMyStateMachine();
MyStateMachine machine = stateMachineBuilder.newAnyStateMachine(context.getInitState());
machine.fire(context.getEvent(),context);
}
}
6.测试类:
public class TestState {
public static void main(String[] args) {
StateContext context = new StateContext();
context.setId("111");
context.setInitState(StateEnum.A);
context.setEvent(EventEnum.TO_B);
StateFireEngine engine = new StateFireEngine();
engine.fire(context);
}
}
image.png
pom文件:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>squirrelStateMachineTest</artifactId>
<version>1.0-SNAPSHOT</version>
<name>squirrelStateMachineTest</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.squirrelframework</groupId>
<artifactId>squirrel-foundation</artifactId>
<version>0.3.8</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.4</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>