Drools -Dyanmic与操纵当前日期的日期比较

时间:2018-06-10 09:35:02

标签: java drools

我有一个java类,它有人员创建日期字段。

public class PersonPer 


public Date getCreationDate() {
    return creationDate == null ? null : new 
Date(creationDate.getTime());
}

public void setCreationDate(Date creationDate) {
    this.creationDate = creationDate == null ? null : new 
    Date(creationDate.getTime());
}

我需要比较这个创建日期应该小于,在Drools中减少当前日期的X个月(我希望在配置文件/ java文件中传递X个月值,比如3个月)。即当前日期 - 2018年6月10日,X月(3个月) - 2018年4月10日,创建日期应在2018年4月10日之前。

但它没有用。

1)如何将dyanmic值(从java)传递给'drools中的函数'?

JAVA(现在是Junit) - >

    `int RepresentativeAppointmentMonth=3
    session.insert(personPer);
    session.insert(RepresentativeAppointmentMonth);`

DRL - >

 declare Facts
 ...
 repMonth:RepMonth

 ..
 end
 declare RepMonth
     month: int
 end

 function Date workWithDates(int m)
 {

 ..

 return NewDateWithMmonthsubtractedFromCurrentDate;
 }

rule "Date check"
when
    RepMonth(m : month)`    //<- But if I add this line, the next line doesn't trigger, if I comment this line, it works, with static value of Months(hardcoding 3 months-> which I dont want.
    PersonPer(CreationDate > workWithDates(m))
then 
    System.out.println("Rep");
end

2)还有其他建议吗?我尝试了很多方法,但有些东西缺失了。我是Drools的新手。

1 个答案:

答案 0 :(得分:0)

正如@Esteban所说,您必须将RepMonth对象添加到您的会话中。在您的Java代码中执行此操作

    int RepresentativeAppointmentMonth=3
    RepMonth repMonth = new RepMonth(RepresentativeAppointmentMonth);
    session.insert(personPer);
    session.insert(repMonth);
    session.insert(RepresentativeAppointmentMonth);