Drools調試

有不同的方法來調試Drools專案。在這裏,我們將編寫一個實用工具類,知道哪些規則正在被觸發或發射。

通過這種方法,可以檢查所有的規則都在Drools專案得到觸發。這裏是我們的工具類

Utility.java

package com.sample;

import org.drools.spi.KnowledgeHelper;

public class Utility {
   public static void help(final KnowledgeHelper drools, final String message){
      System.out.println(message);
      System.out.println("\nrule triggered: " + drools.getRule().getName());
   }

   public static void helper(final KnowledgeHelper drools){
      System.out.println("\nrule triggered: " + drools.getRule().getName());
   }
}

第一種方法幫助列印規則一起,可以通過為String通過DRL檔中的一些額外的資訊觸發。

第二條規則助手列印特定的規則是否被觸發。

我們增加了在每個DRL檔中的實用方法之一。我們在DRL檔(Pune.drl)還增加了導入函數。在當時的部分規則,我們已經加入了效用函數調用。下麵修改Pune.drl。改變以藍色顯示。

Modified Pune.drl

//created on: Dec 24, 2014
package droolsexample

//list any import classes here.
import com.sample.ItemCity;
import java.math.BigDecimal;
import com.sample.HelloCity;

import function com.sample.Utility.helper;

// declare any global variables here
dialect "java"
rule "Pune Medicine Item"

   when
      item : ItemCity(purchaseCity == ItemCity.City.PUNE,
                      typeofItem == ItemCity.Type.MEDICINES)

   then
      BigDecimal tax = new BigDecimal(0.0);
      item.setLocalTax(tax.multiply(item.getSellPrice()));
      HelloCity.writeHello(item.getPurchaseCity().toString());
      helper(drools);
end

rule "Pune Groceries Item"

   when
      item : ItemCity(purchaseCity == ItemCity.City.PUNE,
                      typeofItem == ItemCity.Type.GROCERIES)

   then
      BigDecimal tax = new BigDecimal(2.0);
      item.setLocalTax(tax.multiply(item.getSellPrice()));
      helper(drools);
end

同樣地,我們已經添加在第二個DRL檔(Nagpur.drl)其他效用函數。這裏是修改後的代碼:

修改後的 Nagpur.drl

// created on: Dec 26, 2014
package droolsexample

// list any import classes here.
import com.sample.ItemCity;
import java.math.BigDecimal;

import function com.sample.Utility.help;

//declare any global variables here
dialect "java"

rule "Nagpur Medicine Item"

   when
      item : ItemCity(purchaseCity == ItemCity.City.NAGPUR,
                      typeofItem == ItemCity.Type.MEDICINES)

   then
      BigDecimal tax = new BigDecimal(0.0);
      item.setLocalTax(tax.multiply(item.getSellPrice()));
      help(drools,"added info");
end

rule "Nagpur Groceries Item"

   when
      item : ItemCity(purchaseCity == ItemCity.City.NAGPUR,
                      typeofItem == ItemCity.Type.GROCERIES)

   then
      BigDecimal tax = new BigDecimal(1.0);
      item.setLocalTax(tax.multiply(item.getSellPrice()));
      help(drools,"info");
end

再次運行程式,它應該產生以下的輸出:

info

rule triggered: Nagpur Groceries Item
added info

rule triggered: Nagpur Medicine Item

rule triggered: Pune Groceries Item
HELLO PUNE!!!!!!

rule triggered: Pune Medicine Item
PUNE 0
PUNE 20
NAGPUR 0
NAGPUR 10

這兩個工具函數被調用,它顯示了特定規則是否被調用與否。在上述的例子中,所有的規則都被調用,但在企業應用程式中,該實用程式函數可以是真正有用的調試,並找出一個特定規則是否被觸發或沒有。

使用Debug透視圖在Eclipse

可以將Drools的應用程式的執行過程中調試規則。可以在規則的後果添加中斷點,每當這樣的中斷點的規則的執行過程中遇到,執行暫時停止。然後,可以檢查該點調用Java應用程式,並使用在Eclipse提供正常的調試選項。

創建DRL檔中斷點,只需雙擊創建一個中斷點行。記住,只能在當時的部分規則的創建一個中斷點。中斷點可以通過雙擊在DRL編輯器中的中斷點被刪除。

採用中斷點後,需要將應用程式作為Drools的應用程式進行調試。 Drools的中斷點(以DRL檔的中斷點),如果應用程式正在調試的Drools的應用程式將只工作。這裏是如何需要做的是相同的:

Drools Application

調試應用程式作為Drools的應用程式,會看到如圖所示的下麵的截圖DRL檔中的控制:

Eclipse Platform

可以在該調試點看到的變數和所述對象的當前值。同一控制F6移動到下一行和F8跳轉到下一個調試點也適用在這裏。通過這種方式,可以調試Drools的應用程式。

注:在Drools中的應用調試的角度來看只有當方言是MVEL直到Drools5.x


上一篇: Drools簡單專案 下一篇:無