1 7 8 package com.sun.corba.se.impl.orbutil.fsm ; 9 10 import java.util.HashMap ; 11 import java.util.HashSet ; 12 import java.util.Set ; 13 import java.util.Iterator ; 14 15 import org.omg.CORBA.INTERNAL ; 16 17 import com.sun.corba.se.impl.orbutil.ORBUtility ; 18 19 import com.sun.corba.se.spi.orbutil.fsm.Input ; 20 import com.sun.corba.se.spi.orbutil.fsm.Guard ; 21 import com.sun.corba.se.spi.orbutil.fsm.Action ; 22 import com.sun.corba.se.spi.orbutil.fsm.ActionBase ; 23 import com.sun.corba.se.spi.orbutil.fsm.State ; 24 import com.sun.corba.se.spi.orbutil.fsm.StateEngine ; 25 import com.sun.corba.se.spi.orbutil.fsm.StateImpl ; 26 import com.sun.corba.se.spi.orbutil.fsm.FSM ; 27 import com.sun.corba.se.spi.orbutil.fsm.FSMImpl ; 28 29 import com.sun.corba.se.impl.orbutil.fsm.GuardedAction ; 30 31 36 public class StateEngineImpl implements StateEngine 37 { 38 private static Action emptyAction = new ActionBase( "Empty" ) 40 { 41 public void doIt( FSM fsm, Input in ) 42 { 43 } 44 } ; 45 46 private boolean initializing ; 47 private Action defaultAction ; 48 49 public StateEngineImpl() 50 { 51 initializing = true ; 52 defaultAction = new ActionBase("Invalid Transition") 53 { 54 public void doIt( FSM fsm, Input in ) 55 { 56 throw new INTERNAL ( 57 "Invalid transition attempted from " + 58 fsm.getState() + " under " + in ) ; 59 } 60 } ; 61 } 62 63 public StateEngine add( State oldState, Input input, Guard guard, Action action, 64 State newState ) throws IllegalArgumentException , 65 IllegalStateException 66 { 67 mustBeInitializing() ; 68 69 StateImpl oldStateImpl = (StateImpl)oldState ; 70 GuardedAction ga = new GuardedAction( guard, action, newState ) ; 71 oldStateImpl.addGuardedAction( input, ga ) ; 72 73 return this ; 74 } 75 76 public StateEngine add( State oldState, Input input, Action action, 77 State newState ) throws IllegalArgumentException , 78 IllegalStateException 79 { 80 mustBeInitializing() ; 81 82 StateImpl oldStateImpl = (StateImpl)oldState ; 83 GuardedAction ta = new GuardedAction( action, newState ) ; 84 oldStateImpl.addGuardedAction( input, ta ) ; 85 86 return this ; 87 } 88 89 public StateEngine setDefault( State oldState, Action action, State newState ) 90 throws IllegalArgumentException , IllegalStateException 91 { 92 mustBeInitializing() ; 93 94 StateImpl oldStateImpl = (StateImpl)oldState ; 95 oldStateImpl.setDefaultAction( action ) ; 96 oldStateImpl.setDefaultNextState( newState ) ; 97 98 return this ; 99 } 100 101 public StateEngine setDefault( State oldState, State newState ) 102 throws IllegalArgumentException , IllegalStateException 103 { 104 return setDefault( oldState, emptyAction, newState ) ; 105 } 106 107 public StateEngine setDefault( State oldState ) 108 throws IllegalArgumentException , IllegalStateException 109 { 110 return setDefault( oldState, oldState ) ; 111 } 112 113 public void done() throws IllegalStateException 114 { 115 mustBeInitializing() ; 116 117 122 initializing = false ; 123 } 124 125 public void setDefaultAction( Action act ) throws IllegalStateException 126 { 127 mustBeInitializing() ; 128 defaultAction = act ; 129 } 130 131 public void doIt( FSM fsm, Input in, boolean debug ) 132 { 133 136 if (debug) 137 ORBUtility.dprint( this, "doIt enter: currentState = " + 138 fsm.getState() + " in = " + in ) ; 139 140 try { 141 innerDoIt( fsm, in, debug ) ; 142 } finally { 143 if (debug) 144 ORBUtility.dprint( this, "doIt exit" ) ; 145 } 146 } 147 148 private StateImpl getDefaultNextState( StateImpl currentState ) 149 { 150 StateImpl nextState = (StateImpl)currentState.getDefaultNextState() ; 153 if (nextState == null) 154 nextState = currentState ; 156 157 return nextState ; 158 } 159 160 private Action getDefaultAction( StateImpl currentState ) 161 { 162 Action action = currentState.getDefaultAction() ; 163 if (action == null) 164 action = defaultAction ; 165 166 return action ; 167 } 168 169 private void innerDoIt( FSM fsm, Input in, boolean debug ) 170 { 171 if (debug) { 172 ORBUtility.dprint( this, "Calling innerDoIt with input " + in ) ; 173 } 174 175 StateImpl currentState = null ; 178 StateImpl nextState = null ; 179 Action action = null ; 180 181 boolean deferral = false ; 183 do { 184 deferral = false ; currentState = (StateImpl)fsm.getState() ; 186 nextState = getDefaultNextState( currentState ) ; 187 action = getDefaultAction( currentState ) ; 188 189 if (debug) { 190 ORBUtility.dprint( this, "currentState = " + currentState ) ; 191 ORBUtility.dprint( this, "in = " + in ) ; 192 ORBUtility.dprint( this, "default nextState = " + nextState ) ; 193 ORBUtility.dprint( this, "default action = " + action ) ; 194 } 195 196 Set gas = currentState.getGuardedActions(in) ; 197 if (gas != null) { 198 Iterator iter = gas.iterator() ; 199 200 while (iter.hasNext()) { 203 GuardedAction ga = (GuardedAction)iter.next() ; 204 Guard.Result gr = ga.getGuard().evaluate( fsm, in ) ; 205 if (debug) 206 ORBUtility.dprint( this, 207 "doIt: evaluated " + ga + " with result " + gr ) ; 208 209 if (gr == Guard.Result.ENABLED) { 210 nextState = (StateImpl)ga.getNextState() ; 212 action = ga.getAction() ; 213 if (debug) { 214 ORBUtility.dprint( this, "nextState = " + nextState ) ; 215 ORBUtility.dprint( this, "action = " + action ) ; 216 } 217 break ; 218 } else if (gr == Guard.Result.DEFERED) { 219 deferral = true ; 220 break ; 221 } 222 } 223 } 224 } while (deferral) ; 225 226 performStateTransition( fsm, in, nextState, action, debug ) ; 227 } 228 229 private void performStateTransition( FSM fsm, Input in, 230 StateImpl nextState, Action action, boolean debug ) 231 { 232 StateImpl currentState = (StateImpl)fsm.getState() ; 233 234 237 boolean different = !currentState.equals( nextState ) ; 238 239 if (different) { 240 if (debug) 241 ORBUtility.dprint( this, 242 "doIt: executing postAction for state " + currentState ) ; 243 try { 244 currentState.postAction( fsm ) ; 245 } catch (Throwable thr) { 246 if (debug) 247 ORBUtility.dprint( this, 248 "doIt: postAction threw " + thr ) ; 249 250 if (thr instanceof ThreadDeath ) 251 throw (ThreadDeath )thr ; 252 } 253 } 254 255 try { 256 if (action != null) 262 action.doIt( fsm, in ) ; 263 } finally { 264 if (different) { 265 if (debug) 266 ORBUtility.dprint( this, 267 "doIt: executing preAction for state " + nextState ) ; 268 269 try { 270 nextState.preAction( fsm ) ; 271 } catch (Throwable thr) { 272 if (debug) 273 ORBUtility.dprint( this, 274 "doIt: preAction threw " + thr ) ; 275 276 if (thr instanceof ThreadDeath ) 277 throw (ThreadDeath )thr ; 278 } 279 280 ((FSMImpl)fsm).internalSetState( nextState ) ; 281 } 282 283 if (debug) 284 ORBUtility.dprint( this, "doIt: state is now " + nextState ) ; 285 } 286 } 287 288 public FSM makeFSM( State startState ) throws IllegalStateException 289 { 290 mustNotBeInitializing() ; 291 292 return new FSMImpl( this, startState ) ; 293 } 294 295 private void mustBeInitializing() throws IllegalStateException 296 { 297 if (!initializing) 298 throw new IllegalStateException ( 299 "Invalid method call after initialization completed" ) ; 300 } 301 302 private void mustNotBeInitializing() throws IllegalStateException 303 { 304 if (initializing) 305 throw new IllegalStateException ( 306 "Invalid method call before initialization completed" ) ; 307 } 308 } 309 310 312 | Popular Tags |