为什么这个规则不止一次运行?

时间:2017-04-19 10:09:17

标签: jboss drools

我创建了这条规则:

rule "Product PRODUCT_A is not available from other insurers as INSURER_A"
when
    $p : Policy(insurer.name != "INSURER_A", product.name == "PRODUCT_A", $v : validations)
    not (Validation(level == ValidationLevel.ERROR) from $v)
then
    log.info("Matching rule for {} with validations: {}", $p.getInsurer().getName(), $p.getValidations());
    $p.addValidation(new Validation(ValidationLevel.ERROR, "This insurance is not available for " + $p.getInsurer().getName()));
end

当我注入4个Policy对象时,所有这些对象都是PRODUCT_A,另一个是保险公司名称为INSURER_A,另外三个是其他名称,日志记录显示我:

DroolsController - Matching rule for INSURER_B with validations: []
DroolsController - Matching rule for INSURER_C with validations: [Validation(id=0, level=ERROR, description=This insurance is not available for INSURER_B)]
DroolsController - Matching rule for INSURER_D with validations: [Validation(id=0, level=ERROR, description=This insurance is not available for INSURER_B), Validation(id=0, level=ERROR, description=This insurance is not available for INSURER_C)]
DroolsController - Matching rule for INSURER_B with validations: [Validation(id=0, level=ERROR, description=This insurance is not available for INSURER_D), Validation(id=0, level=ERROR, description=This insurance is not available for INSURER_B), Validation(id=0, level=ERROR, description=This insurance is not available for INSURER_C)]
DroolsController - Matching rule for INSURER_C with validations: [Validation(id=0, level=ERROR, description=This insurance is not available for INSURER_D), Validation(id=0, level=ERROR, description=This insurance is not available for INSURER_B), Validation(id=0, level=ERROR, description=This insurance is not available for INSURER_C)]
DroolsController - Matching rule for INSURER_D with validations: [Validation(id=0, level=ERROR, description=This insurance is not available for INSURER_D), Validation(id=0, level=ERROR, description=This insurance is not available for INSURER_B), Validation(id=0, level=ERROR, description=This insurance is not available for INSURER_C)]

这对我来说很奇怪。我期待每个策略(INSURER_A除外)都会有一个Validation对象。但是,验证对象似乎会添加到所有Policy对象中。当我读取结果时,每个验证对象都会添加到所有策略中,即使是在PRODUCT_A策略中也是如此。

我的规则有什么问题?如何更改规则以使其按预期运行?

2 个答案:

答案 0 :(得分:0)

这与定义的规则无关。

您输入的政策事实是克隆对象。然而,这是一个浅层克隆而不是深层克隆。因此,策略对象中的验证列表仍然指向克隆中的相同验证列表。

在向一个对象添加验证时,由于其他对象引用了相同的列表,因此它们也会被添加到其他对象中。

Drools首先创建一个包含所有条件的议程。修改对象时,它会发现它已更改并将相应地更新议程。但是,策略对象没有更改,因此即使引用列表中有新的验证,规则也会运行。

答案 1 :(得分:-1)

如果您不想多次执行规则,那么您可以撤消'记忆中的事实。它将避免多次执行规则。