Java 在Optaplanner中,drools Accountage元素是如何工作的?

Java 在Optaplanner中,drools Accountage元素是如何工作的?,java,drools,optaplanner,Java,Drools,Optaplanner,我想知道Optaplanner中的drools Accumeration元素是如何工作的?似乎sum()函数没有返回我期望的值。我有两个简单的POJOProductionPackage和SKU @PlanningEntity public class ProductionPackage{ // This is a custom date object containing information about a date. Constant Date initialized on st

我想知道Optaplanner中的drools Accumeration元素是如何工作的?似乎sum()函数没有返回我期望的值。我有两个简单的POJO
ProductionPackage
SKU

@PlanningEntity
public class ProductionPackage{ 
    // This is a custom date object containing information about a date. Constant Date initialized on start. 
    // Custom date object NOT util.Date or sql.Date
    private Date date;
    // The number of units in this package. Constant value initialized on program start.
    private int productionUnits;
    // The ID of the production package
    private int productionPackageId;

    // The SKU assigned to this production package
    @PlanningVariable(valueRangeProviderRefs = "skuRange")
    private SKU sku;
    
    // Getters, setters, constructors...
}

public class SKU {
    // Unique identifier for this product.
    private String sku;
    
    //Getters, setters, constructors...
}
PlanningSolution
如下所示:

@PlanningSolution
public class ProductionPlan {

    @ProblemFactCollectionProperty
    @ValueRangeProvider(id = "skuRange")
    private List<SKU> skuList;

    @ProblemFactCollectionProperty
    private List<Date> dateList;

    @PlanningEntityCollectionProperty
    List<ProductionPackage> ProductionPackageList;

    //Getters, setters, constructors...
}
rule "At least 10 production units per SKU and per Day"
    enabled true
    when
        // Bind SKU to variable
        $sku : SKU()
        // Bind Date object to variable
        $date : Date()
        accumulate(
            ProductionPackage(sku == $sku,
            date == $date,
            $productionUnits : productionUnits);
            $productionUnitsTotal : sum($productionUnits);
            $productionUnitsTotal < 10
            )
    then
        // For debugging purposes
        System.out.println("SKU: " + $sku.toString());
        System.out.println("Date: " + $date.toString());
        System.out.println("ProductionUnits: " + $productionUnitsTotal);
        scoreHolder.addHardConstraintMatch(kcontext, -1);
end
在Drools文档中指出,累积元素迭代集合的所有元素。对我来说,此规则选择了
SKU
Date
的组合。然后它迭代包含
ProductionPackage
对象的
ProductionPackageList
。如果
日期
SKU
与之前选择的
SKU
日期
对象匹配,则对
生产单位
进行求和。我希望这将返回每个
SKU
和每个
日期生产的总单位(有时看起来是这样的)。但是,我注意到,
System.out.println(“ProductionUnits:+$productionUnitsTotal”)语句正在打印
0
。这对我来说真的很奇怪,因为
productionUnits
总是一个大于0的整数。它如何打印
0
?如果两个条件都匹配(
$date=date
$sku==sku
),则
sku
不应具有0个生产单位,因为它已分配给其
生产单位
>0的生产包


我认为这个错误是因为我真的不明白Optaplanner或Drools在做什么。我已打开调试/跟踪模式,但这些模式对我帮助不大。

我认为您的问题将是您应用的限制:
$productionUnitsTotal<10
。你可以拥有你想要的所有正生产单位,但如果它们超过了限制,总数将保持为0。尝试从“累积”中删除该限制,看看会发生什么。@RoddyOfFrozenpeas如果我删除该限制,println语句将打印大于0的数字,但绝大多数打印为0。那么很可能绝大多数输入实际上都有0的值。打印数字大于0的是具有合法值但被限制阻止的数字。我建议检查您的输入(日期条件有点可疑……您可能有取整问题吗?@RoddyOfFrozenpeas我已经检查过了,实际上我正在使用测试数据库用非常高的值初始化我的对象。有时生产单位大于1500,我得到相同的结果。我在任何地方都使用整数,所以我不认为它是四舍五入的。当drools找不到匹配项时,它似乎返回了默认值0。如果您的大多数值都高于
<10
限制值,那么这很可能是罪魁祸首。。。