EzDevInfo.com

machina.js

js ex machina - finite state machines in JavaScript Machina.js - Finite State Machines in JavaScript machina.js is a framework for creating flexible finite state machines in javascript

How to include machina.js into a node.js application

I want to start learning how to use Machina.js but I simply can not get it to work. According to the documentation I should be able to include Machina with

var lodash = require('lodash');
var machina = require('machina')(lodash);

but I get the following error.

var machina = require('machina')(lodash);
                                 ^
TypeError: object is not a function

I noticed that I should also be able to use

var machina = require('machina')();

and underscore will be used instead of lodash, but I get the same error


Source: (StackOverflow)

Execute an action when transition from one specific state to another?

Is there a way to execute an action when transitioning between specific states in Machina.js?

For example, say I have states "A, B, C".

I want to write a function like:

when("A", "C", function(){ console.log("Caught transition from A to C! Yay!"); }

This is in the same spirit as Akka's FSM implementation. Is this possible?

Thank you!


Source: (StackOverflow)

Advertisements

Passing variables in Machina.js

I'm trying to get to grips with the workings of machina.js.

I have definied state1 which does it's own thing, and it produces a variable someVar, and then it transitions to state2.

How can I pass someVar as an argument to state2?

var fsm = new machina.Fsm({
    initialState: "uninitialized",
    states: {
        uninitialized: {},
        state1: {
            _onEnter: function() {
                var someVar = "..."; // Stuff.
                this.transition("state2");
            }
        },
        state2: {
            _onEnter: function() {
                // someVar as argument somehow...?
                console.log(someVar);
            }
        }
    }
});

Source: (StackOverflow)

how do I extend a state's functionality in Machina.js?

On Machina.js (version 0.3.6), how do I create an instance of an extended FSM constructor, where both child and parent FSMs define behaviors in the same states?

Here is my code:

var _ = require('lodash');
var machina = require('machina')(_);


var MyFsm = machina.Fsm.extend({
    eventListeners: {
        NoHandler: _.bind(console.log, console, "NoHandler"),
        invalidstate:_.bind(console.log, console, "invalidstate")
    },
    initialState: "start",
    states: {
        start: {
            _onEnter: _.bind(console.log, console, "started"),
            connect: function () {
                console.log(this.id + " is connecting");
                this.transition("done")
            }
        },
        done: {}
    }
});
var fsmExample = new MyFsm({
    id: "fsmExample",
    states: {
        done: {
            _onEnter: _.bind(console.log, console, "completed")
        }
    }
});

fsmExample.handle("connect");

And I get this error:

...\node_modules\machina\lib\machina.js:149
            if (states[current][inputType] || states[current]["*"] || this
                               ^
TypeError: Cannot read property 'connect' of undefined
    at _.extend.handle (...\node_modules\machina\lib\machina.js:149:36)
    at Object.<anonymous> (...\server.js:81:12)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)
    at node.js:901:3

What am I doing wrong?


Source: (StackOverflow)