예제 #1
0
        public void Aggregate <TSource, TResult>(string aggregateName, IEnumerable <KeyValuePair <string, LambdaExpression> > expressions, Type customFactoryType)
        {
            var previousBuildAction = _buildAction;

            _buildAction = name =>
            {
                var patternBuilder = new PatternBuilder(typeof(TResult), name);

                BuildResult result;
                using (_symbolStack.Frame())
                {
                    var aggregateBuilder = patternBuilder.Aggregate();
                    var previousResult   = previousBuildAction(null);
                    var sourceBuilder    = previousResult.Pattern;

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

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

                    result = new BuildResult(patternBuilder, aggregateBuilder, sourceBuilder);
                }

                _symbolStack.Scope.Add(patternBuilder.Declaration);
                return(result);
            };
        }
예제 #2
0
        public void GroupBy <TSource, TKey, TElement>(Expression <Func <TSource, TKey> > keySelector, Expression <Func <TSource, TElement> > elementSelector)
        {
            var previousBuildAction = _buildAction;

            _buildAction = name =>
            {
                var patternBuilder = new PatternBuilder(typeof(IGrouping <TKey, TElement>), name);

                BuildResult result;
                using (_symbolStack.Frame())
                {
                    var aggregateBuilder          = patternBuilder.Aggregate();
                    var previousResult            = previousBuildAction(null);
                    var sourceBuilder             = previousResult.Pattern;
                    var keySelectorExpression     = sourceBuilder.DslPatternExpression(_symbolStack.Scope.Declarations, keySelector);
                    var elementSelectorExpression = sourceBuilder.DslPatternExpression(_symbolStack.Scope.Declarations, elementSelector);
                    aggregateBuilder.GroupBy(keySelectorExpression, elementSelectorExpression);
                    aggregateBuilder.Pattern(sourceBuilder);
                    result = new BuildResult(patternBuilder, aggregateBuilder, sourceBuilder);
                }

                _symbolStack.Scope.Add(patternBuilder.Declaration);
                return(result);
            };
        }
예제 #3
0
        private IRuleDefinition BuildMultipleOrdersRule()
        {
            //rule "Multiple Orders Rule"
            //when
            //    customer = Customer(x => x.IsPreferred);
            //    orders = Query(
            //        Order(x => x.Customer == customer, x => x.IsOpen)
            //        Collect()
            //        Where(c => c.Count() >= 3)
            //    );
            //then
            //    Console.WriteLine("Customer {0} has {1} open order(s)", customer.Name, orders.Count());

            var builder = new RuleModel.Builders.RuleBuilder();

            builder.Name("Multiple Orders Rule");

            PatternBuilder customerPattern = builder.LeftHandSide().Pattern(typeof(Customer), "customer");
            Expression <Func <Customer, bool> > customerCondition = customer => customer.IsPreferred;

            customerPattern.Condition(customerCondition);

            PatternBuilder ordersPattern = builder.LeftHandSide().Pattern(typeof(IEnumerable <Order>), "orders");
            Expression <Func <IEnumerable <Order>, bool> > aggregateCondition = orders => orders.Count() >= 3;

            ordersPattern.Condition(aggregateCondition);

            var aggregate = ordersPattern.Aggregate();

            aggregate.Collect();

            var orderPattern = aggregate.Pattern(typeof(Order), "order");
            Expression <Func <Order, Customer, bool> > orderCondition1 = (order, customer) => order.Customer == customer;

            orderPattern.Condition(orderCondition1);
            Expression <Func <Order, bool> > orderCondition2 = order => order.IsOpen;

            orderPattern.Condition(orderCondition2);

            Expression <Action <IContext, Customer, IEnumerable <Order> > > action =
                (ctx, customer, orders) => Console.WriteLine("Customer {0} has {1} open order(s)", customer.Name, orders.Count());

            builder.RightHandSide().Action(action);

            return(builder.Build());
        }
예제 #4
0
        public void SelectMany <TSource, TResult>(Expression <Func <TSource, IEnumerable <TResult> > > selector)
        {
            var previousBuildAction = _buildAction;

            _buildAction = name =>
            {
                var patternBuilder = new PatternBuilder(typeof(TResult), name);

                BuildResult result;
                using (_symbolStack.Frame())
                {
                    var aggregateBuilder   = patternBuilder.Aggregate();
                    var previousResult     = previousBuildAction(null);
                    var sourceBuilder      = previousResult.Pattern;
                    var selectorExpression = sourceBuilder.DslPatternExpression(_symbolStack.Scope.Declarations, selector);
                    aggregateBuilder.Flatten(selectorExpression);
                    aggregateBuilder.Pattern(sourceBuilder);
                    result = new BuildResult(patternBuilder, aggregateBuilder, sourceBuilder);
                }

                _symbolStack.Scope.Add(patternBuilder.Declaration);
                return(result);
            };
        }
예제 #5
0
        public void Collect <TSource>()
        {
            var previousBuildAction = _buildAction;

            _buildAction = name =>
            {
                var patternBuilder = new PatternBuilder(typeof(IEnumerable <TSource>), name);

                BuildResult result;
                using (_symbolStack.Frame())
                {
                    var aggregateBuilder = patternBuilder.Aggregate();
                    var previousResult   = previousBuildAction(null);
                    var sourceBuilder    = previousResult.Pattern;
                    aggregateBuilder.Collect();
                    aggregateBuilder.Pattern(sourceBuilder);

                    result = new BuildResult(patternBuilder, aggregateBuilder, sourceBuilder);
                }

                _symbolStack.Scope.Add(patternBuilder.Declaration);
                return(result);
            };
        }