| ILOG JRules User Guide > Creating Rule Projects > Tasks > Defining How Business Elements Map to the XOM > Mapping an Attribute Assignment to a Function |
Mapping an Attribute Assignment to a Function |
PREVIOUS NEXT |
You can map a BOM attribute to an expression that sets the value of this attribute at runtime.
To map an attribute assignment to a function 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 sets the number of items in a shopping cart and a method that sets the total value of all the items.
// Execution class
public class ShoppingCart {
public void setValue(int value) ...
public void setNumberOfItems(int number) ...
}
In the BOM there is an attribute that represents the average item price and which is set using an expression.
// BOM class
public class ShoppingCart {
public double averageItemPrice;
...
}
The expression that sets the value of the attribute is expressed in the body of the attribute setter as:
this.setValue(value*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:
set followed by the name of the attribute. When the attribute is static, the extender method takes the execution class corresponding to the attribute type as parameter. In the case of an instance attribute, the extender method takes two parameters: the execution class type, and the execution class corresponding to the attribute type.
In this example, the method that is used when the attribute averageItemPrice is accessed is provided as a Java static method named setAverageItemPrice.
public class CartExtender {
public static void setAverageItemPrice(ShoppingCart cart, double v) {
cart.setValue(cart.getNumberOfItems()*v);
}
}
| Copyright © 1987-2008 ILOG S.A. All rights reserved. Legal terms. Documentation homepage. | PREVIOUS NEXT |