Example #1
0
        public static IAggregateExpression CompileAggregateExpression(NamedExpressionElement element, IEnumerable <Declaration> declarations)
        {
            var declarationsList = declarations.ToList();

            if (element.Imports.Count() == 1 &&
                Equals(element.Imports.Single(), declarationsList.Last()))
            {
                var optimizer           = new ExpressionSingleParameterOptimizer <Func <object, object> >();
                var optimizedExpression = optimizer.ConvertParameter(element.Expression);
                var @delegate           = optimizedExpression.Compile();
                var fastDelegate        = Create(@delegate, element.Expression.Parameters.Count);
                var expression          = new AggregateFactExpression(element.Name, element.Expression, fastDelegate);
                return(expression);
            }
            else
            {
                var optimizer           = new ExpressionMultiParameterOptimizer <Func <object[], object> >();
                var optimizedExpression = optimizer.CompactParameters(element.Expression, 0);
                var @delegate           = optimizedExpression.Compile();
                var fastDelegate        = Create(@delegate, element.Expression.Parameters.Count);
                var factMap             = IndexMap.CreateMap(element.Imports, declarationsList);
                var expression          = new AggregateExpression(element.Name, element.Expression, fastDelegate, factMap);
                return(expression);
            }
        }
Example #2
0
        public IAggregateExpression CompileAggregateExpression(NamedExpressionElement element, List <Declaration> declarations)
        {
            var compiledExpression = CompileLhsExpression <object>(element, declarations);
            var expression         = new AggregateExpression(element.Name, compiledExpression);

            return(expression);
        }
Example #3
0
        private NamedExpressionElement ToNamedExpression(string name, LambdaExpression expression)
        {
            IEnumerable <Declaration> references = expression.Parameters.Select(p => Scope.Lookup(p.Name, p.Type));
            var element = new NamedExpressionElement(name, Scope.VisibleDeclarations, references, expression);

            return(element);
        }
 public static bool AreEqual(NamedExpressionElement first, NamedExpressionElement second)
 {
     if (!Equals(first.Name, second.Name))
     {
         return(false);
     }
     return(ExpressionComparer.AreEqual(first.Expression, second.Expression));
 }
        private static void AssertNotEqual(NamedExpressionElement first, NamedExpressionElement second)
        {
            //Act
            bool result = ExpressionElementComparer.AreEqual(first, second);

            //Assert
            Assert.False(result);
        }
Example #6
0
        /// <summary>
        /// Creates a named expression element that can be used by other rule elements.
        /// </summary>
        /// <param name="name">Name of the expression.</param>
        /// <param name="expression">The actual expression that this element represents.</param>
        /// <returns>Created element.</returns>
        public static NamedExpressionElement Expression(string name, LambdaExpression expression)
        {
            if (name == null)
            {
                throw new ArgumentNullException(nameof(name), "Expression name not provided");
            }
            if (expression == null)
            {
                throw new ArgumentNullException(nameof(expression), "Expression not provided");
            }

            var element = new NamedExpressionElement(name, expression);

            return(element);
        }
Example #7
0
        /// <summary>
        /// Creates a condition element that represents a condition applied to elements matched by a pattern.
        /// </summary>
        /// <param name="expression">Condition expression. It must have <c>Boolean</c> as its return type.</param>
        /// <returns>Created element.</returns>
        public static NamedExpressionElement Condition(LambdaExpression expression)
        {
            if (expression == null)
            {
                throw new ArgumentNullException(nameof(expression), "Condition expression not provided");
            }
            if (expression.ReturnType != typeof(bool))
            {
                throw new ArgumentException($"Pattern condition must return a Boolean result. Condition={expression}");
            }

            var element = new NamedExpressionElement(PatternElement.ConditionName, expression);

            return(element);
        }
Example #8
0
 protected internal virtual void VisitNamedExpression(TContext context, NamedExpressionElement element)
 {
 }
        private static Func <IAggregator> CreateSingleKeySortedAggregatorFactory(Type sourceType, SortDirection sortDirection, NamedExpressionElement selector, IAggregateExpression compiledSelector)
        {
            var keyType        = selector.Expression.ReturnType;
            var aggregatorType = typeof(SortedAggregator <,>).MakeGenericType(sourceType, keyType);

            var ctor = aggregatorType.GetTypeInfo().DeclaredConstructors.Single();
            var factoryExpression = Expression.Lambda <Func <IAggregator> >(
                Expression.New(ctor, Expression.Constant(compiledSelector), Expression.Constant(sortDirection)));

            return(factoryExpression.Compile());
        }