我有一个课程如下:
import java.sql.Timestamp;
class Order{
private Timestamp orderDate;
private boolean valid = true;
// Getter and Setter methods
}
我将这个对象从另一个类传递给Drools:
class Caller{
Order order = new Order();
order.setOrderDate(new Timestamp(new Date().getTime()));
...
kSession = kContainer.newStatelessKieSession();
kSession.execute(oInfo);
}
现在,如果orderDate超过30天,我需要将“valid”设置为false。我的drools文件模板如下:
package com.drools.rules
//list any import classes here.
import com.test.Order;
//declare any global variables here
rule "Order date checker"
when
$o : Order(orderDate <What should I put here?>)
then
$o.setValid(false);
end
答案 0 :(得分:0)
插入时间戳等于&#34;现在 - 30天&#34;。
Date deadline = new Date( new Date().getTime() - 30L*24*60*60*1000 );
ksession.insert( deadline );
然后你可以比较日期到现在30d:
rule isDue
when
$d: Date()
Foo( $name: name, date < $d )
then
System.out.println( "overdue: " + $name );
end
但是,您正在使用时间戳,因此您必须再编写一些代码:
Date( $t: time )
$o : Order(orderDate.getTime() < $t)
<强>后来强>
您可以在规则中插入Date对象。只需确保以最高优先级触发它:
rule "set Date"
salience Integer.MAX_VALUE
when
then
Date deadline = new Date( new Date().getTime() - 30L*24*60*60*1000 );
insert( deadline );
end