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

Conversation

mibe
Copy link
Contributor

@mibe mibe commented May 13, 2016

Add the ability to remove functions & constants from the CalculationEngine.

@mibe
Copy link
Contributor Author

mibe commented Jul 11, 2018

I'm using "a" to "z" as variables. To achieve this I had to remove "e" as the Euler's number constant. And since I was already into it, I implemented a method for removing functions, too.

Since I'm currently using version 0.8.7, I implemented a reflection hack for this again:

/// <summary>
/// Remove a constant of the calculation engine.
/// </summary>
/// <param name="engine">Instance of the engine</param>
/// <param name="constantName">Name of the constant</param>
/// <returns>TRUE if the constant was removed successfully.</returns>
public static bool RemoveConstant(CalculationEngine engine, string constantName)
{
	// Retrieve ConstantRegistry
	ConstantRegistry registry = (ConstantRegistry)getProperty(engine, "ConstantRegistry");

	// Retrieve & invoke method for converting the constant name
	MethodInfo method = registry.GetType().GetMethod("ConvertConstantName", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.InvokeMethod);
	constantName = (String)method.Invoke(registry, new object[] { constantName });

	// Retrieve internal Dictionary
	Dictionary<string, ConstantInfo> constants = (Dictionary<string, ConstantInfo>)getField(registry, "constants");

	return constants.Remove(constantName);
}

private static object getProperty(object @object, string propertyName)
{
	PropertyInfo property = @object.GetType().GetProperty(propertyName, BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.GetProperty);
	return property.GetValue(@object);
}

private static object getField(object @object, string fieldName)
{
	FieldInfo field = @object.GetType().GetField(fieldName, BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.GetField);
	return field.GetValue(@object);
}

@pieterderycke
Copy link
Owner

Jace 0.9 now has a parameter in the constructor of CalculcationEngine to prevent loading the default contstants

@mibe
Copy link
Contributor Author

mibe commented Jul 21, 2018

I saw that, thanks. Still, removing unwanted constants and functions is a nice feature, IMHO.

@pieterderycke
Copy link
Owner

The main issue, I see is that it might mess up the cache of generated formulas if some functions are suddenly gone. But I will give the request an additional thought.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants