当前位置:   article > 正文

java状态机设计模式_设计模式-如何实现FSM-J中的有限状态机

java addstate

我使用Java设计并实现了一个简单的有限状态机示例。

IFiniteStateMachine:管理有限状态机的公共接口

例如将新状态添加到有限状态机或通过

具体动作。

interface IFiniteStateMachine {

void setStartState(IState startState);

void setEndState(IState endState);

void addState(IState startState, IState newState, Action action);

void removeState(String targetStateDesc);

IState getCurrentState();

IState getStartState();

IState getEndState();

void transit(Action action);

}

IState:获取状态相关信息的公共接口

例如状态名称和到连接状态的映射。

interface IState {

// Returns the mapping for which one action will lead to another state

Map getAdjacentStates();

String getStateDesc();

void addTransit(Action action, IState nextState);

void removeTransit(String targetStateDesc);

}

行动:将导致状态转变的阶级。

public class Action {

private String mActionName;

public Action(String actionName) {

mActionName = actionName;

}

String getActionName() {

return mActionName;

}

@Override

public String toString() {

return mActionName;

}

}

StateImpl:执行IState。 我应用了HashMap之类的数据结构来保留动作状态映射。

public class StateImpl implements IState {

private HashMap mMapping = new HashMap<>();

private String mStateName;

public StateImpl(String stateName) {

mStateName = stateName;

}

@Override

public Map getAdjacentStates() {

return mMapping;

}

@Override

public String getStateDesc() {

return mStateName;

}

@Override

public void addTransit(Action action, IState state) {

mMapping.put(action.toString(), state);

}

@Override

public void removeTransit(String targetStateDesc) {

// get action which directs to target state

String targetAction = null;

for (Map.Entry entry : mMapping.entrySet()) {

IState state = entry.getValue();

if (state.getStateDesc().equals(targetStateDesc)) {

targetAction = entry.getKey();

}

}

mMapping.remove(targetAction);

}

}

FiniteStateMachineImpl:IFiniteStateMachine的实现。 我使用ArrayList保留所有状态。

public class FiniteStateMachineImpl implements IFiniteStateMachine {

private IState mStartState;

private IState mEndState;

private IState mCurrentState;

private ArrayList mAllStates = new ArrayList<>();

private HashMap> mMapForAllStates = new HashMap<>();

public FiniteStateMachineImpl(){}

@Override

public void setStartState(IState startState) {

mStartState = startState;

mCurrentState = startState;

mAllStates.add(startState);

// todo: might have some value

mMapForAllStates.put(startState.getStateDesc(), new ArrayList());

}

@Override

public void setEndState(IState endState) {

mEndState = endState;

mAllStates.add(endState);

mMapForAllStates.put(endState.getStateDesc(), new ArrayList());

}

@Override

public void addState(IState startState, IState newState, Action action) {

// validate startState, newState and action

// update mapping in finite state machine

mAllStates.add(newState);

final String startStateDesc = startState.getStateDesc();

final String newStateDesc = newState.getStateDesc();

mMapForAllStates.put(newStateDesc, new ArrayList());

ArrayList adjacentStateList = null;

if (mMapForAllStates.containsKey(startStateDesc)) {

adjacentStateList = mMapForAllStates.get(startStateDesc);

adjacentStateList.add(newState);

} else {

mAllStates.add(startState);

adjacentStateList = new ArrayList<>();

adjacentStateList.add(newState);

}

mMapForAllStates.put(startStateDesc, adjacentStateList);

// update mapping in startState

for (IState state : mAllStates) {

boolean isStartState = state.getStateDesc().equals(startState.getStateDesc());

if (isStartState) {

startState.addTransit(action, newState);

}

}

}

@Override

public void removeState(String targetStateDesc) {

// validate state

if (!mMapForAllStates.containsKey(targetStateDesc)) {

throw new RuntimeException("Don't have state: " + targetStateDesc);

} else {

// remove from mapping

mMapForAllStates.remove(targetStateDesc);

}

// update all state

IState targetState = null;

for (IState state : mAllStates) {

if (state.getStateDesc().equals(targetStateDesc)) {

targetState = state;

} else {

state.removeTransit(targetStateDesc);

}

}

mAllStates.remove(targetState);

}

@Override

public IState getCurrentState() {

return mCurrentState;

}

@Override

public void transit(Action action) {

if (mCurrentState == null) {

throw new RuntimeException("Please setup start state");

}

Map localMapping = mCurrentState.getAdjacentStates();

if (localMapping.containsKey(action.toString())) {

mCurrentState = localMapping.get(action.toString());

} else {

throw new RuntimeException("No action start from current state");

}

}

@Override

public IState getStartState() {

return mStartState;

}

@Override

public IState getEndState() {

return mEndState;

}

}

例:

public class example {

public static void main(String[] args) {

System.out.println("Finite state machine!!!");

IState startState = new StateImpl("start");

IState endState = new StateImpl("end");

IFiniteStateMachine fsm = new FiniteStateMachineImpl();

fsm.setStartState(startState);

fsm.setEndState(endState);

IState middle1 = new StateImpl("middle1");

middle1.addTransit(new Action("path1"), endState);

fsm.addState(startState, middle1, new Action("path1"));

System.out.println(fsm.getCurrentState().getStateDesc());

fsm.transit(new Action(("path1")));

System.out.println(fsm.getCurrentState().getStateDesc());

fsm.addState(middle1, endState, new Action("path1-end"));

fsm.transit(new Action(("path1-end")));

System.out.println(fsm.getCurrentState().getStateDesc());

fsm.addState(endState, middle1, new Action("path1-end"));

}

}

Github上的完整示例

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/696483
推荐阅读
相关标签
  

闽ICP备14008679号