在drools LHS中编译计算错误



我在编译一个规则时遇到问题,其中在LHS中执行计算并将结果与静态值进行比较。我正在处理的事实是给我的,我无法控制它们,所以请根据类来回答,而不是建议改变数据模型。

public class Item {
    private Map<String, Object> attributes;
    private List<String> errors;
}
public class ItemFacts {
    private Item newItem;
    private Item existingItem;
}
rule "validatePrice"
when
   $itemFacts:itemFacts(newItem != null, newItem.attributes != null,
        $price:newItem.attributes["price"] != null,
        $price#BigDecimal.scale > 4 ||
        $price#BigDecimal.precision - $price.BigDecimal.scale > 9)
then
    itemFacts.errors.add("Invalid size for price attribute.");
end

比例检查可以编译并正常工作,但是从精度中减去比例以确保小数点左边没有太多位置的检查将无法编译。我还尝试了"$price#BigDecimal"。(precision - scale)> 9´,但也不会编译。

如果您发现一个语法定义精确地记录了您打算在该规则的LHS和RHS上编写的内容,那么请遵循该文档。

同时,远离这些结构并尽可能地与Java保持接近。另外,我建议将公式放入函数或静态方法中。

function boolean checkSize( BigDecimal bd ){
    return bd.scale() > 4 || bd.precision() - bd.scale() > 9;
}
rule "validatePrice"
when
    $itemFacts: ItemFacts(newItem != null, newItem.attributes != null,
    $price:newItem.attributes["price"] != null,
    checkSize( (BigDecimal)$price ))
then
    $itemFacts.getNewItem().getErrors().add("Invalid size for price attribute.");
end

最新更新