Search code examples
javadrools

Empty List in Drools rule


I ran into a problem in a rule called "Due date for test1". I pass the list called "tests" as a parameter to check it for Test.Test1, but the list is empty, despite the fact that before that I filled it in the rule "test for type1". What is the problem?

here is code of entity "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;
    }
}

here is code of entity "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());
    }
}

here is drl code:

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

main class code:

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();
        }
    }
}

Solution

  • insert(m);
    insert(m);
    insert(m);
    

    This doesn't make any sense. m is already in Working Memory.

    What you need is one

    update(m);
    

    instead.