function StateMachine(states) {
this.states = states;
this.indexes = {};
for(var i = 0; i < this.states.length; ++i){
this.indexes[this.states[i].name] = i;
if (this.states[i].initial){
this.currentState = this.states[i];
}
}
this.consumeEvent = function(e){
if(this.currentState.events[e]){
this.currentState = this.states[this.indexes[this.currentState.events[e]]];
}
}
this.getStatus = function(){
return this.currentState.name;
}
}