如何在流口水中进行日期计算?

How to do date calculation in drools?

我是 drools 的新手,对 drl 文件中的日期比较感到困惑。我有一个条件可以比较两个 Date 类型的事实。 drl 就像:

rule "TimeComparison"
    when
        $person:Person( date1 >= date2 )
    then
        //
end

在内部,date1 是已知日期之后的三天。如何在drl规则文件中实现(指定日期后的三个days/weeks/months)?

假设您的日期是 java.util.Date,< 运算符使用 Date.before(date) 而 > 运算符使用 Date.after(date).

我建议使用Date.compareTo(),如:

rule "TimeComparison"
    when
        $date
        $person:Person( date1.compareTo(date2) >= 0)
    then
        //
end

如果 date2 不是您想要比较的对象,则将其替换为内联所需的内容,例如 "date1 is after 'now'":

$person : Person(date1.compareTo(new Date()) >= 0)

使用 java.time 类 更容易,例如使用 LocalDate:

$person : Person(date1.plusDays(3).compareTo(date2))

如果比较日期是一个系统"known",可能希望将计算出的日期推断为一个新的事实(这里我只是简单的虚构了一个"PersonEffectiveDate"的概念)并使用在需要的地方(根据您的情况更改 approach/design/concept):

rule "Determine person effective date"
    when
        $person : Person($date1 : date1)
    then
        // do the desired calculation
        Date $effectiveDate = $date1.plusDays(3);
        PersonEffectiveDate ped = new PersonEffectiveDate($person, $effectiveDate);
        insert(ped);
end

rule "TimeComparison"
    when
        PersonEffectiveDate($person : person, $effectiveDate : effectiveDate >= date2)
    then
        //
end

或不同的东西,例如:

$person.date1.compareTo($effectiveDate) >= 0)