Defining custom members and their implementation

You can map different business elements to the XOM. These elements include attributes and method calls.

To define and implement custom members:

  1. Create a BOM file named extension.bom in the reference folder of the external library.
  2. Add the following boilerplate code to the BOM file:
    #loadGetterSetterAsProperties
    package <package_name>;
  3. Declare a class inside the BOM file.

    You can copy a class declaration from the BOM file that was automatically generated in the external library.

  4. Declare the member variable.
  5. Create a BOM-to-XOM mapping file named extension.b2xa in the reference folder of the external library.
  6. Add the following boilerplate code to the BOM-to-XOM file:
    <b2x:translation xmlns:b2x="http://schemas.ilog.com/JRules/1.3/Translation"
                     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                     xsi:schemaLocation="http://schemas.ilog.com/JRules/1.3/Translation ilog/rules/schemas/1_3/b2x.xsd">
    
    </b2x:translation>
  7. Add the member implementation inside the BOM-to-XOM file.

The following sections provide specific details about adding computed attributes and custom methods.

Adding a computed attribute

You can add a BOM read-only attribute and define how it is computed.

For example, you have a class that is named ShoppingCart in the XOM, and the class contains two methods that return the number of items in the shopping cart and the total value of the items:

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

To add a computed attribute:

  1. Open the extension.bom file and declare the class and member variable as shown in the following example:
    #loadGetterSetterAsProperties
    package ads.samples.externalLibrary;
    
    class ShoppingCart
            extends com.ibm.ia.StringProvider
    {
        readonly double averageItemPrice;
    }
    The variable must always be prefixed with the readonly modifier.
  2. Open the extension.b2xa file and add the member implementation as shown in the following example:
    <b2x:translation xmlns:b2x="http://schemas.ilog.com/JRules/1.3/Translation"
                     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                     xsi:schemaLocation="http://schemas.ilog.com/JRules/1.3/Translation ilog/rules/schemas/1_3/b2x.xsd">
        <class>
            <businessName>ads.samples.externalLibrary.ShoppingCart</businessName>
            <executionName>ads.samples.externalLibrary.ShoppingCart</executionName>
            <attribute>
                <name>averageItemPrice</name>
                <getter language="arl"><![CDATA[
                    if (this.getNumberOfItems() == 0 ) 
                        return 0;
                    return this.getValue()/this.getNumberOfItems();
                    ]]></getter>
            </attribute>
        </class>
    </b2x:translation>
    Where:
    • The name of the attribute is the same as the one used in the extension.bom file.
    • The body of the getter returns a value.

    The businessName and executionName values can be copied directly from the model.bom file.

Adding a custom method

You can add a custom method and define how it is implemented.

For example, you have a class Customer in the XOM, and the class contains a method that returns the age of the customer:

// XOM class
public class Customer {
  public int getAge() ...
}

To add a custom method:

  1. Open the extension.bom file and declare the class and member variable as shown in the following example:
    #loadGetterSetterAsProperties
    package ads.samples.externalLibrary;
    
    class Customer
            extends com.ibm.ia.StringProvider
    {
        boolean isOlderThan(int arg)
                    #pureFunction;
    }
    The variable must always be annotated with the #pureFunction; tag.
  2. Open the extension.b2xa file and add the member implementation as shown in the following example:
    <b2x:translation xmlns:b2x="http://schemas.ilog.com/JRules/1.3/Translation"
                     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                     xsi:schemaLocation="http://schemas.ilog.com/JRules/1.3/Translation ilog/rules/schemas/1_3/b2x.xsd">
        <class>
            <businessName>ads.samples.externalLibrary.Customer</businessName>
            <executionName>ads.samples.externalLibrary.Customer</executionName>
            <method>
                <name>isOlderThan</name>
                <parameter type="int"/>
                <body language="arl"><![CDATA[
                 return this.getAge() > arg;
                 ]]></body>
            </method>
        </class>
    </b2x:translation>

    The name and type of the parameter must be the same as the ones you used in the extension.bom file.