speech-rule-engine
Version:
A standalone speech rule engine for XML structures, based on the original engine from ChromeVox.
150 lines • 5.25 kB
JavaScript
import * as Dcstr from '../rule_engine/dynamic_cstr.js';
import * as EngineConst from './engine_const.js';
import * as FileUtil from './file_util.js';
import { SystemExternal } from './system_external.js';
import { Debugger } from './debugger.js';
import { Variables } from './variables.js';
import { Options } from './options.js';
export class SREError extends Error {
constructor(message = '') {
super();
this.message = message;
this.name = 'SRE Error';
}
}
export class Engine {
set defaultLocale(loc) {
this._defaultLocale = 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();
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.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 &&
!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) {
SystemExternal.jsonPath = FileUtil.makePath(feature.json);
}
this.setCustomLoader(feature.custom);
}
json() {
return Object.assign({ 'mode': this.mode }, this.options.json());
}
reset() {
this.options = new Options();
}
}
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.getInstance().output('Illegal configuration ', inner);
}
}
}
export class EnginePromise {
static get(locale = Engine.getInstance().options.locale) {
return EnginePromise.promises[locale] || Promise.resolve('');
}
static getall() {
return Promise.all(Object.values(EnginePromise.promises));
}
}
EnginePromise.loaded = {};
EnginePromise.promises = {};
//# sourceMappingURL=engine.js.map