Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
215 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,42 @@ | ||
|
||
SM.Event = function(name, options) { | ||
console.log("******* creating a new event *********"); | ||
this.name = name; | ||
this.options = options; | ||
SM.Event = function(name, machine, options) { | ||
this.name = name; | ||
this.machine = machine; | ||
this.options = options; | ||
this.guards = new SM.GuardsCollection(); | ||
return this; | ||
} | ||
|
||
SM.Event.prototype = { | ||
transition: function(){ | ||
console.log("******* creating a new transition *********"); | ||
transition: function(options){ | ||
// var guard = new SM.Guard(this.name, options) | ||
// this.guards.push(guard); | ||
this.guards.add(this.name, options); | ||
this.machine.states.add([options.from, options.to]); | ||
return this; | ||
}, | ||
can_fire: function(){ | ||
return this.guards.match(this.name, this.machine.state); | ||
}, | ||
fire: function(){ | ||
console.log("******* fire called *********"); | ||
var transition = this.transition_for(); | ||
if( transition ){ | ||
// do the transition | ||
transition.perform(); | ||
} | ||
else{ | ||
// transition failed | ||
} | ||
}, | ||
transition_for: function(){ | ||
if(this.can_fire()){ | ||
var from = this.machine.state; | ||
var to = this.guards.find_to_state(this.name, from); | ||
return new SM.Transition(this.machine, this, from, to); | ||
} | ||
else{ | ||
return false; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
|
||
SM.Guard = function(name, options){ | ||
this.name = name; | ||
this.from = options.from; | ||
this.to = options.to; | ||
return this; | ||
} | ||
|
||
SM.Guard.prototype = { | ||
match: function(name, from){ | ||
if(name == this.name && from == this.from){ | ||
return true; | ||
} | ||
else{ | ||
return false; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
|
||
SM.GuardsCollection = function() { | ||
this.guards = []; | ||
return this; | ||
} | ||
|
||
SM.GuardsCollection.prototype = { | ||
add: function(name, options) { | ||
var guard = new SM.Guard(name, options) | ||
this.guards.push(guard); | ||
return guard; | ||
}, | ||
all: function() { | ||
return this.guards; | ||
}, | ||
match: function(name, from){ | ||
for(var i=0;i<this.guards.length;i++){ | ||
if( this.guards[i].match(name, from) ){ | ||
return true; | ||
} | ||
} | ||
return false; | ||
}, | ||
find_to_state: function(name, from){ | ||
return 'idling'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
|
||
SM.State = function(name) { | ||
// console.log("******* creating a new state *********"); | ||
this.name = name; | ||
return this; | ||
} | ||
|
||
SM.State.prototype = { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,25 @@ | ||
|
||
SM.StateCollection = function() { | ||
this.events = []; | ||
this.states = []; | ||
return this; | ||
} | ||
|
||
SM.StateCollection.prototype = { | ||
add: function(name, options) { | ||
add: function(name) { | ||
// creates an event | ||
} | ||
// console.log(">>>>>>> adding state: "+name); | ||
// console.log(typeof name); | ||
if(typeof name == 'string'){ | ||
var state = new SM.State(name) | ||
this.states.push(state); | ||
} | ||
else{ | ||
for(var i=0;i<name.length;i++){ | ||
this.add(name[i]); | ||
} | ||
} | ||
}, | ||
all: function() { | ||
return this.states; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,20 @@ | ||
|
||
SM.Transition = function(){ | ||
SM.Transition = function(machine, event, from, to){ | ||
console.log("machine state: "+machine.state); | ||
console.log("event: "+event); | ||
console.log("from: "+from); | ||
console.log("to: "+to); | ||
this.machine = machine; | ||
this.event = event; | ||
this.from = from; | ||
this.to = to; | ||
return this; | ||
} | ||
|
||
SM.Transition.prototype = { | ||
add: function(name, options) { | ||
// creates an event | ||
this.events.push({ name: name, options: options}); | ||
}, | ||
all: function() { | ||
return this.events; | ||
} | ||
perform: function() { | ||
console.log("machine state: "+this.machine.state); | ||
// do the transition with all the before and after callbacks | ||
this.machine.set_state(this.to); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
|
||
jsStateMachineTests.EventTests = function(Y) { | ||
var testSuite = new Y.Test.Suite("Event"); | ||
|
||
testSuite.add(new Y.Test.Case({ | ||
|
||
name: "general", | ||
|
||
setUp : function () { | ||
this.car = {}; | ||
}, | ||
|
||
tearDown : function () { | ||
delete this.car; | ||
}, | ||
|
||
testCanCheckIfEventCanBeFired : function () { | ||
new SM.StateMachine('state', this.car, { initial: 'parked' }); | ||
this.car.event('start') | ||
.transition({ from: 'parked', to: 'idling' }); | ||
|
||
this.car.event('stop') | ||
.transition({ from: 'idling', to: 'parked' }); | ||
|
||
Y.Assert.isTrue(this.car.can_start()); | ||
Y.Assert.isFalse(this.car.can_stop()); | ||
}, | ||
|
||
testCanFireAndEvent : function () { | ||
new SM.StateMachine('state', this.car, { initial: 'parked' }); | ||
this.car.event('start') | ||
.transition({ from: 'parked', to: 'idling' }); | ||
|
||
this.car.event('stop') | ||
.transition({ from: 'idling', to: 'parked' }); | ||
|
||
this.car.start(); | ||
Y.Assert.areEqual('idling', this.car.state()); | ||
// Y.Assert.isTrue(this.car.can_start()); | ||
// Y.Assert.isFalse(this.car.can_stop()); | ||
} | ||
})); | ||
|
||
return testSuite; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters