Skip to content

Unregister functions & constants#21

Open
mibe wants to merge 2 commits into
pieterderycke:masterfrom
mibe:Unregister
Open

Unregister functions & constants#21
mibe wants to merge 2 commits into
pieterderycke:masterfrom
mibe:Unregister

Conversation

@mibe

@mibe mibe commented May 13, 2016

Copy link
Copy Markdown
Contributor

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

@mibe

mibe commented Jul 11, 2018

Copy link
Copy Markdown
Contributor Author

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
Copy Markdown
Owner

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

@mibe

mibe commented Jul 21, 2018

Copy link
Copy Markdown
Contributor Author

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

@pieterderycke

Copy link
Copy Markdown
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