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简单项目 下一篇:无