Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unregister functions & constants #21

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions Jace.Tests/ConstantRegistryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
12 changes: 12 additions & 0 deletions Jace.Tests/FunctionRegistryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,17 @@ public void TestNotOverwritable()
registry.RegisterFunction("test", testFunction2, false);
});
}

[TestMethod]
public void TestRemoveFunc()
{
FunctionRegistry registry = new FunctionRegistry(false);

Func<double, double, double> testFunction = (a, b) => a * b;
registry.RegisterFunction("test", testFunction);
bool result = registry.UnregisterFunction("test");

Assert.IsTrue(result);
}
}
}
5 changes: 5 additions & 0 deletions Jace.Tests/Mocks/MockFunctionRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,10 @@ public void RegisterFunction(string functionName, int numberOfParameters)
{
throw new NotImplementedException();
}

public bool UnregisterFunction(string functionName)
{
throw new NotImplementedException();
}
}
}
22 changes: 22 additions & 0 deletions Jace/CalculationEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ public Func<Dictionary<string, double>, double> Build(string formulaText)
}
}

#region Functions & constants
/// <summary>
/// Add a function to the calculation engine.
/// </summary>
Expand Down Expand Up @@ -211,6 +212,16 @@ public void AddFunction(string functionName, Func<double, double, double, double
FunctionRegistry.RegisterFunction(functionName, function);
}

/// <summary>
/// Remove a function from the calculation engine.
/// </summary>
/// <param name="functionName">The name of the function</param>
/// <returns>TRUE if the function was successfully removed.</returns>
public bool RemoveFunction(string functionName)
{
return FunctionRegistry.UnregisterFunction(functionName);
}

#if !WINDOWS_PHONE_7
/// <summary>
/// Add a function to the calculation engine.
Expand Down Expand Up @@ -243,6 +254,16 @@ public void AddConstant(string constantName, double value)
ConstantRegistry.RegisterConstant(constantName, value);
}

/// <summary>
/// Remove a constant from the calculation engine.
/// </summary>
/// <param name="constantName">The name of the constant</param>
/// <returns>TRUE if the constant was successfully removed.</returns>
public bool RemoveConstant(string constantName)
{
return ConstantRegistry.UnregisterConstant(constantName);
}

private void RegisterDefaultFunctions()
{
FunctionRegistry.RegisterFunction("sin", (Func<double, double>)((a) => Math.Sin(a)), false);
Expand Down Expand Up @@ -278,6 +299,7 @@ private void RegisterDefaultConstants()
ConstantRegistry.RegisterConstant("e", Math.E, false);
ConstantRegistry.RegisterConstant("pi", Math.PI, false);
}
#endregion

/// <summary>
/// Build the abstract syntax tree for a given formula. The formula string will
Expand Down
10 changes: 10 additions & 0 deletions Jace/Execution/ConstantRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
10 changes: 10 additions & 0 deletions Jace/Execution/FunctionRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
1 change: 1 addition & 0 deletions Jace/Execution/IConstantRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ public interface IConstantRegistry : IEnumerable<ConstantInfo>
bool IsConstantName(string constantName);
void RegisterConstant(string constantName, double value);
void RegisterConstant(string constantName, double value, bool isOverWritable);
bool UnregisterConstant(string constantName);
}
}
1 change: 1 addition & 0 deletions Jace/Execution/IFunctionRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}