XLite Expression Evaluator

Often, we run into a situation where we might have to work with expressions that come in as strings. If you wish to evaluate the string “1+2” to give 3 and “1+2*2” to give 5 and “(1+2)*2” to give 6 then XLite Eval is one of the easiest solutions. XLite Eval also provides you with a way to add your own functions to perform additional logic.

I developed XLite Eval to work in a way that is very similar to the way you write formulae in Microsoft Excel’s formula bar. View/Download this project on CodePlex at http://xliteeval.codeplex.com/

Idea behind the design

Excel’s formula bar is probably one of the most commonly used controls to enter mathematical formulate in today’s corporate world. Most of the people are already familiar with this or have people around them that can get them upto speed in a short time. Some of the advanced excel users are familiar with complex excel functions like STDVX(…), HLOOKUP(…), SUMIFS(…), etc., But most excel users have an idea of functions like SUM(…), AVG(…), MIN(…), MAX(…) etc., So creating an expression evaluator that uses the syntactic similarity to Excel greatly reduces the learning curve in using XL Lite Expression Evaluator.

Adding new functions

Let me use a lame example of a function called TIMES3 which always returns 3 times the input value. This lame function would take one input value and return a number. This can be easily added to XL Lite Expression Evaluator by adding the following class file:

ExpressionEvaluator.Lib/Core/Functions/Times3.cs

 

using System;
using System.Collections.Generic;

namespace SR.ExpressionEvaluator.Lib.Core.Functions
{
    public class Times3 : IFunction
    {
        List<object> _Args = new List<object>();

        public System.Collections.Generic.List<object> Args
        {
            get
            {
                return _Args;
            }
            set
            {
                _Args = value;
            }
        }

        public object Evaluate()
        {
            if (!Validate())
            {
                throw new Exception("TIMES3: Validation failed");
            }

            return 3 * Convert.ToDouble(Args.First());
        }

        public bool Validate()
        {
            if (Args.Count != 1)
            {
                return false;
            }
            return true;
        }

        public string GetDefinition()
        {
            return @"TIMES3(<value>)";
        }
    }
}

Using variables

In my view, being able to use variables makes the expressions completely dynamic and changing the values of the variables can give different values for the expression. Let’s assume we have the following expression “MY_VAR+10” where MY_VAR is a variable in the expression. We can create this and assign values using the following code:

IEvaluator eval;
string expression = "MY_VAR+10";
object result;

this.eval = Factory.GetEvaluator();
this.eval.Variables.Add("MY_VAR", 35);
result = eval.Evaluate(expression); 
// Yields result = 45

this.eval.Variables["MY_VAR"] = 22;
result = eval.Evaluate(expression); 
// Yields result = 32

Give XLite Eval a shot. Comment all you want on it.