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