|
| 1 | +/* |
| 2 | + WebMole, an automated explorer and tester for Web 2.0 applications |
| 3 | + Copyright (C) 2012-2013 Gabriel Le Breton, Fabien Maronnaud, |
| 4 | + Sylvain Hallé et al. |
| 5 | +
|
| 6 | + This program is free software: you can redistribute it and/or modify |
| 7 | + it under the terms of the GNU General Public License as published by |
| 8 | + the Free Software Foundation, either version 3 of the License, or |
| 9 | + (at your option) any later version. |
| 10 | +
|
| 11 | + This program is distributed in the hope that it will be useful, |
| 12 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | + GNU General Public License for more details. |
| 15 | +
|
| 16 | + You should have received a copy of the GNU General Public License |
| 17 | + along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | +*/ |
| 19 | + |
| 20 | +/** |
| 21 | + * Implementation of a Web State Machine using the Tansuo exploration |
| 22 | + * algorithm that simulates backtracking. |
| 23 | + * @constructor |
| 24 | + * @this {TansuoWsm} |
| 25 | + */ |
| 26 | +function TansuoWsm() // extends NoBacktrackWsm {{{ |
| 27 | +{ |
| 28 | + // Used to extend the prototype of VanillaWsm |
| 29 | + this.NoBacktrackWsm = NoBacktrackWsm; |
| 30 | + this.NoBacktrackWsm(); |
| 31 | + |
| 32 | + /** |
| 33 | + * Internal member field to store the ID of the node we want to reach |
| 34 | + * @type {number} |
| 35 | + */ |
| 36 | + this.m_targetId = null; |
| 37 | + |
| 38 | + /** |
| 39 | + * Handles the situation where the WSM has reached a dead end and is |
| 40 | + * sent back to its initial state. See {@link WebStateMachine#processReset} |
| 41 | + * for detailed info. |
| 42 | + * <p> |
| 43 | + * In the case of Tansuo, the procedure |
| 44 | + * is to return the last suffix that starts with the initial state. |
| 45 | + * @param {number} The ID of the node in the WSM wher the exploration |
| 46 | + * resumes at |
| 47 | + * @return {boolean} <tt>true</tt> if the exploration must continue, |
| 48 | + * <tt>false</tt> if there is no unvisited page |
| 49 | + */ |
| 50 | + this.processReset = function(node_id) // {{{ |
| 51 | + { |
| 52 | + // Pop last state in the state stack and peek next-to-last |
| 53 | + if (this.m_pathSinceBeginning.getLength() === 0) |
| 54 | + { |
| 55 | + // Not supposed to happen, but anyway, means we are done |
| 56 | + return false; |
| 57 | + } |
| 58 | + var dummy = this.m_pathSinceBeginning.popLastElement(); |
| 59 | + if (this.m_pathSinceBeginning.getLength() === 0) |
| 60 | + { |
| 61 | + // Finished backtracking: we are done |
| 62 | + return false; |
| 63 | + } |
| 64 | + // If no ID is passed, we assume we reset the application, i.e. |
| 65 | + // go back to its initial state (the one with ID=1) |
| 66 | + if (node_id === undefined) |
| 67 | + { |
| 68 | + node_id = 1; |
| 69 | + } |
| 70 | + var len = this.m_pathSinceBeginning.getLength(); |
| 71 | + for (var i = len - 1; i >= 0; i--) |
| 72 | + { |
| 73 | + // Go backwards until we find the initial state |
| 74 | + var pseg = this.m_pathSinceBeginning.getElement(i); |
| 75 | + var dest_id = pseg.getDestination(); |
| 76 | + if (dest_id == node_id) |
| 77 | + { |
| 78 | + // We found it |
| 79 | + break; |
| 80 | + } |
| 81 | + } |
| 82 | + // Assert: i = position of last occurrence of node_id in sequence |
| 83 | + // Path to follow = suffix of sequence from that position |
| 84 | + var out_ps = new PathSequence(); |
| 85 | + for (var j = i; j < len; j++) |
| 86 | + { |
| 87 | + var segment = this.m_pathSinceBeginning.getElement(j); |
| 88 | + out_ps.append(segment); |
| 89 | + } |
| 90 | + this.m_pathToFollow = out_ps; |
| 91 | + return true; |
| 92 | + }; // }}} |
| 93 | + |
| 94 | +} // }}} |
| 95 | + |
| 96 | +/* :folding=explicit:wrap=none: */ |
0 commit comments