JavaScript有限状态机(FSM)如何运行此功能?

时间:2017-01-21 23:11:07

标签: javascript state-machine

我有运行此功能的问题,我没有错误但没有任何反应,我从这里140 bytes Finite State Machine in JavaScript采取此功能:

@param {String}初始化状态机的状态

@param {Object} b状态机的图表:

       { "state1": {
                       "event1": [action1, "state2"],
                       "event2": [action2]
                   },
         "state2": { 
                       "event3": [[action3, context], "state1"]
                   }
      }

@returns {Object}

SM = function(
 a                              // stores the current state
 ,b                             // an object to store all states and their transitions
){
 return{
   event:function(              // The function to send an event to the state machine
      c                         // The name of the event
      ,d                        // The arguments to pass to the action                        
   ){
      console.log(c);
      console.log(b[a][c]);

    return (c=b[a][c])          // Save the array [action, nextState] in c which is carefuly reused,
            && (                // If c is defined.
            (c[0][0]||c[0])     // Either c[0] is the function, or c[0][0] if a scope is given
            .call(c[0][1],d),   // call the function in the context or call it directly
            a=c[1]||a           // The next state is the new state and the new state is returned
        )                      
   }                
  }
}

function msg(x){
  alert(x);
}

function msg1(x){
  alert(x);
}

function msg2(x){
  alert(x);
}

var context = 20;

b =   { "state1": {
  "event1": [msg, "state2"],
  "event2": [msg1]
},
       "state2": { 
         "event3": [[msg2, context], "state1"]
       }
      }

w=SM("state1", b);
w.event;

1 个答案:

答案 0 :(得分:0)

...解决FSM in 140byte (jsfiddle)

SM = function(
 a                              // stores the current state
 ,b                             // an object to store all states and their transitions
){
  //console.log(b);
 return{
   event:function(              // The function to send an event to the state machine
      c                         // The name of the event
      ,d                        // The arguments to pass to the action                        
   ){
      console.log(c[0][1])
    return (c=b[a][c])          // Save the array [action, nextState] in c which is carefuly reused,
            && (                // If c is defined.
            (c[0][0]||c[0])     // Either c[0] is the function, or c[0][0] if a scope is given
            .call(c[0][1],d),   // call the function in the context or call it directly
            a=c[1]||a           // The next state is the new state and the new state is returned
        )                      
   }                
  }
}

function msg(x){
  alert(x*2);
}
var arr=[3,5];
console.log(arr[0]);
function msg1(x){
  alert(x[0]*x[1]);
}

function msg2(){
  alert("I'm here");
}

var context = "";

var person1 = {name: 'Marvin', age: 42, size: '2xM'};

var say = function say(greeting){
    alert(greeting + ', ' + this.name);
};

b =   { 
  "state1": {
    "event1": [msg, "state2"],
    "event2": [msg1]
  },
  "state2": { 
     "event3": [[msg2], "state1"]
  },
  "state3": { 
     "event4": [[say, person1], "state2"]
  }  
}

w=SM("state1", b);
y = w.event("event1",5);
console.log(y);

w=SM(y, b);
y = w.event("event3",8);
console.log(y);

w=SM(y, b);
y = w.event("event2",arr);
console.log(y);

w=SM("state3", b);
y = w.event("event4","My name is:");
console.log(y);