ComputeTransformationNode

We found 10 examples in language CSharp for this search. You will see 28 fragments of code.

Other methods

//---------------------------------------------------------------------
// <copyright file="ComputeTransformationNode.cs" company="Microsoft">
//      Copyright (C) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
// </copyright>
//---------------------------------------------------------------------

namespace Microsoft.OData.UriParser.Aggregation
{
    using System.Collections.Generic;

    /// <summary>
    /// Node representing a compute expression.
    /// </summary>
    public sealed class ComputeTransformationNode : TransformationNode
    {
        private readonly IEnumerable<ComputeExpression> expressions;

        /// <summary>
        /// Create a ComputeTransformationNode.
        /// </summary>
        /// <param name="expressions">A list of <see cref="ComputeExpression"/>.</param>
        public ComputeTransformationNode(IEnumerable<ComputeExpression> expressions)
        {
            ExceptionUtils.CheckArgumentNotNull(expressions, "expressions");

            this.expressions = expressions;
        }

        /// <summary>
        /// Gets the list of <see cref="ComputeExpression"/>.
        /// </summary>
        public IEnumerable<ComputeExpression> Expressions
        {
            get
            {
                return expressions;
            }
        }

        /// <summary>
        /// Gets the kind of the transformation node.
        /// </summary>
        public override TransformationNodeKind Kind
        {
            get
            {
                return TransformationNodeKind.Compute;
            }
        }
    }
}

//---------------------------------------------------------------------
// <copyright file="ComputeTransformationNode.cs" company="Microsoft">
//      Copyright (C) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
// </copyright>
//---------------------------------------------------------------------

namespace Microsoft.OData.UriParser.Aggregation
{
    using System.Collections.Generic;

    /// <summary>
    /// Node representing a compute expression.
    /// </summary>
    public sealed class ComputeTransformationNode : TransformationNode
    {
        private readonly IEnumerable<ComputeExpression> expressions;

        /// <summary>
        /// Create a ComputeTransformationNode.
        /// </summary>
        /// <param name="expressions">A list of <see cref="ComputeExpression"/>.</param>
        public ComputeTransformationNode(IEnumerable<ComputeExpression> expressions)
        {
            ExceptionUtils.CheckArgumentNotNull(expressions, "expressions");

            this.expressions = expressions;
        }

        /// <summary>
        /// Gets the list of <see cref="ComputeExpression"/>.
        /// </summary>
        public IEnumerable<ComputeExpression> Expressions
        {
            get
            {
                return expressions;
            }
        }

        /// <summary>
        /// Gets the kind of the transformation node.
        /// </summary>
        public override TransformationNodeKind Kind
        {
            get
            {
                return TransformationNodeKind.Compute;
            }
        }
    }
}

        private bool CompareComputeTransformation(ComputeTransformationNode? node1, ComputeTransformationNode? node2)
        {
            if (node1 == node2)
                return true;
            if (node1 == null || node2 == null)
                return false;

            return EnumerableComparer.Compare(node1.Expressions, node2.Expressions, CompareComputeExpression);
        }

        private bool CompareComputeTransformation(ComputeTransformationNode? node1, ComputeTransformationNode? node2)
        {
            if (node1 == node2)
                return true;
            if (node1 == null || node2 == null)
                return false;

            return EnumerableComparer.Compare(node1.Expressions, node2.Expressions, CompareComputeExpression);
        }

        private bool CompareComputeTransformation(ComputeTransformationNode? node1, ComputeTransformationNode? node2)
        {
            if (node1 == node2)
                return true;
            if (node1 == null || node2 == null)
                return false;

            return EnumerableComparer.Compare(node1.Expressions, node2.Expressions, CompareComputeExpression);
        }

        private bool CompareComputeTransformation(ComputeTransformationNode? node1, ComputeTransformationNode? node2)
        {
            if (node1 == node2)
                return true;
            if (node1 == null || node2 == null)
                return false;

            return EnumerableComparer.Compare(node1.Expressions, node2.Expressions, CompareComputeExpression);
        }


        internal string GetContextUri()
        {
            IEnumerable<ComputeExpression> computeExpressions = transformations.OfType<ComputeTransformationNode>().SelectMany(n => n.Expressions);
            return CreatePropertiesUriSegment(lastGroupByPropertyNodes, lastAggregateExpressions, computeExpressions);
        }

        internal HashSet<EndPathToken> GetLastAggregatedPropertyNames()
        {
            if (lastAggregateExpressions == null && lastComputeExpressions == null && lastGroupByPropertyNodes == null)
            {
                return null;
            }

            HashSet<EndPathToken> result = new HashSet<EndPathToken>();
            if (lastAggregateExpressions != null)
            {
                result.UnionWith(lastAggregateExpressions.Select(statement => new EndPathToken(statement.Alias, null)));
            }

            if (lastComputeExpressions != null)
            {
                result.UnionWith(lastComputeExpressions.Select(statement => new EndPathToken(statement.Alias, null)));
            }

            if (lastGroupByPropertyNodes != null)
            {
                result.UnionWith(GetGroupByPaths(lastGroupByPropertyNodes, null));
            }

            return result;
        }

        private IEnumerable<EndPathToken> GetGroupByPaths(IEnumerable<GroupByPropertyNode> nodes, EndPathToken token)
        {
            foreach (var node in nodes)
            {
                var nodeToken = new EndPathToken(node.Name, token);
                if (node.ChildTransformations == null || !node.ChildTransformations.Any())
                {
                    yield return nodeToken;
                }
                else
                {
                    foreach (var child in GetGroupByPaths(node.ChildTransformations, nodeToken))
                    {
                        yield return child;
                    }
                }
            }
        }

        private string CreatePropertiesUriSegment(
            IEnumerable<GroupByPropertyNode> groupByPropertyNodes,
            IEnumerable<AggregateExpressionBase> aggregateExpressions,
            IEnumerable<ComputeExpression> computeExpressions)
        {
            Func<GroupByPropertyNode, string> func = (prop) =>
           {
               var children = CreatePropertiesUriSegment(prop.ChildTransformations, null, null);

               return string.IsNullOrEmpty(children)
                      ? prop.Name
                      : prop.Name + ODataConstants.ContextUriProjectionStart + children + ODataConstants.ContextUriProjectionEnd;
           };

            string result = string.Empty;
            if (groupByPropertyNodes != null)
            {
                var groupByPropertyArray =
                    groupByPropertyNodes.Select(prop => func(prop))
                        .ToArray();
                result = string.Join(",", groupByPropertyArray);
                result = aggregateExpressions == null
                    ? result
                    : string.Format(CultureInfo.InvariantCulture, "{0},{1}", result, CreateAggregatePropertiesUriSegment(aggregateExpressions));
            }
            else
            {
                result = aggregateExpressions == null
                    ? string.Empty
                    : CreateAggregatePropertiesUriSegment(aggregateExpressions);
            }

            if (computeExpressions != null && !string.IsNullOrEmpty(result) /* don't add compute if only compute() is present */)
            {
                string computeProperties = string.Join(",", computeExpressions.Select(e => e.Alias).ToArray());
                if (!string.IsNullOrEmpty(computeProperties))
                {
                    result = string.Format(CultureInfo.InvariantCulture, "{0},{1}", result, computeProperties);
                }
            }

            return result;
        }

        private static string CreateAggregatePropertiesUriSegment(IEnumerable<AggregateExpressionBase> aggregateExpressions)
        {
            if (aggregateExpressions != null)
            {
                return string.Join(",", aggregateExpressions.Select(statement => statement.Alias).ToArray());
            }
            else
            {
                return string.Empty;
            }
        }


        public string TranslateApplyClause(ApplyClause applyClause)
        {
            ExceptionUtils.CheckArgumentNotNull(applyClause, nameof(applyClause));

            query.Append(ExpressionConstants.QueryOptionApply);
            query.Append(ExpressionConstants.SymbolEqual);

            bool appendSlash = false;
            foreach (TransformationNode transformation in applyClause.Transformations)
            {
                appendSlash = AppendSlash(appendSlash);
                Translate(transformation);
            }

            return appendSlash ? query.ToString() : string.Empty;
        }

        private bool AppendComma(bool appendComma)
        {
            if (appendComma)
            {
                query.Append(ExpressionConstants.SymbolComma);
            }

            return true;
        }

        private void AppendExpression(SingleValueNode expression)
        {
            string text = Uri.EscapeDataString(nodeToStringBuilder.TranslateNode(expression));
            query.Append(text);
        }

        private void AppendExpression(ODataExpandPath path)
        {
            string text = path.ToContextUrlPathString();
            query.Append(text);
        }

        private bool AppendSlash(bool appendSlash)
        {
            if (appendSlash)
            {
                query.Append(ExpressionConstants.SymbolForwardSlash);
            }

            return true;
        }

        private void AppendWord(string word)
        {
            query.Append(word);
            query.Append(ExpressionConstants.SymbolEscapedSpace);
        }


        internal string GetContextUri()
        {
            IEnumerable<ComputeExpression> computeExpressions = transformations.OfType<ComputeTransformationNode>().SelectMany(n => n.Expressions);
            return CreatePropertiesUriSegment(lastGroupByPropertyNodes, lastAggregateExpressions, computeExpressions);
        }

        internal HashSet<EndPathToken> GetLastAggregatedPropertyNames()
        {
            if (lastAggregateExpressions == null && lastComputeExpressions == null && lastGroupByPropertyNodes == null)
            {
                return null;
            }

            HashSet<EndPathToken> result = new HashSet<EndPathToken>();
            if (lastAggregateExpressions != null)
            {
                result.UnionWith(lastAggregateExpressions.Select(statement => new EndPathToken(statement.Alias, null)));
            }

            if (lastComputeExpressions != null)
            {
                result.UnionWith(lastComputeExpressions.Select(statement => new EndPathToken(statement.Alias, null)));
            }

            if (lastGroupByPropertyNodes != null)
            {
                result.UnionWith(GetGroupByPaths(lastGroupByPropertyNodes, null));
            }

            return result;
        }

        private IEnumerable<EndPathToken> GetGroupByPaths(IEnumerable<GroupByPropertyNode> nodes, EndPathToken token)
        {
            foreach (var node in nodes)
            {
                var nodeToken = new EndPathToken(node.Name, token);
                if (node.ChildTransformations == null || !node.ChildTransformations.Any())
                {
                    yield return nodeToken;
                }
                else
                {
                    foreach (var child in GetGroupByPaths(node.ChildTransformations, nodeToken))
                    {
                        yield return child;
                    }
                }
            }
        }

        private string CreatePropertiesUriSegment(
            IEnumerable<GroupByPropertyNode> groupByPropertyNodes,
            IEnumerable<AggregateExpressionBase> aggregateExpressions,
            IEnumerable<ComputeExpression> computeExpressions)
        {
            Func<GroupByPropertyNode, string> func = (prop) =>
           {
               var children = CreatePropertiesUriSegment(prop.ChildTransformations, null, null);

               return string.IsNullOrEmpty(children)
                      ? prop.Name
                      : prop.Name + ODataConstants.ContextUriProjectionStart + children + ODataConstants.ContextUriProjectionEnd;
           };

            string result = string.Empty;
            if (groupByPropertyNodes != null)
            {
                var groupByPropertyArray =
                    groupByPropertyNodes.Select(prop => func(prop))
                        .ToArray();
                result = string.Join(",", groupByPropertyArray);
                result = aggregateExpressions == null
                    ? result
                    : string.Format(CultureInfo.InvariantCulture, "{0},{1}", result, CreateAggregatePropertiesUriSegment(aggregateExpressions));
            }
            else
            {
                result = aggregateExpressions == null
                    ? string.Empty
                    : CreateAggregatePropertiesUriSegment(aggregateExpressions);
            }

            if (computeExpressions != null && !string.IsNullOrEmpty(result) /* don't add compute if only compute() is present */)
            {
                string computeProperties = string.Join(",", computeExpressions.Select(e => e.Alias).ToArray());
                if (!string.IsNullOrEmpty(computeProperties))
                {
                    result = string.Format(CultureInfo.InvariantCulture, "{0},{1}", result, computeProperties);
                }
            }

            return result;
        }

        private static string CreateAggregatePropertiesUriSegment(IEnumerable<AggregateExpressionBase> aggregateExpressions)
        {
            if (aggregateExpressions != null)
            {
                return string.Join(",", aggregateExpressions.Select(statement => statement.Alias).ToArray());
            }
            else
            {
                return string.Empty;
            }
        }


        [Fact]
        public void BindApplyWithNullShouldThrow()
        {
            ApplyBinder binder = new ApplyBinder(FakeBindMethods.BindSingleComplexProperty, _bindingState);
            Action bind = () => binder.BindApply(null);
            Assert.Throws<ArgumentNullException>("tokens", bind);
        }

        [Fact]
        public void BindApplyWithAggregateShouldReturnApplyClause()
        {
            IEnumerable<QueryToken> tokens = _parser.ParseApply("aggregate(UnitPrice with sum as TotalPrice)");

            ApplyBinder binder = new ApplyBinder(FakeBindMethods.BindSingleComplexProperty, _bindingState);
            ApplyClause actual = binder.BindApply(tokens);

            Assert.NotNull(actual);
            AggregateTransformationNode aggregate = Assert.IsType<AggregateTransformationNode>(Assert.Single(actual.Transformations));

            Assert.Equal(TransformationNodeKind.Aggregate, aggregate.Kind);
            Assert.NotNull(aggregate.AggregateExpressions);

            AggregateExpression statement = Assert.IsType<AggregateExpression>(Assert.Single(aggregate.AggregateExpressions));
            VerifyIsFakeSingleValueNode(statement.Expression);
            Assert.Equal(AggregationMethod.Sum, statement.Method);
            Assert.Equal("TotalPrice", statement.Alias);
        }

        [Fact]
        public void BindApplyWithAverageInAggregateShouldReturnApplyClause()
        {
            IEnumerable<QueryToken> tokens = _parser.ParseApply("aggregate(UnitPrice with average as AveragePrice)");

            ApplyBinder binder = new ApplyBinder(FakeBindMethods.BindMethodReturningASingleFloatPrimitive, _bindingState);
            ApplyClause actual = binder.BindApply(tokens);

            Assert.NotNull(actual);
            AggregateTransformationNode aggregate = Assert.IsType<AggregateTransformationNode>(Assert.Single(actual.Transformations));

            Assert.Equal(TransformationNodeKind.Aggregate, aggregate.Kind);
            Assert.NotNull(aggregate.AggregateExpressions);

            AggregateExpression statement = Assert.IsType<AggregateExpression>(Assert.Single(aggregate.AggregateExpressions));
            Assert.NotNull(statement.Expression);
            Assert.Same(FakeBindMethods.FakeSingleFloatPrimitive, statement.Expression);
            Assert.Equal(AggregationMethod.Average, statement.Method);
            Assert.Equal("AveragePrice", statement.Alias);
        }

        [Fact]
        public void BindApplyWithCountInAggregateShouldReturnApplyClause()
        {
            IEnumerable<QueryToken> tokens = _parser.ParseApply("aggregate($count as TotalCount)");

            ApplyBinder binder = new ApplyBinder(FakeBindMethods.BindSingleComplexProperty, _bindingState);
            ApplyClause actual = binder.BindApply(tokens);

            Assert.NotNull(actual);
            AggregateTransformationNode aggregate = Assert.IsType<AggregateTransformationNode>(Assert.Single(actual.Transformations));

            Assert.Equal(TransformationNodeKind.Aggregate, aggregate.Kind);
            Assert.NotNull(aggregate.AggregateExpressions);
            AggregateExpressionBase statementBase = Assert.Single(aggregate.AggregateExpressions);
            AggregateExpression statement = Assert.IsType<AggregateExpression>(statementBase);

            Assert.Equal(AggregationMethod.VirtualPropertyCount, statement.Method);
            Assert.Equal("TotalCount", statement.Alias);
        }

        [Fact]
        public void BindApplyWithAggregateAndFilterShouldReturnApplyClause()
        {
            IEnumerable<QueryToken> tokens = _parser.ParseApply("aggregate(StockQuantity with sum as TotalPrice)/filter(TotalPrice eq 100)");
            MetadataBinder metadataBiner = new MetadataBinder(_bindingState);
            ApplyBinder binder = new ApplyBinder(metadataBiner.Bind, _bindingState);
            ApplyClause actual = binder.BindApply(tokens);

            Assert.NotNull(actual);
            Assert.Equal(2, actual.Transformations.Count());

            List<TransformationNode> transformations = actual.Transformations.ToList();
            Assert.NotNull(transformations[1]);
            FilterTransformationNode filter = Assert.IsType<FilterTransformationNode>(transformations[1]);

            Assert.Equal(TransformationNodeKind.Filter, filter.Kind);

            FilterClause filterClause = filter.FilterClause;
            Assert.NotNull(filterClause.Expression);
            BinaryOperatorNode binaryOperation = Assert.IsType<BinaryOperatorNode>(filterClause.Expression);

            Assert.NotNull(binaryOperation.Left);
            ConvertNode propertyConvertNode = Assert.IsType<ConvertNode>(binaryOperation.Left);
            Assert.NotNull(propertyConvertNode.Source);
            SingleValueOpenPropertyAccessNode propertyAccess = Assert.IsType< SingleValueOpenPropertyAccessNode>(propertyConvertNode.Source);
            Assert.Equal("TotalPrice", propertyAccess.Name);
        }

        [Fact]
        public void BindApplyWitGroupByShouldReturnApplyClause()
        {
            IEnumerable<QueryToken> tokens = _parser.ParseApply("groupby((UnitPrice, SalePrice))");

            ApplyBinder binder = new ApplyBinder(FakeBindMethods.BindSingleComplexProperty, _bindingState);
            ApplyClause actual = binder.BindApply(tokens);

            Assert.NotNull(actual);

            GroupByTransformationNode groupBy = Assert.IsType<GroupByTransformationNode>(Assert.Single(actual.Transformations));

            Assert.Equal(TransformationNodeKind.GroupBy, groupBy.Kind);
            Assert.NotNull(groupBy.GroupingProperties);
            Assert.Equal(2, groupBy.GroupingProperties.Count());

            List<GroupByPropertyNode> groupingProperties = groupBy.GroupingProperties.ToList();
            VerifyIsFakeSingleValueNode(groupingProperties[0].Expression);
            VerifyIsFakeSingleValueNode(groupingProperties[1].Expression);

            Assert.Null(groupBy.ChildTransformations);
        }

Microsoft.OData.UriParser.Aggregation.ComputeTransformationNode : TransformationNode

Constructors :

public ComputeTransformationNode(IEnumerable<ComputeExpression> expressions = )

Methods :

public IEnumerable<ComputeExpression> get_Expressions()
public TransformationNodeKind get_Kind()
public Type GetType()
public String ToString()
public Boolean Equals(Object obj = )
public Int32 GetHashCode()