examples/ShoppingCartMachine.js
/**
 * In this example, it will be modelized a fictional shopping cart resource as a Machine.
 * Shopping Cart resource has four states:
 * -> NON_PRICEABLE (Initial State) : This state, which is the initial one, represents an empty cart, thus, we
 *    establish such a cart as impossible to be priced.
 * -> PRICEABLE : After an Item is added, cart is priceable, and in that case state moves to PRICEABLE
 * -> PRICED : If a PRICEABLE cart gots priced, then it gets PRICED state
 * -> CHECKED_OUT : Finally, if a PRICED cart is checked out, it moves to CHECKED_OUT state.
 *
 * [NON_PRICEABLE] ---addItem---> [PRICEABLE] ---price---> [PRICED] ---checkOut---> [CHECKED_OUT]
 *
 * Machine will have three operations : addItem, price and checkOut.
 * Machine will have three simple transitions : NON_PRICEABLE->PRICEABLE, PRICEABLE->PRICED, PRICED->CHECKED_OUT
 *
 * !Note this first example is a rather simplistic one, and carts will have just one item. Next examples will contain
 * much more complex flows (operating with lists, mutable transition states, immutable states, error handling, etc)
 *
 * @constructor
 */
In this example, it will be modelized a fictional shopping cart resource as a Machine.
Shopping Cart resource has four states:
-> NON_PRICEABLE (Initial State) : This state, which is the initial one, represents an empty cart, thus, we
establish such a cart as impossible to be priced.
-> PRICEABLE : After an Item is added, cart is priceable, and in that case state moves to PRICEABLE
-> PRICED : If a PRICEABLE cart gots priced, then it gets PRICED state
-> CHECKED_OUT : Finally, if a PRICED cart is checked out, it moves to CHECKED_OUT state.



Machine will have three operations : addItem, price and checkOut.
Machine will have three simple transitions : NON_PRICEABLE->PRICEABLE, PRICEABLE->PRICED, PRICED->CHECKED_OUT

!Note this first example is a rather simplistic one, and carts will have just one item. Next examples will contain
much more complex flows (operating with lists, mutable transition states, immutable states, error handling, etc)

@constructor
In this example it will be modelized a fictional shopping cart resource as a Machine . Shopping Cart resource has four states : - > NON_PRICEABLE ( Initial State ) : This state which is the initial one represents an empty cart thus we establish such a cart as impossible to be priced . - > PRICEABLE : After an Item is added cart is priceable and in that case state moves to PRICEABLE - > PRICED : If a PRICEABLE cart gots priced then it gets PRICED state - > CHECKED_OUT : Finally if a PRICED cart is checked out it moves to CHECKED_OUT state . Machine will have three operations : addItem price and checkOut . Machine will have three simple transitions : NON_PRICEABLE - > PRICEABLE PRICEABLE - > PRICED PRICED - > CHECKED_OUT !Note this first example is a rather simplistic one and carts will have just one item . Next examples will contain much more complex flows ( operating with lists mutable transition states immutable states error handling etc ) @constructor
function ShoppingCartMachine() {
    var addItemOperation = new Operation("addItem", function (machine, parameters, transition, cb) {
        var err = null;
        if(!machine.getProperties()["items"].getValue()) machine.getProperties()["items"].setValue([]);
        machine.getProperties()["items"].getValue().push("Argentina->Brasil");
        if (!err) {
            cb(null, machine.setCurrentState(transition.getFinalState(machine)));
        } else {
            cb(err, null);
        }
    });
    var priceOperation = new Operation("price", function (machine, parameters, transition, cb) {
        var err = null;
        machine.getProperties()["priced"].setValue(true);
        if (!err) {
            cb(null, machine.setCurrentState(transition.getFinalState(machine)));
        } else {
            cb(err, null);
        }
    });
    var checkOutOperation = new Operation("checkOut", function (machine, parameters, transition, cb) {
        var err = null;
        machine.getProperties()["checkedOut"].setValue(true);
        if (!err) {
            cb(null, machine.setCurrentState(transition.getFinalState(machine)));
        } else {
            cb(err, null);
        }
    });
    var addItemInitialState = function(machine){
        if(!machine.getProperties()["items"].getValue() || machine.getProperties()["items"].getValue().length == 0 ) return "NON_PRICEABLE";
        return "PRICEABLE";
    };
    var addItemFinalState = function(){return "PRICEABLE"};
    var machineInitialState = function(machine){
        if(!machine.getProperties()["items"].getValue() || machine.getProperties()["items"].getValue().length == 0 ) return "NON_PRICEABLE";
        return "PRICEABLE";
    };
    var addItemTransition = new Transition(addItemInitialState, addItemFinalState, addItemOperation);
    var priceTransition = new Transition(function(){return "PRICEABLE"}, function(){return "PRICED"}, priceOperation);
    var checkOutTransition = new Transition(function(){return "PRICED"}, function(){return "CHECKED_OUT"}, checkOutOperation);
    var shoppingCartMachine = new Machine("Shopping Cart Machine", machineInitialState,
        [addItemTransition, priceTransition, checkOutTransition], null);
    shoppingCartMachine.addProperty(new Property("items", null, null));
    shoppingCartMachine.addProperty(new Property("priced", false, null));
    shoppingCartMachine.addProperty(new Property("checkedOut", false, null));
    return shoppingCartMachine;
}
function ShoppingCartMachine ( ) { var addItemOperation = new Operation ( "addItem" , function ( machine , parameters , transition , cb ) { var err = null ; if ( ! machine . getProperties ( ) [ "items" ] . getValue ( ) ) machine . getProperties ( ) [ "items" ] . setValue ( [ ] ) ; machine . getProperties ( ) [ "items" ] . getValue ( ) . push ( "Argentina->Brasil" ) ; if ( ! err ) { cb ( null , machine . setCurrentState ( transition . getFinalState ( machine ) ) ) ; } else { cb ( err , null ) ; } } ) ; var priceOperation = new Operation ( "price" , function ( machine , parameters , transition , cb ) { var err = null ; machine . getProperties ( ) [ "priced" ] . setValue ( true ) ; if ( ! err ) { cb ( null , machine . setCurrentState ( transition . getFinalState ( machine ) ) ) ; } else { cb ( err , null ) ; } } ) ; var checkOutOperation = new Operation ( "checkOut" , function ( machine , parameters , transition , cb ) { var err = null ; machine . getProperties ( ) [ "checkedOut" ] . setValue ( true ) ; if ( ! err ) { cb ( null , machine . setCurrentState ( transition . getFinalState ( machine ) ) ) ; } else { cb ( err , null ) ; } } ) ; var addItemInitialState = function ( machine ) { if ( ! machine . getProperties ( ) [ "items" ] . getValue ( ) || machine . getProperties ( ) [ "items" ] . getValue ( ) . length == 0 ) return "NON_PRICEABLE" ; return "PRICEABLE" ; } ; var addItemFinalState = function ( ) { return "PRICEABLE" } ; var machineInitialState = function ( machine ) { if ( ! machine . getProperties ( ) [ "items" ] . getValue ( ) || machine . getProperties ( ) [ "items" ] . getValue ( ) . length == 0 ) return "NON_PRICEABLE" ; return "PRICEABLE" ; } ; var addItemTransition = new Transition ( addItemInitialState , addItemFinalState , addItemOperation ) ; var priceTransition = new Transition ( function ( ) { return "PRICEABLE" } , function ( ) { return "PRICED" } , priceOperation ) ; var checkOutTransition = new Transition ( function ( ) { return "PRICED" } , function ( ) { return "CHECKED_OUT" } , checkOutOperation ) ; var shoppingCartMachine = new Machine ( "Shopping Cart Machine" , machineInitialState , [ addItemTransition , priceTransition , checkOutTransition ] , null ) ; shoppingCartMachine . addProperty ( new Property ( "items" , null , null ) ) ; shoppingCartMachine . addProperty ( new Property ( "priced" , false , null ) ) ; shoppingCartMachine . addProperty ( new Property ( "checkedOut" , false , null ) ) ; return shoppingCartMachine ; }
In this example, it will be modelized a fictional shopping cart resource as a Machine.
In this example it will be modelized a fictional shopping cart resource as a Machine .