在Java中,如果有重复的代码我们会考虑进行重构,抽取公共方法或继承父类,以减少相同的代码在多处出现,达到代码的最优管理和不必要的麻烦。Drools同样提供了类似的功能。下面我们以实例来逐步说明。

像下面最原始的两条规则,有相同的业务判断,也有不同的地方:

package com.rules.conditional
import com.secbro.drools.model.Customer;
import com.secbro.drools.model.Car;

rule "conditional1:Give 10% discount to customers older than 60"
    agenda-group "conditional1"
when
    $customer : Customer( age > 60 )
then
    modify($customer) { setDiscount( 0.1 ) };
    System.out.println("Give 10% discount to customers older than 60");
end

rule "conditional1:Give free parking to customers older than 60"
agenda-group "conditional1"
when
    $customer : Customer( age > 60 )
    $car : Car ( owner == $customer )
then
    modify($car) { setFreeParking( true ) };
    System.out.println("Give free parking to customers older than 60");
end

现在Drools提供了extends特性,也就是一个规则可以继承另外一个规则,并获得其约束条件。改写之后执行效果相同,代码如下:

package com.rules.conditional
import com.secbro.drools.model.Customer;
import com.secbro.drools.model.Car;

rule "conditional2:Give 10% discount to customers older than 60"
    agenda-group "conditional2"
when
    $customer : Customer( age > 60 )
then
    modify($customer) { setDiscount( 0.1 ) };
    System.out.println("conditional2:Give 10% discount to customers older than 60");
end

rule "conditional2:Give free parking to customers older than 60"
    extends "conditional2:Give 10% discount to customers older than 60"
agenda-group "conditional2"
when
    $car : Car ( owner == $customer )
then
    modify($car) { setFreeParking( true ) };
    System.out.println("conditional2:Give free parking to customers older than 60");
end

我们可以看到上面使用了extends,后面紧跟的是另外一条规则的名称。这样,第二条规则同时拥有了第一条规则的约束条件。只需要单独写此条规则自身额外需要的约束条件即可。那么,现在是否是最优的写法吗?当然不是,还可以将两条规则合并成一条来规则。这就用到了do和标记。

package com.rules.conditional
import com.secbro.drools.model.Customer;
import com.secbro.drools.model.Car;

rule "conditional3:Give 10% discount to customers older than 60"
    agenda-group "conditional3"
when
    $customer : Customer( age > 60 )
    do[giveDiscount]
    $car : Car(owner == $customer)
then
    modify($car) { setFreeParking(true) };
        System.out.println("conditional3:Give free parking to customers older than 60");
then[giveDiscount]
    modify($customer){
        setDiscount(0.1)
    };
    System.out.println("conditional3:Give 10% discount to customers older than 60");
end

在then中标记了giveDiscount处理操作,在when中用do来调用标记的操作。这样也当第一个约束条件判断完成之后,就执行标记giveDiscount中的操作,然后继续执行Car的约束判断,通过之后执行默认的操作。

在then中还可以添加一些判断来执行标记的操作,这样就不必每次都执行do操作,而是每当满足if条件之后才执行:

package com.rules.conditional
import com.secbro.drools.model.Customer;
import com.secbro.drools.model.Car;

rule "conditional4:Give 10% discount to customers older than 60"
    agenda-group "conditional4"
when
    $customer : Customer( age > 60 )
    if(type == "Golden") do[giveDiscount]
    $car : Car(owner == $customer)
then
    modify($car) { setFreeParking(true) };
        System.out.println("conditional4:Give free parking to customers older than 60");
then[giveDiscount]
    modify($customer){
        setDiscount(0.1)
    };
    System.out.println("conditional4:Give 10% discount to customers older than 60");
end

同时,还可以通过break来中断后续的判断。

package com.rules.conditional
import com.secbro.drools.model.Customer;
import com.secbro.drools.model.Car;

rule "conditional5:Give 10% discount to customers older than 60"
    agenda-group "conditional5"
when
    $customer : Customer( age > 60 )
    if(type == "Golden") do[giveDiscount10]
    else if (type == "Silver") break[giveDiscount5]
    $car : Car(owner == $customer)
then
    modify($car) { setFreeParking(true) };
        System.out.println("conditional5:Give free parking to customers older than 60");
then[giveDiscount10]
    modify($customer){
        setDiscount(0.1)
    };
    System.out.println("giveDiscount10:Give 10% discount to customers older than 60");
then[giveDiscount5]
    modify($customer){
        setDiscount(0.05)
    };
    System.out.println("giveDiscount5:Give 10% discount to customers older than 60");
end

以上规则的执行测试代码如下,执行结果可自行尝试,源代码已经存放在GitHub:https://github.com/secbr/drools

相关技术视频

QQ技术交流2群:715840230

CSDN学院:《Drools7规则引擎进阶教程》

CSDN学院:《Drools7规则引擎入门教程》

CSDN学院:《Drools7系列优惠套餐》

获得完整的《从Drools规则引擎到风控反洗钱系统》可评论留下邮箱。



drools 规则引擎之条件结果插图

关注公众号:程序新视界,一个让你软实力、硬技术同步提升的平台

除非注明,否则均为程序新视界原创文章,转载必须以链接形式标明本文链接

本文链接:https://www.choupangxia.com/2019/09/05/drools-%e8%a7%84%e5%88%99%e5%bc%95%e6%93%8e%e4%b9%8b%e6%9d%a1%e4%bb%b6%e7%bb%93%e6%9e%9c/