diff --git a/Jace.Tests/ConstantRegistryTests.cs b/Jace.Tests/ConstantRegistryTests.cs index 005ffea..4232897 100644 --- a/Jace.Tests/ConstantRegistryTests.cs +++ b/Jace.Tests/ConstantRegistryTests.cs @@ -54,5 +54,16 @@ public void TestNotOverwritable() registry.RegisterConstant("test", 26.3, false); }); } + + [TestMethod] + public void TestRemoveConstant() + { + ConstantRegistry registry = new ConstantRegistry(false); + + registry.RegisterConstant("test", 42.0); + bool result = registry.UnregisterConstant("test"); + + Assert.IsTrue(result); + } } } diff --git a/Jace.Tests/FunctionRegistryTests.cs b/Jace.Tests/FunctionRegistryTests.cs index 71f6e8a..4c42871 100644 --- a/Jace.Tests/FunctionRegistryTests.cs +++ b/Jace.Tests/FunctionRegistryTests.cs @@ -61,5 +61,17 @@ public void TestNotOverwritable() registry.RegisterFunction("test", testFunction2, false); }); } + + [TestMethod] + public void TestRemoveFunc() + { + FunctionRegistry registry = new FunctionRegistry(false); + + Func testFunction = (a, b) => a * b; + registry.RegisterFunction("test", testFunction); + bool result = registry.UnregisterFunction("test"); + + Assert.IsTrue(result); + } } } diff --git a/Jace.Tests/Mocks/MockFunctionRegistry.cs b/Jace.Tests/Mocks/MockFunctionRegistry.cs index 2ad2c09..5e268d2 100644 --- a/Jace.Tests/Mocks/MockFunctionRegistry.cs +++ b/Jace.Tests/Mocks/MockFunctionRegistry.cs @@ -44,5 +44,10 @@ public void RegisterFunction(string functionName, int numberOfParameters) { throw new NotImplementedException(); } + + public bool UnregisterFunction(string functionName) + { + throw new NotImplementedException(); + } } } diff --git a/Jace/CalculationEngine.cs b/Jace/CalculationEngine.cs index dffa1e7..9e45ec1 100644 --- a/Jace/CalculationEngine.cs +++ b/Jace/CalculationEngine.cs @@ -161,6 +161,7 @@ public Func, double> Build(string formulaText) } } + #region Functions & constants /// /// Add a function to the calculation engine. /// @@ -211,6 +212,16 @@ public void AddFunction(string functionName, Func + /// Remove a function from the calculation engine. + /// + /// The name of the function + /// TRUE if the function was successfully removed. + public bool RemoveFunction(string functionName) + { + return FunctionRegistry.UnregisterFunction(functionName); + } + #if !WINDOWS_PHONE_7 /// /// Add a function to the calculation engine. @@ -243,6 +254,16 @@ public void AddConstant(string constantName, double value) ConstantRegistry.RegisterConstant(constantName, value); } + /// + /// Remove a constant from the calculation engine. + /// + /// The name of the constant + /// TRUE if the constant was successfully removed. + public bool RemoveConstant(string constantName) + { + return ConstantRegistry.UnregisterConstant(constantName); + } + private void RegisterDefaultFunctions() { FunctionRegistry.RegisterFunction("sin", (Func)((a) => Math.Sin(a)), false); @@ -278,6 +299,7 @@ private void RegisterDefaultConstants() ConstantRegistry.RegisterConstant("e", Math.E, false); ConstantRegistry.RegisterConstant("pi", Math.PI, false); } + #endregion /// /// Build the abstract syntax tree for a given formula. The formula string will diff --git a/Jace/Execution/ConstantRegistry.cs b/Jace/Execution/ConstantRegistry.cs index 7db7988..7b9766f 100644 --- a/Jace/Execution/ConstantRegistry.cs +++ b/Jace/Execution/ConstantRegistry.cs @@ -70,6 +70,16 @@ public void RegisterConstant(string constantName, double value, bool isOverWrita constants.Add(constantName, constantInfo); } + public bool UnregisterConstant(string constantName) + { + if (string.IsNullOrEmpty(constantName)) + throw new ArgumentNullException("constantName"); + + constantName = ConvertConstantName(constantName); + + return this.constants.Remove(constantName); + } + private string ConvertConstantName(string constantName) { return caseSensitive ? constantName : constantName.ToLowerInvariant(); diff --git a/Jace/Execution/FunctionRegistry.cs b/Jace/Execution/FunctionRegistry.cs index 99aefbc..5b43167 100644 --- a/Jace/Execution/FunctionRegistry.cs +++ b/Jace/Execution/FunctionRegistry.cs @@ -88,6 +88,16 @@ public bool IsFunctionName(string functionName) return functions.ContainsKey(ConvertFunctionName(functionName)); } + public bool UnregisterFunction(string functionName) + { + if (string.IsNullOrEmpty(functionName)) + throw new ArgumentNullException("functionName"); + + functionName = ConvertFunctionName(functionName); + + return this.functions.Remove(functionName); + } + private string ConvertFunctionName(string functionName) { return caseSensitive ? functionName : functionName.ToLowerInvariant(); diff --git a/Jace/Execution/IConstantRegistry.cs b/Jace/Execution/IConstantRegistry.cs index 88823c1..27ed9f0 100644 --- a/Jace/Execution/IConstantRegistry.cs +++ b/Jace/Execution/IConstantRegistry.cs @@ -11,5 +11,6 @@ public interface IConstantRegistry : IEnumerable bool IsConstantName(string constantName); void RegisterConstant(string constantName, double value); void RegisterConstant(string constantName, double value, bool isOverWritable); + bool UnregisterConstant(string constantName); } } diff --git a/Jace/Execution/IFunctionRegistry.cs b/Jace/Execution/IFunctionRegistry.cs index 4699aae..c0b9e05 100644 --- a/Jace/Execution/IFunctionRegistry.cs +++ b/Jace/Execution/IFunctionRegistry.cs @@ -8,5 +8,6 @@ public interface IFunctionRegistry bool IsFunctionName(string functionName); void RegisterFunction(string functionName, Delegate function); void RegisterFunction(string functionName, Delegate function, bool isOverWritable); + bool UnregisterFunction(string functionName); } }