feat: 状态机现可提供初始化状态.
This commit is contained in:
@@ -6,7 +6,6 @@ import com.serliunx.statemanagement.machine.handler.StateHandlerWrapper;
|
|||||||
import com.serliunx.statemanagement.manager.AbstractStateManager;
|
import com.serliunx.statemanagement.manager.AbstractStateManager;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
|
||||||
import java.util.concurrent.Executor;
|
import java.util.concurrent.Executor;
|
||||||
import java.util.concurrent.ExecutorService;
|
import java.util.concurrent.ExecutorService;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
@@ -29,22 +28,15 @@ public abstract class AbstractStateMachine<S> extends AbstractStateManager<S> im
|
|||||||
/**
|
/**
|
||||||
* 默认的构造函数
|
* 默认的构造函数
|
||||||
*
|
*
|
||||||
* @param entryHandlers 进入事件处理器集合
|
* @param stateList 状态列表
|
||||||
* @param leaveHandlers 离开事件处理器集合
|
* @param context 状态机上下文
|
||||||
* @param exchangeHandlers 交换事件处理器集合
|
|
||||||
* @param executor 异步执行器
|
|
||||||
* @param async 是否异步执行
|
|
||||||
*/
|
*/
|
||||||
AbstractStateMachine(List<S> stateList,
|
public AbstractStateMachine(List<S> stateList, StateMachineContext<S> context) {
|
||||||
Map<S, List<StateHandlerWrapper<S>>> entryHandlers,
|
|
||||||
Map<S, List<StateHandlerWrapper<S>>> leaveHandlers,
|
|
||||||
Map<String, List<StateHandlerWrapper<S>>> exchangeHandlers,
|
|
||||||
Map<Object, List<Consumer<StateMachine<S>>>> eventRegistries,
|
|
||||||
Executor executor,
|
|
||||||
Boolean async
|
|
||||||
) {
|
|
||||||
super(stateList);
|
super(stateList);
|
||||||
context = new StateMachineContext<>(entryHandlers, leaveHandlers, exchangeHandlers, eventRegistries, executor, async);
|
this.context = context;
|
||||||
|
|
||||||
|
// 设置初始状态
|
||||||
|
tryInitialState();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -303,4 +295,13 @@ public abstract class AbstractStateMachine<S> extends AbstractStateManager<S> im
|
|||||||
stateHandler.handle(params);
|
stateHandler.handle(params);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 尝试设置初始状态(如果有指定的话)
|
||||||
|
*/
|
||||||
|
private void tryInitialState() {
|
||||||
|
if (context.initialState != null) {
|
||||||
|
switchTo(context.initialState, false);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,13 +23,15 @@ public class DefaultConcurrentStateMachine<S> extends AbstractStateMachine<S> im
|
|||||||
private final AtomicInteger index = new AtomicInteger(0);
|
private final AtomicInteger index = new AtomicInteger(0);
|
||||||
|
|
||||||
DefaultConcurrentStateMachine(List<S> stateList,
|
DefaultConcurrentStateMachine(List<S> stateList,
|
||||||
Map<S, List<StateHandlerWrapper<S>>> entryHandlers,
|
Map<S, List<StateHandlerWrapper<S>>> entryHandlers,
|
||||||
Map<S, List<StateHandlerWrapper<S>>> leaveHandlers,
|
Map<S, List<StateHandlerWrapper<S>>> leaveHandlers,
|
||||||
Map<String, List<StateHandlerWrapper<S>>> exchangeHandlers,
|
Map<String, List<StateHandlerWrapper<S>>> exchangeHandlers,
|
||||||
Map<Object, List<Consumer<StateMachine<S>>>> eventRegistries,
|
Map<Object, List<Consumer<StateMachine<S>>>> eventRegistries,
|
||||||
Executor executor,
|
Executor executor,
|
||||||
Boolean async) {
|
Boolean async,
|
||||||
super(stateList, entryHandlers, leaveHandlers, exchangeHandlers, eventRegistries, executor, async);
|
S initialState
|
||||||
|
) {
|
||||||
|
super(stateList, new StateMachineContext<>(entryHandlers, leaveHandlers, exchangeHandlers, eventRegistries, executor, async, initialState));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -31,8 +31,10 @@ public class StandardStateMachine<S> extends AbstractStateMachine<S> implements
|
|||||||
Map<String, List<StateHandlerWrapper<S>>> exchangeHandlers,
|
Map<String, List<StateHandlerWrapper<S>>> exchangeHandlers,
|
||||||
Map<Object, List<Consumer<StateMachine<S>>>> eventRegistries,
|
Map<Object, List<Consumer<StateMachine<S>>>> eventRegistries,
|
||||||
Executor executor,
|
Executor executor,
|
||||||
Boolean async
|
Boolean async,
|
||||||
|
S initialState
|
||||||
) {
|
) {
|
||||||
super(stateList, entryHandlers, leaveHandlers, exchangeHandlers, eventRegistries, executor, async);
|
super(stateList, new StateMachineContext<>(entryHandlers, leaveHandlers, exchangeHandlers, eventRegistries,
|
||||||
|
executor, async, initialState));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -32,6 +32,10 @@ public final class StateMachineBuilder<S> {
|
|||||||
* 状态机类型
|
* 状态机类型
|
||||||
*/
|
*/
|
||||||
private StateMachineType type = StateMachineType.STANDARD;
|
private StateMachineType type = StateMachineType.STANDARD;
|
||||||
|
/**
|
||||||
|
* 初始化状态
|
||||||
|
*/
|
||||||
|
private S initialState;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 各种事件
|
* 各种事件
|
||||||
@@ -49,6 +53,15 @@ public final class StateMachineBuilder<S> {
|
|||||||
this(Arrays.asList(states));
|
this(Arrays.asList(states));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 定义初始状态
|
||||||
|
*
|
||||||
|
* @param initialState 初始状态
|
||||||
|
*/
|
||||||
|
public StateMachineBuilder<S> withInitial(S initialState) {
|
||||||
|
this.initialState = initialState;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 添加交换事件
|
* 添加交换事件
|
||||||
@@ -252,10 +265,10 @@ public final class StateMachineBuilder<S> {
|
|||||||
}
|
}
|
||||||
if (type.equals(StateMachineType.STANDARD)) {
|
if (type.equals(StateMachineType.STANDARD)) {
|
||||||
return (M)new StandardStateMachine<>(stateList, entryHandlers,
|
return (M)new StandardStateMachine<>(stateList, entryHandlers,
|
||||||
leaveHandlers, exchangeHandlers, eventRegistries, executor, async);
|
leaveHandlers, exchangeHandlers, eventRegistries, executor, async, initialState);
|
||||||
} else if (type.equals(StateMachineType.CONCURRENT)) {
|
} else if (type.equals(StateMachineType.CONCURRENT)) {
|
||||||
return (M)new DefaultConcurrentStateMachine<>(stateList, entryHandlers,
|
return (M)new DefaultConcurrentStateMachine<>(stateList, entryHandlers,
|
||||||
leaveHandlers, exchangeHandlers, eventRegistries, executor, async);
|
leaveHandlers, exchangeHandlers, eventRegistries, executor, async, initialState);
|
||||||
}
|
}
|
||||||
throw new IllegalArgumentException("未知的状态机类型: " + type);
|
throw new IllegalArgumentException("未知的状态机类型: " + type);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,9 +7,6 @@ import com.serliunx.statemanagement.support.ExecutorUtils;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.concurrent.Executor;
|
import java.util.concurrent.Executor;
|
||||||
import java.util.concurrent.RejectedExecutionHandler;
|
|
||||||
import java.util.concurrent.ThreadPoolExecutor;
|
|
||||||
import java.util.concurrent.atomic.AtomicInteger;
|
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -47,13 +44,18 @@ public final class StateMachineContext<S> {
|
|||||||
* 当具体的执行器没有指定是否异步时, 将根据该值决定是否异步执行.
|
* 当具体的执行器没有指定是否异步时, 将根据该值决定是否异步执行.
|
||||||
*/
|
*/
|
||||||
final Boolean async;
|
final Boolean async;
|
||||||
|
/**
|
||||||
|
* 初始化状态
|
||||||
|
*/
|
||||||
|
final S initialState;
|
||||||
|
|
||||||
StateMachineContext(Map<S, List<StateHandlerWrapper<S>>> entryHandlers,
|
public StateMachineContext(Map<S, List<StateHandlerWrapper<S>>> entryHandlers,
|
||||||
Map<S, List<StateHandlerWrapper<S>>> leaveHandlers,
|
Map<S, List<StateHandlerWrapper<S>>> leaveHandlers,
|
||||||
Map<String, List<StateHandlerWrapper<S>>> exchangeHandlers,
|
Map<String, List<StateHandlerWrapper<S>>> exchangeHandlers,
|
||||||
Map<Object, List<Consumer<StateMachine<S>>>> eventRegistries,
|
Map<Object, List<Consumer<StateMachine<S>>>> eventRegistries,
|
||||||
Executor executor,
|
Executor executor,
|
||||||
Boolean async
|
Boolean async,
|
||||||
|
S initialState
|
||||||
) {
|
) {
|
||||||
this.entryHandlers = entryHandlers;
|
this.entryHandlers = entryHandlers;
|
||||||
this.leaveHandlers = leaveHandlers;
|
this.leaveHandlers = leaveHandlers;
|
||||||
@@ -61,6 +63,17 @@ public final class StateMachineContext<S> {
|
|||||||
this.executor = executorAutoConfiguration(executor);
|
this.executor = executorAutoConfiguration(executor);
|
||||||
this.async = async;
|
this.async = async;
|
||||||
this.eventRegistries = eventRegistries;
|
this.eventRegistries = eventRegistries;
|
||||||
|
this.initialState = initialState;
|
||||||
|
}
|
||||||
|
|
||||||
|
public StateMachineContext(Map<S, List<StateHandlerWrapper<S>>> entryHandlers,
|
||||||
|
Map<S, List<StateHandlerWrapper<S>>> leaveHandlers,
|
||||||
|
Map<String, List<StateHandlerWrapper<S>>> exchangeHandlers,
|
||||||
|
Map<Object, List<Consumer<StateMachine<S>>>> eventRegistries,
|
||||||
|
Executor executor,
|
||||||
|
Boolean async
|
||||||
|
) {
|
||||||
|
this(entryHandlers, leaveHandlers, exchangeHandlers, eventRegistries, executor, async, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -26,6 +26,11 @@ public abstract class AbstractStateManager<S> implements StateManager<S> {
|
|||||||
*/
|
*/
|
||||||
private volatile int index;
|
private volatile int index;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 默认状态序号
|
||||||
|
*/
|
||||||
|
private int defaultIndex = 0;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 锁
|
* 锁
|
||||||
*/
|
*/
|
||||||
@@ -180,7 +185,7 @@ public abstract class AbstractStateManager<S> implements StateManager<S> {
|
|||||||
* @return 是第一个时返回真, 否则返回假.
|
* @return 是第一个时返回真, 否则返回假.
|
||||||
*/
|
*/
|
||||||
protected boolean isFirst() {
|
protected boolean isFirst() {
|
||||||
return index == getDefault();
|
return index == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -203,6 +208,15 @@ public abstract class AbstractStateManager<S> implements StateManager<S> {
|
|||||||
* 状态序号默认值(等同于默认状态)
|
* 状态序号默认值(等同于默认状态)
|
||||||
*/
|
*/
|
||||||
protected int getDefault() {
|
protected int getDefault() {
|
||||||
return 0;
|
return defaultIndex;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置默认值
|
||||||
|
*
|
||||||
|
* @param defaultIndex 默认值
|
||||||
|
*/
|
||||||
|
protected void setDefault(int defaultIndex) {
|
||||||
|
this.defaultIndex = defaultIndex;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,8 +7,6 @@ import com.serliunx.statemanagement.support.PrinterEvent;
|
|||||||
import com.serliunx.statemanagement.support.PrinterState;
|
import com.serliunx.statemanagement.support.PrinterState;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import java.util.concurrent.Executors;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 状态机测试
|
* 状态机测试
|
||||||
*
|
*
|
||||||
@@ -21,23 +19,12 @@ public class MachineTest {
|
|||||||
@Test
|
@Test
|
||||||
public void testStandardStateMachine() throws Exception {
|
public void testStandardStateMachine() throws Exception {
|
||||||
StateMachine<PrinterState> stateMachine = StateMachineBuilder.from(PrinterState.values())
|
StateMachine<PrinterState> stateMachine = StateMachineBuilder.from(PrinterState.values())
|
||||||
.async(false)
|
.async(true)
|
||||||
.standard()
|
.standard()
|
||||||
.exchange(PrinterState.IDLE, PrinterState.SCANNING, h -> {
|
.withInitial(PrinterState.STOPPING)
|
||||||
System.out.println("hello~");
|
|
||||||
})
|
|
||||||
.whenLeave(PrinterState.IDLE, h -> {
|
|
||||||
System.out.println(Thread.currentThread().getName() + ": leave IDLE");
|
|
||||||
})
|
|
||||||
.whenEntry(PrinterState.STOPPING, h -> {
|
|
||||||
System.out.println(Thread.currentThread().getName() + ": entry STOPPING, from " + h.getFrom());
|
|
||||||
})
|
|
||||||
.whenEntry(PrinterState.STOPPED, h -> {
|
|
||||||
System.out.println(Thread.currentThread().getName() + ": entry STOPPED, from " + h.getFrom());
|
|
||||||
})
|
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
stateMachine.switchTo(PrinterState.SCANNING);
|
System.out.println(stateMachine.current());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|||||||
Reference in New Issue
Block a user