Skip to content

Commit

Permalink
Update IMemory interface and tryEvaluate for expression (#3635)
Browse files Browse the repository at this point in the history
* expand the interface expression evaluation to take an extra option

* expand IMemory to accept a boolean flag

* Update ExpressionEvaluator.cs

* adjust the order of parameter of EvalauteChildren

* fix boolean context in where

* fix doc

* fix customzied memory in LG

* fix customized funtion inside onConditions

* fix doc

* Swallow error in bool

* Fix DSM for the new interface

* Revert "Fix DSM for the new interface"

This reverts commit ae1aa0e.

* Switch to a specific null substitution delegate

* Fix Element

* fix the type issue

* revert LG memory change

* update OnCondition

* Remove delegate in favor of Func

* Add constructor and copy constructor

* Made comparision boolean context also with test cases

* remove unintended change in csproj

* revert changes in csproj

* go back using Comparison for bool()

* catch error in Where func
  • Loading branch information
boydc2014 committed Mar 31, 2020
1 parent f110ffc commit 0a7bb4f
Show file tree
Hide file tree
Showing 7 changed files with 233 additions and 130 deletions.
2 changes: 1 addition & 1 deletion libraries/AdaptiveExpressions/Constant.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class Constant : Expression
/// </summary>
/// <param name="value">Constant value.</param>
public Constant(object value = null)
: base(new ExpressionEvaluator(ExpressionType.Constant, (expression, state) => ((expression as Constant).Value, null)))
: base(new ExpressionEvaluator(ExpressionType.Constant, (expression, state, _) => ((expression as Constant).Value, null)))
{
Value = value;
}
Expand Down
23 changes: 14 additions & 9 deletions libraries/AdaptiveExpressions/Expression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ public static Expression LambaExpression(EvaluateExpressionDelegate function)
/// <param name="function">Lambda expression to evaluate.</param>
/// <returns>New expression.</returns>
public static Expression Lambda(Func<object, object> function)
=> new Expression(new ExpressionEvaluator(ExpressionType.Lambda, (expression, state) =>
=> new Expression(new ExpressionEvaluator(ExpressionType.Lambda, (expression, state, _) =>
{
object value = null;
string error = null;
Expand Down Expand Up @@ -481,9 +481,10 @@ public void ValidateTree()
/// Global state to evaluate accessor expressions against. Can be <see cref="System.Collections.Generic.IDictionary{String, Object}"/>,
/// <see cref="System.Collections.IDictionary"/> otherwise reflection is used to access property and then indexer.
/// </param>
/// <param name="options">Options used in the evaluation. </param>
/// <returns>Computed value and an error string. If the string is non-null, then there was an evaluation error.</returns>
public (object value, string error) TryEvaluate(object state)
=> this.TryEvaluate<object>(MemoryFactory.Create(state));
public (object value, string error) TryEvaluate(object state, Options options = null)
=> this.TryEvaluate<object>(MemoryFactory.Create(state), options);

/// <summary>
/// Evaluate the expression.
Expand All @@ -492,9 +493,10 @@ public void ValidateTree()
/// Global state to evaluate accessor expressions against. Can be <see cref="System.Collections.Generic.IDictionary{String, Object}"/>,
/// <see cref="System.Collections.IDictionary"/> otherwise reflection is used to access property and then indexer.
/// </param>
/// <param name="options">Options used in the evaluation. </param>
/// <returns>Computed value and an error string. If the string is non-null, then there was an evaluation error.</returns>
public (object value, string error) TryEvaluate(IMemory state)
=> this.TryEvaluate<object>(state);
public (object value, string error) TryEvaluate(IMemory state, Options options = null)
=> this.TryEvaluate<object>(state, options);

/// <summary>
/// Evaluate the expression.
Expand All @@ -504,9 +506,10 @@ public void ValidateTree()
/// Global state to evaluate accessor expressions against. Can be <see cref="System.Collections.Generic.IDictionary{String, Object}"/>,
/// <see cref="System.Collections.IDictionary"/> otherwise reflection is used to access property and then indexer.
/// </param>
/// <param name="options">Options used in the evaluation. </param>
/// <returns>Computed value and an error string. If the string is non-null, then there was an evaluation error.</returns>
public (T value, string error) TryEvaluate<T>(object state)
=> this.TryEvaluate<T>(MemoryFactory.Create(state));
public (T value, string error) TryEvaluate<T>(object state, Options options = null)
=> this.TryEvaluate<T>(MemoryFactory.Create(state), options);

/// <summary>
/// Evaluate the expression.
Expand All @@ -516,10 +519,12 @@ public void ValidateTree()
/// Global state to evaluate accessor expressions against. Can be <see cref="System.Collections.Generic.IDictionary{String, Object}"/>,
/// <see cref="System.Collections.IDictionary"/> otherwise reflection is used to access property and then indexer.
/// </param>
/// <param name="options">Options used in the evaluation. </param>
/// <returns>Computed value and an error string. If the string is non-null, then there was an evaluation error.</returns>
public (T value, string error) TryEvaluate<T>(IMemory state)
public (T value, string error) TryEvaluate<T>(IMemory state, Options options = null)
{
var (result, error) = Evaluator.TryEvaluate(this, state);
var opts = options ?? new Options();
var (result, error) = Evaluator.TryEvaluate(this, state, opts);
if (error != null)
{
return (default(T), error);
Expand Down
8 changes: 5 additions & 3 deletions libraries/AdaptiveExpressions/ExpressionEvaluator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ namespace AdaptiveExpressions
/// </remarks>
/// <param name="expression">Expression to evaluate.</param>
/// <param name="state">Global state information.</param>
/// <param name="options">Options for the evaluation.</param>
/// <returns>Value and error string that is non-null if there is an error.</returns>
public delegate (object value, string error) EvaluateExpressionDelegate(Expression expression, IMemory state);
public delegate (object value, string error) EvaluateExpressionDelegate(Expression expression, IMemory state, Options options);

/// <summary>
/// Delegate to lookup function information from the type.
Expand Down Expand Up @@ -105,9 +106,10 @@ public ExpressionEvaluator Negation
/// </summary>
/// <param name="expression">Expression to evaluate.</param>
/// <param name="state">Global state information.</param>
/// <param name="options">Options used in the evaluation. </param>
/// <returns>Value and error string that is non-null if there is an error.</returns>
public (object value, string error) TryEvaluate(Expression expression, IMemory state)
=> _evaluator(expression, state);
public (object value, string error) TryEvaluate(Expression expression, IMemory state, Options options)
=> _evaluator(expression, state, options);

/// <summary>
/// Validate an expression.
Expand Down
Loading

0 comments on commit 0a7bb4f

Please sign in to comment.