1. /*
  2. * @(#)FSMImpl.java 1.13 03/12/19
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package com.sun.corba.se.spi.orbutil.fsm ;
  8. import java.util.Set ;
  9. import java.util.HashSet ;
  10. import com.sun.corba.se.spi.orbutil.fsm.Input ;
  11. import com.sun.corba.se.spi.orbutil.fsm.StateEngine ;
  12. import com.sun.corba.se.impl.orbutil.fsm.StateEngineImpl ;
  13. import com.sun.corba.se.impl.orbutil.ORBUtility ;
  14. import com.sun.corba.se.spi.orbutil.fsm.FSM ;
  15. /**
  16. * This is the main class that represents an instance of a state machine
  17. * using a state engine. It may be used as a base class, in which case
  18. * the guards and actions have access to the derived class.
  19. *
  20. * @version @(#)FSMImpl.java 1.13 03/12/19
  21. * @author Ken Cavanaugh
  22. */
  23. public class FSMImpl implements FSM
  24. {
  25. private boolean debug ;
  26. private State state ;
  27. private StateEngineImpl stateEngine ;
  28. /** Create an instance of an FSM using the StateEngine
  29. * in a particular start state.
  30. */
  31. public FSMImpl( StateEngine se, State startState )
  32. {
  33. this( se, startState, false ) ;
  34. }
  35. public FSMImpl( StateEngine se, State startState, boolean debug )
  36. {
  37. state = startState ;
  38. stateEngine = (StateEngineImpl)se ;
  39. this.debug = debug ;
  40. }
  41. /** Return the current state.
  42. */
  43. public State getState()
  44. {
  45. return state ;
  46. }
  47. /** Perform the transition for the given input in the current state. This proceeds as follows:
  48. * <p>Let S be the current state of the FSM.
  49. * If there are guarded actions for S with input in, evaluate their guards successively until
  50. * all have been evaluted, or one returns a non-DISABLED Result.
  51. * <ol>
  52. * <li>If a DEFERED result is returned, retry the input
  53. * <li>If a ENABLED result is returned, the action for the guarded action
  54. * is the current action
  55. * <li>Otherwise there is no enabled action. If S has a default action and next state, use them; otherwise
  56. * use the state engine default action (the next state is always the current state).
  57. * </ol>
  58. * After the action is available, the transition proceeds as follows:
  59. * <ol>
  60. * <li>If the next state is not the current state, execute the current state postAction method.
  61. * <li>Execute the action.
  62. * <li>If the next state is not the current state, execute the next state preAction method.
  63. * <li>Set the current state to the next state.
  64. * </ol>
  65. */
  66. public void doIt( Input in )
  67. {
  68. stateEngine.doIt( this, in, debug ) ;
  69. }
  70. // Methods for use only by StateEngineImpl
  71. public void internalSetState( State nextState )
  72. {
  73. if (debug) {
  74. ORBUtility.dprint( this, "Calling internalSetState with nextState = " +
  75. nextState ) ;
  76. }
  77. state = nextState ;
  78. if (debug) {
  79. ORBUtility.dprint( this, "Exiting internalSetState with state = " +
  80. state ) ;
  81. }
  82. }
  83. }
  84. // end of FSMImpl.java