Drools 规则中的空列表

Empty List in Drools rule

我 运行 遇到一个名为 "Due date for test1" 的规则中的问题。我将名为 "tests" 的列表作为参数传递给 检查它是否有 Test.Test1,但列表是空的,尽管在此之前我在规则 "test for type1" 中填写了它。有什么问题?

这里是实体的代码 "Machine":

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class Machine {
    private String type;
    public    ArrayList<Test> tests= new ArrayList<Test>();
    private List<String> functions = new ArrayList<String>();
    private Date creationTime =null;
    private Date testTime = null;
    public Machine (String type) {
        this.type= type;
    }
    public void setType(String type) {
        this.type= type;
    }
    public String getType() {
        return type;
    }
    public void setTests(ArrayList<Test> tests) {
        this.tests = tests;
    }
    public void setFunctions(List<String> functions) {
        this.functions = functions;
    }
    public List<String> functions() {
        return functions;
    }
    public List<Test> getTests() {
        return tests;
    }
    public void setCreationTime(Date creationTime) {
        this.creationTime = creationTime;
    }
    public Date getCreationTime() {
        return creationTime;
    }
    public void setTestTime(Date testTime) {
        this.testTime = testTime;
    }
    public Date getTestTime() {
        return testTime;
    }
}

这里是实体的代码 "Test":

 package com.sample;
import java.util.Calendar;
public enum Test {
    Test1(1),Test2(2),Test3(3);
    private Integer id;
    Test(Integer id) {
        this.id=id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public Integer getId() {
        return id;
    }
    public void setTestsDueTime(Machine m,int numOfDays) {
        setTest(m,Calendar.DAY_OF_YEAR,numOfDays);
    }
    public void setTest(Machine m,int calendarType,int numOfDays) {
        Calendar c = Calendar.getInstance();
        c.setTime(m.getCreationTime());
        c.add(calendarType, numOfDays);
        m.setTestTime(c.getTime());
    }
}

这里是drl代码:

package com.sample
import com.sample.Machine;
import com.sample.Test;
rule "test for type1"
when
m : Machine(type == "type1");
then
Test t1=Test.Test1;
Test t2 = Test.Test2;
Test t3 = Test.Test3;
m.getTests().add(t1);
m.getTests().add(t2);
m.getTests().add(t3);
insert(m);
insert(m);
insert(m) ;
end
rule "Due date for test1"
when
m:Machine(type == "type1", tests contains Test.Test1);
then
System.out.print("условие прошло");
//t.setTestsDueTime( m,3);
end

主要class代码:

package com.sample;
import org.kie.api.KieServices;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
public class DroolsTest {
    public static final void main(String[] args) {
        try {
            // load up the knowledge base
            KieServices ks = KieServices.Factory.get();
            KieContainer kContainer = ks.getKieClasspathContainer();
            KieSession kSession = kContainer.newKieSession("ksession-rules");
            // go !
            Machine m = new Machine("type1");
            kSession.insert(m);
            kSession.fireAllRules();
            } catch (Throwable t) {
            t.printStackTrace();
        }
    }
}
insert(m);
insert(m);
insert(m);

这没有任何意义。 m 已在工作内存中。

你需要的是一个

update(m);

相反。