|
859 | 859 | return this; |
860 | 860 | }; |
861 | 861 |
|
| 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 | + |
862 | 871 | /** |
863 | 872 | * @lend FSM.State.prototype |
864 | 873 | */ |
865 | 874 | FSM.State.prototype= { |
866 | 875 |
|
| 876 | + serialize : function() { |
| 877 | + return { |
| 878 | + "class" : "State", |
| 879 | + "name" : this.name |
| 880 | + }; |
| 881 | + }, |
| 882 | + |
867 | 883 | /** |
868 | 884 | * Get this state name. |
869 | 885 | * @returns {string} |
|
1031 | 1047 | */ |
1032 | 1048 | this.initialState= null; |
1033 | 1049 |
|
| 1050 | + /** |
| 1051 | + * FSM declarative description. |
| 1052 | + * @type {FSM.FSMState[]} |
| 1053 | + */ |
| 1054 | + this.states = null; |
| 1055 | + |
1034 | 1056 | return this; |
1035 | 1057 | }; |
1036 | 1058 |
|
|
1039 | 1061 | */ |
1040 | 1062 | FSM.FSM.prototype= { |
1041 | 1063 |
|
| 1064 | + getStateByName : function( n ) { |
| 1065 | + |
| 1066 | + var s= this.states[n]; |
| 1067 | + return s ? s : null; |
| 1068 | + }, |
| 1069 | + |
1042 | 1070 | /** |
1043 | 1071 | * Initialize a Finite State Machine. |
1044 | 1072 | * Create the initial transition to the supplied state. |
|
1137 | 1165 | return this; |
1138 | 1166 | }; |
1139 | 1167 |
|
| 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 | + |
1140 | 1187 | /** |
1141 | 1188 | * @lend FSM.SessionContext.prototype |
1142 | 1189 | */ |
|
1182 | 1229 | */ |
1183 | 1230 | printStackTrace : function() { |
1184 | 1231 | FSM.Log.d(" "+this.currentState.getName()); |
| 1232 | + }, |
| 1233 | + |
| 1234 | + serialize : function() { |
| 1235 | + return { |
| 1236 | + "class" : "SessionContext", |
| 1237 | + "state" : this.currentState.serialize() |
| 1238 | + } |
1185 | 1239 | } |
1186 | 1240 | }; |
1187 | 1241 |
|
|
1456 | 1510 | } |
1457 | 1511 | }; |
1458 | 1512 |
|
| 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 | + |
1459 | 1543 | /** |
1460 | 1544 | * @lend FSM.Session.prototype |
1461 | 1545 | */ |
1462 | 1546 | FSM.Session.prototype= { |
1463 | 1547 |
|
| 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 | + |
1464 | 1564 | /** |
1465 | 1565 | * Start a Session object. |
1466 | 1566 | * The session can be started only once. |
|
2109 | 2209 |
|
2110 | 2210 | fsm.initialize( initial_state ); |
2111 | 2211 | fsmContext.registerFSM( fsm.name, fsm ); |
| 2212 | + |
| 2213 | + fsm.states = states; |
2112 | 2214 | } |
2113 | 2215 |
|
2114 | 2216 | /** |
|
2142 | 2244 | registerFDA : registerFSM, |
2143 | 2245 | createSession : createSession, |
2144 | 2246 | newGuardException : guardException, |
2145 | | - newSessionListener : newSessionListener |
| 2247 | + newSessionListener : newSessionListener, |
| 2248 | + deserializeSession : FSM.Session.deserialize |
2146 | 2249 | }; |
2147 | 2250 |
|
2148 | 2251 | if (typeof define!=='undefined' && define.amd) { // AMD / RequireJS |
|
0 commit comments