UNPKG

botbuilder-dialogs-adaptive

Version:

Rule system for the Microsoft BotBuilder dialog system.

58 lines 2.02 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.RandomSelector = void 0; /** * @module botbuilder-dialogs-adaptive */ /** * Copyright (c) Microsoft Corporation. All rights reserved. * Licensed under the MIT License. */ const adaptive_expressions_1 = require("adaptive-expressions"); const triggerSelector_1 = require("../triggerSelector"); /** * Select a random true rule implementation of TriggerSelector. */ class RandomSelector extends triggerSelector_1.TriggerSelector { /** * Initialize the selector with the set of rules. * * @param conditionals Possible rules to match. * @param evaluate A boolean representing if rules should be evaluated on select. */ initialize(conditionals, evaluate) { this._conditionals = conditionals; this._evaluate = evaluate; } /** * Select the best rule to execute. * * @param actionContext Dialog context for evaluation. * @returns A Promise with a number array. */ select(actionContext) { const candidates = []; for (let i = 0; i < this._conditionals.length; i++) { const conditional = this._conditionals[i]; if (this._evaluate) { const expression = conditional.getExpression(); const { value, error } = expression.tryEvaluate(actionContext.state); if (value && !error) { candidates.push(conditional); } } else { candidates.push(conditional); } } const result = []; if (candidates.length > 0) { const selection = adaptive_expressions_1.Extensions.randomNext(actionContext.state, 0, candidates.length); result.push(candidates[selection]); } return Promise.resolve(result); } } exports.RandomSelector = RandomSelector; RandomSelector.$kind = 'Microsoft.RandomSelector'; //# sourceMappingURL=randomSelector.js.map