User-Defined Functions

  • Updated

In addition to importing external assemblies to use custom functions with rules, InRule provides users with a way to create helper functions inline in a rule application. User-Defined Functions (UDFs) have the following benefits:

  • Reduce deployment complexity by using custom functions without having to associate a separate .NET Assembly and import its public methods
  • Manage changes to functions as revisions of the rule applications that contain them
  • Author simple functions not already available with InRule
  • Extend imported external function libraries
  • Reuse functions across multiple rule applications with irCatalog element sharing

Creating UDFs

User-Defined Function Libraries
UDFs are organized into libraries in irAuthor. To insert a library, click the Add Library button. As with other rule application elements, the user may name the library.

User-Defined Functions (UDFs)
To add a User-Defined Function (UDF), right-click on a User-Defined Function Library.

UDF Settings Tab
The following properties are available to the user in the UDF Settings Tab:

  • Name - The user-configured name of the UDF. Along with the library name, this will be the name used to refer to the function from rules in the rule application.
  • Return Type - The data type of the value returned when the function is invoked from a rule.
  • Entity - The applicable entity context of the data structure for the function. Indicates the accessibility of fields, calculations, and other entities/collections in the function.
  • Parameters - The zero or more arguments that are passed into the function when it is invoked.
  • irScript - The set of statements that define the function using a dynamic, JavaScript-like language called irScript. A complete definition of irScript can be found in the irScript User Guide, which is included as part of the InRule installation. Select Start->All Programs -> InRule to launch the irScript User Guide

Autocompletion menus are available for user guidance when creating UDFs. Press Ctrl+SPACE to open the autocompletion menus.

mceclip0.png

Modify State Refresh Options Hyperlink
State Refresh Options specify how the state is maintained for any bound assembly schema accessed from the UDF. By default, a full state refresh for all assembly-bound entity arguments is enabled. Users can author these options similarly to bound assembly methods and function library methods.

Using the Context Object in a UDF

The Context object within a UDF encapsulates specific aspects of the Rule Execution request including the following:

  • RuleApplication - The current executing rule.
  • RuleSet - The current executing RuleSet.
  • RuleElement - The current executing RuleElement. The element returned will sometimes be a rule or an action, such as FireNotification or SetValue, depending on where it is called.
  • Entity - The current executing Entity.
  • Calc - The current executing Calculation. This will be null
    • If the UDF is called by a RuleElement - Calc will be null.
    • If the UDF is called from a calculation - RuleElement and RuleSet will be null.
  • ContextProperties - A dictionary that persists for the lifetime of the rule execution request where rules can get or set values that are not bound to the rule application schema.

Many of the elements mentioned above also provide access to their corresponding design-time definitions ("defs") and metadata. For example, Context.RuleSet.RuleSetDef provides access to the RuleSet's definition.

Using UDFs in Rules

UDFs are referenced in rules similarly to external function libraries.

UDFs in Language Rules
UDFs are grouped under a specific heading in the menus for the business language editor.

As with imported external function libraries, UDFs can be used as both expressions and actions of rules.

UDFs in Syntax Rules
To use a UDF in a syntax rule expression or action, refer to the library name and function name using dot notation as displayed:

mceclip0.png

Because UDFs are available for use in syntax expressions and actions, they can therefore be utilized in the following locations:

mceclip1.png

Execute Method Action
The Execute Method action in irAuthor can also be used to invoke a UDF.

User-Defined Function Samples

See the article, User-Defined Function Samples for our categorized lists of samples. 

Was this article helpful?

0 out of 1 found this helpful

Comments

0 comments

Please sign in to leave a comment.