Task 4: Extending the business object model
You learn how to add a method to the BOM, test the vocabulary, and map a business method to the XOM.
About this task
In your vocabulary, you need a term for the age of the customer. However, the BOM has only a birthDate attribute. You extend the BOM to add the method that corresponds to your needs in the vocabulary, and then map it to the XOM.
Procedure
- Add a method to the BOM:
- In the Rule Explorer, expand vocabulary-rule > bom > model > carrental, and double-click the class RentalAgreement.
- In the Members section on the Class page, click New.
- In the New Member wizard:
- Select Method
- In the Name field, type getCustomerAge.
- In the Type field, click Browse, and then select the return type int. Scroll down the list to find it.
- Click OK, and then Finish.
The Members section now displays the method getCustomerAge.
- In the Members section, double-click the method getCustomerAge.
- In the Member Verbalization section, click Create
a default verbalization.
This action displays a navigation phrase with the verbalization of the method getCustomerAge, generated by default as {customer age} of {this}.
- Save your work.
For now, you test your new vocabulary element by creating a rule.
- Test the new vocabulary:
- Create a rule package named Eligibility.
- In the Eligibility package, create an action rule named UnderAge.
- In the Content section of the Intellirule editor, type if, and then press Enter.
- Type the following rule. Start with the keyboard, and
then use Content Assist:
if the customer age of the rental agreement is less than 18 then make it true that the rental agreement is rejected; the session .displayMessage ( "The rental agreement is rejected.");
- Save your work.
- Map the business method to the XOM:
- Double-click the BOM Entry model in the Rule
Explorer. In the Outline view, click the method
getCustomerAge under the RentalAgreement class. Note:
The Outline view shows the element that is being edited in the Rule Designer. To see the BOM outline view, open the bom folder and double-click model in the Rule Explorer.
- Scroll down to the bottom of the BOM Editor, and open the BOM to XOM Mapping section.
- In the body field, copy and paste the following code:
Calendar birth = Calendar.getInstance(); Customer cust = this.getCustomer(); birth.setTime(cust.getBirthDate()); // Create a calendar object with today's date Calendar now = Calendar.getInstance(); now.setTime(this.getPickupDate()); // Get age based on year int age = now.get(Calendar.YEAR) - birth.get(Calendar.YEAR); // Add the tentative age to the date of birth // to get this year's birthday birth.add(Calendar.YEAR, age); // If this year's birthday has not happened yet, // subtract one from age if (now.before(birth)) { age--; } return age;Errors are displayed because import statements are missing.
- Click Edit the imports.
- In the BOM to XOM Mapping section, open Imports and
type:
import carrental.Customer; import java.util.Calendar; - On the File menu, click Save, and then click the
Member tab to reopen the BOM to XOM Mapping section for
getCustomerAge.
The errors are now resolved, and the method is now properly mapped to the XOM.
- In the Rule Explorer, double-click the rule UnderAge.
As you can see, the rule no longer displays warnings.
- Double-click the BOM Entry model in the Rule
Explorer. In the Outline view, click the method
getCustomerAge under the RentalAgreement class.
Results
In the next task, you refine the vocabulary to make rules easier to write and understand.