corba/src/share/classes/com/sun/corba/se/spi/orbutil/fsm/FSMTest.java
author ohair
Tue, 25 May 2010 15:52:11 -0700
changeset 5555 b2b5ed3f0d0d
parent 4 02bb8761fcce
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4
02bb8761fcce Initial load
duke
parents:
diff changeset
     1
/*
5555
b2b5ed3f0d0d 6943119: Rebrand source copyright notices
ohair
parents: 4
diff changeset
     2
 * Copyright (c) 2002, 2003, Oracle and/or its affiliates. All rights reserved.
4
02bb8761fcce Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
02bb8761fcce Initial load
duke
parents:
diff changeset
     4
 *
02bb8761fcce Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
02bb8761fcce Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5555
b2b5ed3f0d0d 6943119: Rebrand source copyright notices
ohair
parents: 4
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
4
02bb8761fcce Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5555
b2b5ed3f0d0d 6943119: Rebrand source copyright notices
ohair
parents: 4
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
4
02bb8761fcce Initial load
duke
parents:
diff changeset
    10
 *
02bb8761fcce Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
02bb8761fcce Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
02bb8761fcce Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
02bb8761fcce Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
02bb8761fcce Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
02bb8761fcce Initial load
duke
parents:
diff changeset
    16
 *
02bb8761fcce Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
02bb8761fcce Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
02bb8761fcce Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
02bb8761fcce Initial load
duke
parents:
diff changeset
    20
 *
5555
b2b5ed3f0d0d 6943119: Rebrand source copyright notices
ohair
parents: 4
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
b2b5ed3f0d0d 6943119: Rebrand source copyright notices
ohair
parents: 4
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
b2b5ed3f0d0d 6943119: Rebrand source copyright notices
ohair
parents: 4
diff changeset
    23
 * questions.
4
02bb8761fcce Initial load
duke
parents:
diff changeset
    24
 */
02bb8761fcce Initial load
duke
parents:
diff changeset
    25
02bb8761fcce Initial load
duke
parents:
diff changeset
    26
package com.sun.corba.se.spi.orbutil.fsm ;
02bb8761fcce Initial load
duke
parents:
diff changeset
    27
02bb8761fcce Initial load
duke
parents:
diff changeset
    28
import com.sun.corba.se.spi.orbutil.fsm.Input ;
02bb8761fcce Initial load
duke
parents:
diff changeset
    29
import com.sun.corba.se.spi.orbutil.fsm.Action ;
02bb8761fcce Initial load
duke
parents:
diff changeset
    30
import com.sun.corba.se.spi.orbutil.fsm.Guard ;
02bb8761fcce Initial load
duke
parents:
diff changeset
    31
import com.sun.corba.se.spi.orbutil.fsm.StateEngine ;
02bb8761fcce Initial load
duke
parents:
diff changeset
    32
import com.sun.corba.se.spi.orbutil.fsm.StateImpl ;
02bb8761fcce Initial load
duke
parents:
diff changeset
    33
import com.sun.corba.se.spi.orbutil.fsm.StateEngineFactory ;
02bb8761fcce Initial load
duke
parents:
diff changeset
    34
import com.sun.corba.se.spi.orbutil.fsm.FSM ;
02bb8761fcce Initial load
duke
parents:
diff changeset
    35
02bb8761fcce Initial load
duke
parents:
diff changeset
    36
class TestInput {
02bb8761fcce Initial load
duke
parents:
diff changeset
    37
    TestInput( Input value, String msg )
02bb8761fcce Initial load
duke
parents:
diff changeset
    38
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
    39
        this.value = value ;
02bb8761fcce Initial load
duke
parents:
diff changeset
    40
        this.msg = msg ;
02bb8761fcce Initial load
duke
parents:
diff changeset
    41
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
    42
02bb8761fcce Initial load
duke
parents:
diff changeset
    43
    public String toString()
02bb8761fcce Initial load
duke
parents:
diff changeset
    44
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
    45
        return "Input " + value + " : " + msg ;
02bb8761fcce Initial load
duke
parents:
diff changeset
    46
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
    47
02bb8761fcce Initial load
duke
parents:
diff changeset
    48
    public Input getInput()
02bb8761fcce Initial load
duke
parents:
diff changeset
    49
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
    50
        return value ;
02bb8761fcce Initial load
duke
parents:
diff changeset
    51
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
    52
02bb8761fcce Initial load
duke
parents:
diff changeset
    53
    Input value ;
02bb8761fcce Initial load
duke
parents:
diff changeset
    54
    String msg ;
02bb8761fcce Initial load
duke
parents:
diff changeset
    55
}
02bb8761fcce Initial load
duke
parents:
diff changeset
    56
02bb8761fcce Initial load
duke
parents:
diff changeset
    57
class TestAction1 implements Action
02bb8761fcce Initial load
duke
parents:
diff changeset
    58
{
02bb8761fcce Initial load
duke
parents:
diff changeset
    59
    public void doIt( FSM fsm, Input in )
02bb8761fcce Initial load
duke
parents:
diff changeset
    60
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
    61
        System.out.println( "TestAction1:" ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
    62
        System.out.println( "\tlabel    = " + label ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
    63
        System.out.println( "\toldState = " + oldState ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
    64
        System.out.println( "\tnewState = " + newState ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
    65
        if (label != in)
02bb8761fcce Initial load
duke
parents:
diff changeset
    66
            throw new Error( "Unexcepted Input " + in ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
    67
        if (oldState != fsm.getState())
02bb8761fcce Initial load
duke
parents:
diff changeset
    68
            throw new Error( "Unexpected old State " + fsm.getState() ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
    69
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
    70
02bb8761fcce Initial load
duke
parents:
diff changeset
    71
    public TestAction1( State oldState, Input label, State newState )
02bb8761fcce Initial load
duke
parents:
diff changeset
    72
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
    73
        this.oldState = oldState ;
02bb8761fcce Initial load
duke
parents:
diff changeset
    74
        this.newState = newState ;
02bb8761fcce Initial load
duke
parents:
diff changeset
    75
        this.label = label ;
02bb8761fcce Initial load
duke
parents:
diff changeset
    76
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
    77
02bb8761fcce Initial load
duke
parents:
diff changeset
    78
    private State oldState ;
02bb8761fcce Initial load
duke
parents:
diff changeset
    79
    private Input label ;
02bb8761fcce Initial load
duke
parents:
diff changeset
    80
    private State newState ;
02bb8761fcce Initial load
duke
parents:
diff changeset
    81
}
02bb8761fcce Initial load
duke
parents:
diff changeset
    82
02bb8761fcce Initial load
duke
parents:
diff changeset
    83
class TestAction2 implements Action
02bb8761fcce Initial load
duke
parents:
diff changeset
    84
{
02bb8761fcce Initial load
duke
parents:
diff changeset
    85
    private State oldState ;
02bb8761fcce Initial load
duke
parents:
diff changeset
    86
    private State newState ;
02bb8761fcce Initial load
duke
parents:
diff changeset
    87
02bb8761fcce Initial load
duke
parents:
diff changeset
    88
    public void doIt( FSM fsm, Input in )
02bb8761fcce Initial load
duke
parents:
diff changeset
    89
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
    90
        System.out.println( "TestAction2:" ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
    91
        System.out.println( "\toldState = " + oldState ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
    92
        System.out.println( "\tnewState = " + newState ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
    93
        System.out.println( "\tinput    = " + in ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
    94
        if (oldState != fsm.getState())
02bb8761fcce Initial load
duke
parents:
diff changeset
    95
            throw new Error( "Unexpected old State " + fsm.getState() ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
    96
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
    97
02bb8761fcce Initial load
duke
parents:
diff changeset
    98
    public TestAction2( State oldState, State newState )
02bb8761fcce Initial load
duke
parents:
diff changeset
    99
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
   100
        this.oldState = oldState ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   101
        this.newState = newState ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   102
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   103
}
02bb8761fcce Initial load
duke
parents:
diff changeset
   104
02bb8761fcce Initial load
duke
parents:
diff changeset
   105
class TestAction3 implements Action {
02bb8761fcce Initial load
duke
parents:
diff changeset
   106
    private State oldState ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   107
    private Input label ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   108
02bb8761fcce Initial load
duke
parents:
diff changeset
   109
    public void doIt( FSM fsm, Input in )
02bb8761fcce Initial load
duke
parents:
diff changeset
   110
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
   111
        System.out.println( "TestAction1:" ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   112
        System.out.println( "\tlabel    = " + label ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   113
        System.out.println( "\toldState = " + oldState ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   114
        if (label != in)
02bb8761fcce Initial load
duke
parents:
diff changeset
   115
            throw new Error( "Unexcepted Input " + in ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   116
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   117
02bb8761fcce Initial load
duke
parents:
diff changeset
   118
    public TestAction3( State oldState, Input label )
02bb8761fcce Initial load
duke
parents:
diff changeset
   119
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
   120
        this.oldState = oldState ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   121
        this.label = label ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   122
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   123
}
02bb8761fcce Initial load
duke
parents:
diff changeset
   124
02bb8761fcce Initial load
duke
parents:
diff changeset
   125
class NegateGuard implements Guard {
02bb8761fcce Initial load
duke
parents:
diff changeset
   126
    Guard guard ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   127
02bb8761fcce Initial load
duke
parents:
diff changeset
   128
    public NegateGuard( Guard guard )
02bb8761fcce Initial load
duke
parents:
diff changeset
   129
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
   130
        this.guard = guard ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   131
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   132
02bb8761fcce Initial load
duke
parents:
diff changeset
   133
    public Guard.Result evaluate( FSM fsm, Input in )
02bb8761fcce Initial load
duke
parents:
diff changeset
   134
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
   135
        return guard.evaluate( fsm, in ).complement() ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   136
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   137
}
02bb8761fcce Initial load
duke
parents:
diff changeset
   138
02bb8761fcce Initial load
duke
parents:
diff changeset
   139
class MyFSM extends FSMImpl {
02bb8761fcce Initial load
duke
parents:
diff changeset
   140
    public MyFSM( StateEngine se )
02bb8761fcce Initial load
duke
parents:
diff changeset
   141
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
   142
        super( se, FSMTest.STATE1 ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   143
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   144
02bb8761fcce Initial load
duke
parents:
diff changeset
   145
    public int counter = 0 ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   146
}
02bb8761fcce Initial load
duke
parents:
diff changeset
   147
02bb8761fcce Initial load
duke
parents:
diff changeset
   148
public class FSMTest {
02bb8761fcce Initial load
duke
parents:
diff changeset
   149
    public static final State   STATE1 = new StateImpl( "1" ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   150
    public static final State   STATE2 = new StateImpl( "2" ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   151
    public static final State   STATE3 = new StateImpl( "3" ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   152
    public static final State   STATE4 = new StateImpl( "4" ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   153
02bb8761fcce Initial load
duke
parents:
diff changeset
   154
    public static final Input   INPUT1 = new InputImpl( "1" ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   155
    public static final Input   INPUT2 = new InputImpl( "2" ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   156
    public static final Input   INPUT3 = new InputImpl( "3" ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   157
    public static final Input   INPUT4 = new InputImpl( "4" ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   158
02bb8761fcce Initial load
duke
parents:
diff changeset
   159
    private Guard counterGuard = new Guard() {
02bb8761fcce Initial load
duke
parents:
diff changeset
   160
        public Guard.Result evaluate( FSM fsm, Input in )
02bb8761fcce Initial load
duke
parents:
diff changeset
   161
        {
02bb8761fcce Initial load
duke
parents:
diff changeset
   162
            MyFSM mfsm = (MyFSM) fsm ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   163
            return Guard.Result.convert( mfsm.counter < 3 ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   164
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   165
    } ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   166
02bb8761fcce Initial load
duke
parents:
diff changeset
   167
    private static void add1( StateEngine se, State oldState, Input in, State newState )
02bb8761fcce Initial load
duke
parents:
diff changeset
   168
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
   169
        se.add( oldState, in, new TestAction1( oldState, in, newState ), newState ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   170
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   171
02bb8761fcce Initial load
duke
parents:
diff changeset
   172
    private static void add2( StateEngine se, State oldState, State newState )
02bb8761fcce Initial load
duke
parents:
diff changeset
   173
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
   174
        se.setDefault( oldState, new TestAction2( oldState, newState ), newState ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   175
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   176
02bb8761fcce Initial load
duke
parents:
diff changeset
   177
    public static void main( String[] args )
02bb8761fcce Initial load
duke
parents:
diff changeset
   178
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
   179
        TestAction3 ta3 = new TestAction3( STATE3, INPUT1 ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   180
02bb8761fcce Initial load
duke
parents:
diff changeset
   181
        StateEngine se = StateEngineFactory.create() ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   182
        add1( se, STATE1, INPUT1, STATE1 ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   183
        add2( se, STATE1,         STATE2 ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   184
02bb8761fcce Initial load
duke
parents:
diff changeset
   185
        add1( se, STATE2, INPUT1, STATE2 ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   186
        add1( se, STATE2, INPUT2, STATE2 ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   187
        add1( se, STATE2, INPUT3, STATE1 ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   188
        add1( se, STATE2, INPUT4, STATE3 ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   189
02bb8761fcce Initial load
duke
parents:
diff changeset
   190
        se.add(   STATE3, INPUT1, ta3,  STATE3 ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   191
        se.add(   STATE3, INPUT1, ta3,  STATE4 ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   192
        add1( se, STATE3, INPUT2, STATE1 ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   193
        add1( se, STATE3, INPUT3, STATE2 ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   194
        add1( se, STATE3, INPUT4, STATE2 ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   195
02bb8761fcce Initial load
duke
parents:
diff changeset
   196
        MyFSM fsm = new MyFSM( se ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   197
        TestInput in11 = new TestInput( INPUT1, "1.1" ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   198
        TestInput in12 = new TestInput( INPUT1, "1.2" ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   199
        TestInput in21 = new TestInput( INPUT2, "2.1" ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   200
        TestInput in22 = new TestInput( INPUT2, "2.2" ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   201
        TestInput in31 = new TestInput( INPUT3, "3.1" ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   202
        TestInput in32 = new TestInput( INPUT3, "3.2" ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   203
        TestInput in33 = new TestInput( INPUT3, "3.3" ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   204
        TestInput in41 = new TestInput( INPUT4, "4.1" ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   205
02bb8761fcce Initial load
duke
parents:
diff changeset
   206
        fsm.doIt( in11.getInput() ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   207
        fsm.doIt( in12.getInput() ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   208
        fsm.doIt( in41.getInput() ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   209
        fsm.doIt( in11.getInput() ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   210
        fsm.doIt( in22.getInput() ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   211
        fsm.doIt( in31.getInput() ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   212
        fsm.doIt( in33.getInput() ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   213
        fsm.doIt( in41.getInput() ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   214
        fsm.doIt( in41.getInput() ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   215
        fsm.doIt( in41.getInput() ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   216
        fsm.doIt( in22.getInput() ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   217
        fsm.doIt( in32.getInput() ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   218
        fsm.doIt( in41.getInput() ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   219
        fsm.doIt( in11.getInput() ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   220
        fsm.doIt( in12.getInput() ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   221
        fsm.doIt( in11.getInput() ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   222
        fsm.doIt( in11.getInput() ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   223
        fsm.doIt( in11.getInput() ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   224
        fsm.doIt( in11.getInput() ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   225
        fsm.doIt( in11.getInput() ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   226
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   227
}