[英]Optaplanner - Drools for int array constraint
我正在尝试编写一个约束来检查 integer 数组(binUsedArray)的任何元素是否大于界限(dailyMaxNb)。
rule "BinResourceConstraint"
when
BinResource($array : binUsedArray, $dailyMaxNb : dailyMaxNb)
$x : Integer() from $array
$x > $dailyMaxNb
then
scoreHolder.addHardConstraintMatch(kcontext, -1);
end
我阅读了 drools doc 5.1.8 并尝试编写类似的规则
rule "Iterate the numbers"
when
$xs : List()
$x : Integer() from $xs
then
$x matches and binds to each Integer in the collection
end
但有一些错误:
Caused by: java.lang.RuntimeException: [Message [id=1, kieBase=defaultKieBase, level=ERROR, path=com/cttq/aps/solver/taskScheduleConstraint.drl, line=78, column=0
text=[ERR 102] Line 78:11 mismatched input '>' in rule "BinResourceConstraint"], Message [id=2, kieBase=defaultKieBase, level=ERROR, path=com/cttq/aps/solver/taskScheduleConstraint.drl, line=0, column=0
text=Parser returned a null Package]]
===== 使用 BinResource class 更新, binUsedArray
是一个大小为 30 的 int 数组,用于保留接下来 30 天使用的 bin 数量。
@PlanningEntity
public class BinResource {
private String index;
private String binType;
private String binArea;
private int dailyMaxNb;
@CustomShadowVariable(variableListenerRef = @PlanningVariableReference(entityClass = TaskAssignment.class, variableName = "modifiedProcessTimeInShift"))
private int[] binUsedArray;
简单的解决方案是将您的数组转换为 List ,然后它就可以工作了。 可以在 LHS 上调用Arrays.asList( array )
来转换数组...类似于:
BinResource($array : binUsedArray, $dailyMaxNb : dailyMaxNb)
$binUsed: List() from Arrays.asList($array)
然后您可以从第二条规则中执行相同的逻辑来查找符合您条件的值:
$x : Integer(this > $dailyMaxNb) from $binUsed
这样,您的规则将为数组/列表中大于dailyMaxNb
值的每个值 ( $x
) 触发一次。
rule "BinResourceConstraint"
when
BinResource($array : binUsedArray, $dailyMaxNb : dailyMaxNb)
$binUsed: List() from Arrays.asList($array)
$x : Integer(this > $dailyMaxNb) from $binUsed
then
// this will trigger once PER MATCH ...
// eg if there are 3 values that > dailyMaxNb, this will trigger 3 times
scoreHolder.addHardConstraintMatch(kcontext, -1);
end
但是,如果您的binUsedArray
实际上是ArrayList
,则可以省略转换并仅使用$x: Integer(this > $dailyMaxNb) from $array
。 我提到这一点是因为有时当人们询问“数组”时,他们实际上指的是数组列表,而您没有提供 BinResource class 的代码。 话虽如此,您可以将这个模式( MyType( <condition> ) from $collection
)用于任何可迭代的集合,以尝试匹配该集合中的所有值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.