Skip to content

Commit 11a07a5

Browse files
author
hyperandroid
committed
Added serialization/deserialization capabilities of Sessions.
1 parent 9de8c39 commit 11a07a5

File tree

3 files changed

+106
-2
lines changed

3 files changed

+106
-2
lines changed

automata.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,5 +138,6 @@ declare module Automata {
138138
export function createSession( fda_name : string, controller : any ) : FSM.Session;
139139
export function newGuardException( message : string ) : FSM.GuardException;
140140
export function newSessionListener( obj : any ) : FSM.SessionListener;
141+
export function deserializeSession( obj : any, controllerDeserializerFunction ) : FSM.Session;
141142

142143
}

automata.js

Lines changed: 104 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -859,11 +859,27 @@
859859
return this;
860860
};
861861

862+
FSM.State.deserialize = function( obj, parentState ) {
863+
864+
if ( null==parentState ) {
865+
return fsmContext.getFSM( obj.name );
866+
}
867+
868+
return parentState.getStateByName( obj.name );
869+
};
870+
862871
/**
863872
* @lend FSM.State.prototype
864873
*/
865874
FSM.State.prototype= {
866875

876+
serialize : function() {
877+
return {
878+
"class" : "State",
879+
"name" : this.name
880+
};
881+
},
882+
867883
/**
868884
* Get this state name.
869885
* @returns {string}
@@ -1031,6 +1047,12 @@
10311047
*/
10321048
this.initialState= null;
10331049

1050+
/**
1051+
* FSM declarative description.
1052+
* @type {FSM.FSMState[]}
1053+
*/
1054+
this.states = null;
1055+
10341056
return this;
10351057
};
10361058

@@ -1039,6 +1061,12 @@
10391061
*/
10401062
FSM.FSM.prototype= {
10411063

1064+
getStateByName : function( n ) {
1065+
1066+
var s= this.states[n];
1067+
return s ? s : null;
1068+
},
1069+
10421070
/**
10431071
* Initialize a Finite State Machine.
10441072
* Create the initial transition to the supplied state.
@@ -1137,6 +1165,25 @@
11371165
return this;
11381166
};
11391167

1168+
/**
1169+
*
1170+
* @param obj {object}
1171+
* @param parentFDA {FSM.State}
1172+
*/
1173+
FSM.SessionContext.deserialize= function( obj, parentFDA ) {
1174+
1175+
if ( obj && obj["class"] && obj["class"]==="SessionContext" ) {
1176+
var s= FSM.State.deserialize( obj.state, parentFDA );
1177+
if ( s ) {
1178+
return new FSM.SessionContext(s);
1179+
}
1180+
1181+
throw "Unknown state '"+obj.state.name+"' in FDA "+parentFDA.getName();
1182+
}
1183+
1184+
throw "SessionContext invalid data.";
1185+
};
1186+
11401187
/**
11411188
* @lend FSM.SessionContext.prototype
11421189
*/
@@ -1182,6 +1229,13 @@
11821229
*/
11831230
printStackTrace : function() {
11841231
FSM.Log.d(" "+this.currentState.getName());
1232+
},
1233+
1234+
serialize : function() {
1235+
return {
1236+
"class" : "SessionContext",
1237+
"state" : this.currentState.serialize()
1238+
}
11851239
}
11861240
};
11871241

@@ -1456,11 +1510,57 @@
14561510
}
14571511
};
14581512

1513+
/**
1514+
* @param obj
1515+
*/
1516+
FSM.Session.deserialize= function( obj, controllerDeserializer ) {
1517+
1518+
if ( obj && obj["class"] && obj["class"]==="Session" ) {
1519+
1520+
var fsm = fsmContext.getFSM(obj.fda);
1521+
if (!fsm) {
1522+
throw "Unknown FSM '" + obj.fda + "'";
1523+
}
1524+
var s = new FSM.Session(fsm);
1525+
s._started= obj.started;
1526+
1527+
s.sessionContextList= [];
1528+
var prevState= null;
1529+
obj.sessionContextList.forEach( function(scdef) {
1530+
var ns= FSM.SessionContext.deserialize( scdef, prevState );
1531+
s.sessionContextList.push( ns );
1532+
prevState= ns.getState();
1533+
});
1534+
1535+
s.controller= controllerDeserializer( obj.controller );
1536+
1537+
return s;
1538+
}
1539+
1540+
throw "Invalid session object definition.";
1541+
};
1542+
14591543
/**
14601544
* @lend FSM.Session.prototype
14611545
*/
14621546
FSM.Session.prototype= {
14631547

1548+
serialize : function() {
1549+
var ret= {
1550+
"class" : "Session",
1551+
"fda" : this._fda.getName(),
1552+
"sessionContextList" : [],
1553+
"started" : this._started,
1554+
"controller" : this.controller.serialize ? this.controller.serialize() : ""
1555+
};
1556+
1557+
this.sessionContextList.forEach( function( sc ) {
1558+
ret["sessionContextList"].push( sc.serialize() );
1559+
});
1560+
1561+
return ret;
1562+
},
1563+
14641564
/**
14651565
* Start a Session object.
14661566
* The session can be started only once.
@@ -2109,6 +2209,8 @@
21092209

21102210
fsm.initialize( initial_state );
21112211
fsmContext.registerFSM( fsm.name, fsm );
2212+
2213+
fsm.states = states;
21122214
}
21132215

21142216
/**
@@ -2142,7 +2244,8 @@
21422244
registerFDA : registerFSM,
21432245
createSession : createSession,
21442246
newGuardException : guardException,
2145-
newSessionListener : newSessionListener
2247+
newSessionListener : newSessionListener,
2248+
deserializeSession : FSM.Session.deserialize
21462249
};
21472250

21482251
if (typeof define!=='undefined' && define.amd) { // AMD / RequireJS

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "automata",
3-
"version": "2.0.1",
3+
"version": "2.0.2",
44
"main": "automata",
55
"keywords": [
66
"DFA",

0 commit comments

Comments
 (0)