Ejemplo n.º 1
0
        protected virtual async Task <DialogTurnResult> NextItemAsync(DialogContext dc, CancellationToken cancellationToken = default)
        {
            // Get list information
            var itemsProperty = new ExpressionEngine().Parse(this.ItemsProperty);

            var(itemList, error) = itemsProperty.TryEvaluate(dc.GetState());
            var list  = JArray.FromObject(itemList);
            var index = dc.GetState().GetIntValue(INDEX);

            // Next item
            if (++index < list.Count)
            {
                // Persist index and value
                dc.GetState().SetValue(VALUE, list[index]);
                dc.GetState().SetValue(INDEX, index);

                // Start loop
                return(await this.BeginActionAsync(dc, 0, cancellationToken).ConfigureAwait(false));
            }
            else
            {
                // End of list has been reached
                return(await dc.EndDialogAsync(cancellationToken : cancellationToken).ConfigureAwait(false));
            }
        }
Ejemplo n.º 2
0
        public void Evaluate(string input, object expected, HashSet <string> expectedRefs)
        {
            var parsed = new ExpressionEngine().Parse(input);

            Assert.IsNotNull(parsed);
            var(actual, msg) = parsed.TryEvaluate(scope);
            Assert.AreEqual(null, msg);
            AssertObjectEquals(expected, actual);
            if (expectedRefs != null)
            {
                var actualRefs = parsed.References();
                Assert.IsTrue(expectedRefs.SetEquals(actualRefs), $"References do not match, expected: {string.Join(',', expectedRefs)} acutal: {string.Join(',', actualRefs)}");
            }
        }
Ejemplo n.º 3
0
        protected override Task <InputState> OnRecognizeInput(DialogContext dc)
        {
            var input = dc.GetState().GetValue <object>(VALUE_PROPERTY);

            var culture = GetCulture(dc);
            var results = NumberRecognizer.RecognizeNumber(input.ToString(), culture);

            if (results.Count > 0)
            {
                // Try to parse value based on type
                var text = results[0].Resolution["value"].ToString();

                if (float.TryParse(text, out var value))
                {
                    input = value;
                }
                else
                {
                    return(Task.FromResult(InputState.Unrecognized));
                }
            }
            else
            {
                return(Task.FromResult(InputState.Unrecognized));
            }

            dc.GetState().SetValue(VALUE_PROPERTY, input);

            if (!string.IsNullOrEmpty(OutputFormat))
            {
                var outputExpression = new ExpressionEngine().Parse(OutputFormat);
                var(outputValue, error) = outputExpression.TryEvaluate(dc.GetState());
                if (error == null)
                {
                    dc.GetState().SetValue(VALUE_PROPERTY, outputValue);
                }
                else
                {
                    throw new Exception($"In TextInput, OutputFormat Expression evaluation resulted in an error. Expression: {outputExpression.ToString()}. Error: {error}");
                }
            }

            return(Task.FromResult(InputState.Valid));
        }
Ejemplo n.º 4
0
        public override async Task <DialogTurnResult> BeginDialogAsync(DialogContext dc, object options = null, CancellationToken cancellationToken = default(CancellationToken))
        {
            if (options is CancellationToken)
            {
                throw new ArgumentException($"{nameof(options)} cannot be a cancellation token");
            }

            foreach (var propValue in this.Assignments)
            {
                var valexp = new ExpressionEngine().Parse(propValue.Value);
                var(value, valueError) = valexp.TryEvaluate(dc.GetState());
                if (valueError != null)
                {
                    throw new Exception($"Expression evaluation resulted in an error. Expression: {valexp.ToString()}. Error: {valueError}");
                }

                dc.GetState().SetValue(propValue.Property, value);
            }

            return(await dc.EndDialogAsync(cancellationToken : cancellationToken).ConfigureAwait(false));
        }
Ejemplo n.º 5
0
        protected override Task <InputState> OnRecognizeInput(DialogContext dc)
        {
            var input = dc.GetState().GetValue <string>(VALUE_PROPERTY);

            if (!string.IsNullOrEmpty(OutputFormat))
            {
                var outputExpression = new ExpressionEngine().Parse(OutputFormat);
                var(outputValue, error) = outputExpression.TryEvaluate(dc.GetState());
                if (error == null)
                {
                    input = outputValue.ToString();
                }
                else
                {
                    throw new Exception($"In TextInput, OutputFormat Expression evaluation resulted in an error. Expression: {outputExpression.ToString()}. Error: {error}");
                }
            }

            dc.GetState().SetValue(VALUE_PROPERTY, input);
            return(input.Length > 0 ? Task.FromResult(InputState.Valid) : Task.FromResult(InputState.Unrecognized));
        }
Ejemplo n.º 6
0
        protected override Task <InputState> OnRecognizeInput(DialogContext dc)
        {
            var input = dc.GetState().GetValue <object>(VALUE_PROPERTY);

            var culture = GetCulture(dc);
            var results = DateTimeRecognizer.RecognizeDateTime(input.ToString(), culture);

            if (results.Count > 0)
            {
                // Return list of resolutions from first match
                var result = new List <DateTimeResolution>();
                var values = (List <Dictionary <string, string> >)results[0].Resolution["values"];
                foreach (var value in values)
                {
                    result.Add(ReadResolution(value));
                }

                dc.GetState().SetValue(VALUE_PROPERTY, result);
                if (!string.IsNullOrEmpty(OutputFormat))
                {
                    var outputExpression = new ExpressionEngine().Parse(OutputFormat);
                    var(outputValue, error) = outputExpression.TryEvaluate(dc.GetState());
                    if (error == null)
                    {
                        dc.GetState().SetValue(VALUE_PROPERTY, outputValue);
                    }
                    else
                    {
                        throw new Exception($"OutputFormat Expression evaluation resulted in an error. Expression: {outputExpression.ToString()}. Error: {error}");
                    }
                }
            }
            else
            {
                return(Task.FromResult(InputState.Unrecognized));
            }

            return(Task.FromResult(InputState.Valid));
        }
Ejemplo n.º 7
0
        private async Task <DialogTurnResult> NextPageAsync(DialogContext dc, CancellationToken cancellationToken)
        {
            Expression itemsProperty = new ExpressionEngine().Parse(this.ItemsProperty);
            int        pageIndex     = dc.GetState().GetIntValue(FOREACHPAGEINDEX, 0);
            int        pageSize      = this.PageSize;
            int        itemOffset    = pageSize * pageIndex;

            var(items, error) = itemsProperty.TryEvaluate(dc.GetState());
            if (error == null)
            {
                var page = this.GetPage(items, itemOffset, pageSize);

                if (page.Any())
                {
                    dc.GetState().SetValue(FOREACHPAGE, page);
                    dc.GetState().SetValue(FOREACHPAGEINDEX, ++pageIndex);
                    return(await this.BeginActionAsync(dc, 0, cancellationToken).ConfigureAwait(false));
                }
            }

            // End of list has been reached
            return(await dc.EndDialogAsync(cancellationToken : cancellationToken).ConfigureAwait(false));
        }
Ejemplo n.º 8
0
        public override async Task <DialogTurnResult> BeginDialogAsync(DialogContext dc, object options = null, CancellationToken cancellationToken = default(CancellationToken))
        {
            if (options is CancellationToken)
            {
                throw new ArgumentException($"{nameof(options)} cannot be a cancellation token");
            }

            // Ensure planning context
            if (dc is SequenceContext sc)
            {
                Expression itemsProperty = new ExpressionEngine().Parse(this.ItemsProperty);
                int        offset        = 0;
                int        pageSize      = 0;
                if (options != null && options is ForeachPageOptions)
                {
                    var opt = options as ForeachPageOptions;
                    itemsProperty = opt.Items;
                    offset        = opt.Offset;
                    pageSize      = opt.PageSize;
                }

                if (pageSize == 0)
                {
                    pageSize = this.PageSize;
                }

                var(items, error) = itemsProperty.TryEvaluate(dc.GetState());
                if (error == null)
                {
                    var page = this.GetPage(items, offset, pageSize);

                    if (page.Count() > 0)
                    {
                        dc.GetState().SetValue(ForEachPage, page);
                        var changes = new ActionChangeList()
                        {
                            ChangeType = ActionChangeType.InsertActions,
                            Actions    = new List <ActionState>()
                        };
                        this.Actions.ForEach(step => changes.Actions.Add(new ActionState(step.Id)));

                        changes.Actions.Add(new ActionState()
                        {
                            DialogStack = new List <DialogInstance>(),
                            DialogId    = this.Id,
                            Options     = new ForeachPageOptions()
                            {
                                Items    = itemsProperty,
                                Offset   = offset + pageSize,
                                PageSize = pageSize
                            }
                        });
                        sc.QueueChanges(changes);
                    }
                }

                return(await sc.EndDialogAsync(cancellationToken : cancellationToken).ConfigureAwait(false));
            }
            else
            {
                throw new Exception("`Foreach` should only be used in the context of an adaptive dialog.");
            }
        }
Ejemplo n.º 9
0
        private async Task <InputState> RecognizeInput(DialogContext dc, int turnCount)
        {
            dynamic input = null;

            // Use Property expression for input first
            if (!string.IsNullOrEmpty(this.Property))
            {
                dc.GetState().TryGetValue(this.Property, out input);

                // Clear property to avoid it being stuck on the next turn. It will get written
                // back if the value passes validations.
                dc.GetState().SetValue(this.Property, null);
            }

            // Use Value expression for input second
            if (input == null && !string.IsNullOrEmpty(this.Value))
            {
                var(value, valueError) = new ExpressionEngine().Parse(this.Value).TryEvaluate(dc.GetState());
                if (valueError != null)
                {
                    throw new Exception($"In InputDialog, this.Value expression evaluation resulted in an error. Expression: {this.Value}. Error: {valueError}");
                }

                input = value;
            }

            // Fallback to using activity
            bool activityProcessed = dc.GetState().GetBoolValue(TurnPath.ACTIVITYPROCESSED);

            if (!activityProcessed && input == null && turnCount > 0)
            {
                if (this.GetType().Name == nameof(AttachmentInput))
                {
                    input = dc.Context.Activity.Attachments;
                }
                else
                {
                    input = dc.Context.Activity.Text;
                }
            }

            // Update "this.value" and perform additional recognition and validations
            dc.GetState().SetValue(VALUE_PROPERTY, input);
            if (input != null)
            {
                var state = await this.OnRecognizeInput(dc).ConfigureAwait(false);

                if (state == InputState.Valid)
                {
                    foreach (var validation in this.Validations)
                    {
                        var exp = new ExpressionEngine().Parse(validation);
                        var(value, error) = exp.TryEvaluate(dc.GetState());
                        if (value == null || (value is bool && (bool)value == false))
                        {
                            return(InputState.Invalid);
                        }
                    }

                    dc.GetState().SetValue(TurnPath.ACTIVITYPROCESSED, true);
                    return(InputState.Valid);
                }
                else
                {
                    return(state);
                }
            }
            else
            {
                return(InputState.Missing);
            }
        }
Ejemplo n.º 10
0
        public override async Task <DialogTurnResult> BeginDialogAsync(DialogContext dc, object options = null, CancellationToken cancellationToken = default(CancellationToken))
        {
            if (options is CancellationToken)
            {
                throw new ArgumentException($"{nameof(options)} cannot be a cancellation token");
            }

            // Ensure planning context
            if (dc is SequenceContext sc)
            {
                Expression itemsProperty = null;
                int        offset        = 0;
                if (options != null && options is ForeachOptions)
                {
                    var opt = options as ForeachOptions;
                    if (!string.IsNullOrEmpty(opt.List))
                    {
                        itemsProperty = new ExpressionEngine().Parse(opt.List);
                    }

                    offset = opt.Offset;
                }

                itemsProperty        = new ExpressionEngine().Parse(this.ItemsProperty);
                var(itemList, error) = itemsProperty.TryEvaluate(dc.State);

                if (error == null)
                {
                    var item = this.GetItem(itemList, offset);
                    if (item != null)
                    {
                        dc.State.SetValue(VALUE, item);
                        dc.State.SetValue(INDEX, offset);
                        var changes = new ActionChangeList()
                        {
                            ChangeType = ActionChangeType.InsertActions,
                            Actions    = new List <ActionState>()
                        };
                        this.Actions.ForEach(step => changes.Actions.Add(new ActionState(step.Id)));

                        changes.Actions.Add(new ActionState()
                        {
                            DialogStack = new List <DialogInstance>(),
                            DialogId    = this.Id,
                            Options     = new ForeachOptions()
                            {
                                List   = ItemsProperty,
                                Offset = offset + 1
                            }
                        });
                        sc.QueueChanges(changes);
                    }
                }

                return(await sc.EndDialogAsync(cancellationToken : cancellationToken).ConfigureAwait(false));
            }
            else
            {
                throw new Exception("`Foreach` should only be used in the context of an adaptive dialog.");
            }
        }