public void TestOverwritable() { ConstantRegistry registry = new ConstantRegistry(false); registry.RegisterConstant("test", 42.0); registry.RegisterConstant("test", 26.3); }
public void TestNotOverwritable() { ConstantRegistry registry = new ConstantRegistry(false); registry.RegisterConstant("test", 42.0, false); AssertExtensions.ThrowsException <Exception>(() => { registry.RegisterConstant("test", 26.3, false); }); }
/// <summary> /// Build a .NET func for the provided formula. /// </summary> /// <param name="formulaText">The formula that must be converted into a .NET func.</param> /// <param name="constants">Constant values for variables defined into the formula. They variables will be replaced by the constant value at pre-compilation time.</param> /// <returns>A .NET func for the provided formula.</returns> public Func <IDictionary <string, double>, double> Build(string formulaText, IDictionary <string, double> constants) { if (string.IsNullOrEmpty(formulaText)) { throw new ArgumentNullException("formulaText"); } ConstantRegistry compiledConstants = new ConstantRegistry(caseSensitive); if (constants != null) { foreach (var constant in constants) { compiledConstants.RegisterConstant(constant.Key, constant.Value); } } if (IsInFormulaCache(formulaText, compiledConstants, out var result)) { return(result); } else { Operation operation = BuildAbstractSyntaxTree(formulaText, compiledConstants); return(BuildFormula(formulaText, compiledConstants, operation)); } }
public void TestAddConstant() { ConstantRegistry registry = new ConstantRegistry(false); registry.RegisterConstant("test", 42.0); ConstantInfo functionInfo = registry.GetConstantInfo("test"); Assert.IsNotNull(functionInfo); Assert.AreEqual("test", functionInfo.ConstantName); Assert.AreEqual(42.0, functionInfo.Value); }
private void RegisterDefaultConstants() { ConstantRegistry.RegisterConstant("e", Math.E, false); ConstantRegistry.RegisterConstant("pi", Math.PI, false); }
/// <summary> /// Add a constant to the calculation engine. /// </summary> /// <param name="constantName">The name of the constant. This name can be used in mathematical formulas.</param> /// <param name="value">The value of the constant.</param> public void AddConstant(string constantName, ExecutionResult value) { ConstantRegistry.RegisterConstant(constantName, value); }
/// <summary> /// Add a constant to the calculation engine. /// </summary> /// <param name="constantName">The name of the constant. This name can be used in mathematical formulas.</param> /// <param name="value">The value of the constant.</param> public void AddConstant(string constantName, double value) { ConstantRegistry.RegisterConstant(constantName, value); }