I completely understand the concept of a FSM, but I'd like to see one get written, from the ground-up, in PHP (if possible). It's one of those things I probably missed out on by not taking CS at uni.
So, say I have a Car class that has methods:
Code: Select all
Car {
const GEAR_ONE = 1;
const GEAR_TWO = 2;
const GEAR_THREE = 3;
const GEAR_REVERSE = -1;
start()
changeGear($gearNumber)
drive()
brake()
turnLeft()
turnRight()
stop()
}This is hopefully a simple example? I'm not being very accurate with the logic/physics here for the sake of simplicity...
A Car can only be started if:
It is "not started".
Once it is started, it is "started".
A Car can only drive if:
It is "started" AND it is "not braking".
Once it is driving, it is "driving".
A Car can only turn left or right if:
It is "driving"
A Car can only change gear if it:
It is "started".
This has no state change effect.
A Car can only change gear into reverse if:
It is "not driving" and it is "started".
This has no state change effect.
A car can only brake if:
If it is "started"
When braking, (simplicity) the car is "not driving".
A Car can only stop if:
It is "started" AND it is "not driving"
Once stopped, it is "not started".
So this is a pretty simple model for the states this FSM needs.
Does anybody want to help me create a state machine to model this? I could hack my way through it (and I'm expecting people to post such hacked solutions... please don't post a solution if you don't know what I mean by FSM), but what I'm looking for is the generally accepted algorithm for tackling a FSM.
I see a lot of talk of lookup tables for state transitions. Although this example is so simple that it's not needed, it would be helpful (for my learning benefit) if we could throw that in.
Does anyone have experience in this area?
I'm going to be away for a few hours but I'm looking forward to coming back and watching this discussion evolve.
Again, please don't post solutions that "just work" if what you're posting is not a traditional finite state machine. The point of the exercise is to learn