使用
State:状态
Event:事件,状态由事件触发,引起变化
Transition:流转,表示从一个状态到另一个状态
External Transition:外部流转,两个不同状态之间的流转
Internal Transition:内部流转,同一个状态之间的流转
Condition:条件,表示是否允许到达某个状态
Action:动作,到达某个状态之后,可以做什么
StateMachine:状态机
<dependency>
<groupId>com.alibaba.cola</groupId>
<artifactId>cola-component-statemachine</artifactId>
<version>4.1.0</version>
</dependency>
import com.alibaba.cola.statemachine.Action;
import com.alibaba.cola.statemachine.Condition;
import com.alibaba.cola.statemachine.StateMachine;
import com.alibaba.cola.statemachine.builder.StateMachineBuilder;
import com.alibaba.cola.statemachine.builder.StateMachineBuilderFactory;
import lombok.Data;
public class TestColaStatemachine {
public static void main(String[] args) {
// 第一步:生成一个状态机builder
StateMachineBuilder<States, Events, Context> builder = StateMachineBuilderFactory.create();
// 第二步:设置一个外部状态转移类型的builder,并设置from\to\on\when\perform
builder.externalTransition()
.from(States.STATE1)
.to(States.STATE2)
.on(Events.EVENT1)
.when(new MyCondition())
.perform(new MyAction()); //这个action 我们可以按自己所需修改,比如这种Action<R,T> service的方法Service::method
// 第三步:设置状态机的id和ready,并在StateMachineFactory中的stateMachineMap进行注册
StateMachine<States, Events, Context> stateMachine = builder.build(MACHINE_ID);
// 第四步:触发状态机
States target = stateMachine.fireEvent(States.STATE1, Events.EVENT1, new Context());
System.out.println(target);
}
private static String MACHINE_ID = "TestStateMachine";
static class MyCondition implements Condition<Context> {
@Override
public boolean isSatisfied(Context context) {
return true;
}
}
static class MyAction implements Action<States, Events, Context> {
@Override
public void execute(States from, States to, Events event, Context context) {
System.out.println("from: " + from);
System.out.println("to: " + to);
System.out.println("event: " + event);
System.out.println("context: " + context);
}
}
enum States {
STATE1, STATE2, STATE3, STATE4
}
enum Events {
EVENT1, EVENT2, EVENT3, EVENT4, INTERNAL_EVENT
}
@Data
static class Context {
String operator = "frank";
String entityId = "123465";
}
}
原理
核心类
- StateMachine 状态机
public class StateMachineImpl<S, E, C> implements StateMachine<S, E, C> {
private String machineId;
private final Map<S, State<S, E, C>> stateMap;//核心字段,保存所有状态
}
S:state 我们自己项目中的状态类
E:Event 我们自己的事件类
C:Context 我们自己的上下文
- State 状态
public class StateImpl<S,E,C> implements State<S,E,C> {
protected final S stateId;
private EventTransitions eventTransitions = new EventTransitions();//
}
- EventTransitions 关于Transition的一个包装
public class EventTransitions<S,E,C> {
private HashMap<E, List<Transition<S,E,C>>> eventTransitions;
public EventTransitions(){
eventTransitions = new HashMap<>();
}
- Transition 流转
public class TransitionImpl<S,E,C> implements Transition<S,E,C> {
private State<S, E, C> source; //源状态
private State<S, E, C> target; //目标状态
private E event; //事件
private Condition<C> condition;//判断条件
private Action<S,E,C> action; //流转到目标状态后的操作
private TransitionType type = TransitionType.EXTERNAL;//内部还是外部
内部:统一状态
外部:不同状态
参考
COLA下的cola-statemachine状态机
管理订单状态,该上状态机吗?轻量级状态机COLA StateMachine保姆级入门教程