how can i code a fsm?
i have design a fsm, with entry state, input state and find the action state.
how will be the way to code it?
thanks
i have design a fsm, with entry state, input state and find the action state.
how will be the way to code it?
thanks
is finite state machine.
i have decide the entry state and the input state and what is the action to take.
i'm looking froward for a solution to code them
這是你的功課嗎?
public class FSM {
private int myState;
public
FSM()
{
myState = 0;
}
public
FSM(
int entryState
){
myState = entryState;
}
public void
doStateChange()
{
doStateChange(myState);
}
public void
doStateChange(
int inputState
){
myState = inputState;
switch (myState++) {
case 0:
System.out.println("Am I a lazy, useless person?\n");
break;
case 1:
System.out.println("I sure am!!\n");
break;
default:
myState = 0;
doStateChange();
}
}
}
If you couldn't figure that one out, you're gonna flunk out for sure.
i just want to find some solution to help me shorten up the code. cause i using a lot of the if...else statement for the change of state.
Sorry, that was a little rude of me. Well, I hope you liked my example; it doesn't have a single if/then/else. When handling multiple states, switch...case is pretty common. Some advice, though: don't nest switch/case. Some people code their FSMs that way, and it's awful.
currently i use a 2d array to store the action, then i check for the entry and input and give a number. then only i compare with the action. there fore i'm using if...else.
is there any problem of using nested switch?
because i also thinking of using it.
You should basically never nest switch..case statements because it is ugly, and becomes unreadable. Believe me, I used to work at a place where the software was full of 1000+ line nested switch/case statements. It was a nightmare... especially on the day somebody ran an auto-format script that messed up tab-spacing...
So anyway, I think I undersand your homework assignment. Your fsm needs to perform different actions for the same input, based on its state. Well, you might at least consider making a method for each state. So then you have one switch...case, and one method for each state. That's fine as long as you only have a couple states...if you have many many states, then you need to design things differently.
so for just a few states:
public class FSM {
private int myState;
public
FSM(
int entryState
){
myState = entryState;
}
public void
processInput(
Object input
){
switch (myState) {
case 0:
handleState0(input);
break;
case 1:
handleState1(input);
break;
default:
System.out.println("unknown state\n");
}
}
public void
handleState0(
Object input
){
}
public void
handleState1(
Object input
){
}
}
If you have many states, but just a few ways to handle them, then you're on the right track with a mapping array, but still better not to nest switch..case:
public class FSM2 {
private interface Handler{
public void handle(Object input);
}
private static class
HandlerTypeA
implements Handler {
public void handle (Object input){
System.out.println("This is handling method A");
}
}
private static HandlerTypeA handlerTypeA;
private static class
HandlerTypeB
implements Handler {
public void handle (Object input){
System.out.println("This is handling method B");
}
}
private static HandlerTypeB handlerTypeB;
private static intstatecount = 30;
private intmyState;
private Handler map[];
public
FSM2(
int entryState
){
myState = entryState;
map= new Handler[statecount];
map[0] = handlerTypeA;
map[1] = handlerTypeB;
//...
map[30] = handlerTypeA;
}
public void
processInput(
Object input
){
map[myState].handle(input);
}
}
The reason we have interfaces here is because java doesn't have pointers to functions. It's more labor intensive, in terms of lines of code, but it's cleaner, safer, and more object oriented.
Does that help?