任务 4:扩展业务对象模型
学习如何向 BOM 中添加方法,测试词汇表以及将业务方法映射到 XOM。
关于此任务
在您的词汇表中,您需要用于表示客户年龄的术语。但是,BOM 仅具有 birthDate 属性。 您可以扩展 BOM 以在词汇表中添加与您的需要对应的方法,然后将其映射到 XOM。
过程
- 向 BOM 中添加方法:
- 在 Rule Explorer 中,展开vocabulary-rule > bom > 模型 > carrental,并双击类 RentalAgreement。
- 在“类”页面上的“成员”部分中,单击新建。
- 在“新建成员”向导中:
- 选择方法
- 在名称字段中,输入 getCustomerAge。
- 在类型字段中,单击浏览,然后选择返回类型 int。向下滚动列表以查找该类型。
- 单击确定,然后单击完成。
“成员”部分现在显示方法 getCustomerAge。
- 在“成员”部分中,双击方法 getCustomerAge。
- 在“成员表述”部分中,单击创建缺省表述。
此操作显示包含方法 getCustomerAge 的表述的导航短语,缺省情况下生成为 {customer age} of {this}。
- 保存工作。
目前,您通过创建规则来测试新词汇表元素。
- 测试新词汇表:
- 创建名为 Eligibility 的规则包。
- 在 Eligibility 包中,创建名为 UnderAge 的操作规则。
- 在 Intellirule 编辑器的“内容”部分中,输入 if,然后按 Enter 键。
- 输入以下规则。首先使用键盘,然后使用“内容辅助”:
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.");
- 保存工作。
- 将业务方法映射到 XOM:
- 在 Rule Explorer 中双击 BOM 条目 model。在“概要”视图中,单击 RentalAgreement 类下的方法 getCustomerAge。
注:
“概要”视图显示了正在 Rule Designer 中进行编辑的元素。要查看 BOM 概览视图,打开 bom 文件夹并在 Rule Explorer 中双击 model。
- 向下滚动到 BOM 编辑器的底部,然后打开“BOM 到 XOM 映射”部分。
- 在主体字段中,复制并粘贴以下代码:
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;由于缺少导入语句,因此显示了错误。
- 单击编辑导入。
- 在“BOM 到 XOM 映射”部分中,打开导入并输入以下内容:
import carrental.Customer; import java.util.Calendar; - 在文件菜单上,单击保存,然后单击成员选项卡以重新打开 getCustomerAge 的“BOM 到 XOM 映射”部分。
此时会解析错误,并且方法现在正确映射到 XOM。
- 在 Rule Explorer 中,双击规则 UnderAge。
如您所见,规则不再显示警告。
- 在 Rule Explorer 中双击 BOM 条目 model。在“概要”视图中,单击 RentalAgreement 类下的方法 getCustomerAge。
结果
在下一个任务中,您将优化词汇表以使规则更易于编写和理解。