首页 文章

禁用另一个drools规则从触发

提问于
浏览
2

嗨我想禁用其他一些drools规则,当另一组drools规则触发时,你会怎么做?

假设我有一个议程组“每日”,它有两套drools规则集A有规则“Default-1”,“Default-2”,“Default-3”set B有规则“Custom-1”,“ Custom-2“,”Custom-3“

当议程组“每日”聚焦,并且当前事实与自定义模式匹配时,我想要“自定义-1”和/或“自定义-2”和/或“自定义-3”仅触发;否则,只有“Default-1”和/或“Default-2”和/或“Default-3”触发 .

问题是,Default-1/2/3总是被触发 . 我需要一种方法在Custom-1/2/3中禁用它们 . 首先,我将Custom-1/2/3中的显着性级别设置为高于Default-1/2/3 . 然后我尝试使用激活组 . 但是,如果我将所有这些设置为同一个激活组,则六个规则中只有一个会触发,这不是我想要的 .

我不允许更改.java模块,它每次都会加载所有规则 . 我只能更改.drl drools规则 .

谢谢 .

2 回答

  • 1

    使用Declarative Agenda存在Drools对此类行为的支持 .

    它主要提供了这种上下文方法:

    void blockMatch(Match match);
    void unblockAllMatches(Match match);
    void cancelMatch(Match match);
    

    阻止其他规则的规则阻止其他规则仍然是 true 或实际上是明确解除阻止 .

  • 5

    您可以尝试用标记对象解决您的问题 . 假设您定义了Marker类:

    public class Marker {
        String uniqueIdentifier;
        //getter and setter, etc
    }
    

    (drools允许您在* .drl代码中定义新类而无需使用* .java)
    然后让自定义组在默认组之前运行(显着性可以工作,定义流也可以工作)和"mark"那些通过在内存中插入新的Marker事实来触发自定义规则的对象,如下所示:

    when
       SomeObject($unique: someIdentifier)
       //normal conditions
    then
       insert(new Marker($unique))
       //normal action
    

    并且默认规则仅对未触发自定义规则的对象起作用:

    when
       SomeObject($unique: someIdentifier)
       not Marker(uniqueIdentifier = $unique)
       //normal conditions
    then
       //normal action
    

    另外,为防止泄漏,您可能需要清理第3(最后)组规则:

    when
       SomeObject($unique: someIdentifier)
       $marker : Marker(uniqueIdentifier = $unique)
    then
       retract($marker)
    

相关问题