A fast library used to calculate a text based formula input all contained in a convienent serilizable object. Add your own custom functions and variables. Calculate Boolean, String and Float outputs.
$ dotnet add package SerializableFormulaSerializable Formula is a fast library used to calculate a text based formula input all contained in a convienent serializable object. Add your own custom functions and variables. Calculate Boolean, String and Float outputs.
Start by creating a simple SerializableFormula object, setting the desired result type and the formula text.
var newFormula = new SerializableFormula();
newFormula.FormulaResultType = TResultType.Float;
newformula.FormulaText = "Average(a, b, c, d, e)";
Now you have to define each part of the function. The SerializableFormula object above has no ability to process each function until you tell it what each part does.
Define your variables:
var a = new FormulaVariable("a", 4);
var b = new FormulaVariable("b", 2);
var c = new FormulaVariable("c", 7);
var d = new FormulaVariable("d", 10);
var e = new FormulaVariable("e", 20);
newFormula.AddVariable(a);
newFormula.AddVariable(b);
newFormula.AddVariable(c);
newFormula.AddVariable(d);
newFormula.AddVariable(e);
Define the Average function:
var average = new TAverageFunction();
newFormula.AddFunction(average);
The Average function is a custom function that comes bundled with the SerializableFormula. More about custom functions later.
Use the VerifySyntax() function to return an object SyntaxCheckResult which contains:
ExpressionError : A string containing a human friendly error messageExpressionErrorToken : A string with just the erroneous text tokenExpressionErrorPosition : A integer representing the position in the formula string where the error occursFinally, we can obtain our result by calling the function that matches our output type:
var result = newFormula.CalculateText();var result = newFormula.CalculateLogical();var result = newFormula.CalculateArithmetic();In order to create a custom function all you have to do is create a new object that implements IFormulaFunction.
Once implemented you can add your function to a SerializableFormula using the AddFunction() method.
These identifiers are always a part of SerializableFormula. They cannot be used to name Functions or Variables.
Boolean
orxorandtruefalsenotifcaseMath Operators
+-<<=>>=<>=/*^(),