Permalink
Browse files

Added onEnter/onExit for FSM.

Changed some documentation.
  • Loading branch information...
1 parent 1c1af86 commit 41d9498501865ecb362b4bbb169d85935af093a1 hyperandroid committed Apr 12, 2012
Showing with 73 additions and 11 deletions.
  1. +38 −6 automata.js
  2. +3 −2 readme.md
  3. +3 −0 test/test.html
  4. +9 −1 test/test1.js
  5. +20 −2 test/test4.js
View
44 automata.js
@@ -8,9 +8,9 @@
/**
* requireJS available ???
*/
-var module= module || {};
+var module = module || {};
-(function(root) {
+(function (root) {
/**
@@ -21,6 +21,7 @@ var module= module || {};
*/
function extend(subc, superc) {
var subcp = subc.prototype;
+ var method;
// Class pattern.
var F = function() {
@@ -38,7 +39,7 @@ var module= module || {};
// los metodos de superc, que no esten en esta clase, crear un metodo que
// llama al metodo de superc.
- for (var method in subcp) {
+ for ( method in subcp ) {
if (subcp.hasOwnProperty(method)) {
subc.prototype[method] = subcp[method];
}
@@ -48,7 +49,7 @@ var module= module || {};
/**
* Bind mechanism. Honors already existing bind functions.
*/
- Function.prototype.bind= Function.prototype.bind || function( /* this */ ) {
+ Function.prototype.bind = Function.prototype.bind || function( /* this */ ) {
var fn= this; // the function
var args= Array.prototype.slice.call(arguments); // copy the arguments.
@@ -619,7 +620,7 @@ var module= module || {};
var me= this;
- this.setOnEnter( function( session, state, transition, msg ) {
+ FSM.FSM.superclass.setOnEnter.call( this, function( session, state, transition, msg ) {
me.initialTransition.fireTransition( {
msgId : __InitialTransitionId
},
@@ -633,8 +634,31 @@ var module= module || {};
});
},
+ setOnEnter : function( m ) {
+ this._onEnter= m;
+ return this;
+ },
+
+ callOnEnter : function( session, transition, msg ) {
+ session.callMethod( this._onEnter, session, transition, msg );
+ FSM.FSM.superclass.callOnEnter.call( this, session, transition, msg );
+ },
+
/**
* Build a Session for this FSM object.
+ * A session is (of course) initially empty.
+ * This method is called only once, and from this on, sub-state automata go on with the normal lifecycle
+ * calling their custom onEnter method which launcher the initialTransition.
+ * Strictly talking, automata object should be constructed from a building block where just an FSM defined
+ * just one state being a substate of the target FSM.
+ *
+ * To avoid such automata definition inefficiencies, here I'm calling the block manually:
+ * + pushing a top level FSM context
+ * + calling its onEnter method as if an initialTransition was fired.
+ *
+ * I'm not happy with the semantics of manually calling a (supposed to be) initial transition. Will keep it
+ * this way for the sake of simplicity, but will probably change this semantics in the future,
+ * (by adding an Automata with just one substate) which could cause backward incompatibilities.
*/
createSession : function() {
@@ -644,7 +668,7 @@ var module= module || {};
var session= new FSM.Session( new this.sessionObjectFactory() );
session.push( this );
- this.initialTransition.fireTransition( __InitialTransitionId, session );
+ this.callOnEnter( session, null, null );
return session;
}
@@ -1107,6 +1131,14 @@ var module= module || {};
}
}
+ if ( fsmd.onExit ) {
+ fsm.setOnExit( fsmd.onExit );
+ }
+
+ if ( fsmd.onEnter ) {
+ fsm.setOnEnter( fsmd.onEnter );
+ }
+
fsm.initialize( initial_state );
fsmContext.registerFSM( fsm.name, fsm );
}
View
5 readme.md
@@ -31,7 +31,8 @@ module.exports= {
}
```
-First of all, one or more FSM must be registered in the system. A minimal state machine could be:
+First of all, one or more FSM must be registered in the system. One State must be labeled as initial,
+which will be the entry point. A minimal state machine could be:
```javascript
fsmContext.registerFSM( {
@@ -113,7 +114,7 @@ In either case, the calling **this** scope will be the logic object itself.
Automata offers many activy hooks on its activity. The following hooks are available:
-State:
+State and FSM:
* **onEnter**. Code fired on state enter.
* **onExit**. Code fired on state exit.
View
3 test/test.html
@@ -13,9 +13,12 @@
</script>
<script src="test1.js"></script>
+<!--
+
<script src="test2.js"></script>
<script src="test3.js"></script>
<script src="test4.js"></script>
+-->
</body>
</html>
View
10 test/test1.js
@@ -71,7 +71,15 @@ context.registerFSM( {
to : "c",
onTransition: "action"
}
- ]
+ ],
+
+ onEnter : function( session, state, transition, msg ) {
+ console.log(" --> FSM enter");
+ },
+
+ onExit : function( session, state, transition, msg ) {
+ console.log(" --> FSM exit");
+ }
} );
var session= context.createSession("Test1");
View
22 test/test4.js
@@ -70,7 +70,16 @@ context.registerFSM( {
from : "2",
to : "3"
}
- ]
+ ],
+
+ onExit : function() {
+ console.log(" --> Exit sub-automata SubStateTest");
+ },
+
+ onEnter : function() {
+ console.log(" --> Enter sub-automata SubStateTest");
+ }
+
} );
// register another FSM model
@@ -121,7 +130,16 @@ context.registerFSM( {
to : "c",
onTransition: "transition"
}
- ]
+ ],
+
+ onExit : function() {
+ console.log(" --> Exit automata Test4");
+ },
+
+ onEnter : function() {
+ console.log(" --> Enter automata Test4");
+ }
+
} );
var session= context.createSession("Test4");

0 comments on commit 41d9498

Please sign in to comment.