我想知道是否可以使用drools累积表达式获得具有更高优先级的对象? 这是我的代码:
rule "dropShiftWithTheLowestPriorityTaskForWeekdayMorningShiftReassignment"
when
ShiftAssignment( isNeedToReassign == true, shiftType.code == 'E', weekend == false,
$shiftDate : shiftDate,
$shiftType : shiftType )
$max : Number(intValue >= 0) from accumulate(
$assignment : ShiftAssignment(
weekend == false,
shiftDate == $shiftDate,
shiftType == $shiftType,
$totalWeight : totalWeight),
max($totalWeight)
)
then
System.out.println('----------');
System.out.println('max='+$max);
我刚刚获得最大totalWeight,但我不知道如何获得包含该totalWeight 的对象。 请帮助我,谢谢。
最佳答案
前几天有一个类似的问题: How to get max min item from a list in Drools
那里发布了 2 个解决方案:
- 创建您自己的累积函数
- 使用 2 个简单的模式
按照第一种方法,您需要编写如下内容:
when
$maxAssignment: ShiftAssignment() from accumulate(
$sa: ShiftAssignment(),
init( ShiftAssignment max = null; ),
action( if( max == null || max.totalWeight < $sa.totalWeight ){
max = $sa;
} ),
result( max ) )
then
//$maxAssignment contains the ShiftAssignment with the highest weight.
end
请注意,此实现只能用于原型(prototype)设计或测试目的。在 Java 中实现自定义累加函数被认为是一个很好的实践。
按照第二种方法,您的规则可以重写为:
when
$maxAssignment: ShiftAssignment(..., $maxWeight: totalWeight)
not ShiftAssignment(..., totalWeight > $maxWeight)
then
//$maxAssignment contains the ShiftAssignment with the highest weight.
end
希望对你有帮助
关于java - Drools 获取具有更高优先级的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29052839/