Beispiel #1
0
        public ExpEvalResultType Next(string input, out ActionLink actionLink)
        {
            actionLink = null;
            ExpEvalResultType res = ExpEvalResultType.Empty;

            if (SkipEvaluation)
            {
                actionLink = GetDefaultLink();
                if (actionLink != null)
                {
                    res = ExpEvalResultType.Skipped;
                }
                return(res);
            }

            res = Evaluate(input);
            switch (res)
            {
            case ExpEvalResultType.False:
                actionLink = FallbackAction;
                break;

            case ExpEvalResultType.True:
                actionLink = ForwardAction;
                break;

            default:
                break;
            }
            if (actionLink == null)
            {
                res = ExpEvalResultType.Empty;
            }
            return(res);
        }
        protected ExpEvalResultType _Evaluate(string rValString)
        {
            if (ExpressionTree == null || ExpressionTree.Nodes == null || ExpressionTree.Nodes.Count == 0)
            {
                return(ExpEvalResultType.Empty);
            }

            ExpEvalResultType res = ExpEvalResultType.Invalid;

            object rValObject = null;

            try
            {
                var type = ExpressionTree?.Nodes?.Select(item => item.RVal.GetType()).FirstOrDefault();
                rValObject = Convert(type, rValString);
            }
            catch
            {
            }

            if (rValObject != null)
            {
                var refExpTree = ExpressionTree;
                res = EvaluateExpressionTree(ref refExpTree, ref rValObject);
                if (InnerExpressions != null && res != ExpEvalResultType.Invalid)
                {
                    foreach (var ie in InnerExpressions)
                    {
                        switch (ie.With)
                        {
                        case LogicalOpType.And:
                            if (res == ExpEvalResultType.True)
                            {
                                var et = ie.Expression;
                                res = EvaluateExpressionTree(ref et, ref rValObject);
                            }
                            break;

                        case LogicalOpType.Or:
                            if (res == ExpEvalResultType.False)
                            {
                                var et = ie.Expression;
                                res = EvaluateExpressionTree(ref et, ref rValObject);
                            }
                            break;
                        }
                    }
                }
            }

            return(res);
        }
        protected ExpEvalResultType EvaluateExpressionTree(ref ExpressionTree expressionTree, ref object rValObject)
        {
            ExpEvalResultType res = ExpEvalResultType.Empty;

            foreach (var op in ExpressionTree.Nodes)
            {
                res = op.Evaluate(ref rValObject);
                bool toBreak = false;
                switch (res)
                {
                case ExpEvalResultType.False:
                {
                    if (op.With == LogicalOpType.And)
                    {
                        toBreak = true;
                    }
                }
                break;

                case ExpEvalResultType.Invalid:
                    toBreak = true;
                    break;

                case ExpEvalResultType.True:
                    if (op.With == LogicalOpType.Or)
                    {
                        toBreak = true;
                    }
                    break;

                default:
                    toBreak = true;
                    break;
                }
                if (toBreak)
                {
                    break;
                }
            }
            return(res);
        }
Beispiel #4
0
        private async Task <bool> EvaluateExpressionInput(ITurnContext turnContext, RequestState requestState)
        {
            var               res     = false;
            var               text    = turnContext.Activity.Text;
            var               curLink = requestState.CurrentLink;
            ActionLink        actionResult;
            ExpEvalResultType op = curLink.NeuralExp.Next(text, out actionResult);

            switch (op)
            {
            case ExpEvalResultType.Skipped:
            case ExpEvalResultType.False:
            case ExpEvalResultType.True:
            {
                if (op == ExpEvalResultType.Skipped)
                {
                    await SendNotes(curLink, turnContext);
                }
                switch (actionResult.Type)
                {
                case LinkType.NeuralLink:
                {
                    var link = await DbLinkCollection.FindOneById(actionResult.LinkId);

                    if (link != null)
                    {
                        curLink = link;
                        requestState.StepForward(curLink);
                        await SendResponseForCurrentNode(turnContext, requestState);
                    }
                    else
                    {
                        //Invalid expression evaluation link id.
                        await SendReply(turnContext, $"Invalid node information : '{actionResult.LinkId}',please report this issue to the bot administrator.", SuggestionExtension.GetCommonSuggestionActions());
                    }
                }
                break;

                case LinkType.ActionLink:
                {
                    var action = await DbActionCollection.FindOneById(actionResult.LinkId);

                    await turnContext.SendActivityAsync(action.BuildActionRespose(turnContext));

                    requestState.CurrentState = ChatStateType.RecordFeedback;
                }
                break;

                case LinkType.NeuralResource:
                {
                    var resource = await DbResourceCollection.FindOneById(actionResult.LinkId);

                    await turnContext.SendActivityAsync(resource.BuildResourceResponse(turnContext));

                    requestState.CurrentState = ChatStateType.RecordFeedback;
                }
                break;

                default:
                    break;
                }
                if (requestState.CurrentState == ChatStateType.RecordFeedback)
                {
                    await Task.Delay(1000).ContinueWith(async ct =>
                        {
                            await SendReply(turnContext, StringsProvider.TryGet(BotResourceKeyConstants.Feedback), SuggestionExtension.GetFeedbackSuggestionActions());
                        });
                }
                res = true;
            }
            break;

            case ExpEvalResultType.Exception:
            case ExpEvalResultType.Invalid:
            {
                await SendReply(turnContext, "Invalid input, please try again!", SuggestionExtension.GetCommonSuggestionActions("Exit:exit"));

                requestState.CurrentState = ChatStateType.InvalidInput;
            }
            break;

            case ExpEvalResultType.Empty:    //TODO
            {
                await SendReply(turnContext, $"Evaluated empty expression of node : '{curLink._id}',please report this issue to the bot administrator.", SuggestionExtension.GetCommonSuggestionActions());
            }
            break;

            default:

                break;
            }
            return(res);
        }
        private async Task <bool> EvaluateExpressionInput(ITurnContext turnContext, RequestState requestState)
        {
            var               res     = false;
            var               text    = turnContext.Activity.Text;
            var               curLink = requestState.CurrentLink;
            ActionLink        actionResult;
            ExpEvalResultType op = curLink.NeuralExp.Next(text, out actionResult);

            switch (op)
            {
            case ExpEvalResultType.Skipped:
            case ExpEvalResultType.False:
            case ExpEvalResultType.True:
            {
                switch (actionResult.Type)
                {
                case LinkType.NeuralLink:
                {
                    var link = await DbLinkCollection.FindOneById(actionResult.LinkId);

                    if (link != null)
                    {
                        curLink = link;
                        requestState.StepForward(curLink);
                        await SendResponseForCurrentNode(turnContext, requestState);
                    }
                    else
                    {
                        //Invalid expression evaluation link id.
                    }
                }
                break;

                case LinkType.ActionLink:
                {
                    var action = await DbActionCollection.FindOneById(actionResult.LinkId);

                    await turnContext.SendActivityAsync(action.BuildActionRespose(turnContext));

                    await Task.Delay(1000).ContinueWith(async ct =>
                            {
                                await SendReply(turnContext, StringsProvider.TryGet(BotResourceKeyConstants.Feedback), SuggestionExtension.GetFeedbackSuggestionActions());
                            });

                    requestState.CurrentState = ChatStateType.RecordFeedback;
                }
                break;

                case LinkType.NeuralResource:
                    break;

                default:
                    break;
                }
            }
                res = true;
                break;

            case ExpEvalResultType.Exception:
            case ExpEvalResultType.Invalid:
            {
                await SendReply(turnContext, "Invalid input, Try again!", SuggestionExtension.GetCommonSuggestionActions());

                requestState.CurrentState = ChatStateType.InvalidInput;
            }
            break;

            case ExpEvalResultType.Empty:    //TODO
            default:

                break;
            }
            return(res);
        }