作業 4:擴充商業物件模型
學習如何新增方法至 BOM 中,測試詞彙,以及將商業方法對映至 XOM。
關於這項作業
在您的詞彙中,您需要用一個術語來說明客戶的年齡。不過,BOM 只有一個 birthDate 屬性。您將擴充 BOM,將對應至您需求的方法新增至詞彙中,然後將它對映至 XOM。
程序
- 新增方法至 BOM:
- 在「規則瀏覽器」中,展開 vocabulary-rule > bom > model > 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:
- 在「規則瀏覽器」中,按兩下 BOM 項目 model。
在「大綱」視圖中,按一下 RentalAgreement 類別之下的 getCustomerAge。
註:
「大綱」視圖顯示 Rule Designer 中正在編輯的元素。 如果要查看 BOM 大綱視圖,請在「規則瀏覽器」中開啟 bom 資料夾,然後按兩下 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;會顯示錯誤,因為遺漏 import 陳述式。
- 按一下編輯匯入項目。
- 在「BOM 至 XOM 對映」區段中,開啟匯入項目,並且輸入:
import carrental.Customer; import java.util.Calendar; - 在檔案功能表中,按一下儲存,然後按一下成員標籤,重新開啟 getCustomerAge 的「BOM 至 XOM 對映」區段。
現在,錯誤已解決,方法已適當對映至 XOM。
- 在「規則瀏覽器」中,按兩下 UnderAge 規則。
如您所見,規則不再顯示警告。
- 在「規則瀏覽器」中,按兩下 BOM 項目 model。
在「大綱」視圖中,按一下 RentalAgreement 類別之下的 getCustomerAge。
結果
在下一項作業中,您將修訂詞彙,使規則更易於撰寫與瞭解。