Newer
Older
CactusServer / src / main / java / framework / scenario / FSM.java
y-ota on 10 May 2018 960 bytes 初うp
  1. package framework.scenario;
  2.  
  3. import java.util.Collection;
  4. import java.util.Hashtable;
  5. import java.util.Iterator;
  6.  
  7. public class FSM {
  8. private State initialState = null;
  9. protected State currentState = null;
  10. private Hashtable<String, State> states = new Hashtable<String, State>();
  11. public FSM(State initialState, Hashtable<String, State> states) {
  12. this.initialState = initialState;
  13. this.states = states;
  14. currentState = initialState;
  15. Collection<State> allStates = states.values();
  16. Iterator<State> it = allStates.iterator();
  17. while (it.hasNext()) {
  18. State s = it.next();
  19. s.setOwner(this);
  20. }
  21. }
  22.  
  23. public void addState(String stateName, State s) {
  24. states.put(stateName, s);
  25. s.setOwner(this);
  26. }
  27. public boolean trans(Event e) {
  28. currentState = currentState.getSuccessor(e);
  29. if (currentState == null) return false;
  30. return true;
  31. }
  32. public State getCurrentState() {
  33. return currentState;
  34. }
  35. }