Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DCR] Normalize all expression and add '=' to force expression. #3253

Merged
merged 23 commits into from
Jan 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion libraries/Microsoft.Bot.Builder.AI.LUIS/LuisRecognizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ public class LuisRecognizer : Recognizer, ITelemetryRecognizer
/// <summary>
/// Initializes a new instance of the <see cref="LuisRecognizer"/> class.
/// </summary>
/// <param name="application">The LUIS application to use to recognize text.</param>
/// <param name="recognizerOptions"> The LUIS recognizer version options.</param>
/// <param name="clientHandler">(Optional) Custom handler for LUIS API calls to allow mocking.</param>
public LuisRecognizer(LuisRecognizerOptions recognizerOptions, HttpClientHandler clientHandler = null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,10 @@ public override async Task<DialogTurnResult> ResumeDialogAsync(DialogContext dc,
return await OnActionScopeResultAsync(dc, actionScopeResult, cancellationToken).ConfigureAwait(false);
}

var dcState = dc.GetState();

// When we are resumed, we increment our offset into the actions and being the next action
var nextOffset = dc.GetState().GetIntValue(OFFSETKEY, 0) + 1;
var nextOffset = dcState.GetIntValue(OFFSETKEY, 0) + 1;
if (nextOffset < this.Actions.Count)
{
return await this.BeginActionAsync(dc, nextOffset, cancellationToken: cancellationToken).ConfigureAwait(false);
Expand Down Expand Up @@ -135,7 +137,9 @@ protected virtual async Task<DialogTurnResult> OnEndOfActionsAsync(DialogContext
protected virtual async Task<DialogTurnResult> BeginActionAsync(DialogContext dc, int offset, CancellationToken cancellationToken = default)
{
// get the action for the offset
dc.GetState().SetValue(OFFSETKEY, offset);
var dcState = dc.GetState();

dcState.SetValue(OFFSETKEY, offset);
var actionId = this.Actions[offset].Id;

// begin Action dialog
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using Microsoft.Bot.Builder.Dialogs.Declarative;
using Microsoft.Bot.Expressions;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
Expand All @@ -16,12 +17,13 @@ namespace Microsoft.Bot.Builder.Dialogs.Adaptive.Actions
public abstract class BaseInvokeDialog : Dialog, IDialogDependencies
{
// Expression for dialogId to call (allowing dynamic expression)
private string dialogIdToCall;

public BaseInvokeDialog(string dialogIdToCall = null, IDictionary<string, string> bindingOptions = null)
public BaseInvokeDialog(string dialogIdToCall = null, object bindingOptions = null)
: base()
{
this.dialogIdToCall = dialogIdToCall;
if (dialogIdToCall != null)
{
this.Dialog = dialogIdToCall;
}

if (bindingOptions != null)
{
Expand All @@ -36,7 +38,7 @@ public BaseInvokeDialog(string dialogIdToCall = null, IDictionary<string, string
/// Configurable options for the dialog.
/// </value>
[JsonProperty("options")]
public object Options { get; set; } = new JObject();
public ObjectExpression<object> Options { get; set; } = new ObjectExpression<object>();

/// <summary>
/// Gets or sets the dialog to call.
Expand All @@ -45,60 +47,66 @@ public BaseInvokeDialog(string dialogIdToCall = null, IDictionary<string, string
/// The dialog to call.
/// </value>
[JsonProperty("dialog")]
public Dialog Dialog { get; set; }
public DialogExpression Dialog { get; set; }

/// <summary>
/// Gets or sets a value indicating whether to have the new dialog should process the activity.
/// </summary>
/// <value>The default for this will be true, which means the new dialog should not look the activity. You can set this to false to dispatch the activity to the new dialog.</value>
[DefaultValue(true)]
[JsonProperty("activityProcessed")]
public bool ActivityProcessed { get; set; } = true;
public BoolExpression ActivityProcessed { get; set; } = true;

public virtual IEnumerable<Dialog> GetDependencies()
{
if (Dialog != null)
if (Dialog?.Value != null)
{
yield return Dialog;
yield return Dialog.Value;
}

yield break;
}

protected override string OnComputeId()
{
return $"{this.GetType().Name}[{Dialog?.Id ?? this.dialogIdToCall}]";
return $"{this.GetType().Name}[{Dialog?.ToString()}]";
}

protected Dialog ResolveDialog(DialogContext dc)
{
if (this.Dialog != null)
if (this.Dialog?.Value != null)
{
return this.Dialog;
return this.Dialog.Value;
}

var dialogId = this.dialogIdToCall ?? throw new Exception($"{this.GetType().Name} requires a dialog to be called.");
return dc.FindDialog(dialogId) ?? throw new Exception($"{dialogId} not found.");
var dcState = dc.GetState();

// NOTE: we call TryEvaluate instead of TryGetValue because we want the result of the expression as a string so we can
// look up the string using external FindDialog().
var (dialogId, _) = this.Dialog.Expression.TryEvaluate<string>(dcState);
return dc.FindDialog(dialogId ?? throw new Exception($"{this.Dialog.ToString()} not found."));
}

protected object BindOptions(DialogContext dc, object options)
{
var dcState = dc.GetState();

// binding options are static definition of options with overlay of passed in options);
var bindingOptions = (JObject)ObjectPath.Merge(Options, options ?? new JObject());
var bindingOptions = (JObject)ObjectPath.Merge(this.Options.GetValue(dcState), options ?? new JObject());
var boundOptions = new JObject();

foreach (var binding in bindingOptions)
{
// evalute the value
var (result, error) = new ExpressionEngine().Parse(binding.Value.ToString()).TryEvaluate(dc.GetState());
var (value, error) = new ValueExpression(binding.Value).TryGetValue(dcState);

if (error != null)
{
throw new Exception(error);
}

// and store in options as the result
boundOptions[binding.Key] = JToken.FromObject(result);
ObjectPath.SetPathValue(boundOptions, binding.Key, value);
}

return boundOptions;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@ public class BeginDialog : BaseInvokeDialog
[JsonProperty("$kind")]
public const string DeclarativeType = "Microsoft.BeginDialog";

private Expression disabled;

[JsonConstructor]
public BeginDialog(string dialogIdToCall = null, IDictionary<string, string> options = null, [CallerFilePath] string callerPath = "", [CallerLineNumber] int callerLine = 0)
public BeginDialog(string dialogIdToCall = null, object options = null, [CallerFilePath] string callerPath = "", [CallerLineNumber] int callerLine = 0)
: base(dialogIdToCall, options)
{
this.RegisterSourceLocation(callerPath, callerLine);
Expand All @@ -38,11 +36,7 @@ public BeginDialog(string dialogIdToCall = null, IDictionary<string, string> opt
/// A boolean expression.
/// </value>
[JsonProperty("disabled")]
public string Disabled
{
get { return disabled?.ToString(); }
set { disabled = value != null ? new ExpressionEngine().Parse(value) : null; }
}
public BoolExpression Disabled { get; set; }

/// <summary>
/// Gets or sets the property path to store the dialog result in.
Expand All @@ -51,16 +45,18 @@ public string Disabled
/// The property path to store the dialog result in.
/// </value>
[JsonProperty("resultProperty")]
public string ResultProperty { get; set; }
public StringExpression ResultProperty { get; set; }

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");
}

var dcState = dc.GetState();

if (this.disabled != null && (bool?)this.disabled.TryEvaluate(dc.GetState()).value == true)
if (this.Disabled != null && this.Disabled.GetValue(dcState) == true)
Copy link
Contributor

@chrimc62 chrimc62 Jan 16, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this.Disabled != null [](start = 16, length = 21)

This is testing to see if there is an expression assigned, right? #ByDesign

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes


In reply to: 367266589 [](ancestors = 367266589)

{
return await dc.EndDialogAsync(cancellationToken: cancellationToken).ConfigureAwait(false);
}
Expand All @@ -71,17 +67,19 @@ public string Disabled
var boundOptions = BindOptions(dc, options);

// set the activity processed state (default is true)
dc.GetState().SetValue(TurnPath.ACTIVITYPROCESSED, this.ActivityProcessed);
dcState.SetValue(TurnPath.ACTIVITYPROCESSED, this.ActivityProcessed);

// start dialog with bound options passed in as the options
return await dc.BeginDialogAsync(dialog.Id, options: boundOptions, cancellationToken: cancellationToken).ConfigureAwait(false);
}

public override async Task<DialogTurnResult> ResumeDialogAsync(DialogContext dc, DialogReason reason, object result = null, CancellationToken cancellationToken = default(CancellationToken))
{
var dcState = dc.GetState();

if (this.ResultProperty != null)
{
dc.GetState().SetValue(this.ResultProperty, result);
dcState.SetValue(this.ResultProperty.GetValue(dcState), result);
}

// By default just end the current dialog and return result to parent.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ public class BreakLoop : Dialog
[JsonProperty("$kind")]
public const string DeclarativeType = "Microsoft.BreakLoop";

private Expression disabled;

public BreakLoop([CallerFilePath] string callerPath = "", [CallerLineNumber] int callerLine = 0)
{
this.RegisterSourceLocation(callerPath, callerLine);
Expand All @@ -35,20 +33,18 @@ public BreakLoop([CallerFilePath] string callerPath = "", [CallerLineNumber] int
/// A boolean expression.
/// </value>
[JsonProperty("disabled")]
public string Disabled
{
get { return disabled?.ToString(); }
set { disabled = value != null ? new ExpressionEngine().Parse(value) : null; }
}
public BoolExpression Disabled { get; set; }

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");
}

var dcState = dc.GetState();

if (this.disabled != null && (bool?)this.disabled.TryEvaluate(dc.GetState()).value == true)
if (this.Disabled != null && this.Disabled.GetValue(dcState) == true)
{
return await dc.EndDialogAsync(cancellationToken: cancellationToken).ConfigureAwait(false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ public class CancelAllDialogs : Dialog
[JsonProperty("$kind")]
public const string DeclarativeType = "Microsoft.CancelAllDialogs";

private Expression disabled;

[JsonConstructor]
public CancelAllDialogs([CallerFilePath] string callerPath = "", [CallerLineNumber] int callerLine = 0)
: base()
Expand All @@ -37,11 +35,7 @@ public CancelAllDialogs([CallerFilePath] string callerPath = "", [CallerLineNumb
/// A boolean expression.
/// </value>
[JsonProperty("disabled")]
public string Disabled
{
get { return disabled?.ToString(); }
set { disabled = value != null ? new ExpressionEngine().Parse(value) : null; }
}
public BoolExpression Disabled { get; set; }

/// <summary>
/// Gets or sets event name.
Expand All @@ -50,7 +44,7 @@ public string Disabled
/// Event name.
/// </value>
[JsonProperty("eventName")]
public string EventName { get; set; }
public StringExpression EventName { get; set; }

/// <summary>
/// Gets or sets value expression for EventValue.
Expand All @@ -59,7 +53,7 @@ public string Disabled
/// Value expression for EventValue.
/// </value>
[JsonProperty("eventValue")]
public string EventValue { get; set; }
public StringExpression EventValue { get; set; }

public override async Task<DialogTurnResult> BeginDialogAsync(DialogContext dc, object options = null, CancellationToken cancellationToken = default(CancellationToken))
{
Expand All @@ -68,24 +62,20 @@ public string Disabled
throw new ArgumentException($"{nameof(options)} cannot be a cancellation token");
}

if (this.disabled != null && (bool?)this.disabled.TryEvaluate(dc.GetState()).value == true)
{
return await dc.EndDialogAsync(cancellationToken: cancellationToken).ConfigureAwait(false);
}
var dcState = dc.GetState();

object eventValue = null;
if (this.EventValue != null)
if (this.Disabled != null && this.Disabled.GetValue(dcState) == true)
{
eventValue = new ExpressionEngine().Parse(this.EventValue).TryEvaluate(dc.GetState());
return await dc.EndDialogAsync(cancellationToken: cancellationToken).ConfigureAwait(false);
}

if (dc.Parent == null)
{
return await dc.CancelAllDialogsAsync(true, EventName, eventValue, cancellationToken).ConfigureAwait(false);
return await dc.CancelAllDialogsAsync(true, EventName.GetValue(dcState), this.EventValue.GetValue(dcState), cancellationToken).ConfigureAwait(false);
}
else
{
var turnResult = await dc.Parent.CancelAllDialogsAsync(true, EventName, eventValue, cancellationToken).ConfigureAwait(false);
var turnResult = await dc.Parent.CancelAllDialogsAsync(true, EventName.GetValue(dcState), this.EventValue.GetValue(dcState), cancellationToken).ConfigureAwait(false);
turnResult.ParentEnded = true;
return turnResult;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ namespace Microsoft.Bot.Builder.Dialogs.Adaptive.Actions
public class CodeAction : Dialog
{
private readonly CodeActionHandler codeHandler;
private Expression disabled;

public CodeAction(CodeActionHandler codeHandler, [CallerFilePath] string callerPath = "", [CallerLineNumber] int callerLine = 0)
: base()
Expand All @@ -34,11 +33,7 @@ public CodeAction(CodeActionHandler codeHandler, [CallerFilePath] string callerP
/// A boolean expression.
/// </value>
[JsonProperty("disabled")]
public string Disabled
{
get { return disabled?.ToString(); }
set { disabled = value != null ? new ExpressionEngine().Parse(value) : null; }
}
public BoolExpression Disabled { get; set; }

public override async Task<DialogTurnResult> BeginDialogAsync(DialogContext dc, object options = null, CancellationToken cancellationToken = default(CancellationToken))
{
Expand All @@ -47,7 +42,9 @@ public string Disabled
throw new ArgumentException($"{nameof(options)} cannot be a cancellation token");
}

if (this.disabled != null && (bool?)this.disabled.TryEvaluate(dc.GetState()).value == true)
var dcState = dc.GetState();

if (this.Disabled != null && this.Disabled.GetValue(dcState) == true)
{
return await dc.EndDialogAsync(cancellationToken: cancellationToken).ConfigureAwait(false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ public class ContinueLoop : Dialog
[JsonProperty("$kind")]
public const string DeclarativeType = "Microsoft.ContinueLoop";

private Expression disabled;

public ContinueLoop([CallerFilePath] string callerPath = "", [CallerLineNumber] int callerLine = 0)
{
this.RegisterSourceLocation(callerPath, callerLine);
Expand All @@ -35,11 +33,7 @@ public ContinueLoop([CallerFilePath] string callerPath = "", [CallerLineNumber]
/// A boolean expression.
/// </value>
[JsonProperty("disabled")]
public string Disabled
{
get { return disabled?.ToString(); }
set { disabled = value != null ? new ExpressionEngine().Parse(value) : null; }
}
public BoolExpression Disabled { get; set; }

public override async Task<DialogTurnResult> BeginDialogAsync(DialogContext dc, object options = null, CancellationToken cancellationToken = default(CancellationToken))
{
Expand All @@ -48,7 +42,9 @@ public string Disabled
throw new ArgumentException($"{nameof(options)} cannot be a cancellation token");
}

if (this.disabled != null && (bool?)this.disabled.TryEvaluate(dc.GetState()).value == true)
var dcState = dc.GetState();

if (this.Disabled != null && this.Disabled.GetValue(dcState) == true)
{
return await dc.EndDialogAsync(cancellationToken: cancellationToken).ConfigureAwait(false);
}
Expand Down
Loading