Skip to content
Permalink
Browse files

Expression collection minor code cleanup

  • Loading branch information...
snikolayev committed Feb 20, 2019
1 parent 0bab635 commit 3b177d9646548c060c71b744ccbb859356209f4e
@@ -18,7 +18,7 @@ public interface IQueryBuilder
void GroupBy<TSource, TKey, TElement>(Expression<Func<TSource, TKey>> keySelector, Expression<Func<TSource, TElement>> elementSelector);
void Collect<TSource>();
void OrderBy<TSource, TKey>(Expression<Func<TSource, TKey>> keySelector, SortDirection sortDirection);
void Aggregate<TSource, TResult>(string name, IEnumerable<KeyValuePair<string, LambdaExpression>> expressionCollection);
void Aggregate<TSource, TResult>(string name, IEnumerable<KeyValuePair<string, LambdaExpression>> expressionCollection, Type customFactoryType);
void Aggregate<TSource, TResult>(string name, IEnumerable<KeyValuePair<string, LambdaExpression>> expressions);
void Aggregate<TSource, TResult>(string name, IEnumerable<KeyValuePair<string, LambdaExpression>> expressions, Type customFactoryType);
}
}
@@ -170,12 +170,12 @@ public void Collect<TSource>()
};
}

public void Aggregate<TSource, TResult>(string aggregateName, IEnumerable<KeyValuePair<string, LambdaExpression>> expressionCollection)
public void Aggregate<TSource, TResult>(string aggregateName, IEnumerable<KeyValuePair<string, LambdaExpression>> expressions)
{
Aggregate<TSource, TResult>(aggregateName, expressionCollection, null);
Aggregate<TSource, TResult>(aggregateName, expressions, null);
}

public void Aggregate<TSource, TResult>(string aggregateName, IEnumerable<KeyValuePair<string, LambdaExpression>> expressionCollection, Type customFactoryType)
public void Aggregate<TSource, TResult>(string aggregateName, IEnumerable<KeyValuePair<string, LambdaExpression>> expressions, Type customFactoryType)
{
var previousBuildAction = _buildAction;
_buildAction = name =>
@@ -190,7 +190,7 @@ public void Collect<TSource>()
var sourceBuilder = previousResult.Pattern;

var rewrittenExpressionCollection = new List<KeyValuePair<string, LambdaExpression>>();
foreach (var item in expressionCollection)
foreach (var item in expressions)
{
var expression = sourceBuilder.DslPatternExpression(_symbolStack.Scope.Declarations, item.Value);
rewrittenExpressionCollection.Add(new KeyValuePair<string, LambdaExpression>(item.Key, expression));
@@ -60,7 +60,7 @@ public void Collect()
public void OrderBy(LambdaExpression keySelector, SortDirection sortDirection)
{
var expressionName = sortDirection == SortDirection.Ascending ? "KeySelectorAscending" : "KeySelectorDescending";
_expressions.Add(CreateNamedLambdaExpression(expressionName, keySelector));
AddExpression(expressionName, keySelector);
}

/// <summary>
@@ -71,8 +71,8 @@ public void OrderBy(LambdaExpression keySelector, SortDirection sortDirection)
public void GroupBy(LambdaExpression keySelector, LambdaExpression elementSelector)
{
_name = AggregateElement.GroupByName;
_expressions.Add(CreateNamedLambdaExpression("KeySelector", keySelector));
_expressions.Add(CreateNamedLambdaExpression("ElementSelector", elementSelector));
AddExpression("KeySelector", keySelector);
AddExpression("ElementSelector", elementSelector);
}

/// <summary>
@@ -82,7 +82,7 @@ public void GroupBy(LambdaExpression keySelector, LambdaExpression elementSelect
public void Project(LambdaExpression selector)
{
_name = AggregateElement.ProjectName;
_expressions.Add(CreateNamedLambdaExpression("Selector", selector));
AddExpression("Selector", selector);
}

/// <summary>
@@ -92,7 +92,7 @@ public void Project(LambdaExpression selector)
public void Flatten(LambdaExpression selector)
{
_name = AggregateElement.FlattenName;
_expressions.Add(CreateNamedLambdaExpression("Selector", selector));
AddExpression("Selector", selector);
}

/// <summary>
@@ -148,9 +148,9 @@ AggregateElement IBuilder<AggregateElement>.Build()
return aggregateElement;
}

private static KeyValuePair<string, LambdaExpression> CreateNamedLambdaExpression(string name, LambdaExpression lambdaExpression)
private void AddExpression(string name, LambdaExpression expression)
{
return new KeyValuePair<string, LambdaExpression>(name, lambdaExpression);
_expressions.Add(new KeyValuePair<string, LambdaExpression>(name, expression));
}

private void AssertSingleSource()
@@ -1,6 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using NRules.Rete;
using NRules.RuleModel;
@@ -13,6 +11,11 @@ namespace NRules.Aggregators
/// </summary>
public interface IAggregateExpression
{
/// <summary>
/// Name of the aggregate expression.
/// </summary>
string Name { get; }

/// <summary>
/// Invokes the expression with the given inputs.
/// </summary>
@@ -21,39 +24,6 @@ public interface IAggregateExpression
/// <param name="fact">Fact being processed by the aggregate element.</param>
/// <returns>Result of the expression.</returns>
object Invoke(AggregationContext context, ITuple tuple, IFact fact);

/// <summary>
/// Name of the aggregate expression.
/// </summary>
string Name { get; }
}

/// <summary>
/// Extension methods used for working with collections of aggregate expressions.
/// </summary>
public static class IAggregateExpressionExtensions
{
/// <summary>
/// Get an enumerable of matching aggregate expressions.
/// </summary>
/// <param name="expressions">The list of aggregate expressions to search through.</param>
/// <param name="name">Name of the aggregate expressions to find.</param>
/// <returns></returns>
public static IEnumerable<IAggregateExpression> Find(this IEnumerable<IAggregateExpression> expressions, string name)
{
return expressions.Where(e => e.Name == name);
}

/// <summary>
/// Get a single matching aggregate expression.
/// </summary>
/// <param name="expressions">The list of aggregate expressions to search through.</param>
/// <param name="name">Name of the aggregate expression to find.</param>
/// <returns></returns>
public static IAggregateExpression FindSingle(this IEnumerable<IAggregateExpression> expressions, string name)
{
return expressions.Find(name).Single();
}
}

internal class AggregateFactExpression : IAggregateExpression
@@ -108,6 +78,8 @@ public AggregateExpression(string name, LambdaExpression expression, FastDelegat
Name = name;
}

public string Name { get; }

public object Invoke(AggregationContext context, ITuple tuple, IFact fact)
{
var args = new object[_compiledExpression.ArrayArgumentCount];
@@ -139,8 +111,6 @@ public object Invoke(AggregationContext context, ITuple tuple, IFact fact)
}
}

public string Name { get; }

public override string ToString()
{
return _expression.ToString();
@@ -0,0 +1,33 @@
using System.Collections.Generic;
using System.Linq;

namespace NRules.Aggregators
{
/// <summary>
/// Extension methods used for working with collections of aggregate expressions.
/// </summary>
public static class AggregateExpressionExtensions
{
/// <summary>
/// Get an enumerable of matching aggregate expressions.
/// </summary>
/// <param name="expressions">The list of aggregate expressions to search through.</param>
/// <param name="name">Name of the aggregate expressions to find.</param>
/// <returns></returns>
public static IEnumerable<IAggregateExpression> Find(this IEnumerable<IAggregateExpression> expressions, string name)
{
return expressions.Where(e => e.Name == name);
}

/// <summary>
/// Get a single matching aggregate expression.
/// </summary>
/// <param name="expressions">The list of aggregate expressions to search through.</param>
/// <param name="name">Name of the aggregate expression to find.</param>
/// <returns></returns>
public static IAggregateExpression FindSingle(this IEnumerable<IAggregateExpression> expressions, string name)
{
return expressions.Find(name).Single();
}
}
}
@@ -95,8 +95,8 @@ public static class FirstQueryExtensions
{
public static IQuery<TSource> First<TSource>(this IQuery<IEnumerable<TSource>> source)
{
var expressionCollection = new List<KeyValuePair<string, LambdaExpression>>();
source.Builder.Aggregate<IEnumerable<TSource>, TSource>("First", expressionCollection, typeof(CustomFirstAggregatorFactory));
var expressions = new List<KeyValuePair<string, LambdaExpression>>();
source.Builder.Aggregate<IEnumerable<TSource>, TSource>("First", expressions, typeof(CustomFirstAggregatorFactory));
return new QueryExpression<TSource>(source.Builder);
}
}
@@ -129,11 +129,11 @@ public static class CustomSelectQuery
{
public static IQuery<TResult> CustomSelect<TSource, TResult>(this IQuery<TSource> source, Expression<Func<TSource, TResult>> selector)
{
var expressionCollection = new List<KeyValuePair<string, LambdaExpression>>
var expressions = new List<KeyValuePair<string, LambdaExpression>>
{
new KeyValuePair<string, LambdaExpression>("Selector", selector)
};
source.Builder.Aggregate<TSource, TResult>("CustomSelect", expressionCollection);
source.Builder.Aggregate<TSource, TResult>("CustomSelect", expressions);
return new QueryExpression<TResult>(source.Builder);
}
}

0 comments on commit 3b177d9

Please sign in to comment.
You can’t perform that action at this time.