------------Maven配置------------
<dependency>
<groupId>org.squirrelframework</groupId>
<artifactId>squirrel-foundation</artifactId>
<version>0.3.8</version>
</dependency>
------------业务数据模型定义------------
public class InputObject
{
String theName;
Integer theAge;
@Override
public String toString()
{
return "InputObject [theName=" + theName + ", theAge=" + theAge + "]";
}
public InputObject(String theName, Integer theAge)
{
super();
this.theName = theName;
this.theAge = theAge;
}
}
------------状态机的使用------------
import org.squirrelframework.foundation.fsm.StateMachineBuilderFactory;
import org.squirrelframework.foundation.fsm.UntypedStateMachine;
import org.squirrelframework.foundation.fsm.UntypedStateMachineBuilder;
import org.squirrelframework.foundation.fsm.annotation.StateMachineParameters;
import org.squirrelframework.foundation.fsm.impl.AbstractUntypedStateMachine;
public class TestStateMachine
{
// 定义状态机内的事件
enum EventType
{
ToA, ToB, ToC, ToD
}
// 定义状态机内的状态
enum StateType
{
A, B, C
}
// 状态机:定义事件的处理逻辑
@StateMachineParameters(stateType = StateType.class, eventType = EventType.class, contextType = InputObject.class)
static class StateMachineSample extends AbstractUntypedStateMachine
{
protected void fromAToB(StateType from, StateType to, EventType event, InputObject context)
{
System.out.println("fromAToB Transition from '" + from + "' to '" + to + "' on event '" + event
+ "' with context '" + context + "'.");
}
protected void fromBToC(StateType from, StateType to, EventType event, InputObject context)
{
System.out.println("fromBToC Transition from '" + from + "' to '" + to + "' on event '" + event
+ "' with context '" + context + "'.");
}
protected void fromCToB(StateType from, StateType to, EventType event, InputObject context)
{
System.out.println("fromCToB Transition from '" + from + "' to '" + to + "' on event '" + event
+ "' with context '" + context + "'.");
}
protected void fromAToC(StateType from, StateType to, EventType event, InputObject context)
{
System.out.println("fromAToC Transition from '" + from + "' to '" + to + "' on event '" + event
+ "' with context '" + context + "'.");
this.getThis().fire(EventType.ToB, context);
}
protected void fromBToB(StateType from, StateType to, EventType event, InputObject context)
{
System.out.println("fromBToB Transition from '" + from + "' to '" + to + "' on event '" + event
+ "' with context '" + context + "'.");
this.getThis().fire(EventType.ToB, context);
}
protected void ontoB(StateType from, StateType to, EventType event, InputObject context)
{
System.out.println("Entry State \'" + to + "\'.");
}
}
public static void main(String[] args)
{
// 构建状态机流程与事件回调(只在此流程中有定义的,状态才会中转过去;From+On重复时,只有第一个To定义的事件会起作用;From、To、On三者不能全部相同)
UntypedStateMachineBuilder builder = StateMachineBuilderFactory.create(StateMachineSample.class);
builder.externalTransition().from(StateType.A).to(StateType.C).on(EventType.ToC).callMethod("fromAToC");
builder.externalTransition().from(StateType.A).to(StateType.B).on(EventType.ToC).callMethod("fromAToB");
builder.externalTransition().from(StateType.B).to(StateType.C).on(EventType.ToC).callMethod("fromBToC");
builder.externalTransition().from(StateType.A).to(StateType.B).on(EventType.ToB).callMethod("fromAToB");
builder.externalTransition().from(StateType.A).to(StateType.C).on(EventType.ToB).callMethod("fromAToC");
builder.externalTransition().from(StateType.B).to(StateType.C).on(EventType.ToB).callMethod("fromBToC");
builder.externalTransition().from(StateType.B).to(StateType.B).on(EventType.ToB).callMethod("fromBToB");
builder.externalTransition().from(StateType.C).to(StateType.B).on(EventType.ToB).callMethod("fromCToB");
builder.onEntry(StateType.B).callMethod("ontoB");
// 使用状态机
UntypedStateMachine stateMachine = builder.newStateMachine(StateType.A);
stateMachine.fire(EventType.ToC, new InputObject("tonyToC", 12));
stateMachine.fire(EventType.ToB, new InputObject("tonyToB", 10));
System.out.println("Current state is " + stateMachine.getCurrentState());
}
}