UNPKG

speech-rule-engine

Version:

A standalone speech rule engine for XML structures, based on the original engine from ChromeVox.

161 lines (160 loc) 5.72 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.EnginePromise = exports.Engine = exports.SREError = void 0; const Dcstr = require("../rule_engine/dynamic_cstr.js"); const EngineConst = require("./engine_const.js"); const FileUtil = require("./file_util.js"); const system_external_js_1 = require("./system_external.js"); const debugger_js_1 = require("./debugger.js"); const variables_js_1 = require("./variables.js"); const options_js_1 = require("./options.js"); class SREError extends Error { constructor(message = '') { super(); this.message = message; this.name = 'SRE Error'; } } exports.SREError = SREError; class Engine { set defaultLocale(loc) { this._defaultLocale = variables_js_1.Variables.ensureLocale(loc, this._defaultLocale); } get defaultLocale() { return this._defaultLocale; } static getInstance() { Engine.instance = Engine.instance || new Engine(); return Engine.instance; } static defaultEvaluator(str, _cstr) { return str; } static evaluateNode(node) { return Engine.nodeEvaluator(node); } getRate() { const numeric = parseInt(this.options.rate, 10); return isNaN(numeric) ? 100 : numeric; } setDynamicCstr(opt_dynamic) { if (this.defaultLocale) { Dcstr.DynamicCstr.DEFAULT_VALUES[Dcstr.Axis.LOCALE] = this.defaultLocale; } if (opt_dynamic) { const keys = Object.keys(opt_dynamic); for (let i = 0; i < keys.length; i++) { const feature = keys[i]; if (Dcstr.DynamicCstr.DEFAULT_ORDER.indexOf(feature) !== -1) { const value = opt_dynamic[feature]; this.options[feature] = value; } } } const dynamic = [ this.options.locale, this.options.modality, this.options.domain, this.options.style ].join('.'); const fallback = Dcstr.DynamicProperties.createProp([Dcstr.DynamicCstr.DEFAULT_VALUES[Dcstr.Axis.LOCALE]], [Dcstr.DynamicCstr.DEFAULT_VALUES[Dcstr.Axis.MODALITY]], [Dcstr.DynamicCstr.DEFAULT_VALUES[Dcstr.Axis.DOMAIN]], [Dcstr.DynamicCstr.DEFAULT_VALUES[Dcstr.Axis.STYLE]]); const comparator = this.comparators[this.options.domain]; const parser = this.parsers[this.options.domain]; this.parser = parser ? parser : this.defaultParser; this.dynamicCstr = this.parser.parse(dynamic); this.dynamicCstr.updateProperties(fallback.getProperties()); this.comparator = comparator ? comparator() : new Dcstr.DefaultComparator(this.dynamicCstr); } constructor() { this.options = new options_js_1.Options(); this.config = false; this.customLoader = null; this.parsers = {}; this.comparator = null; this.mode = EngineConst.Mode.SYNC; this.init = true; this.comparators = {}; this._defaultLocale = Dcstr.DynamicCstr.DEFAULT_VALUES[Dcstr.Axis.LOCALE]; this.isIE = false; this.isEdge = false; this.options.locale = this.defaultLocale; this.evaluator = Engine.defaultEvaluator; this.defaultParser = new Dcstr.DynamicCstrParser(Dcstr.DynamicCstr.DEFAULT_ORDER); this.parser = this.defaultParser; this.dynamicCstr = Dcstr.DynamicCstr.defaultCstr(); } configurate(feature) { if (this.mode === EngineConst.Mode.HTTP && !system_external_js_1.SystemExternal.webworker && !this.config) { configBlocks(feature); this.config = true; } configFeature(feature); } setCustomLoader(fn) { if (fn) { this.customLoader = fn; } } setup(feature) { if (typeof feature['mode'] !== 'undefined') { this.mode = feature['mode']; } this.configurate(feature); this.options.set(feature); if (feature.json) { system_external_js_1.SystemExternal.jsonPath = FileUtil.makePath(feature.json); } if (feature.xpath) { system_external_js_1.SystemExternal.WGXpath = feature.xpath; } this.setCustomLoader(feature.custom); } json() { return Object.assign({ 'mode': this.mode }, this.options.json()); } reset() { this.options = new options_js_1.Options(); } } exports.Engine = Engine; Engine.nodeEvaluator = function (_node) { return []; }; function configFeature(feature) { if (typeof SREfeature !== 'undefined') { for (const [name, feat] of Object.entries(SREfeature)) { feature[name] = feat; } } } function configBlocks(feature) { const scripts = document.documentElement.querySelectorAll('script[type="text/x-sre-config"]'); for (let i = 0, m = scripts.length; i < m; i++) { let inner; try { inner = scripts[i].innerHTML; const config = JSON.parse(inner); for (const [key, val] of Object.entries(config)) { feature[key] = val; } } catch (_err) { debugger_js_1.Debugger.getInstance().output('Illegal configuration ', inner); } } } class EnginePromise { static get(locale = Engine.getInstance().options.locale) { return EnginePromise.promises[locale] || Promise.resolve(''); } static getall() { return Promise.all(Object.values(EnginePromise.promises)); } } exports.EnginePromise = EnginePromise; EnginePromise.loaded = {}; EnginePromise.promises = {};