FileDocCategorySizeDatePackage
StateEngineImpl.javaAPI DocJava SE 5 API8492Fri Aug 26 14:54:30 BST 2005com.sun.corba.se.impl.orbutil.fsm

StateEngineImpl

public class StateEngineImpl extends Object implements com.sun.corba.se.spi.orbutil.fsm.StateEngine
Encodes the state transition function for a finite state machine.
author
Ken Cavanaugh

Fields Summary
private static com.sun.corba.se.spi.orbutil.fsm.Action
emptyAction
private boolean
initializing
private com.sun.corba.se.spi.orbutil.fsm.Action
defaultAction
Constructors Summary
public StateEngineImpl()


     
    
	initializing = true ;
	defaultAction = new ActionBase("Invalid Transition")
	    {
		public void doIt( FSM fsm, Input in )
		{
		    throw new INTERNAL(
			"Invalid transition attempted from " + 
			    fsm.getState() + " under " + in ) ;
		}
	    } ;
    
Methods Summary
public com.sun.corba.se.spi.orbutil.fsm.StateEngineadd(com.sun.corba.se.spi.orbutil.fsm.State oldState, com.sun.corba.se.spi.orbutil.fsm.Input input, com.sun.corba.se.spi.orbutil.fsm.Guard guard, com.sun.corba.se.spi.orbutil.fsm.Action action, com.sun.corba.se.spi.orbutil.fsm.State newState)

	mustBeInitializing() ;

	StateImpl oldStateImpl = (StateImpl)oldState ;
	GuardedAction ga = new GuardedAction( guard, action, newState ) ;
	oldStateImpl.addGuardedAction( input, ga ) ;

	return this ;
    
public com.sun.corba.se.spi.orbutil.fsm.StateEngineadd(com.sun.corba.se.spi.orbutil.fsm.State oldState, com.sun.corba.se.spi.orbutil.fsm.Input input, com.sun.corba.se.spi.orbutil.fsm.Action action, com.sun.corba.se.spi.orbutil.fsm.State newState)

	mustBeInitializing() ;

	StateImpl oldStateImpl = (StateImpl)oldState ;
	GuardedAction ta = new GuardedAction( action, newState ) ;
	oldStateImpl.addGuardedAction( input, ta ) ;

	return this ;
    
public voiddoIt(com.sun.corba.se.spi.orbutil.fsm.FSM fsm, com.sun.corba.se.spi.orbutil.fsm.Input in, boolean debug)

	// This method is present only for debugging.
	// innerDoIt does the actual transition.

	if (debug)
	    ORBUtility.dprint( this, "doIt enter: currentState = " + 
		fsm.getState() + " in = " + in ) ;

	try {
	    innerDoIt( fsm, in, debug ) ;
	} finally {
	    if (debug)
		ORBUtility.dprint( this, "doIt exit" ) ;
	}
    
public voiddone()

	mustBeInitializing() ;

	// optimize FSM here if desired.  For example,
	// we could choose different strategies for implementing
	// the state transition function based on the distribution 
	// of values for states and input labels.

	initializing = false ;
    
private com.sun.corba.se.spi.orbutil.fsm.ActiongetDefaultAction(com.sun.corba.se.spi.orbutil.fsm.StateImpl currentState)

	Action action = currentState.getDefaultAction() ;
	if (action == null)
	    action = defaultAction ;

	return action ;
    
private com.sun.corba.se.spi.orbutil.fsm.StateImplgetDefaultNextState(com.sun.corba.se.spi.orbutil.fsm.StateImpl currentState)

	// Use the currentState defaults if 
	// set, otherwise use the state engine default.
	StateImpl nextState = (StateImpl)currentState.getDefaultNextState() ;
	if (nextState == null)
	    // The state engine default never changes the state
	    nextState = currentState ;

	return nextState ;
    
private voidinnerDoIt(com.sun.corba.se.spi.orbutil.fsm.FSM fsm, com.sun.corba.se.spi.orbutil.fsm.Input in, boolean debug)

	if (debug) {
	    ORBUtility.dprint( this, "Calling innerDoIt with input " + in ) ;
	}

	// Locals needed for performing the state transition, once we determine
	// the required transition.
	StateImpl currentState = null ;
	StateImpl nextState = null ;
	Action action = null ;

	// Do until no guard has deferred. 
	boolean deferral = false ;
	do {
	    deferral = false ; // clear this after each deferral!
	    currentState = (StateImpl)fsm.getState() ;
	    nextState = getDefaultNextState( currentState ) ;
	    action = getDefaultAction( currentState ) ;

	    if (debug) {
		ORBUtility.dprint( this, "currentState      = " + currentState ) ;
		ORBUtility.dprint( this, "in                = " + in ) ;
		ORBUtility.dprint( this, "default nextState = " + nextState    ) ;
		ORBUtility.dprint( this, "default action    = " + action ) ;
	    }

	    Set gas = currentState.getGuardedActions(in) ;
	    if (gas != null) {
		Iterator iter = gas.iterator() ;

		// Search for a guard that is not DISABLED.  
		// All DISABLED means use defaults.
		while (iter.hasNext()) {
		    GuardedAction ga = (GuardedAction)iter.next() ;
		    Guard.Result gr = ga.getGuard().evaluate( fsm, in ) ;
		    if (debug)
			ORBUtility.dprint( this, 
			    "doIt: evaluated " + ga + " with result " + gr ) ;

		    if (gr == Guard.Result.ENABLED) {
			// ga has the next state and action.
			nextState = (StateImpl)ga.getNextState() ;
			action = ga.getAction() ;
			if (debug) {
			    ORBUtility.dprint( this, "nextState = " + nextState ) ;
			    ORBUtility.dprint( this, "action    = " + action ) ;
			}
			break ;
		    } else if (gr == Guard.Result.DEFERED) {
			deferral = true ;
			break ;
		    }
		}
	    }
	} while (deferral) ;

	performStateTransition( fsm, in, nextState, action, debug ) ;
    
public com.sun.corba.se.spi.orbutil.fsm.FSMmakeFSM(com.sun.corba.se.spi.orbutil.fsm.State startState)

	mustNotBeInitializing() ;

	return new FSMImpl( this, startState ) ;
    
private voidmustBeInitializing()

	if (!initializing)
	    throw new IllegalStateException( 
		"Invalid method call after initialization completed" ) ;
    
private voidmustNotBeInitializing()

	if (initializing)
	    throw new IllegalStateException( 
		"Invalid method call before initialization completed" ) ;
    
private voidperformStateTransition(com.sun.corba.se.spi.orbutil.fsm.FSM fsm, com.sun.corba.se.spi.orbutil.fsm.Input in, com.sun.corba.se.spi.orbutil.fsm.StateImpl nextState, com.sun.corba.se.spi.orbutil.fsm.Action action, boolean debug)

	StateImpl currentState = (StateImpl)fsm.getState() ;

	// Perform the state transition.  Pre and post actions are only
	// performed if the state changes (see UML hidden transitions).

	boolean different = !currentState.equals( nextState ) ;

	if (different) {
	    if (debug)
		ORBUtility.dprint( this, 
		    "doIt: executing postAction for state " + currentState ) ;
	    try {
		currentState.postAction( fsm ) ;
	    } catch (Throwable thr) {
		if (debug)
		    ORBUtility.dprint( this, 
			"doIt: postAction threw " + thr ) ;

		if (thr instanceof ThreadDeath)
		    throw (ThreadDeath)thr ;
	    }
	}

	try {
	    // Note that action may be null in a transition, which simply
	    // means that no action is needed.  Note that action.doIt may
	    // throw an exception, in which case the exception is
	    // propagated after making sure that the transition is properly
	    // completed.
	    if (action != null)
		action.doIt( fsm, in ) ;
	} finally {
	    if (different) {
		if (debug)
		    ORBUtility.dprint( this, 
			"doIt: executing preAction for state " + nextState ) ;

		try {
		    nextState.preAction( fsm ) ;
		} catch (Throwable thr) {
		    if (debug)
			ORBUtility.dprint( this, 
			    "doIt: preAction threw " + thr ) ;

		    if (thr instanceof ThreadDeath)
			throw (ThreadDeath)thr ;
		}

		((FSMImpl)fsm).internalSetState( nextState ) ;
	    }

	    if (debug)
		ORBUtility.dprint( this, "doIt: state is now " + nextState ) ;
	}
    
public com.sun.corba.se.spi.orbutil.fsm.StateEnginesetDefault(com.sun.corba.se.spi.orbutil.fsm.State oldState, com.sun.corba.se.spi.orbutil.fsm.Action action, com.sun.corba.se.spi.orbutil.fsm.State newState)

	mustBeInitializing() ;

	StateImpl oldStateImpl = (StateImpl)oldState ;
	oldStateImpl.setDefaultAction( action ) ;
	oldStateImpl.setDefaultNextState( newState ) ;

	return this ;
    
public com.sun.corba.se.spi.orbutil.fsm.StateEnginesetDefault(com.sun.corba.se.spi.orbutil.fsm.State oldState, com.sun.corba.se.spi.orbutil.fsm.State newState)

	return setDefault( oldState, emptyAction, newState ) ;
    
public com.sun.corba.se.spi.orbutil.fsm.StateEnginesetDefault(com.sun.corba.se.spi.orbutil.fsm.State oldState)

	return setDefault( oldState, oldState ) ;
    
public voidsetDefaultAction(com.sun.corba.se.spi.orbutil.fsm.Action act)

	mustBeInitializing() ;
	defaultAction = act ;