< 上一個課程 | 下一個課程 >

作業 4:擴充商業物件模型

學習如何新增方法至 BOM 中,測試詞彙,以及將商業方法對映至 XOM。

關於這項作業

在您的詞彙中,您需要用一個術語來說明客戶的年齡。不過,BOM 只有一個 birthDate 屬性。您將擴充 BOM,將對應至您需求的方法新增至詞彙中,然後將它對映至 XOM。

程序

  1. 新增方法至 BOM:
    1. 在「規則瀏覽器」中,展開 vocabulary-rule > bom > model > carrental,然後按兩下 RentalAgreement 類別。
    2. 在「類別」頁面的「成員」區段中,按一下新建。
    3. 在「新建成員」精靈中:
      • 選取方法
      • 在名稱欄位中,輸入 getCustomerAge。
      • 在類型欄位中,按一下瀏覽,然後選取傳回類型 int。請往下捲動清單來找到它。
    4. 按一下確定,然後按一下完成。

      現在「成員」區段會顯示 getCustomerAge 方法。

    5. 在「成員」區段中,按兩下 getCustomerAge 方法。
    6. 在「成員文字化」區段中,按一下建立預設文字化。

      這個動作會顯示 getCustomerAge 方法文字化後的導覽詞組,依預設,會產生為 {customer age} of {this}。

    7. 儲存您的工作。
    儲存產生的錯誤。雖然您新增至 BOM 的方法可供您作為商業規則中的導覽詞組,它並沒有對映至 XOM 層次的任何執行檔。之後,您會將方法對映至 XOM 來修正這個錯誤。

    現在,您將建立規則,來測試新的詞彙元素。

  2. 測試新詞彙:
    1. 建立一個名稱是 Eligibility 的規則套件。
    2. 在 Eligibility 套件中,建立一個名稱是 UnderAge 的動作規則。
    3. 在 Intellirule 編輯器的「內容」區段中,輸入 if,然後按 Enter 鍵。
    4. 輸入下列規則。一開始先使用鍵盤,然後使用「內容輔助」:
      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.");
    5. 儲存您的工作。
    現在,將方法對映至 XOM,來更正因為在 BOM 中新增方法所造成的錯誤。您必須將 IRL 對映程式碼關聯於方法,以便使用客戶的生日,來計算客戶在取車日期的實際年齡。
  3. 將商業方法對映至 XOM:
    1. 在「規則瀏覽器」中,按兩下 BOM 項目 model。 在「大綱」視圖中,按一下 RentalAgreement 類別之下的 getCustomerAge。
      註:

      「大綱」視圖顯示 Rule Designer 中正在編輯的元素。 如果要查看 BOM 大綱視圖,請在「規則瀏覽器」中開啟 bom 資料夾,然後按兩下 model。

    2. 往下捲動到「BOM 編輯器」底端,然後開啟「BOM 至 XOM 對映」區段。
    3. 在主體欄位中,複製並貼上下列程式碼:
      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 陳述式。

    4. 按一下編輯匯入項目。
    5. 在「BOM 至 XOM 對映」區段中,開啟匯入項目,並且輸入:
      import carrental.Customer;
      import java.util.Calendar; 
    6. 在檔案功能表中,按一下儲存,然後按一下成員標籤,重新開啟 getCustomerAge 的「BOM 至 XOM 對映」區段。

      現在,錯誤已解決,方法已適當對映至 XOM。

    7. 在「規則瀏覽器」中,按兩下 UnderAge 規則。

      如您所見,規則不再顯示警告。

結果

在下一項作業中,您將修訂詞彙,使規則更易於撰寫與瞭解。

< 上一個課程 | 下一個課程 >