1 /*
   2  * Copyright (c) 2002, 2003, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.corba.se.spi.orbutil.fsm ;
  27 
  28 import java.util.Set ;
  29 import java.util.HashSet ;
  30 
  31 import com.sun.corba.se.spi.orbutil.fsm.Input ;
  32 import com.sun.corba.se.spi.orbutil.fsm.StateEngine ;
  33 import com.sun.corba.se.impl.orbutil.fsm.StateEngineImpl ;
  34 import com.sun.corba.se.impl.orbutil.ORBUtility ;
  35 import com.sun.corba.se.spi.orbutil.fsm.FSM ;
  36 
  37 /**
  38  * This is the main class that represents an instance of a state machine
  39  * using a state engine.  It may be used as a base class, in which case
  40  * the guards and actions have access to the derived class.
  41  *
  42  * @author Ken Cavanaugh
  43  */
  44 public class FSMImpl implements FSM
  45 {
  46     private boolean debug ;
  47     private State state ;
  48     private StateEngineImpl stateEngine ;
  49 
  50     /** Create an instance of an FSM using the StateEngine
  51     * in a particular start state.
  52     */
  53     public FSMImpl( StateEngine se, State startState )
  54     {
  55         this( se, startState, false ) ;
  56     }
  57 
  58     public FSMImpl( StateEngine se, State startState, boolean debug )
  59     {
  60         state = startState ;
  61         stateEngine = (StateEngineImpl)se ;
  62         this.debug = debug ;
  63     }
  64 
  65     /** Return the current state.
  66     */
  67     public State getState()
  68     {
  69         return state ;
  70     }
  71 
  72     /** Perform the transition for the given input in the current state.  This proceeds as follows:
  73     * <p>Let S be the current state of the FSM.
  74     * If there are guarded actions for S with input in, evaluate their guards successively until
  75     * all have been evaluted, or one returns a non-DISABLED Result.
  76     * <ol>
  77     * <li>If a DEFERED result is returned, retry the input
  78     * <li>If a ENABLED result is returned, the action for the guarded action
  79     * is the current action
  80     * <li>Otherwise there is no enabled action.  If S has a default action and next state, use them; otherwise
  81     * use the state engine default action (the next state is always the current state).
  82     * </ol>
  83     * After the action is available, the transition proceeds as follows:
  84     * <ol>
  85     * <li>If the next state is not the current state, execute the current state postAction method.
  86     * <li>Execute the action.
  87     * <li>If the next state is not the current state, execute the next state preAction method.
  88     * <li>Set the current state to the next state.
  89     * </ol>
  90     */
  91     public void doIt( Input in )
  92     {
  93         stateEngine.doIt( this, in, debug ) ;
  94     }
  95 
  96     // Methods for use only by StateEngineImpl
  97 
  98     public void internalSetState( State nextState )
  99     {
 100         if (debug) {
 101             ORBUtility.dprint( this, "Calling internalSetState with nextState = " +
 102                 nextState ) ;
 103         }
 104 
 105         state = nextState ;
 106 
 107         if (debug) {
 108             ORBUtility.dprint( this, "Exiting internalSetState with state = " +
 109                 state ) ;
 110         }
 111     }
 112 }
 113 
 114 // end of FSMImpl.java