首页 > 解决方案 > 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)如何将动态值(来自java)传递给“流口水功能”?

JAVA(现在是Junit)->

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

深度学习-->

 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 的新手。

标签: javadrools

解决方案


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

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

推荐阅读