KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > corba > se > spi > orbutil > fsm > FSMImpl


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
8 package com.sun.corba.se.spi.orbutil.fsm ;
9
10 import java.util.Set JavaDoc ;
11 import java.util.HashSet JavaDoc ;
12
13 import com.sun.corba.se.spi.orbutil.fsm.Input ;
14 import com.sun.corba.se.spi.orbutil.fsm.StateEngine ;
15 import com.sun.corba.se.impl.orbutil.fsm.StateEngineImpl ;
16 import com.sun.corba.se.impl.orbutil.ORBUtility ;
17 import com.sun.corba.se.spi.orbutil.fsm.FSM ;
18
19 /**
20  * This is the main class that represents an instance of a state machine
21  * using a state engine. It may be used as a base class, in which case
22  * the guards and actions have access to the derived class.
23  *
24  * @version @(#)FSMImpl.java 1.13 03/12/19
25  * @author Ken Cavanaugh
26  */

27 public class FSMImpl implements FSM
28 {
29     private boolean debug ;
30     private State state ;
31     private StateEngineImpl stateEngine ;
32
33     /** Create an instance of an FSM using the StateEngine
34     * in a particular start state.
35     */

36     public FSMImpl( StateEngine se, State startState )
37     {
38     this( se, startState, false ) ;
39     }
40
41     public FSMImpl( StateEngine se, State startState, boolean debug )
42     {
43     state = startState ;
44     stateEngine = (StateEngineImpl)se ;
45     this.debug = debug ;
46     }
47
48     /** Return the current state.
49     */

50     public State getState()
51     {
52     return state ;
53     }
54
55     /** Perform the transition for the given input in the current state. This proceeds as follows:
56     * <p>Let S be the current state of the FSM.
57     * If there are guarded actions for S with input in, evaluate their guards successively until
58     * all have been evaluted, or one returns a non-DISABLED Result.
59     * <ol>
60     * <li>If a DEFERED result is returned, retry the input
61     * <li>If a ENABLED result is returned, the action for the guarded action
62     * is the current action
63     * <li>Otherwise there is no enabled action. If S has a default action and next state, use them; otherwise
64     * use the state engine default action (the next state is always the current state).
65     * </ol>
66     * After the action is available, the transition proceeds as follows:
67     * <ol>
68     * <li>If the next state is not the current state, execute the current state postAction method.
69     * <li>Execute the action.
70     * <li>If the next state is not the current state, execute the next state preAction method.
71     * <li>Set the current state to the next state.
72     * </ol>
73     */

74     public void doIt( Input in )
75     {
76     stateEngine.doIt( this, in, debug ) ;
77     }
78
79     // Methods for use only by StateEngineImpl
80

81     public void internalSetState( State nextState )
82     {
83     if (debug) {
84         ORBUtility.dprint( this, "Calling internalSetState with nextState = " +
85         nextState ) ;
86     }
87
88     state = nextState ;
89
90     if (debug) {
91         ORBUtility.dprint( this, "Exiting internalSetState with state = " +
92         state ) ;
93     }
94     }
95 }
96
97 // end of FSMImpl.java
98

99
Popular Tags