CalculationEngine is a useful tool for calculating complex formulas.
It's inspired by Jace.NET, and some code was taken from Jace.NET.
- Here is a simple piece of code that demonstrates what a CalculationEngine is.
// Define CalculationEngine var engine = new CalculationEngine(); // Calculate Formulas var answer = engine.Calculate("1 + 3"); var answer2 = engine.Calculate("3 + 5 * 9"); var answer3 = engine.Calculate("pow(3, 2) + (2 - 3)"); /****************** RESULT ******************/ // answer : 4 // answer2 : 48 // answer3 : 8
- Calculating String Constants And Variables
var engine = new CalculationEngine(); var variables = new Dictionary<string, object>() { { "code", "PRESENT" } }; var answer = engine.Calculate("if(CODE == \"PRESENT\", \"TRUE\", \"FALSE\")", variables); /****************** RESULT ******************/ // answer : "TRUE"
The CalculationEngine can access the object properties by PropertyConnector
- Point Class for formula
public class Point { public Point(string name, double x, double y) { Name = name; X = x; Y = y; } public string Name { get; set; } public double X { get; set; } public double Y { get; set; } }
- Implementing a PropertyConnector to access Point class properties
public class PointPropertyConnector : PropertyConnector { public PointPropertyConnector(bool caseSensitive) : base(caseSensitive) { } protected override object? OnGetPropertyValue(object target, string propertyName) { if (target is Point point) { if (propertyName.Equals(ConvertPropertyName(".Name"))) return point.Name; else if (propertyName.Equals(ConvertPropertyName(".X"))) return point.X; else if (propertyName.Equals(ConvertPropertyName(".Y"))) return point.Y; } return null; } }
- Calculate formulas with the Point class
var engine = new CalculationEngine(new CalculationEngineOptions() { PropertyConnectorFactory = caseSensitive => new PointPropertyConnector(caseSensitive) }); var variables = new Dictionary<string, object>() { { "p1", new Point("A", 0, 1) }, { "p2", new Point("B", 1.25, -2.56) }, { "Code", "A" } }; var answer = engine.Calculate("p1.x + p2.y", variables); var answer2 = engine.Calculate("if ( Code == p1.Name, \"True\", \"False\")", variables); /****************** RESULT ******************/ // answer : -2.56 // answer2 : "True"