| ILOG JRules User Guide > Creating Rule Projects > Tasks > Defining How Business Elements Map to the XOM > Mapping an Attribute to a Computed Value |
Mapping an Attribute to a Computed Value |
PREVIOUS NEXT |
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:
this to represent the current object.
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:
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.
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();
}
}
| Copyright © 1987-2008 ILOG S.A. All rights reserved. Legal terms. Documentation homepage. | PREVIOUS NEXT |