Custom Function

Jan 11, 2008 at 2:17 PM
If I try to parse the following:

MyFunc(a,b)

Is there an event that I can hook into to tell me that this is custom function? This why I can handle processing it.

thanks

brian
Coordinator
Jan 11, 2008 at 8:18 PM
Define a custom function class:
public static class CustomFunctions
{
   public static int MyFunc(int a, int b)
   {
       return a + b;
   }
}

Import it into the expression and it will be called just like a regular function:
ExpressionContext context = new ExpressionContext();
context.Imports.ImportStaticMembers(typeof(CustomFunctions));
 
IDynamicExpression e = ExpressionFactory.CreateDynamic("myFunc(3,4)", context);
int result = (int)e.Evaluate();
Jan 31, 2008 at 11:15 AM
Eugene,

Will Flee throw an exception if it parses and expression with a function that does not exist either in Flee or the CustomFunctions context?

brian
Coordinator
Jan 31, 2008 at 4:44 PM
Yes, it will throw an ExpressionCompileException if it can't resolve the function in the imports or expression owner.
Feb 5, 2008 at 4:01 PM
Thanks,

It's just that I need to know when to fire the OnEvaluateCustomFunction in the FleeCalc implementation.

brian
Feb 6, 2008 at 3:58 PM
Is there any I can obtain the parsed expression? I need this so that I can fire OnEvalCustomFunction event with ten function name and it paramater values.

thanks,

brian
Coordinator
Feb 7, 2008 at 12:53 AM
In Flee, all functions are essentially custom so the OnEvalCustomFunction event would basically be raised on every function call.

Maybe you are looking more for something like on-demand variables but for functions? This way, you don't import any functions and flee will raise an event when it needs to resolve a function call.

Feb 7, 2008 at 5:56 PM
Yes something like the on-demand variable but for functions is what I'm looking for. I have three events that need to be raised:


CheckCustom

Fired when a formula containing custom function call is checked. The types of function arguments are passed in argTypes array. The event handler should check whether the custom function call contains the right number and types of arguments. The return value type of the custom function can be returned for further formula check. If the event is not handled the custom function is assumed to be in proper format with an empty return value type.

ConstructCustomFunction

This event is fired for all custom functions when the Culture property is changed. This event can be handled in order to update the custom function name for the specific culture.

OnEvalCustomFunction

Fired when a formula containing custom function call is evaluated. The values of function arguments are passed in argVals array. The return value of the custom function call can be returned. If the event is not handled, it is assumed that the result of the custom function evaluation is an empty value.


brian
Feb 7, 2008 at 6:47 PM
You indicated to us "context.Imports.ImportStaticMembers" to register the custom. Has this changed since I'm unable to find this method within the instance of ExpressionContext.

thanks

brian
Coordinator
Feb 7, 2008 at 10:43 PM

briviere wrote:
You indicated to us "context.Imports.ImportStaticMembers" to register the custom. Has this changed since I'm unable to find this method within the instance of ExpressionContext.

thanks

brian


It's now context.Imports.AddType(Type t)
Coordinator
Feb 8, 2008 at 2:04 AM
I will try to implement on-demand functions for the next release.
Coordinator
Feb 21, 2008 at 2:23 AM
On-demand functions are implemented in Flee-0.9.17.1. The VariableCollection.ResolveFunction event will be called to determine the return type of the function and the InvokeFunction event will be called to get the actual result.