| ILOG JRules User Guide > Creating Rule Projects > Tasks > Defining How Business Elements Map to the XOM > Implementing Synthetic Objects |
Implementing Synthetic Objects |
PREVIOUS NEXT |
A synthetic object allows you to simulate the state of real objects. Like a real object, a synthetic object can store attributes. SDO (Service Data Objects) is an example of a synthetic objects framework.
You can use synthetic objects instead of regular Java objects as application data that is used for execution. You can then use BOM to XOM mapping to implement a complete business object model using only synthetic objects.
To implement synthetic objects using IRL mapping:
In the following example, the java.util.Map interface is used to implement synthetic objects, where attributes are stored as key-value pairs. Therefore, there is only the regular java.util.Map interface in the XOM.
In the BOM, there is a business class Customer defined as:
Class Customer {
Customer(String name);
readonly String name;
int money;
}
For the name attribute, the getter part of the BOM to XOM mapping is expressed as:
return (String)this.get("name");
For the money attribute, assuming there is always an entry money in the Map containing an Integer, the getter part of the BOM to XOM mapping is expressed as:
return ((Integer)this.get("money")).intValue();
And the setter part is expressed as:
this.put("money",new Integer(value));
Because the same execution class (java.util.Map in this example) may be used for more than one business class, you need to distinguish which instances belong to which business class. This information must be stored in the Map when objects are constructed, and is used by the tester.
To do this, the BOM to XOM mapping body for the constructor would be expressed as:
Map result = new HashMap(3);
result.put("name", name);
result.put("money",new Integer(0));
// as assumed, it always contain an Integer
result.put("className", "Customer");
return result;
The tester part of the BOM to XOM mapping for the class Customer uses the className entry to differentiate java.util.Maps:
return "Customer".equals(this.get("className"));
Extender mapping can be used to implement synthetic objects.
To implement synthetic objects using extender mapping:
java.util.Map in the example below).
In this example, the extender class defines all the necessary mappings to the java.util.Map interface.
public class CustomerExtender {
public static String getName(Map customer) {
return (String) customer.get("name");
}
public static int getMoney(Map customer) {
return ((Integer) customer.get("money")).intValue();
}
public static void setMoney(Map customer, int value) {
customer.put("money", new Integer(value));
}
public static Map create(String name) {
Map result = new HashMap(3);
result.put("name", name);
result.put("money", new Integer(0));
// as assumed it always contains an Integer
result.put("className", "Customer");
return result;
}
public static boolean tester(Map customer) {
return "Customer".equals(customer.get("className"));
}
}
| Copyright © 1987-2008 ILOG S.A. All rights reserved. Legal terms. Documentation homepage. | PREVIOUS NEXT |