Skip to content

Instantly share code, notes, and snippets.

@caracal7
Forked from podefr/LICENSE.txt
Created February 1, 2018 20:21
Show Gist options
  • Save caracal7/a1803c6c8fc09c3cb4740f5c3c67f148 to your computer and use it in GitHub Desktop.
Save caracal7/a1803c6c8fc09c3cb4740f5c3c67f148 to your computer and use it in GitHub Desktop.
Finite State Machine

Finite State Machine

Generates a state machine with the value you pass in parameter. You can define states, transitions to these states, pass parameters to the actions, and execute the actions in a given context. This code was test driven developed with JsTestDriver, the TestCase is attached.

Downsized with the help of UglifyJS on marijn's website : http://marijnhaverbeke.nl/uglifyjs

The example of the state machine comes from http://www.objectmentor.com/resources/articles/umlfsm.pdf

v1: only 85 bytes but the actions were ran in the global scope

v2: 128 bytes and the actions can be executed in a given context

v3: 126 bytes with careful reuse of an already declared var -c- in the event function

v4: 125 bytes by using the assignment c=b[a][c] directly to determine whether or not the event exists

v5: 106 bytes by removing unecessary code! What feature should I add now?

/**
* Create a finite state machine
* @param {String} a the state on which the state machine will be initialized
* @param {Object} b the state machine's diagram :
* { "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
){
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(a,b){return{event:function(c,d){return(c=b[a][c])&&((c[0][0]||c[0]).call(c[0][1],d),a=c[1]||a)}}}
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2011 Olivier Scherrer
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
{
"name": "StateMachineGenerator",
"description": "A state machine generator",
"keywords": [
"state",
"machine",
"generator",
"fsm",
"design pattern"
]
}
TestCase("SM", {
"test SM API": function() {
var sm = SM();
assertFunction(sm.event);
},
"test SM transits between 2 states": function() {
var open=function(){open.called=true;},
close=function(){close.called=true;},
sm = SM("closed",
{
// When in closed state, if the event is "open!" then execute the open action and go to opened state.
"closed":{"open!": [open, "opened"]},
// The opposite
"opened":{ "close!": [close, "closed"]}
});
assertUndefined(open.called);
assertEquals("opened", sm.event("open!"));
assertTrue(open.called);
assertUndefined(close.called);
assertEquals("closed", sm.event("close!"));
assertTrue(close.called);
},
"test SM doesn't crash if wrong event in given state": function() {
var sm = SM("state", {
"state": {"event": [function(){}, "nextState"]}
});
assertUndefined(sm.event("crash!"));
},
"test SM can pass arguments to the action": function () {
var action = function(arg) {action.arg = arg; },
sm = SM("state", {
"state": {"event": [action, "state2"]},
"state2": {"event": [action]}
}),
arg = {},
arg2 = {};
sm.event("event", arg);
assertSame(arg, action.arg);
sm.event("event", arg2);
assertSame(arg2, action.arg);
},
"test SM can run actions in given context": function () {
var action = function() { action.ctx = this; },
ctx = {},
sm = SM("state", {
"state": { "event": [[action, ctx], "nextState"]}});
sm.event("event");
assertSame(ctx, action.ctx);
},
"test the whole diagram": function() {
var alarm = function () { alarm.called = true; alarm.args = arguments; },
open = function () { open.called = true; open.args = arguments; },
thank = function () { thank.called = true; thank.args = arguments; },
close = function () { close.called = true; close.args = arguments; },
sm = SM("closed", {
"closed": {
"pass": [alarm],
"coin": [open, "opened"]
},
"opened": {
"coin": [thank],
"pass": [close, "closed"]
}
});
assertEquals("closed", sm.event("pass", "picture"));
assertTrue(alarm.called);
assertEquals("picture", alarm.args[0]);
assertEquals("opened", sm.event("coin", "$2"));
assertTrue(open.called);
assertEquals("$2", open.args[0]);
assertEquals("opened", sm.event("coin", "c50"));
assertTrue(thank.called);
assertEquals("c50", thank.args[0]);
assertEquals("closed", sm.event("pass"));
assertTrue(close.called);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment