Skip to content
Permalink
Browse files

Merge pull request #179 from a046/feature-multiaggregator

Refactor ExpressionMap into ExpressionCollection
  • Loading branch information...
snikolayev committed Feb 20, 2019
2 parents d7ec2f1 + 819d439 commit 0bab6356f876ae6ba9372e32b25855ab2a823b9e
Showing with 196 additions and 148 deletions.
  1. +2 −2 src/NRules/NRules.Fluent/Dsl/IQueryBuilder.cs
  2. +7 −7 src/NRules/NRules.Fluent/Expressions/QueryExpression.cs
  3. +3 −3 src/NRules/NRules.RuleModel/AggregateElement.cs
  4. +14 −12 src/NRules/NRules.RuleModel/Builders/AggregateBuilder.cs
  5. +35 −41 src/NRules/NRules.RuleModel/Builders/Element.cs
  6. +6 −6 src/NRules/NRules.RuleModel/Builders/ElementValidator.cs
  7. +3 −3 src/NRules/NRules.RuleModel/Builders/RuleTransformation.cs
  8. +21 −12 src/NRules/NRules.RuleModel/{ExpressionMap.cs → ExpressionCollection.cs}
  9. +2 −2 src/NRules/NRules.RuleModel/NamedExpressionElement.cs
  10. +1 −1 src/NRules/NRules.RuleModel/RuleElementVisitor.cs
  11. +43 −2 src/NRules/NRules/Aggregators/AggregateExpression.cs
  12. +6 −6 src/NRules/NRules/Aggregators/CollectionAggregatorFactory.cs
  13. +2 −2 src/NRules/NRules/Aggregators/FlatteningAggregatorFactory.cs
  14. +5 −5 src/NRules/NRules/Aggregators/GroupByAggregatorFactory.cs
  15. +1 −1 src/NRules/NRules/Aggregators/IAggregatorFactory.cs
  16. +3 −3 src/NRules/NRules/Aggregators/ProjectionAggregatorFactory.cs
  17. 0 src/NRules/NRules/{Aggregators → Collections}/SortedFactCollection.cs
  18. +1 −1 src/NRules/NRules/Diagnostics/NodeInfo.cs
  19. +3 −3 src/NRules/NRules/Rete/AggregateNode.cs
  20. +3 −3 src/NRules/NRules/Rete/{ExpressionMapComparer.cs → ExpressionCollectionComparer.cs}
  21. +6 −6 src/NRules/NRules/Rete/ReteBuilder.cs
  22. +4 −4 src/NRules/NRules/Utilities/ExpressionCompiler.cs
  23. +12 −12 src/NRules/Tests/NRules.IntegrationTests/CustomFirstAggregatorTest.cs
  24. +10 −10 src/NRules/Tests/NRules.IntegrationTests/CustomSelectAggregatorTest.cs
  25. +3 −1 src/NRules/Tests/NRules.Tests/Aggregators/AggregatorTest.cs
@@ -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, IDictionary<string, LambdaExpression> expressionMap);
void Aggregate<TSource, TResult>(string name, IDictionary<string, LambdaExpression> expressionMap, Type customFactoryType);
void Aggregate<TSource, TResult>(string name, IEnumerable<KeyValuePair<string, LambdaExpression>> expressionCollection);
void Aggregate<TSource, TResult>(string name, IEnumerable<KeyValuePair<string, LambdaExpression>> expressionCollection, Type customFactoryType);
}
}
@@ -170,12 +170,12 @@ public void Collect<TSource>()
};
}

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

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

var rewrittenExpressionMap = new Dictionary<string, LambdaExpression>();
foreach (var item in expressionMap)
var rewrittenExpressionCollection = new List<KeyValuePair<string, LambdaExpression>>();
foreach (var item in expressionCollection)
{
var expression = sourceBuilder.DslPatternExpression(_symbolStack.Scope.Declarations, item.Value);
rewrittenExpressionMap[item.Key] = expression;
rewrittenExpressionCollection.Add(new KeyValuePair<string, LambdaExpression>(item.Key, expression));
}

aggregateBuilder.Aggregator(aggregateName, rewrittenExpressionMap, customFactoryType);
aggregateBuilder.Aggregator(aggregateName, rewrittenExpressionCollection, customFactoryType);
aggregateBuilder.Pattern(sourceBuilder);

result = new BuildResult(patternBuilder, aggregateBuilder, sourceBuilder);
@@ -30,13 +30,13 @@ public class AggregateElement : PatternSourceElement
/// <summary>
/// Expressions used by the aggregate.
/// </summary>
public ExpressionMap ExpressionMap { get; }
public ExpressionCollection ExpressionCollection { get; }

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

@@ -11,14 +11,14 @@ public class AggregateBuilder : RuleElementBuilder, IBuilder<AggregateElement>
{
private string _name;
private Type _resultType;
private readonly Dictionary<string, LambdaExpression> _expressions = new Dictionary<string, LambdaExpression>();
private readonly List<KeyValuePair<string, LambdaExpression>> _expressions = new List<KeyValuePair<string, LambdaExpression>>();
private Type _customFactoryType;
private IBuilder<PatternElement> _sourceBuilder;

/// <summary>
/// Initializes a new instance of the <see cref="AggregateBuilder"/>.
/// </summary>
public AggregateBuilder()
public AggregateBuilder()
{
}

@@ -37,13 +37,10 @@ public void ResultType(Type resultType)
/// <param name="name">Name of the aggregator.</param>
/// <param name="expressions">Named expressions used by the aggregator.</param>
/// <param name="customFactoryType">The type of the custom aggregate factory</param>
public void Aggregator(string name, IDictionary<string, LambdaExpression> expressions, Type customFactoryType = null)
public void Aggregator(string name, IEnumerable<KeyValuePair<string, LambdaExpression>> expressions, Type customFactoryType = null)
{
_name = name;
foreach (var item in expressions)
{
_expressions[item.Key] = item.Value;
}
_expressions.AddRange(expressions);
_customFactoryType = customFactoryType;
}

@@ -63,7 +60,7 @@ public void Collect()
public void OrderBy(LambdaExpression keySelector, SortDirection sortDirection)
{
var expressionName = sortDirection == SortDirection.Ascending ? "KeySelectorAscending" : "KeySelectorDescending";
_expressions[expressionName] = keySelector;
_expressions.Add(CreateNamedLambdaExpression(expressionName, keySelector));
}

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

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

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

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

private static KeyValuePair<string, LambdaExpression> CreateNamedLambdaExpression(string name, LambdaExpression lambdaExpression)
{
return new KeyValuePair<string, LambdaExpression>(name, lambdaExpression);
}

private void AssertSingleSource()
{
if (_sourceBuilder != null)
Oops, something went wrong.

0 comments on commit 0bab635

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