Instancof在Drools规则引擎Drl文件中不起作用

时间:2019-12-17 11:05:26

标签: drools rule-engine business-rules

我正在将列表List<ArrayList> list = new ArrayList<>();传递给流口水,并且内部arraylist包含TaxPayer类的Objects,我的规则就像

rule "test1"
when
    $l:TaxList($k : list)
    $b:ArrayList() from $k
    $a:Object() from $b
then
    if($a instanceof TaxPayer){
        $b.add(new TaxPayer(7,6,5,4,3,2,1));
    }
    if($a instanceof Object){
        $b.add(new TaxPayer(1,2,3,4,5,6,7));
    }

end

第二个if可以正常工作,但是即使它是TaxPayer的一个实例,也不能在第一个中使用

1 个答案:

答案 0 :(得分:0)

我尝试执行上述规则:

rule "test"
when
    $c : TaxList($list : list)
    $b : ArrayList()  from $list
    $testobj: Object() from $b
then
    if ($testobj instanceof TaxPayer){
        System.out.println("inside if 1");
    }
    if ($testobj instanceof Object) {
        System.out.println("inside if 2 ");
    }
end

对我来说很好。我得到的输出是:

inside if 1
inside if 2 
inside if 1
inside if 2 ...

您能否详细说明问题并将相应的Java代码添加到问题中,以便我能够理解如何将对象插入drools会话中。请添加Java代码段和TaxList类。

编辑:

Main.java:

import java.util.ArrayList;

public class Main {
    public static void main(String[] args) {
        KieServices kieServices = KieServices.Factory.get();
        KieContainer kieContainer = kieServices.newKieClasspathContainer();
        KieSession kieSession = kieContainer.newKieSession("test");
        ArrayList<TaxPayer> taxPayers = new ArrayList<>();
        taxPayers.add(new TaxPayer(1));
        taxPayers.add(new TaxPayer(2));
        ArrayList<ArrayList> arrayLists = new ArrayList<>();
        arrayLists.add(taxPayers);
        TaxList taxList = new TaxList(arrayLists);
        kieSession.insert(taxList);
        kieSession.fireAllRules();
    }
}

TaxList.java:

import java.util.ArrayList;

public class TaxList {
    ArrayList<ArrayList> list = new ArrayList<>();


    public TaxList(ArrayList<ArrayList> list) {
        this.list = list;
    }

    public ArrayList<ArrayList> getList() {
        return list;
    }

    public void setList(ArrayList<ArrayList> list) {
        this.list = list;
    }
}

TaxPayer.java:

public class TaxPayer {
    private int id;

    public TaxPayer(int id) {
        this.id = id;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }
}