2017-01-21 6 views
2

У меня есть проблема, чтобы запустить эту функцию, у меня нет ошибки, но ничего не произошло, я беру эту функцию здесь 140 bytes Finite State Machine in JavaScript:JavaScript Конечный автомат (FSM) как запустить эту функцию?

@param {строка} состояние, на котором будет инициализирована государственная машина

@param {Объект} б диаграммы государственного аппарата:

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

@returns {Объект}

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; 

ответ

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);