Fork me on GitHub

How to Install

In nodeJS:
npm install automatta

In javascript:

Load the file lib/automatta/automatta.js usually:
<script type='text/javascript' src='automatta/lib/auttomatta/automatta/automatta.js'></script>

Quick start

Create an Automata is very simple and easy. We have to define states (name, definition and condicions to arrive to other states), and init & final states. A dump example (in nodeJS):
var myAutomaton = require('automatta').createAutomata();

myAutomaton.addStatus("init"
,function(){
        console.log("init!" + x);
 }
,function(){
    if(x%2 == 0) return "middle";
 }
);

myAutomaton.addStatus("middle",
function(){
    console.log("middle!" + x);
}
,function(){
    if (x == 3) {
        return "init"; 
    }
    else if(x > 4) {
        return "final"; 
    }
});

myAutomaton.addStatus("final"
,function(){
    console.log("final!" + x);
}
,function(){});

myAutomaton.setInitialStatus("init");
myAutomaton.setFinalStatus("final");

So, this Automata is defined with the next diagram:

+------------+              +-------------+            +------------+
|            |   if(x%2==0) |             | if(x>4)    |            |
|            |              |             |            |            |
| Init       |+------------>| Middle      |+---------->| Final      |
|            |              |             |            |            |
|            |              |             |            |            |
+------------+              +-------------+            +------------+
       ^                            +
       |                            |
       |                            |
       |      if(x==3)              |
       +----------------------------+

So, if we execute this:
for(var x = 0; x < 6; x++){
    myAutomaton.exec();
}
console.log("trace of this automata: " + myAutomaton.getTrace());

The output will be:
middle!0
middle!1
middle!2
init!3
middle!4
final!5
trace of this automata: [ 'init', 'middle', 'middle', 'middle', 'init', 'middle', 'final' ]

Documentation

CreateAutomata()

Create an Automata without any definition. It will be necessary to define status, init status and final status.
var automatta = require('automatta');
var myAutomata = automatta.createAutomata();

getCurrentStatus()

Return the current status of an automata. If it not exists, returns false.
var automatta = require('automatta');

var myAutomata = automatta.createAutomata();

myAutomata.addStatus("hi",function(){console.log("hi")},function(){});

myAutomata.setInitStatus("hi");

myAutomata.getcurrentStatus(); //This should return "hi"

addStatus(name,execution,conditions)

Create a new state for the automata with the name of this state, execution, and conditions. Execution is a function with the set of instruccions that the automata will execute when it will be in this state. Conditions is the filter function that it will decide the next state. Conditions should return the next state's name; Otherwise, the automata will continue in the current status. For instance:
var automatta = require('automatta');

var myAutomata = automatta.createAutomata();

myAutomata.addStatus("hi",function(){console.log("hi")},function(){ if(x==1)return "one"});
It's equivalent to:
      +----------------+                   +----------------+
      |                |    if(x == 1)     |                |
      |   HI           |                   |   ONE          |
      |                |+----------------->|                |
      |                |                   |                |
      +----------------+                   +----------------+
However, it would be needed declare a new state with name "one".

removeStatus(name)

Remove the state with the specified name.

setInitialStatus(name)

Set the state with the specified name as the initial status. Attention: if you don't specify the initial status, the automata never will be executed.. A example for its use:
var automatta = require('automatta');

var myAutomata = automatta.createAutomata();

myAutomata.addStatus("hi",function(){console.log("hi")},function(){});

myAutomata.setInitStatus("hi");

SetFinalStatus(name)

Matemathically, an automata contains one or more final states, where it decides if an input is accepted or not. You can use final states if you want with this function. Set the state with the specified name as a posible final status.

removeFinalStatus(name)

Removes the state with the specified name of the final states list.

Exec()

Executes the automata once. For this, the automata will make the next steps:
- First of all, it detects the currentState (init if the automata never has been executed)
- After, the automata executes the function "execution" for the currentState.
- Finally, the automata check the conditions for the currentState and changes the currentState for the next (if any).
For instance:
var myAutomaton = require('automatta').createAutomata();


myAutomaton.addStatus("init",function(){console.log("init!" + x);},function(){if(x%2 == 0) return "final";});
myAutomaton.addStatus("final",function(){console.log("final!" + x);},function(){ if(x%2 == 1) return "init";});
myAutomaton.setInitialStatus("init");
myAutomaton.setFinalStatus("final");
for(var x = 0; x <= 6; x++){
    myAutomaton.exec();
}
it returns:
final!0
init!1
final!2
init!3
final!4
init!5

isFinished()

Check if the currentStatus is a final Status. If you want a explation more mathematical, it checks if the input for this automata is accepted or not.

getTrace()

Returns a string array with all states where the automata had stayed. For instance:
var myAutomaton = require('automatta').createAutomata();


myAutomaton.addStatus("init",function(){console.log("init!" + x);},function(){if(x%2 == 0) return "final";});
myAutomaton.addStatus("final",function(){console.log("final!" + x);},function(){ if(x%2 == 1) return "init";});
myAutomaton.setInitialStatus("init");
myAutomaton.setFinalStatus("final");
for(var x = 0; x <= 6; x++){
    myAutomaton.exec();
}
console.log("A automata to say if a number is even");
console.log(myAutomaton.getTrace());
It returns:
final!0
init!1
final!2
init!3
final!4
init!5
init,final,init,final,init,final,init