Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2990_VariableImpactBasedFeatureSelection/HeuristicLab.ExtLibs/HeuristicLab.EPPlus/4.0.3/EPPlus-4.0.3/FormulaParsing/ExpressionGraph/FunctionExpression.cs

Last change on this file was 12074, checked in by sraggl, 10 years ago

#2341: Added EPPlus-4.0.3 to ExtLibs

File size: 5.7 KB
Line 
1/*******************************************************************************
2 * You may amend and distribute as you like, but don't remove this header!
3 *
4 * EPPlus provides server-side generation of Excel 2007/2010 spreadsheets.
5 * See http://www.codeplex.com/EPPlus for details.
6 *
7 * Copyright (C) 2011  Jan Källman
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
17 * See the GNU Lesser General Public License for more details.
18 *
19 * The GNU Lesser General Public License can be viewed at http://www.opensource.org/licenses/lgpl-license.php
20 * If you unfamiliar with this license or have questions about it, here is an http://www.gnu.org/licenses/gpl-faq.html
21 *
22 * All code and executables are provided "as is" with no warranty either express or implied.
23 * The author accepts no liability for any damage or loss of business that this product may cause.
24 *
25 * Code change notes:
26 *
27 * Author             Change            Date
28 * ******************************************************************************
29 * Mats Alm                       Added                   2013-03-01 (Prior file history on https://github.com/swmal/ExcelFormulaParser)
30 *******************************************************************************/
31using System;
32using System.Collections.Generic;
33using System.Globalization;
34using System.Linq;
35using System.Text;
36using OfficeOpenXml.FormulaParsing.Excel;
37using OfficeOpenXml.FormulaParsing.Excel.Functions;
38using OfficeOpenXml.FormulaParsing.Exceptions;
39using OfficeOpenXml.FormulaParsing.ExpressionGraph.FunctionCompilers;
40using OfficeOpenXml.Utils;
41
42namespace OfficeOpenXml.FormulaParsing.ExpressionGraph
43{
44    /// <summary>
45    /// Expression that handles execution of a function.
46    /// </summary>
47    public class FunctionExpression : AtomicExpression
48    {
49        /// <summary>
50        /// Constructor
51        /// </summary>
52        /// <param name="expression">should be the of the function</param>
53        /// <param name="parsingContext"></param>
54        /// <param name="isNegated">True if the numeric result of the function should be negated.</param>
55        public FunctionExpression(string expression, ParsingContext parsingContext, bool isNegated)
56            : base(expression)
57        {
58            _parsingContext = parsingContext;
59            _functionCompilerFactory = new FunctionCompilerFactory(parsingContext.Configuration.FunctionRepository);
60            _isNegated = isNegated;
61            base.AddChild(new FunctionArgumentExpression(this));
62        }
63
64        private readonly ParsingContext _parsingContext;
65        private readonly FunctionCompilerFactory _functionCompilerFactory;
66        private readonly bool _isNegated;
67
68
69        public override CompileResult Compile()
70        {
71            try
72            {
73                var function = _parsingContext.Configuration.FunctionRepository.GetFunction(ExpressionString);
74                if (function == null)
75                {
76                    if (_parsingContext.Debug)
77                    {
78                        _parsingContext.Configuration.Logger.Log(_parsingContext, string.Format("'{0}' is not a supported function", ExpressionString));
79                    }
80                    return new CompileResult(ExcelErrorValue.Create(eErrorType.Name), DataType.ExcelError);
81                }
82                if (_parsingContext.Debug)
83                {
84                    _parsingContext.Configuration.Logger.LogFunction(ExpressionString);
85                }
86                var compiler = _functionCompilerFactory.Create(function);
87                var result = compiler.Compile(HasChildren ? Children : Enumerable.Empty<Expression>(), _parsingContext);
88                if (_isNegated)
89                {
90                    if (!result.IsNumeric)
91                    {
92                        if (_parsingContext.Debug)
93                        {
94                            var msg = string.Format("Trying to negate a non-numeric value ({0}) in function '{1}'",
95                                result.Result, ExpressionString);
96                            _parsingContext.Configuration.Logger.Log(_parsingContext, msg);
97                        }
98                        return new CompileResult(ExcelErrorValue.Create(eErrorType.Value), DataType.ExcelError);
99                    }
100                    return new CompileResult(result.ResultNumeric * -1, result.DataType);
101                }
102                return result;
103            }
104            catch (ExcelErrorValueException e)
105            {
106                if (_parsingContext.Debug)
107                {
108                    _parsingContext.Configuration.Logger.Log(_parsingContext, e);
109                }
110                return new CompileResult(e.ErrorValue, DataType.ExcelError);
111            }
112           
113        }
114
115        public override Expression PrepareForNextChild()
116        {
117            return base.AddChild(new FunctionArgumentExpression(this));
118        }
119
120        public override bool HasChildren
121        {
122            get
123            {
124                return (Children.Any() && Children.First().Children.Any());
125            }
126        }
127
128        public override Expression AddChild(Expression child)
129        {
130            Children.Last().AddChild(child);
131            return child;
132        }
133    }
134}
Note: See TracBrowser for help on using the repository browser.