Java Reference
In-Depth Information
Sidebar 5.2 FSA implementation
This procedure translates a FSA into Java code. The procedure can handle only:
State machines, i.e. non-hierarchical states.
States with only entry actions.
Transitions having only the guard, i.e. without action.
Procedure for implementing a state machine:
1 For each state S define two constant integers: S and S_entry , where:
- all S_entry variables have odd values
- S # S_entry ! 1
2 Define an integer state attribute state .
3 Initialize state to I_entry , where I is the initial state.
4 Define the state evolution method with a switch construct having state as the
index variable.
5 For each state S add the following code to the switch:
-
a case branch having index S_entry :
case S_entry :
-
the entry action code
-
an instruction incrementing the state attribute
-
a case branch having index S :
case S :
-
for each transition T from the current state to a state Snext , add the code:
if( G ) state # Snext_entry ;
where G is the guard of the transition
-
add the break statement:
break;
implemented as String attributes.
public class CPU {
private Bus bus;
private String RegA;
private String RegB;
CPU(Bus bus) { this .bus # bus; }
The CPU must execute a sequence of operations as specified in the test.
Using the automatic procedure, the state machine described in Figure 5.7 can
be implemented in Java using the algorithm described in Sidebar 5.2. The
detailed steps of the algorithm applied to the given FSA are described below.
Step 1. Define the integer constants:
private final static int READ_MEM0_entry # 1;
private final static int READ_MEM0 # 2;
private final static int READ_A_entry # 3;
private final static int READ_A # 4;
private final static int READ_MEM1_entry # 5;
private final static int READ_MEM1 # 6;
Search WWH ::




Custom Search