function evaluate()

in Cognitive Services/Node/lib/LuisActionBinding.js [28:130]


function evaluate(modelUrl, actions, currentActionModel, userInput, onContextCreationHandler) {
    if (!modelUrl) {
        throw new Error('modelUrl not set');
    }
    actions.forEach(validateAction);
    onContextCreationHandler = validateContextCreationHandler(onContextCreationHandler);
    return new Promise(function (resolve, reject) {
        var actionModel = _.merge({}, EmptyActionModel, currentActionModel);
        if (actionModel.status === Status.ContextSwitch) {
            if (actionModel.confirmSwitch) {
                actionModel.intentName = actionModel.contextSwitchData.intentName;
                actionModel.parameters = actionModel.contextSwitchData.parameters;
            }
            actionModel.contextSwitchData = null;
            actionModel.currentParameter = null;
            actionModel.userInput = null;
        }
        actionModel.userInput = userInput ? userInput.trim() : null;
        delete actionModel.subcontextResult;
        switch (actionModel.status) {
            case Status.NoActionRecognized:
                builder.LuisRecognizer.recognize(actionModel.userInput, modelUrl, function (err, intents, entities) {
                    if (err) {
                        return reject(err);
                    }
                    var action = chooseBestIntentAction(intents, actions);
                    if (action) {
                        actionModel.intentName = action.intentName;
                        if (action.parentAction && !actionModel.contextModel) {
                            popupateContextParent(actionModel, action, actions);
                        }
                        actionModel.parameters = extractParametersFromEntities(action.schema, entities);
                        var next = function () {
                            tryExecute(action, actionModel)
                                .then(resolve)
                                .catch(reject);
                        };
                        if (action.parentAction) {
                            onContextCreationHandler(action.parentAction, actionModel.contextModel, next);
                        }
                        else {
                            next();
                        }
                    }
                    else {
                        actionModel.status = Status.NoActionRecognized;
                        resolve(actionModel);
                    }
                });
                break;
            case Status.MissingParameters:
            case Status.ContextSwitch:
                var action = _.find(actions, function (action) { return actionModel.intentName === action.intentName; });
                if (actionModel.userInput) {
                    builder.LuisRecognizer.recognize(actionModel.userInput, modelUrl, function (err, intents, entities) {
                        if (err) {
                            return reject(err);
                        }
                        var newAction = chooseBestIntentAction(intents, actions, action);
                        if (newAction && newAction.intentName !== action.intentName) {
                            if (newAction.parentAction === action) {
                                actionModel = _.merge({}, EmptyActionModel, {
                                    contextModel: actionModel,
                                    intentName: newAction.intentName
                                });
                                actionModel.parameters = [];
                                action = newAction;
                            }
                            else if (equalsTrue(action.confirmOnContextSwitch, true)) {
                                actionModel.status = Status.ContextSwitch;
                                actionModel.contextSwitchData = {
                                    intentName: newAction.intentName,
                                    parameters: extractParametersFromEntities(newAction.schema, entities)
                                };
                                var currentActionName = action.friendlyName || action.intentName;
                                var newActionName = newAction.friendlyName || newAction.intentName;
                                actionModel.contextSwitchPrompt = util.format('Do you want to discard the current action \'%s\' and start the with \'%s\' action?', currentActionName, newActionName);
                                return resolve(actionModel);
                            }
                            else {
                                action = newAction;
                                actionModel.intentName = newAction.intentName;
                                actionModel.currentParameter = null;
                            }
                        }
                        var parameters = extractParametersFromEntities(action.schema, entities, actionModel);
                        actionModel.parameters = _.merge({}, actionModel.parameters, parameters);
                        tryExecute(action, actionModel)
                            .then(resolve)
                            .catch(reject);
                    });
                }
                else {
                    tryExecute(action, actionModel)
                        .then(resolve)
                        .catch(reject);
                }
                break;
            default:
                reject('Unknown action.status "' + actionModel.status + '"');
        }
    });
}