简体   繁体   English

在Drools对象的所有日期中查找最近的日期

[英]Finding the most recent date in from all dates in Drools object

How can I find the most recent date among all joining dates in a Drools object? 如何在Drools对象的所有加入日期中找到最近的日期?

My rule is something like this: 我的规则是这样的:

declare RecentDate
      dates : java.util.Date
end

rule "insert_dates"
      when
       //reading dates from back end
       Employee($date : dateOfJoining, $name : name, salary > 10000)
       then
       insert(new RecentDate($date))
end

Now I would like to find the most recent employee based on dateOfJoining. 现在我想基于dateOfJoining找到最近的员工。 Any help is appreciated. 任何帮助表示赞赏。 Thank you. 谢谢。

One way to do it is to, given an employee, make sure that there is no other with a higher date: 一种方法是,给予员工,确保没有其他更高的日期:

declare RecentDate
      dates : java.util.Date
end

rule "insert_dates"
when
  //reading dates from back end
  Employee( $date : dateOfJoining, $name : name, salary > 10000)
  not Employee (dateOfJoining > $date)
then
  insert(new RecentDate($date))
end

The rule above only works on the case you have all your Employee facts in your session before you execute fireAllRules() . 上面的规则仅适用于您在执行fireAllRules()之前在会话中拥有所有Employee事实的情况。 If this is not the case, you can try to initially insert a RecentDate with a null date and then compare each Employee against it and update it accordingly: 如果不是这种情况,您可以尝试最初插入带有空日期的RecentDate ,然后将每个Employee与它进行比较并相应地更新它:

rule "Init RecentDate"
when
  not RecentDate()
then
  insert new RecentDate(null);
end

rule "update_dates"
when
  Employee( $date : dateOfJoining, $name : name, salary > 10000)
  $d: RecentDate ( date == null || date < $date)
then
  modify ($d){
    setDate($date)
  }
end

Hope it helps, 希望能帮助到你,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM