Accessing classic rule engine and related artifacts from IRL mapping code

You can access the classic rule engine and its related artifacts, by writing IRL mapping code in the BOM-to-XOM mapping section of the BOM Editor.

About this task

The engine-related artifacts can be accessed directly from the IRL mapping code.

Note: You cannot use extender mapping to access the engine, ruleset variables, parameters, and rules.

Procedure

To access ruleset variables or parameters:

  1. In the Outline view, click the member that contains the IRL mapping that you want to edit.
  2. In the BOM Editor, open the BOM-to-XOM mapping section and then go to the IRL statement in the Body field.
  3. You have the following options:
    Option Description
    Accessing functions from IRL mapping code

    Use the method IlrContext.invokeFunction to access the function.

    For example, you have a BOM class ShoppingCart with an attribute public readonly double averageItemPrice.

    In your rule project, you have a function that computes the average price of the items in a shopping cart:

    function double computeAveragePrice(ShoppingCart cart) {
       if (cart.numberOfItems != 0)
        return cart.value / cart.numberOfItems;
      return 0;
    }

    This IRL function is based on the BOM, not the XOM.

    To map the averageItemPrice attribute to a call to the computeAveragePrice function, you can write the following IRL mapping code for the attribute averageItemPrice:

    Double result = (Double) invokeFunction(“computeAveragePriceShopping”,
                                            new Object[] {this});
    return result.doubleValue();
    Accessing rule instance from IRL mapping code

    You can access the current rule instance by using the predefined variable instance. You can also write this variable ?instance. You can use this predefined variable in any of the execution modes: Rete, Sequential, or Fastpath.

    In IRL mapping code, the instance variable can be null. It is not null when the member is used in a rule action.

    In this example, you use IRL mapping to add a trace:

    if (instance != null)
      note("IRL mapping of method M being called
                             by rule "+instance.ruleName);
    Accessing ruleset variables and parameters from IRL mapping code

    Use the IlrContext.getParameterValue method in the BOM editor to access ruleset variables or parameters from IRL mapping code.

    For example, you have a parameter business class b.Customer. The business class b.Customer is being mapped to x.Customer. You can access the parameter in the IRL mapping by writing the following code:

    x.Customer customer = (x.Customer)getParameterValue("customer");
  4. Save the BOM.

Results

You access the functions, ruleset variables, parameters, and rules directly from the BOM Editor, in which you edit the IRL mapping code.