Skip to content
mrcope edited this page Apr 17, 2020 · 7 revisions

What does it do?

Jace.NET can interprete and execute strings containing mathematical formulas. These formulas can rely on variables. If variables are used, values can be provided for these variables at execution time of the mathematical formula.

Jace can execute formulas in two modes: in interpreted mode and in a dynamic compilation mode. If dynamic compilation mode is used, Jace will create a dynamic method at runtime and will generate the necessary MSIL opcodes for native execution of the formula. If a formula is re-executed with other variables, Jace will take the dynamically generated method from its cache. It is recommended to use Jace in dynamic compilation mode.

How do I use it?

The easiest way to start using Jace.NET in your project is to use NuGet. Right click on your project in Visual Studio and choose “Manage NuGet Packages”. Search for Jace.NET in the NuGet official package source. Jace can be used in all .NET, Windows Store, Windows Phone 7 and Windows Phone 8 applications.

To start using the Jace.NET calculation engine, create a new instance of the calculation engine class. This class can be used in a couple of ways.

Option 1: To directly execute a given mathematical formula using the provided variables:

Dictionary<string, double> variables = new Dictionary<string, double>();
variables.Add("var1", 2.5);
variables.Add("var2", 3.4);

CalculationEngine engine = new CalculationEngine();
double result = engine.Calculate("var1*var2", variables);

Option 2: To build a .NET Func accepting a dictionary as input containing the values for each variable:

CalculationEngine engine = new CalculationEngine();
Func<Dictionary<string, double>, double> formula = engine.Build("var1+2/(3*otherVariable)");

Dictionary<string, double> variables = new Dictionary<string, double>();
variables.Add("var1", 2);
variables.Add("otherVariable", 4.2);
	
double result = formula(variables);

Option 3: To build a typed .NET Func:

CalculationEngine engine = new CalculationEngine();
Func<int, double, double> formula = (Func<int, double, double>)engine.Formula("var1+2/(3*otherVariable)")
	.Parameter("var1", DataType.Integer)
    .Parameter("otherVariable", DataType.FloatingPoint)
    .Result(DataType.FloatingPoint)
    .Build();
	
double result = formula(2, 4.2);
Clone this wiki locally