1. /*
  2. * @(#)FSMTest.java 1.11 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 com.sun.corba.se.spi.orbutil.fsm.Input ;
  9. import com.sun.corba.se.spi.orbutil.fsm.Action ;
  10. import com.sun.corba.se.spi.orbutil.fsm.Guard ;
  11. import com.sun.corba.se.spi.orbutil.fsm.StateEngine ;
  12. import com.sun.corba.se.spi.orbutil.fsm.StateImpl ;
  13. import com.sun.corba.se.spi.orbutil.fsm.StateEngineFactory ;
  14. import com.sun.corba.se.spi.orbutil.fsm.FSM ;
  15. class TestInput {
  16. TestInput( Input value, String msg )
  17. {
  18. this.value = value ;
  19. this.msg = msg ;
  20. }
  21. public String toString()
  22. {
  23. return "Input " + value + " : " + msg ;
  24. }
  25. public Input getInput()
  26. {
  27. return value ;
  28. }
  29. Input value ;
  30. String msg ;
  31. }
  32. class TestAction1 implements Action
  33. {
  34. public void doIt( FSM fsm, Input in )
  35. {
  36. System.out.println( "TestAction1:" ) ;
  37. System.out.println( "\tlabel = " + label ) ;
  38. System.out.println( "\toldState = " + oldState ) ;
  39. System.out.println( "\tnewState = " + newState ) ;
  40. if (label != in)
  41. throw new Error( "Unexcepted Input " + in ) ;
  42. if (oldState != fsm.getState())
  43. throw new Error( "Unexpected old State " + fsm.getState() ) ;
  44. }
  45. public TestAction1( State oldState, Input label, State newState )
  46. {
  47. this.oldState = oldState ;
  48. this.newState = newState ;
  49. this.label = label ;
  50. }
  51. private State oldState ;
  52. private Input label ;
  53. private State newState ;
  54. }
  55. class TestAction2 implements Action
  56. {
  57. private State oldState ;
  58. private State newState ;
  59. public void doIt( FSM fsm, Input in )
  60. {
  61. System.out.println( "TestAction2:" ) ;
  62. System.out.println( "\toldState = " + oldState ) ;
  63. System.out.println( "\tnewState = " + newState ) ;
  64. System.out.println( "\tinput = " + in ) ;
  65. if (oldState != fsm.getState())
  66. throw new Error( "Unexpected old State " + fsm.getState() ) ;
  67. }
  68. public TestAction2( State oldState, State newState )
  69. {
  70. this.oldState = oldState ;
  71. this.newState = newState ;
  72. }
  73. }
  74. class TestAction3 implements Action {
  75. private State oldState ;
  76. private Input label ;
  77. public void doIt( FSM fsm, Input in )
  78. {
  79. System.out.println( "TestAction1:" ) ;
  80. System.out.println( "\tlabel = " + label ) ;
  81. System.out.println( "\toldState = " + oldState ) ;
  82. if (label != in)
  83. throw new Error( "Unexcepted Input " + in ) ;
  84. }
  85. public TestAction3( State oldState, Input label )
  86. {
  87. this.oldState = oldState ;
  88. this.label = label ;
  89. }
  90. }
  91. class NegateGuard implements Guard {
  92. Guard guard ;
  93. public NegateGuard( Guard guard )
  94. {
  95. this.guard = guard ;
  96. }
  97. public Guard.Result evaluate( FSM fsm, Input in )
  98. {
  99. return guard.evaluate( fsm, in ).complement() ;
  100. }
  101. }
  102. class MyFSM extends FSMImpl {
  103. public MyFSM( StateEngine se )
  104. {
  105. super( se, FSMTest.STATE1 ) ;
  106. }
  107. public int counter = 0 ;
  108. }
  109. public class FSMTest {
  110. public static final State STATE1 = new StateImpl( "1" ) ;
  111. public static final State STATE2 = new StateImpl( "2" ) ;
  112. public static final State STATE3 = new StateImpl( "3" ) ;
  113. public static final State STATE4 = new StateImpl( "4" ) ;
  114. public static final Input INPUT1 = new InputImpl( "1" ) ;
  115. public static final Input INPUT2 = new InputImpl( "2" ) ;
  116. public static final Input INPUT3 = new InputImpl( "3" ) ;
  117. public static final Input INPUT4 = new InputImpl( "4" ) ;
  118. private Guard counterGuard = new Guard() {
  119. public Guard.Result evaluate( FSM fsm, Input in )
  120. {
  121. MyFSM mfsm = (MyFSM) fsm ;
  122. return Guard.Result.convert( mfsm.counter < 3 ) ;
  123. }
  124. } ;
  125. private static void add1( StateEngine se, State oldState, Input in, State newState )
  126. {
  127. se.add( oldState, in, new TestAction1( oldState, in, newState ), newState ) ;
  128. }
  129. private static void add2( StateEngine se, State oldState, State newState )
  130. {
  131. se.setDefault( oldState, new TestAction2( oldState, newState ), newState ) ;
  132. }
  133. public static void main( String[] args )
  134. {
  135. TestAction3 ta3 = new TestAction3( STATE3, INPUT1 ) ;
  136. StateEngine se = StateEngineFactory.create() ;
  137. add1( se, STATE1, INPUT1, STATE1 ) ;
  138. add2( se, STATE1, STATE2 ) ;
  139. add1( se, STATE2, INPUT1, STATE2 ) ;
  140. add1( se, STATE2, INPUT2, STATE2 ) ;
  141. add1( se, STATE2, INPUT3, STATE1 ) ;
  142. add1( se, STATE2, INPUT4, STATE3 ) ;
  143. se.add( STATE3, INPUT1, ta3, STATE3 ) ;
  144. se.add( STATE3, INPUT1, ta3, STATE4 ) ;
  145. add1( se, STATE3, INPUT2, STATE1 ) ;
  146. add1( se, STATE3, INPUT3, STATE2 ) ;
  147. add1( se, STATE3, INPUT4, STATE2 ) ;
  148. MyFSM fsm = new MyFSM( se ) ;
  149. TestInput in11 = new TestInput( INPUT1, "1.1" ) ;
  150. TestInput in12 = new TestInput( INPUT1, "1.2" ) ;
  151. TestInput in21 = new TestInput( INPUT2, "2.1" ) ;
  152. TestInput in22 = new TestInput( INPUT2, "2.2" ) ;
  153. TestInput in31 = new TestInput( INPUT3, "3.1" ) ;
  154. TestInput in32 = new TestInput( INPUT3, "3.2" ) ;
  155. TestInput in33 = new TestInput( INPUT3, "3.3" ) ;
  156. TestInput in41 = new TestInput( INPUT4, "4.1" ) ;
  157. fsm.doIt( in11.getInput() ) ;
  158. fsm.doIt( in12.getInput() ) ;
  159. fsm.doIt( in41.getInput() ) ;
  160. fsm.doIt( in11.getInput() ) ;
  161. fsm.doIt( in22.getInput() ) ;
  162. fsm.doIt( in31.getInput() ) ;
  163. fsm.doIt( in33.getInput() ) ;
  164. fsm.doIt( in41.getInput() ) ;
  165. fsm.doIt( in41.getInput() ) ;
  166. fsm.doIt( in41.getInput() ) ;
  167. fsm.doIt( in22.getInput() ) ;
  168. fsm.doIt( in32.getInput() ) ;
  169. fsm.doIt( in41.getInput() ) ;
  170. fsm.doIt( in11.getInput() ) ;
  171. fsm.doIt( in12.getInput() ) ;
  172. fsm.doIt( in11.getInput() ) ;
  173. fsm.doIt( in11.getInput() ) ;
  174. fsm.doIt( in11.getInput() ) ;
  175. fsm.doIt( in11.getInput() ) ;
  176. fsm.doIt( in11.getInput() ) ;
  177. }
  178. }