Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ Using npm:

```shell
npm install --save-dev javascript-state-machine
or
yarn add javascript-state-machine
```

In Node.js:
Expand Down
37 changes: 30 additions & 7 deletions bin/examples
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,40 @@
//
// This script is used to regenerate the example visualizations
//
// It will scan all the .js files in the 'examples' folder and generate a
// * .dot - file with dot directives for graphviz
// * .svg - svg graphics representation
// * .png - png graphics representation
//
// If provided with an argument, it will only generate output for the specified
// argument.
//
//=================================================================================================

var fs = require('fs'),
path = require('path'),
child = require('child_process');

//-------------------------------------------------------------------------------------------------
process.chdir(path.dirname(process.argv[1]));

if (process.argv.length > 2) {
var fn = process.argv[2],
fnfull = '../examples/' + fn;
if (!fs.existsSync(fnfull)) {
console.log('Error - file: ' + fnfull + ' does not exist');
}

fs.readdirSync('examples')
.filter(function(file) { return path.extname(file) === ".js" })
.map(visualize);
else {
visualize(fn);
}
}

else {
fs.readdirSync('../examples')
.filter(function(file) { return path.extname(file) === ".js" })
.map(visualize);
}

//-------------------------------------------------------------------------------------------------

Expand All @@ -24,10 +47,10 @@ function visualize(example) {
dot = fsm.visualize(),
svg = dot2svg(dot),
png = dot2png(dot);
console.log('visualizing examples/' + example);
fs.writeFileSync('examples/' + name + '.dot', dot);
fs.writeFileSync('examples/' + name + '.svg', svg);
fs.writeFileSync('examples/' + name + '.png', png, 'binary');
console.log('visualizing ../examples/' + example);
fs.writeFileSync('../examples/' + name + '.dot', dot);
fs.writeFileSync('../examples/' + name + '.svg', svg);
fs.writeFileSync('../examples/' + name + '.png', png, 'binary');
}

//-------------------------------------------------------------------------------------------------
Expand Down
63 changes: 59 additions & 4 deletions dist/state-machine-visualize.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,9 @@ function dotcfg(fsm, options) {
name = options.name,
rankdir = dotcfg.rankdir(options.orientation),
states = dotcfg.states(config, options),
statedefs = dotcfg.statedefs(config, options),
transitions = dotcfg.transitions(config, options),
dotPrefix = config.dotPrefix,
result = { }

if (name)
Expand All @@ -135,9 +137,15 @@ function dotcfg(fsm, options) {
if (states && states.length > 0)
result.states = states

if (statedefs && statedefs.length > 0)
result.statedefs = statedefs;

if (transitions && transitions.length > 0)
result.transitions = transitions

if (dotPrefix)
result.dotPrefix = dotPrefix;

return result
}

Expand All @@ -164,6 +172,11 @@ dotcfg.states = function(config, options) {
return states;
}

dotcfg.statedefs = function(config, options) {
return config.options['statedefs'];
// can be null
}

dotcfg.transitions = function(config, options) {
var n, max, transition,
init = config.init,
Expand Down Expand Up @@ -198,7 +211,6 @@ dotcfg.transition = function(name, from, to, dot, config, options, output) {
else {
output.push(mixin({}, { from: from, to: to, label: pad(name) }, dot || {}))
}

}

//-------------------------------------------------------------------------------------------------
Expand All @@ -218,30 +230,64 @@ function dotify(dotcfg) {
var name = dotcfg.name || 'fsm',
states = dotcfg.states || [],
transitions = dotcfg.transitions || [],
statedefs = dotcfg.statedefs,
dotPrefix = dotcfg.dotPrefix,
rankdir = dotcfg.rankdir,
output = [],
n, max;

output.push("digraph " + quote(name) + " {")
if (rankdir)
output.push(" rankdir=" + rankdir + ";")
for(n = 0, max = states.length ; n < max ; n++)
output.push(dotify.state(states[n]))

if (dotPrefix) {
output.push(dotify.dotPrefix(dotPrefix));
}

if (statedefs) {
for(n = 0, max = statedefs.length ; n < max ; n++)
output.push(dotify.statedef(statedefs[n]))
}
else {
for(n = 0, max = states.length ; n < max ; n++)
output.push(dotify.gen(states[n]))
}

for(n = 0, max = transitions.length ; n < max ; n++)
output.push(dotify.edge(transitions[n]))
output.push("}")
return output.join("\n")

}

dotify.state = function(state) {
dotify.gen = function(state) {
return " " + quote(state) + ";"
}

dotify.statedef = function(statedef) {
var retstr = " " + quote(statedef.name);
if (statedef.dot) {
retstr += dotify.gen.attr(statedef.dot);
}
return retstr + ";";
}

dotify.edge = function(edge) {
return " " + quote(edge.from) + " -> " + quote(edge.to) + dotify.edge.attr(edge) + ";"
}

dotify.dotPrefix = function(dotPrefix) {
var prefixStrAry = [];
var ix, key;
var keys = Object.keys(dotPrefix);
for (ix = 0; ix < keys.length; ix++) {
key = keys[ix];
prefixStrAry.push(' ' + key + ' ' + dotify.gen.attr(dotPrefix[key]) + ";" );
}

return prefixStrAry.join("\n");
}

dotify.edge.attr = function(edge) {
var n, max, key, keys = Object.keys(edge).sort(), output = [];
for(n = 0, max = keys.length ; n < max ; n++) {
Expand All @@ -252,6 +298,15 @@ dotify.edge.attr = function(edge) {
return output.length > 0 ? " [ " + output.join(" ; ") + " ]" : ""
}

dotify.gen.attr = function(attrs) {
var n, max, key, keys = Object.keys(attrs).sort(), output = [];
for(n = 0, max = keys.length ; n < max ; n++) {
key = keys[n];
output.push(key + "=" + quote(attrs[key]))
}
return output.length > 0 ? " [ " + output.join(", ") + " ]" : ""
}

//-------------------------------------------------------------------------------------------------

visualize.dotcfg = dotcfg;
Expand Down
2 changes: 1 addition & 1 deletion dist/state-machine-visualize.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 35 additions & 4 deletions dist/state-machine.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,9 +210,16 @@ function Config(options, StateMachine) {
this.init = this.configureInitTransition(options.init);
this.data = this.configureData(options.data);
this.methods = this.configureMethods(options.methods);
this.dotPrefix = options['dotPrefix'] || this.defaults.dotPrefix;
this.hasStateDefs = false;

this.map[this.defaults.wildcard] = {};

if (options['statedefs']) {
this.hasStateDefs = true;
this.configureStateDefs(options['statedefs']);
}

this.configureTransitions(options.transitions || []);

this.plugins = this.configurePlugins(options.plugins, StateMachine.plugin);
Expand Down Expand Up @@ -311,16 +318,35 @@ mixin(Config.prototype, {
return plugins
},

configureStateDefs: function(statedefs) {
var n;
for (n = 0; n < statedefs.length; n++) {
this.addState(statedefs[n].name);
}
},

configureTransitions: function(transitions) {
var i, n, transition, from, to, wildcard = this.defaults.wildcard;
var i, n, transition, fromStates, from, to, wildcard = this.defaults.wildcard;
var undefinedStates = [];
for(n = 0 ; n < transitions.length ; n++) {
transition = transitions[n];
from = Array.isArray(transition.from) ? transition.from : [transition.from || wildcard]
fromStates = Array.isArray(transition.from) ? transition.from : [transition.from || wildcard]
to = transition.to || wildcard;
for(i = 0 ; i < from.length ; i++) {
this.mapTransition({ name: transition.name, from: from[i], to: to });
if (this.hasStateDefs && to !== wildcard && this.states.indexOf(to) === -1) {
undefinedStates.push(to);
}
for(i = 0 ; i < fromStates.length ; i++) {
from = fromStates[i];
if (this.hasStateDefs && from !== wildcard && from !== 'none' &&
this.states.indexOf(from) === -1) {
undefinedStates.push(from);
}
this.mapTransition({ name: transition.name, from: from, to: to });
}
}
if (undefinedStates.length > 0) {
throw new Error('Undefined states in transitions: "' + undefinedStates.join(', ') + '"');
}
},

transitionFor: function(state, transition) {
Expand Down Expand Up @@ -640,6 +666,11 @@ StateMachine.defaults = {
init: {
name: 'init',
from: 'none'
},
dotPrefix: {
graph: { fontcolor: "dimgray", fontname:"Helvetica", splines: "spline"},
node: {color: "dimgray", fontsize: 13, fontcolor: "dimgray", fontname: "Helvetica"},
edge: { fontcolor: "dimgray", fontsize: 10, fontname: "Arial"}
}
}

Expand Down
Loading