Drools 获取具有更高优先级的对象

Drools Get Object That Has The Higher Priority

我想知道我们是否可以使用drools accumulate expression 来获取具有更高优先级的对象? 这是我的代码:

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);

我只获得了最大总重量,但我不知道如何获得包含该总重量的对象。 请帮助我,谢谢。

前几天也有人发过类似的问题:

那里发布了 2 个解决方案:

  1. 创建您自己的累积函数
  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

请注意,此实现应仅用于原型设计或测试目的。在 Java 中实现您的自定义累积函数被认为是一种很好的做法。

按照第二种方法,您的规则可以重写为:

when 
    $maxAssignment: ShiftAssignment(..., $maxWeight: totalWeight)
    not ShiftAssignment(..., totalWeight > $maxWeight)
then
    //$maxAssignment contains the ShiftAssignment with the highest weight. 
end

希望对您有所帮助,