ILOG JRules User Guide > Creating Rule Projects > Tasks > Defining How Business Elements Map to the XOM > Mapping an Attribute to a Computed Value

You can map a BOM attribute to an expression that returns the value of an attribute at runtime.

To map an attribute to a computed value using IRL mapping:

  1. In the Outline view, click the attribute that you want to map to a computed value.
  2. In the BOM Editor, in the BOM to XOM Mapping section, type an IRL statement that computes the value of the attribute in the Getter field. Use this to represent the current object.
  3. Save the BOM.
  4. The attribute is now mapped to a computed value.

In this example, there is a class ShoppingCart in the XOM which contains a method that returns the number of items in a shopping cart, and a method that returns the total value of all the items.

// Execution class
public class ShoppingCart {
   public double getValue() ...
   public int getNumberOfItems() ...
}

In the BOM, there is an attribute that represents the average item price and which is computed using an expression.

// BOM class
public class ShoppingCart {
   public double averageItemPrice;
   ...
}

The expression that computes the value of this attribute is expressed in the body of the attribute getter as:

if (this.getNumberOfItems() == 0)
  return 0;
return this.getValue()/this.getNumberOfItems();

Extender mapping can be used to map an attribute to a computed value.

To map an attribute to a computed value using extender mapping:

  1. In the Outline view, click the class containing the attribute that you want to map to a computed value.
  2. In the BOM Editor, in the BOM to XOM Mapping section, specify the name of your extender class in the Extender name field.
  3. Define the attribute as a Java static method named get followed by the name of the attribute. When the attribute is static, the extender method has no parameter. In the case of an instance attribute, the extender method takes the execution class type as parameter.
  4. Save the BOM.
  5. The attribute is now mapped to a computed value.

In this example, the method that is used when the attribute averageItemPrice is accessed is provided as a Java static method named getAverageItemPrice.

public class CartExtender {
  public static double getAverageItemPrice(ShoppingCart cart) {
    if (cart.getNumberOfItems() == 0)
      return 0;
    return cart.getValue()/ cart.getNumberOfItems();    
  }
}

Related Concepts

Business Object Model (BOM)
Execution Object Model (XOM)
BOM and XOM

Related Tasks

Defining How Business Elements Map to the XOM
Managing XOM Changes in the BOM

Related Reference

BOM Editor
ILOG Rule Language

Related Samples and Tutorials

Tutorial: Defining a Vocabulary