vaniglia 源码学习(二)

学习vaniglia的状态机特性源代码,这个特性在企业化开发中也是适用的,类似于struts2里配置action。基本功能如下,

1. 针对配置文件XX.XML

<StateMachine>
    <States>
        <State name="One" description="State One"/>
        <State name="Two" description="State Two"/>
    </States>
    <Events>
        <Event name="E-One" description="Event One"/>
    </Events>
    <Transitions>
        <Transition fromState="One" event="E-One" toState="Two">
            <Action class="org.vaniglia.statemachine.actions.LoggingAction">
                <LoggingPrefix value="ARC-ONE"/>
            </Action>
        </Transition>
    </Transitions>
</StateMachine>

2.针对上述配置文件操作如下:

public class Main {

    public static void main(String[] args) throws StateMachineConfigurationException, StateMachineException {
        StateFactory stateFactory = StateFactory.getInstance();
        EventFactory eventFactory = EventFactory.getInstance();


        StateMachine stateMachine1 = StateMachineConfigurator
        .createStateMachine("xml/statemachine/StateMachine.xml");//将Statemachine转换为stateMachine类

        State stateOne = stateFactory.getStateByName("One");
        Event eventOne = eventFactory.getEventByName("E-One");

        MyObject obj = new MyObject();
        obj.setState(stateOne);
        System.out.println("Initial State = "+obj.getState().getName());

        stateMachine1.handleEvent(obj, eventOne);//在状态机stateOne下处理事件eventOne
//完成状态机迁移,及action的操作。 stateMachine1.handleEvent(obj, eventOne); stateMachine1.handleEvent(obj, eventOne); stateMachine1.handleEvent(obj, eventOne);

3.整理类图如下:

vaniglia 源码学习(二)_第1张图片

4.核心代码算法在Statemachine类中,该类中保存两个映射表:

  • private ActionList[][] actionMatrix; 通过inputstate ID和outputstate ID索引action List
  • private int[][] transactionMatrix; 通过inputstate ID和event ID索引outputstate ID
  • state和event都有单例的工厂产生,主要是维护唯一的ID(nextID)
  • 在action中用户要实现的类(如LoggingAction)通过继承Action接口:
    public void handleEvent(ObjectWithState object, Event event, ActionParameter param) throws StateMachineException {
        State inputState = object.getState();
        if (inputState == null) {
            throw new StateMachineException("Object doesn't have a state assigned.");
        }

        int inputStateId = inputState.getId();
        int inputEventId = event.getId();

        if (inputStateId >= stateIDTraslator.length) {
            throw new StateMachineException("Invalid State");
        }

        if (inputEventId >= eventIDTraslator.length) {
            throw new StateMachineException("Invalid Event");
        }

        int outputStateId = transactionMatrix[stateIDTraslator[inputStateId]][eventIDTraslator[inputEventId]];
        if (outputStateId >= 0) {
            State outputState = states[stateIDTraslator[outputStateId]];
            ActionList actionList = actionMatrix[stateIDTraslator[inputStateId]][stateIDTraslator[outputStateId]];

            if (actionList != null) {
                for (int i = 0; i < actionList.size(); i++) {
                    Action action = actionList.get(i);
                    if (param != null) {
                        action.execute(inputState, outputState, object, param);
                    }
                    else {
                        action.execute(inputState, outputState, object);
                    }
                }
            }
            object.setState(outputState);
        }
    }

 

 

你可能感兴趣的:(学习)