AlphaX.FormulaEngine
3.1.0
This package has been migrated.
See the version list below for details.
dotnet add package AlphaX.FormulaEngine --version 3.1.0
NuGet\Install-Package AlphaX.FormulaEngine -Version 3.1.0
<PackageReference Include="AlphaX.FormulaEngine" Version="3.1.0" />
<PackageVersion Include="AlphaX.FormulaEngine" Version="3.1.0" />
<PackageReference Include="AlphaX.FormulaEngine" />
paket add AlphaX.FormulaEngine --version 3.1.0
#r "nuget: AlphaX.FormulaEngine, 3.1.0"
#:package AlphaX.FormulaEngine@3.1.0
#addin nuget:?package=AlphaX.FormulaEngine&version=3.1.0
#tool nuget:?package=AlphaX.FormulaEngine&version=3.1.0
AlphaX.FormulaEngine
A robust, extensible, and blazing fast engine to parse and evaluate formulas dynamically. Built on top of AlphaX.Parserz, it seamlessly supports both natively provided formulas and your own custom logic.
🔗 AlphaX.FormulaEngine GitHub Repo | 💬 Feedback & Queries
🚀 What's New in v3.1.0
We're thrilled to introduce massive improvements to AlphaX.FormulaEngine!
- 14 New Native Formulas: Added full support for
MIN,MAX,POWER,ROUND,SQRT,TRIM,SUBSTRING,INDEXOF,COALESCE,ISNUMBER,ISSTRING,INDEX,JOIN,COUNT,YEAR,MONTH, andDAY. - Enhanced Arithmetic Engine: Flawless nested evaluation with accurate left-associative arithmetic (e.g.
1+1-(1+2)). Zero recursive timeout errors on deeply nested syntax! - Complete Developer Reference: Be sure to check out our overhauled Formulas.md reference.
⚡ Quick Start
You can initialize the engine and evaluate expressions either synchronously or asynchronously:
using AlphaX.FormulaEngine;
AlphaXFormulaEngine engine = new AlphaXFormulaEngine();
// Synchronous Evaluation
IEvaluationResult resultSync = engine.Evaluate("SUM(1, 2, 12.3, 5.9)");
Console.WriteLine(resultSync.Value); // 21.2
// Asynchronous Evaluation
IEvaluationResult resultAsync = await engine.EvaluateAsync("SUM(1, 2, 12.3, 5.9)");
Console.WriteLine(resultAsync.Value); // 21.2
Pro Tip: Formulas can naturally nest! Feel free to parse deep trees natively:
(SUM(1,2) + AVERAGE(5,10)) * 10.
📚 Inbuilt Formulas
AlphaX.FormulaEngine ships with a wide array of powerful formulas right out of the box:
- 🧮 Arithmetic:
SUM,AVERAGE,FLOOR,ROUND,MIN,MAX,POWER,SQRT - 🔤 String:
LOWER,UPPER,TEXTSPLIT,CONCAT,LENGTH,TRIM,SUBSTRING,INDEXOF - 📅 DateTime:
TODAY,NOW,YEAR,MONTH,DAY - 🧠 Logical:
EQUALS,GREATERTHAN,OR,AND,IF,COALESCE,ISNUMBER,ISSTRING - 📦 Array:
ARRAYCONTAINS,ARRAYINCLUDES,INDEX,JOIN,COUNT
👉 Click here to see the full list of inbuilt formulas and examples
🛠 Creating Your Own Formulas
AlphaX.FormulaEngine provides maximum flexibility to write and integrate your own custom logic effortlessly.
1. Create a Formula Class
Inherit from AlphaX.FormulaEngine.Formula. Below is a custom StartsWith formula implementation:
public class StartsWithFormula : AlphaX.FormulaEngine.Formula
{
public StartsWithFormula() : base("StartsWith") { }
public override object Evaluate(IFormulaContext context)
{
// Throws error if argument count doesn't match
ValidateArgumentCount(context.Args);
// Throws error if 0th/1st arguments aren't strings
string source = context.GetStringArg(0);
string value = context.GetStringArg(1);
// Safely retrieves the 3rd argument, or defaults to false
context.TryGetArg(2, out bool matchCase);
return source.StartsWith(value, matchCase ? StringComparison.Ordinal : StringComparison.InvariantCultureIgnoreCase);
}
protected override FormulaInfo GetFormulaInfo()
{
FormulaInfo info = new FormulaInfo(Name)
{
Description = "Checks if the provided string starts with the specified value."
};
// Define arguments for function documentation/validation
info.AddArgument(new StringArgument("source", true) { Description = "The source string." });
info.AddArgument(new StringArgument("value", true) { Description = "The value to check for." });
info.AddArgument(new BooleanArgument("matchCase", false) { Description = "Match case while checking." });
return info;
}
}
2. Register & Evaluate
Simply add your formula to the FormulaStore and it is immediately ready for use!
AlphaXFormulaEngine engine = new AlphaXFormulaEngine();
engine.FormulaStore.Add(new StartsWithFormula());
var result1 = engine.Evaluate("StartsWith(\"This is test\", \"This\")");
Console.WriteLine(result1.Value); // true
var result2 = engine.Evaluate("StartsWith(\"This is test\", \"hello\")");
Console.WriteLine(result2.Value); // false
Async Formulas: To implement asynchronous logic (e.g. hitting an API inside a formula), inherit from
AlphaX.FormulaEngine.AsyncFormulaand override theEvaluateAsyncmethod.
⚙️ Advanced Configuration
AlphaXFormulaEngine allows you to configure the engine to fit your exact domain needs.
1. Toggle String Quotes
By default, strings are parsed with double quotes ("text"). You can toggle this to accept single quotes ('text') by updating the engine settings:
engine.ApplySettings(new EngineSettings()
{
DoubleQuotedStrings = false
});
2. Logical Operator Modes
You can configure the engine to parse query-like operators (eq instead of =) via LogicalOperatorMode.
engine.ApplySettings(new EngineSettings()
{
LogicalOperatorMode = LogicalOperatorMode.Query
});
Query Mode Operators:
=→eq|!=→ne<→lt|>→gt<=→le|>=→ge&&→and|||→or
3. Parsing Optimization Order
You can manually sequence the type resolution tree (e.g. parse Numbers before Strings) to drastically improve performance if you know your data bounds:
ParseOrder order = new ParseOrder(ParseType.Number);
order.Add(ParseType.String);
order.Add(ParseType.Boolean);
engine.ApplySettings(new EngineSettings() { EngineParseOrder = order });
🎯 Variables (Custom Names)
AlphaXFormulaEngine allows you to inject dynamic variables (prefixed with $) directly into expressions by providing a custom IEngineContext.
public class TestEngineContext : IEngineContext
{
public async Task<object> Resolve(string key)
{
return key switch
{
"UserId" => 1024,
"Role" => "Admin",
_ => throw new Exception("Invalid custom name")
};
}
}
// Pass the context into the Engine constructor
AlphaXFormulaEngine engine = new AlphaXFormulaEngine(new TestEngineContext());
// Expressions resolve variables at runtime
IEvaluationResult result = engine.Evaluate("EQUALS($UserId, 1024)");
Console.WriteLine(result.Value); // true
🔗 Sequenced / Chained Expressions
Evaluating massive, complicated expression walls can be extremely difficult to read or debug (e.g. SUM(1, 2, AVERAGE(1, 2, SUM(1, 2, 12)))).
AlphaX provides a SequencedExpressionBuilder to break these down natively into readable variables:
var engine = new AlphaXFormulaEngine();
var expression = SequencedExpressionBuilder
.Create("Step1", "SUM(1, 2, 12)")
.Next("Step2", "AVERAGE(1, 2, $Step1)")
.Next("Final", "SUM(1, 2, $Step2)");
var result = engine.Evaluate(expression);
// Final Result evaluates properly by cascading through the sequenced variables!
Built by developers, for developers 😃
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
| .NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen40 was computed. tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- AlphaX.Parserz (>= 2.1.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated | |
|---|---|---|---|
| 3.4.0 | 163 | 7/21/2026 | |
| 3.3.1 | 121 | 7/20/2026 | |
| 3.3.0 | 134 | 7/20/2026 | |
| 3.2.0 | 128 | 7/20/2026 | |
| 3.1.1 | 134 | 7/17/2026 | |
| 3.1.0 | 128 | 7/17/2026 | |
| 3.0.0 | 332 | 10/27/2025 | |
| 2.4.0 | 1,323 | 10/3/2024 | |
| 2.3.0 | 808 | 5/29/2023 | |
| 2.2.1 | 703 | 5/23/2023 | |
| 2.2.0 | 692 | 5/19/2023 | |
| 2.1.0 | 551 | 5/18/2023 | |
| 2.0.2 | 649 | 5/18/2023 | |
| 2.0.1 | 683 | 5/17/2023 | |
| 2.0.0 | 679 | 5/16/2023 | |
| 1.0.8 | 578 | 5/15/2023 | |
| 1.0.7 | 675 | 3/2/2023 | |
| 1.0.5 | 766 | 12/13/2022 | |
| 1.0.4.4 | 766 | 12/6/2022 |