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