UNPKG

botbuilder-dialogs-adaptive

Version:

Rule system for the Microsoft BotBuilder dialog system.

176 lines 8.67 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.SwitchCondition = void 0; /** * @module botbuilder-dialogs-adaptive */ /** * Copyright (c) Microsoft Corporation. All rights reserved. * Licensed under the MIT License. */ const converters_1 = require("../converters"); const actionScope_1 = require("./actionScope"); const case_1 = require("./case"); const adaptive_expressions_1 = require("adaptive-expressions"); const botbuilder_dialogs_1 = require("botbuilder-dialogs"); class CasesConverter { convert(items) { return items.map((item) => (item instanceof case_1.Case ? item : new case_1.Case(item.value, item.actions))); } } /** * Conditional branch with multiple cases. */ class SwitchCondition extends botbuilder_dialogs_1.Dialog { /** * Initializes a new instance of the [SwitchCondition](xref:botbuilder-dialogs-adaptive.SwitchCondition) class * * @param condition Optional. Condition expression against memory. * @param defaultDialogs Optional. Default [Dialog](xref:botbuilder-dialogs.Dialog) array. * @param cases Optional. Cases. */ constructor(condition, defaultDialogs, cases) { super(); /** * Default case. */ this.default = []; /** * Cases. */ this.cases = []; if (condition) { this.condition = new adaptive_expressions_1.ExpressionParser().parse(condition); } if (defaultDialogs) { this.default = defaultDialogs; } if (cases) { this.cases = cases; } } /** * @param property The key of the conditional selector configuration. * @returns The converter for the selector configuration. */ getConverter(property) { switch (property) { case 'condition': return new adaptive_expressions_1.ExpressionConverter(); case 'default': return converters_1.DialogListConverter; case 'cases': return new CasesConverter(); case 'disabled': return new adaptive_expressions_1.BoolExpressionConverter(); default: return super.getConverter(property); } } /** * Gets the child [Dialog](xref:botbuilder-dialogs.Dialog) dependencies so they can be added to the containers [Dialog](xref:botbuilder-dialogs.Dialog) set. * * @returns The child [Dialog](xref:botbuilder-dialogs.Dialog) dependencies. */ getDependencies() { let dialogs = []; if (this.default) { dialogs = dialogs.concat(this.defaultScope); } if (this.cases) { dialogs = dialogs.concat(this.cases); } return dialogs; } /** * Starts a new [Dialog](xref:botbuilder-dialogs.Dialog) and pushes it onto the dialog stack. * * @param dc The [DialogContext](xref:botbuilder-dialogs.DialogContext) for the current turn of conversation. * @param _options Optional. Initial information to pass to the dialog. * @returns A `Promise` representing the asynchronous operation. */ beginDialog(dc, _options) { return __awaiter(this, void 0, void 0, function* () { if (this.disabled && this.disabled.getValue(dc.state)) { return yield dc.endDialog(); } if (!this._caseExpresssions) { this._caseExpresssions = new Map(); for (let i = 0; i < this.cases.length; i++) { const caseScope = this.cases[i]; const intVal = parseInt(caseScope.value, 10); if (!isNaN(intVal)) { // you don't have to put quotes around numbers, "23" => 23 OR "23". this._caseExpresssions.set(caseScope.value, adaptive_expressions_1.Expression.orExpression(adaptive_expressions_1.Expression.equalsExpression(this.condition, new adaptive_expressions_1.Constant(intVal)), adaptive_expressions_1.Expression.equalsExpression(this.condition, new adaptive_expressions_1.Constant(caseScope.value)))); continue; } const floatVal = parseFloat(caseScope.value); if (!isNaN(floatVal)) { // you don't have to put quotes around numbers, "23" => 23 OR "23". this._caseExpresssions.set(caseScope.value, adaptive_expressions_1.Expression.orExpression(adaptive_expressions_1.Expression.equalsExpression(this.condition, new adaptive_expressions_1.Constant(floatVal)), adaptive_expressions_1.Expression.equalsExpression(this.condition, new adaptive_expressions_1.Constant(caseScope.value)))); continue; } const caseValue = caseScope.value.trim().toLowerCase(); if (caseValue === 'true') { // you don't have to put quotes around bools, "true" => true OR "true". this._caseExpresssions.set(caseScope.value, adaptive_expressions_1.Expression.orExpression(adaptive_expressions_1.Expression.equalsExpression(this.condition, new adaptive_expressions_1.Constant(true)), adaptive_expressions_1.Expression.equalsExpression(this.condition, new adaptive_expressions_1.Constant(caseScope.value)))); continue; } if (caseValue === 'false') { // you don't have to put quotes around bools, "false" => false OR "false". this._caseExpresssions.set(caseScope.value, adaptive_expressions_1.Expression.orExpression(adaptive_expressions_1.Expression.equalsExpression(this.condition, new adaptive_expressions_1.Constant(false)), adaptive_expressions_1.Expression.equalsExpression(this.condition, new adaptive_expressions_1.Constant(caseScope.value)))); continue; } // if someone does "=23" that will be numeric comparison or "='23'" that will be string comparison, // or it can be a real expression bound to memory. const { value } = new adaptive_expressions_1.ValueExpression(caseScope.value).tryGetValue(dc.state); this._caseExpresssions.set(caseScope.value, adaptive_expressions_1.Expression.equalsExpression(this.condition, new adaptive_expressions_1.Constant(value))); } } let actionScope = this.defaultScope; for (let i = 0; i < this.cases.length; i++) { const caseScope = this.cases[i]; const caseCondition = this._caseExpresssions.get(caseScope.value); const { value, error } = caseCondition.tryEvaluate(dc.state); if (error) { throw new Error(`Expression evaluation resulted in an error. Expression: ${caseCondition.toString()}. Error: ${error}`); } if (value) { actionScope = caseScope; } } return yield dc.replaceDialog(actionScope.id); }); } /** * @protected * Gets the default scope. * @returns An [ActionScope](xref:botbuilder-dialogs-adaptive.ActionScope) with the scope. */ get defaultScope() { if (!this._defaultScope) { this._defaultScope = new actionScope_1.ActionScope(this.default); } return this._defaultScope; } /** * @protected * Builds the compute Id for the [Dialog](xref:botbuilder-dialogs.Dialog). * @returns A `string` representing the compute Id. */ onComputeId() { return `SwitchCondition[${this.condition.toString()}]`; } } exports.SwitchCondition = SwitchCondition; SwitchCondition.$kind = 'Microsoft.SwitchCondition'; //# sourceMappingURL=switchCondition.js.map