This is a lightweight expression evaluation library that allows you to validate, evaluate, and compute mathematical expressions with support for variables.
License
—
Deps
0
Install Size
—
Vulns
✓ 0
Published
May 30, 2023
$ dotnet add package OchoaLopes.ExprEngineExprEngine is a lightweight expression evaluation library that allows you to validate, evaluate, and compute mathematical expressions with support for variables.
The ExprEngine library can be installed via NuGet. Use the package manager console or the NuGet package manager UI in Visual Studio to search for and install the package.
Install-Package OchoaLopes.ExprEngine
ExprEngine supports a few formats as input parameters in expressions:
ExprEngine provides support for date operations, making it flexible and adaptable to a variety of scenarios. It allows for adding or subtracting days from a date and comparing dates.
string expression = ":birthdate + 1 <= '05/01/2001't";
string[] formats = { "yyyy-MM-dd", "dd/MM/yyyy", "MM/dd/yyyy", "yyyy/MM/dd" };
To use variable you have to add a placeholder in your expression, then you can use a dictionary or an ordered list of values, this is available for ValidateExpression, EvaluateExpression and ComputeExpression.
string expression = ":firstVariable * 10i <= :secondVariable";
You can performe some validations or comparions with strings using key-word "like" or "not like".
string expression = ":firstVariable * 10i <= :secondVariable";
ExprEngine supports comparisons with operators like !, ==, !=, >, <, >=, <=, and, or, is, is not, like, not like. For example, :birthDate > :otherDate.
Please note that the date format and operations depend on the CultureInfo passed to the ExpressionService constructor or by the methods.
You can also use both, you can pass by constructor and then you are going to set the default CultureInfo, but if you want to use for a specific method a different one you can pass another CultureInfo. If you don't pass any CultureInfo the default is CultureInfo.CurrentInfo.
This will influence the following:
using OchoaLopes.ExprEngine;
ExpressionService:var expressionService = new ExpressionService();
var expressionService = new ExpressionService(new CultureInfo("en-US"));
string expression = "(:x + 5i) > 10i";
var variables = new Dictionary<string, object>
{
{ "x", 7 }
};
bool isValid = expressionService.ValidateExpression(expression, variables);
string expression = "(:x > :y) && (:z < :w)";
var values = new List<object> { 2, 4, 10, 5 }; // x=2, y=4, z=10, w=5
bool result = expressionService.EvaluateExpression(expression, values);
string expression = "(:x + 5i) * (3.14f - :y)";
var variables = new Dictionary<string, object>
{
{ "x", 7 },
{ "y", 2.5 }
};
object result = expressionService.ComputeExpression(expression, variables, CultureInfo.InvariantCulture);
In addition to the basic expression evaluation functionality, the ExprEngine library provides the following methods:
bool ValidateExpression(string expression, CultureInfo? cultureInfo = null): Validates the syntax and structure of an expression, considering the specified culture information.
bool ValidateExpression(string expression, IDictionary<string, object> variables, CultureInfo? cultureInfo = null): Validates the syntax and structure of an expression with the help of a dictionary of variables and their values, considering the specified culture information.
bool ValidateExpression(string expression, IList<object> values, CultureInfo? cultureInfo = null): Validates the syntax and structure of an expression with the help of a list of values for indexed placeholders, considering the specified culture information.
bool EvaluateExpression(string expression, CultureInfo? cultureInfo = null): Evaluates the expression and returns a boolean result, considering the specified culture information.
bool EvaluateExpression(string expression, IDictionary<string, object> variables, CultureInfo? cultureInfo = null): Evaluates the expression and returns a boolean result with the help of a dictionary of variables and their values, considering the specified culture information.
bool EvaluateExpression(string expression, IList<object> values, CultureInfo? cultureInfo = null): Evaluates the expression and returns a boolean result with the help of a list of values for indexed placeholders, considering the specified culture information.
object ComputeExpression(string expression, CultureInfo? cultureInfo = null): Evaluates and computes the expression, returning the result, considering the specified culture information.
object ComputeExpression(string expression, IDictionary<string, object> variables, CultureInfo? cultureInfo = null): Evaluates and computes the expression, returning the result with the help of a dictionary of variables and their values, considering the specified culture information.
object ComputeExpression(string expression, IList<object> values, CultureInfo? cultureInfo = null): Evaluates and computes the expression, returning the result with the help of a list of values for indexed placeholders, considering the specified culture information.
While ExprEngine provides a range of expression evaluation capabilities, it does have some limitations:
The library supports a subset of mathematical and logical operations, including addition, subtraction, multiplication, division, comparison and logical operation.
The library only supports variables as input parameters. It does not provide facilities for defining custom functions or complex expressions.
ExprEngine is designed for simple arithmetic and logical expressions and may not be suitable for complex symbolic or mathematical computations.
The library may have performance limitations when dealing with large expressions or complex calculations. It is recommended to benchmark and profile your specific use cases to ensure satisfactory performance.
We welcome your feedback and contributions to the ExprEngine library. If you have any questions, suggestions, or issues, please feel free to open an issue on the GitHub repository. We appreciate your feedback and strive to continuously improve the library.
The ExprEngine library is licensed under the Apache License 2.0. You are free to use, modify, and distribute this library in accordance with the terms of the license. Please see the LICENSE file for more details.