| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 /** | 5 /** |
| 6 * @fileoverview Base class for all speech rule stores. | 6 * @fileoverview Base class for all speech rule stores. |
| 7 * | 7 * |
| 8 * The base rule store implements some basic functionality that is common to | 8 * The base rule store implements some basic functionality that is common to |
| 9 * most speech rule stores. | 9 * most speech rule stores. |
| 10 */ | 10 */ |
| (...skipping 26 matching lines...) Expand all Loading... |
| 37 this.customStrings = new cvox.SpeechRuleFunctions.CustomStrings(); | 37 this.customStrings = new cvox.SpeechRuleFunctions.CustomStrings(); |
| 38 | 38 |
| 39 /** | 39 /** |
| 40 * Set of context functions for the store. | 40 * Set of context functions for the store. |
| 41 * @type {cvox.SpeechRuleFunctions.ContextFunctions} | 41 * @type {cvox.SpeechRuleFunctions.ContextFunctions} |
| 42 */ | 42 */ |
| 43 this.contextFunctions = new cvox.SpeechRuleFunctions.ContextFunctions(); | 43 this.contextFunctions = new cvox.SpeechRuleFunctions.ContextFunctions(); |
| 44 | 44 |
| 45 /** | 45 /** |
| 46 * Set of speech rules in the store. | 46 * Set of speech rules in the store. |
| 47 * @type {!Array.<cvox.SpeechRule>} | 47 * @type {!Array<cvox.SpeechRule>} |
| 48 * @private | 48 * @private |
| 49 */ | 49 */ |
| 50 this.speechRules_ = []; | 50 this.speechRules_ = []; |
| 51 | 51 |
| 52 /** | 52 /** |
| 53 * A priority list of dynamic constraint attributes. | 53 * A priority list of dynamic constraint attributes. |
| 54 * @type {!Array.<cvox.SpeechRule.DynamicCstrAttrib>} | 54 * @type {!Array<cvox.SpeechRule.DynamicCstrAttrib>} |
| 55 */ | 55 */ |
| 56 this.dynamicCstrAttribs = [cvox.SpeechRule.DynamicCstrAttrib.STYLE]; | 56 this.dynamicCstrAttribs = [cvox.SpeechRule.DynamicCstrAttrib.STYLE]; |
| 57 | 57 |
| 58 /** | 58 /** |
| 59 * List of TTS properties overridden by the store when it is active. | 59 * List of TTS properties overridden by the store when it is active. |
| 60 * @type {!Array.<string>} | 60 * @type {!Array<string>} |
| 61 */ | 61 */ |
| 62 this.defaultTtsProps = []; | 62 this.defaultTtsProps = []; |
| 63 }; | 63 }; |
| 64 | 64 |
| 65 | 65 |
| 66 /** | 66 /** |
| 67 * @override | 67 * @override |
| 68 */ | 68 */ |
| 69 cvox.BaseRuleStore.prototype.lookupRule = function(node, dynamic) { | 69 cvox.BaseRuleStore.prototype.lookupRule = function(node, dynamic) { |
| 70 if (!node || | 70 if (!node || |
| (...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 190 } | 190 } |
| 191 } | 191 } |
| 192 }; | 192 }; |
| 193 | 193 |
| 194 | 194 |
| 195 // TODO (sorge) These should move into the speech rule functions. | 195 // TODO (sorge) These should move into the speech rule functions. |
| 196 /** | 196 /** |
| 197 * Checks if we have a custom query and applies it. Otherwise returns null. | 197 * Checks if we have a custom query and applies it. Otherwise returns null. |
| 198 * @param {!Node} node The initial node. | 198 * @param {!Node} node The initial node. |
| 199 * @param {string} funcName A function name. | 199 * @param {string} funcName A function name. |
| 200 * @return {Array.<Node>} The list of resulting nodes. | 200 * @return {Array<Node>} The list of resulting nodes. |
| 201 */ | 201 */ |
| 202 cvox.BaseRuleStore.prototype.applyCustomQuery = function( | 202 cvox.BaseRuleStore.prototype.applyCustomQuery = function( |
| 203 node, funcName) { | 203 node, funcName) { |
| 204 var func = this.customQueries.lookup(funcName); | 204 var func = this.customQueries.lookup(funcName); |
| 205 return func ? func(node) : null; | 205 return func ? func(node) : null; |
| 206 }; | 206 }; |
| 207 | 207 |
| 208 | 208 |
| 209 /** | 209 /** |
| 210 * Applies either an Xpath selector or a custom query to the node | 210 * Applies either an Xpath selector or a custom query to the node |
| 211 * and returns the resulting node list. | 211 * and returns the resulting node list. |
| 212 * @param {!Node} node The initial node. | 212 * @param {!Node} node The initial node. |
| 213 * @param {string} expr An Xpath expression string or a name of a custom | 213 * @param {string} expr An Xpath expression string or a name of a custom |
| 214 * query. | 214 * query. |
| 215 * @return {Array.<Node>} The list of resulting nodes. | 215 * @return {Array<Node>} The list of resulting nodes. |
| 216 */ | 216 */ |
| 217 cvox.BaseRuleStore.prototype.applySelector = function(node, expr) { | 217 cvox.BaseRuleStore.prototype.applySelector = function(node, expr) { |
| 218 var result = this.applyCustomQuery(node, expr); | 218 var result = this.applyCustomQuery(node, expr); |
| 219 return result || cvox.XpathUtil.evalXPath(expr, node); | 219 return result || cvox.XpathUtil.evalXPath(expr, node); |
| 220 }; | 220 }; |
| 221 | 221 |
| 222 | 222 |
| 223 /** | 223 /** |
| 224 * Applies either an Xpath selector or a custom query to the node | 224 * Applies either an Xpath selector or a custom query to the node |
| 225 * and returns the first result. | 225 * and returns the first result. |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 257 * @param {cvox.SpeechRule} rule The rule. | 257 * @param {cvox.SpeechRule} rule The rule. |
| 258 * @return {boolean} True if the preconditions apply to the node. | 258 * @return {boolean} True if the preconditions apply to the node. |
| 259 * @protected | 259 * @protected |
| 260 */ | 260 */ |
| 261 cvox.BaseRuleStore.prototype.testDynamicConstraints = function( | 261 cvox.BaseRuleStore.prototype.testDynamicConstraints = function( |
| 262 dynamic, rule) { | 262 dynamic, rule) { |
| 263 // We allow a default value for each dynamic constraints attribute. | 263 // We allow a default value for each dynamic constraints attribute. |
| 264 // The idea is that when we can not find a speech rule matching the value for | 264 // The idea is that when we can not find a speech rule matching the value for |
| 265 // a particular attribute in the dynamic constraintwe choose the one that has | 265 // a particular attribute in the dynamic constraintwe choose the one that has |
| 266 // the value 'default'. | 266 // the value 'default'. |
| 267 var allKeys = /** @type {Array.<cvox.SpeechRule.DynamicCstrAttrib>} */ ( | 267 var allKeys = /** @type {Array<cvox.SpeechRule.DynamicCstrAttrib>} */ ( |
| 268 Object.keys(dynamic)); | 268 Object.keys(dynamic)); |
| 269 return allKeys.every( | 269 return allKeys.every( |
| 270 function(key) { | 270 function(key) { |
| 271 return dynamic[key] == rule.dynamicCstr[key] || | 271 return dynamic[key] == rule.dynamicCstr[key] || |
| 272 rule.dynamicCstr[key] == 'default'; | 272 rule.dynamicCstr[key] == 'default'; |
| 273 }); | 273 }); |
| 274 }; | 274 }; |
| 275 | 275 |
| 276 | 276 |
| 277 /** | 277 /** |
| 278 * Get a set of all dynamic constraint values. | 278 * Get a set of all dynamic constraint values. |
| 279 * @return {!Object.<cvox.SpeechRule.DynamicCstrAttrib, Array.<string>>} The | 279 * @return {!Object<cvox.SpeechRule.DynamicCstrAttrib, Array<string>>} The |
| 280 * object with all annotations. | 280 * object with all annotations. |
| 281 */ | 281 */ |
| 282 cvox.BaseRuleStore.prototype.getDynamicConstraintValues = function() { | 282 cvox.BaseRuleStore.prototype.getDynamicConstraintValues = function() { |
| 283 var result = {}; | 283 var result = {}; |
| 284 for (var i = 0, rule; rule = this.speechRules_[i]; i++) { | 284 for (var i = 0, rule; rule = this.speechRules_[i]; i++) { |
| 285 for (var key in rule.dynamicCstr) { | 285 for (var key in rule.dynamicCstr) { |
| 286 var newKey = [rule.dynamicCstr[key]]; | 286 var newKey = [rule.dynamicCstr[key]]; |
| 287 if (result[key]) { | 287 if (result[key]) { |
| 288 result[key] = cvox.MathUtil.union(result[key], newKey); | 288 result[key] = cvox.MathUtil.union(result[key], newKey); |
| 289 } else { | 289 } else { |
| (...skipping 23 matching lines...) Expand all Loading... |
| 313 } | 313 } |
| 314 return result; | 314 return result; |
| 315 }; | 315 }; |
| 316 | 316 |
| 317 | 317 |
| 318 /** | 318 /** |
| 319 * Picks the result of the most constraint rule by prefering those: | 319 * Picks the result of the most constraint rule by prefering those: |
| 320 * 1) that best match the dynamic constraints. | 320 * 1) that best match the dynamic constraints. |
| 321 * 2) with the most additional constraints. | 321 * 2) with the most additional constraints. |
| 322 * @param {cvox.SpeechRule.DynamicCstr} dynamic Dynamic constraints. | 322 * @param {cvox.SpeechRule.DynamicCstr} dynamic Dynamic constraints. |
| 323 * @param {!Array.<cvox.SpeechRule>} rules An array of rules. | 323 * @param {!Array<cvox.SpeechRule>} rules An array of rules. |
| 324 * @return {cvox.SpeechRule} The most constraint rule. | 324 * @return {cvox.SpeechRule} The most constraint rule. |
| 325 * @private | 325 * @private |
| 326 */ | 326 */ |
| 327 cvox.BaseRuleStore.prototype.pickMostConstraint_ = function(dynamic, rules) { | 327 cvox.BaseRuleStore.prototype.pickMostConstraint_ = function(dynamic, rules) { |
| 328 rules.sort(goog.bind( | 328 rules.sort(goog.bind( |
| 329 function(r1, r2) { | 329 function(r1, r2) { |
| 330 var count1 = this.countMatchingDynamicConstraintValues_(dynamic, r1); | 330 var count1 = this.countMatchingDynamicConstraintValues_(dynamic, r1); |
| 331 var count2 = this.countMatchingDynamicConstraintValues_(dynamic, r2); | 331 var count2 = this.countMatchingDynamicConstraintValues_(dynamic, r2); |
| 332 // Rule one is a better match, don't swap. | 332 // Rule one is a better match, don't swap. |
| 333 if (count1 > count2) { | 333 if (count1 > count2) { |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 382 return false; | 382 return false; |
| 383 } | 383 } |
| 384 } | 384 } |
| 385 return true; | 385 return true; |
| 386 }; | 386 }; |
| 387 | 387 |
| 388 | 388 |
| 389 /** | 389 /** |
| 390 * Compares two static constraints (i.e., lists of precondition constraints) and | 390 * Compares two static constraints (i.e., lists of precondition constraints) and |
| 391 * returns true if they are equal. | 391 * returns true if they are equal. |
| 392 * @param {Array.<string>} cstr1 First static constraints. | 392 * @param {Array<string>} cstr1 First static constraints. |
| 393 * @param {Array.<string>} cstr2 Second static constraints. | 393 * @param {Array<string>} cstr2 Second static constraints. |
| 394 * @return {boolean} True if the static constraints are equal. | 394 * @return {boolean} True if the static constraints are equal. |
| 395 * @private | 395 * @private |
| 396 */ | 396 */ |
| 397 cvox.BaseRuleStore.compareStaticConstraints_ = function( | 397 cvox.BaseRuleStore.compareStaticConstraints_ = function( |
| 398 cstr1, cstr2) { | 398 cstr1, cstr2) { |
| 399 if (cstr1.length != cstr2.length) { | 399 if (cstr1.length != cstr2.length) { |
| 400 return false; | 400 return false; |
| 401 } | 401 } |
| 402 for (var i = 0, cstr; cstr = cstr1[i]; i++) { | 402 for (var i = 0, cstr; cstr = cstr1[i]; i++) { |
| 403 if (cstr2.indexOf(cstr) == -1) { | 403 if (cstr2.indexOf(cstr) == -1) { |
| (...skipping 13 matching lines...) Expand all Loading... |
| 417 */ | 417 */ |
| 418 cvox.BaseRuleStore.comparePreconditions_ = function(rule1, rule2) { | 418 cvox.BaseRuleStore.comparePreconditions_ = function(rule1, rule2) { |
| 419 var prec1 = rule1.precondition; | 419 var prec1 = rule1.precondition; |
| 420 var prec2 = rule2.precondition; | 420 var prec2 = rule2.precondition; |
| 421 if (prec1.query != prec2.query) { | 421 if (prec1.query != prec2.query) { |
| 422 return false; | 422 return false; |
| 423 } | 423 } |
| 424 return cvox.BaseRuleStore.compareStaticConstraints_( | 424 return cvox.BaseRuleStore.compareStaticConstraints_( |
| 425 prec1.constraints, prec2.constraints); | 425 prec1.constraints, prec2.constraints); |
| 426 }; | 426 }; |
| OLD | NEW |