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

时间:2015-03-14 18:50:37

标签: java drools

我想知道是否可以使用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);

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

1 个答案:

答案 0 :(得分:1)

几天前发布了一个类似的问题: How to get max min item from a list in Drools

有两个解决方案:

  1. 创建自己的累积功能
  2. 使用2个简单模式
  3. 按照第一种方法,您需要编写如下内容:

    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
    

    希望它有所帮助,