Scripting Function Example?

In the SDK docs there is a brief outline (section 4.9) on Scripting Functions. However no example project illustrating such functionality. A simple example showing a function returning a string (e.g. “Hello World”) and a function taking some arguments, doing something, then returning a result, would both be ideal to satisfy what I am looking for. Thanks for any guidance/assistance on this request!

Doh, I can’t believe there’s no example for this. It’s one of the easiest things you can do with the SDK!

The gist of it is:

  1. For each scope you want the functions available in, in the hook for that scope implement #initializeScriptManager(ScriptManager).

  2. Call one of ScriptManager’s #addScriptModule functions. These calls should have enough JavaDoc on them to be self-explanatory, but feel free to ask any questions here.

Kevin,

So I figured I’d start with the designer hook. My goal, however would be allow my functions to be used in Gateway scripts AND in Designer scripts (e.g. within components, etc). So:

First within the hook, I have:
ScriptManager MyScrMgr = new ScriptManager(“designer”);

Then within the startup() event, a try/catch block calling:
MyScrMgr.addScriptModule(“system.foo”, Class.forName(“MyFuncs”));

Where I am really fuzzy, mainly that I am green on my Java after too many years of C/C++/VB. I could not find where the .initializeScriptManager() method was defined, I tried a few things but they all result in errors (in eclipse). I am also uncertain if I am passing in my class reference properly by Java convention. In C I could just pass in a function pointer (basically nothing more than the name of the function) and I’d be good. Here I am unsure of the syntax to pass in the class I want to have house all my implemented functions.

Note that I did make a separate .java file that has nothing more than the “MyFuncs” class defined. I figure I am on the right path as eclipse isn’t barking at me, but I know I am probably missing something basic that I am just not aware of. Thus the interest in the example as I would probably resolve it myself with some poking.

Thanks for any more guidance you can provide… Guess we all start somewhere!

You don’t create your own ScriptManager instance, you override the initializeScriptManager(ScriptManager) call from the DesignerHook interface (although you should probably be extended AbstractDesignerModuleHook instead, which is easier).

@Override
public void initializeScriptManager(ScriptManager manager) {
	super.initializeScriptManager(manager);

	manager.addScriptModule("system.foo", MyFooFuncs.class);
}

Ok, cool, I was actually on that track first with the override, so maybe I was originally closer than I thought. The other line (super.*) was where I had a miss. I’ll give this a try and see if I am all good. Thanks for the tip Kevin.

Kevin,

Got this all working. Quick last question – what are the similar setup calls to engage a custom function into the expression language? I see “through the ExpressionFunctionFactory passed into the new configureFunctionFactory(…) function on module hooks.” Just not sure how this is to get called? Doing some digging, it involves ExpressionFunctionManager.addFunction() to add a Function. Then it falls apart for me a bit in that I am not certain how to define valid functions to tie into the .addFunction calls that actually link them into the expression language? Thanks as always for the guidance…

Any help on this one? Kept poking at it giving it my best, but still can’t quite figure out how to link up the pieces to get the functions to register. A small snippet similar to before would be perfect and probably get me over the hump on this one. Thanks in advance!

Here’s an example eclipse project (well, projects) showing how custom expressions can be made:
ExpressionExample.zip (44.4 KB)

Regards,