流口水如何动态触发指定组

drools how to fire specified group dynamically

我正在使用 drools 7.x

我的逻辑如下所示:

if(variableA == 1) {

    if(variableA1 == 2) {
        ....
    } else if(variableA1 == 3) {
        ....
    }
}else {

    if(variableB1 == 1) {
        ....
    }else if(variableB1 == 2) {

        if(variableN == 1) {

        }else if(variableN == 2) {

        }
    }
}

顺便说一句,这些变量不在同一个 class,我打算将它们作为事实插入 drl。

如何定义规则?或者我如何定义像 :

这样的规则
rule 1
    when
    then
end

rule 2
    when
    then
end

rule 1-1
    when
    then
end

rule 1-2
    when
    then
end

rule 2-1
    when
    then
end

rule 2-2
    when
    then
end

其中,在rule 1rule 2rule 1-1rule 1-2中只会触发一个规则是group1rule 2-1 rule 2-2group2.

如果 rule 1 被触发,那么 group1 中只有一个规则被触发,不需要测试 group2。而如果 rule 2 被触发,那么 group2 中只有一个规则被触发,没有必要测试 group1.

我将假设所有这些变量都存在于我将称之为输入的 class 中。您可以通过将 Inputs 的实例传递到规则中来调用这些规则。

我还假设您示例中的最后一个 'else if' 是一个类型,并且您实际上正在检查变量 N == 2。

rule "A1-2"
when
  Inputs( variableA == 1,
          variableA1 == 2 )
then
  // ...
end

rule "A1-3"
when
  Inputs( variableA == 1,
          variableA1 == 3 )
then
  // ...
end

rule "B1"
when
  Inputs( variableA != 1,
          variableB1 == 1 )
then
  // ...
end

rule "B2-N1"
when
  Inputs( variableA != 1,
          variableB1 == 2,
          variableN == 1 )
then
  // ...
end

rule "B2-N2"
when
  Inputs( variableA != 1,
          variableB1 == 2,
          variableN == 2 )
then
  // ...
end

相当直接。关键是您需要检查变量 A ==1 的条件对于处理 B 的规则是否 not 为真。基本上当您将 if/elsif/else 转换为规则,你需要否定你左边前面的 if 子句的条件。

不涉及“特定群体”。主要是因为没有解释这些词的意思。

Drools也有继承性。没有真正的理由在这里使用它,但如果你想的话你可以:

rule "A1"
when
  $i: Inputs(variableA == 1)
then // notice empty "then"
end

rule "A1 = 2" extends "A1"
when
  Inputs( variableA1 == 2 ) from $i
then
  //
end
  • 规则优先级的显着性
  • 逻辑插入依赖于其他规则
  • xor 逻辑的激活组

rule "1"
  salience 1
  activation-group "group 0"
  when
    $model : Model(a == 1)
  then
    insertLogical(new GroupActivation("group 1", $model));
    System.out.println("rule 1");
end

rule "2"
  salience 1
  activation-group "group 0"
  when
    $model : Model(b == 1)
  then
    insertLogical(new GroupActivation("group 2", $model));
    System.out.println("rule 2");
end

rule "1-1"
  activation-group "group 1"
  when
    GroupActivation(name == "group 1", $model : model)
    Model(this == $model, a1 == 1)
  then
    System.out.println("rule 1-1");
end

rule "1-2"
  activation-group "group 1"
  when
    GroupActivation(name == "group 1", $model : model)
    Model(this == $model, a2 == 1)
  then
    System.out.println("rule 1-2");
end

rule "2-1"
  activation-group "group 2"
  when
    GroupActivation(name == "group 2", $model : model)
    Model(this == $model, b1 == 1)
  then
    System.out.println("rule 2-1");
end

rule "2-2"
  activation-group "group 2"
  when
    GroupActivation(name == "group 2", $model : model)
    Model(this == $model, b2 == 1)
  then
    System.out.println("rule 2-2");
end

Model.java

public class Model {
    
    private int a;
    private int a1;
    private int a2;
    private int b;
    private int b1;
    private int b2;
...

GroupActivation.java

public class GroupActivation {
    
    private String name;
    private Model model;
...