Skip to content
Permalink
Browse files

Code cleanup #171

Rename ExpressionCollection property.
Use KVP for aggregate expressions to avoid order mismatch.
  • Loading branch information...
snikolayev committed Feb 22, 2019
1 parent 88adac2 commit 8fef5b11ca06dfbbf7fbca8770eb3c9af6114d06
@@ -30,13 +30,13 @@ public class AggregateElement : PatternSourceElement
/// <summary>
/// Expressions used by the aggregate.
/// </summary>
public ExpressionCollection ExpressionCollection { get; }
public ExpressionCollection Expressions { get; }

internal AggregateElement(Type resultType, string name, ExpressionCollection expressionCollection, PatternElement source, Type customFactoryType)
internal AggregateElement(Type resultType, string name, ExpressionCollection expressions, PatternElement source, Type customFactoryType)
: base(resultType)
{
Name = name;
ExpressionCollection = expressionCollection;
Expressions = expressions;
Source = source;
CustomFactoryType = customFactoryType;

@@ -506,10 +506,10 @@ public static AggregateElement GroupBy(Type resultType, LambdaExpression keySele
resultType = groupingType.MakeGenericType(keySelector.ReturnType, elementSelector.ReturnType);
}

var expressions = new Dictionary<string, LambdaExpression>
var expressions = new List<KeyValuePair<string, LambdaExpression>>
{
{ "KeySelector", keySelector },
{ "ElementSelector", elementSelector }
new KeyValuePair<string, LambdaExpression>("KeySelector", keySelector),
new KeyValuePair<string, LambdaExpression>("ElementSelector", elementSelector)
};
var element = Aggregate(resultType, AggregateElement.GroupByName, expressions, source);
return element;
@@ -544,9 +544,9 @@ public static AggregateElement Project(Type resultType, LambdaExpression selecto
resultType = selector.ReturnType;
}

var expressions = new Dictionary<string, LambdaExpression>
var expressions = new List<KeyValuePair<string, LambdaExpression>>
{
{ "Selector", selector }
new KeyValuePair<string, LambdaExpression>("Selector", selector)
};
var element = Aggregate(resultType, AggregateElement.ProjectName, expressions, source);
return element;
@@ -564,9 +564,9 @@ public static AggregateElement Flatten(Type resultType, LambdaExpression selecto
if (selector == null)
throw new ArgumentNullException(nameof(selector), "Flattening selector not provided");

var expressions = new Dictionary<string, LambdaExpression>
var expressions = new List<KeyValuePair<string, LambdaExpression>>
{
{ "Selector", selector }
new KeyValuePair<string, LambdaExpression>("Selector", selector)
};
var element = Aggregate(resultType, AggregateElement.FlattenName, expressions, source);
return element;
@@ -93,8 +93,8 @@ public static void ValidateCollectAggregate(AggregateElement element)
$"Collect result must be a collection of source elements. ElementType={sourceType}, ResultType={resultType}");
}

var keySelectorAscending = element.ExpressionCollection.FindSingleOrDefault("KeySelectorAscending")?.Expression;
var keySelectorDescending = element.ExpressionCollection.FindSingleOrDefault("KeySelectorDescending")?.Expression;
var keySelectorAscending = element.Expressions.FindSingleOrDefault("KeySelectorAscending")?.Expression;
var keySelectorDescending = element.Expressions.FindSingleOrDefault("KeySelectorDescending")?.Expression;

if (keySelectorAscending != null && keySelectorDescending != null)
{
@@ -123,7 +123,7 @@ public static void ValidateGroupByAggregate(AggregateElement element)
{
var sourceType = element.Source.ValueType;
var resultType = element.ResultType;
var keySelector = element.ExpressionCollection["KeySelector"].Expression;
var keySelector = element.Expressions["KeySelector"].Expression;
if (keySelector.Parameters.Count == 0)
{
throw new ArgumentException(
@@ -136,7 +136,7 @@ public static void ValidateGroupByAggregate(AggregateElement element)
$"KeySelector={keySelector}, ExpectedType={sourceType}, ActualType={keySelector.Parameters[0].Type}");
}

var elementSelector = element.ExpressionCollection["ElementSelector"].Expression;
var elementSelector = element.Expressions["ElementSelector"].Expression;
if (elementSelector.Parameters.Count == 0)
{
throw new ArgumentException(
@@ -162,7 +162,7 @@ public static void ValidateProjectAggregate(AggregateElement element)
{
var sourceType = element.Source.ValueType;
var resultType = element.ResultType;
var selector = element.ExpressionCollection["Selector"].Expression;
var selector = element.Expressions["Selector"].Expression;
if (selector.Parameters.Count == 0)
{
throw new ArgumentException(
@@ -186,7 +186,7 @@ public static void ValidateFlattenAggregate(AggregateElement element)
{
var sourceType = element.Source.ValueType;
var resultType = element.ResultType;
var selector = element.ExpressionCollection["Selector"].Expression;
var selector = element.Expressions["Selector"].Expression;
if (selector.Parameters.Count != 1)
{
throw new ArgumentException(
@@ -81,7 +81,7 @@ protected internal override void VisitAggregate(Context context, AggregateElemen
var source = Transform<PatternElement>(context, element.Source);
if (context.IsModified)
{
var aggregateExpressions = element.ExpressionCollection.Select(x => new KeyValuePair<string, LambdaExpression>(x.Name, x.Expression));
var aggregateExpressions = element.Expressions.Select(x => new KeyValuePair<string, LambdaExpression>(x.Name, x.Expression));
var newElement = Element.Aggregate(element.ResultType, element.Name, aggregateExpressions, source, element.CustomFactoryType);
Result(context, newElement);
}
@@ -6,7 +6,7 @@
namespace NRules.RuleModel
{
/// <summary>
/// Sorted readonly map of named expressions.
/// Ordered readonly collection of named expressions.
/// </summary>
public class ExpressionCollection : IEnumerable<NamedExpressionElement>
{
@@ -18,7 +18,7 @@ public ExpressionCollection(IEnumerable<NamedExpressionElement> expressions)
}

/// <summary>
/// Number of expressions in the map.
/// Number of expressions in the collection.
/// </summary>
public int Count => _expressions.Count;

@@ -52,7 +52,7 @@ public IEnumerable<NamedExpressionElement> Find(string name)
}

/// <summary>
/// Retrieves only expression by name.
/// Retrieves single expression by name.
/// </summary>
/// <param name="name">Expression name.</param>
/// <returns>Matching expression or <c>null</c>.</returns>
@@ -30,7 +30,7 @@ protected internal virtual void VisitCondition(TContext context, ConditionElemen

protected internal virtual void VisitAggregate(TContext context, AggregateElement element)
{
foreach (var expression in element.ExpressionCollection)
foreach (var expression in element.Expressions)
{
expression.Accept(context, this);
}
@@ -18,8 +18,8 @@ public void Compile(AggregateElement element, IEnumerable<IAggregateExpression>
{
var sourceType = element.Source.ValueType;

var ascendingSortSelector = element.ExpressionCollection.FindSingleOrDefault("KeySelectorAscending");
var descendingSortSelector = element.ExpressionCollection.FindSingleOrDefault("KeySelectorDescending");
var ascendingSortSelector = element.Expressions.FindSingleOrDefault("KeySelectorAscending");
var descendingSortSelector = element.Expressions.FindSingleOrDefault("KeySelectorDescending");
if (ascendingSortSelector != null)
{
_factory = CreateSortedAggregatorFactory(sourceType, SortDirection.Ascending, ascendingSortSelector, compiledExpressions.FindSingle("KeySelectorAscending"));
@@ -37,7 +37,7 @@ public void Compile(AggregateElement element, IEnumerable<IAggregateExpression>
}
}

static Func<IAggregator> CreateSortedAggregatorFactory(Type sourceType, SortDirection sortDirection, NamedExpressionElement selector, IAggregateExpression compiledSelector)
private static Func<IAggregator> CreateSortedAggregatorFactory(Type sourceType, SortDirection sortDirection, NamedExpressionElement selector, IAggregateExpression compiledSelector)
{
var resultType = selector.Expression.ReturnType;
var aggregatorType = typeof(SortedAggregator<,>).MakeGenericType(sourceType, resultType);
@@ -16,8 +16,8 @@ internal class GroupByAggregatorFactory : IAggregatorFactory

public void Compile(AggregateElement element, IEnumerable<IAggregateExpression> compiledExpressions)
{
var keySelector = element.ExpressionCollection["KeySelector"];
var elementSelector = element.ExpressionCollection["ElementSelector"];
var keySelector = element.Expressions["KeySelector"];
var elementSelector = element.Expressions["ElementSelector"];

var sourceType = element.Source.ValueType;
var keyType = keySelector.Expression.ReturnType;
@@ -16,7 +16,7 @@ internal class ProjectionAggregatorFactory : IAggregatorFactory

public void Compile(AggregateElement element, IEnumerable<IAggregateExpression> compiledExpressions)
{
var selector = element.ExpressionCollection["Selector"];
var selector = element.Expressions["Selector"];
var sourceType = element.Source.ValueType;
var resultType = selector.Expression.ReturnType;
Type aggregatorType = typeof(ProjectionAggregator<,>).MakeGenericType(sourceType, resultType);
@@ -75,7 +75,7 @@ internal static NodeInfo Create(ExistsNode node)

internal static NodeInfo Create(AggregateNode node)
{
var expressions = node.ExpressionCollection.Select(e => $"{e.Name}={e.Expression.ToString()}");
var expressions = node.Expressions.Select(e => $"{e.Name}={e.Expression.ToString()}");
return new NodeInfo(NodeType.Aggregate, node.Name, Empty, expressions, Empty);
}

@@ -10,13 +10,13 @@ internal class AggregateNode : BinaryBetaNode
private readonly bool _isSubnetJoin;

public string Name { get; }
public ExpressionCollection ExpressionCollection { get; }
public ExpressionCollection Expressions { get; }

public AggregateNode(ITupleSource leftSource, IObjectSource rightSource, string name, ExpressionCollection expressionCollection, IAggregatorFactory aggregatorFactory, bool isSubnetJoin)
public AggregateNode(ITupleSource leftSource, IObjectSource rightSource, string name, ExpressionCollection expressions, IAggregatorFactory aggregatorFactory, bool isSubnetJoin)
: base(leftSource, rightSource)
{
Name = name;
ExpressionCollection = expressionCollection;
Expressions = expressions;
_aggregatorFactory = aggregatorFactory;
_isSubnetJoin = isSubnetJoin;
}
@@ -231,12 +231,12 @@ private void BuildAggregateNode(ReteBuilderContext context, AggregateElement ele
x.RightSource == context.AlphaSource &&
x.LeftSource == context.BetaSource &&
x.Name == element.Name &&
ExpressionCollectionComparer.AreEqual(x.ExpressionCollection, element.ExpressionCollection));
ExpressionCollectionComparer.AreEqual(x.Expressions, element.Expressions));
if (node == null)
{
var aggregatorFactory = BuildAggregatorFactory(context, element);
node = new AggregateNode(context.BetaSource, context.AlphaSource, element.Name,
element.ExpressionCollection, aggregatorFactory, context.HasSubnet);
element.Expressions, aggregatorFactory, context.HasSubnet);
}
BuildBetaMemoryNode(context, node);
context.ResetAlphaSource();
@@ -355,7 +355,7 @@ private static IEnumerable<IAggregateExpression> CompileExpressions(ReteBuilderC
{
var declarations = context.Declarations.Concat(element.Source.Declaration).ToList();
var result = new List<IAggregateExpression>();
foreach (var expression in element.ExpressionCollection)
foreach (var expression in element.Expressions)
{
var aggregateExpression = ExpressionCompiler.CompileAggregateExpression(expression, declarations);
result.Add(aggregateExpression);
@@ -144,7 +144,7 @@ internal class CustomSelectAggregateFactory : IAggregatorFactory

public void Compile(AggregateElement element, IEnumerable<IAggregateExpression> compiledExpressions)
{
var selector = element.ExpressionCollection["Selector"];
var selector = element.Expressions["Selector"];
var sourceType = element.Source.ValueType;
var resultType = selector.Expression.ReturnType;
var aggregatorType = typeof(CustomSelectAggregator<,>).MakeGenericType(sourceType, resultType);

0 comments on commit 8fef5b1

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