【问题标题】:Drools: Default value for queries in case of no matchDrools:不匹配时查询的默认值
【发布时间】:2017-08-05 04:51:27
【问题描述】:

我正在编写一个 Drools 规则集处理 AB 类型的事件;在许多规则中,我需要将A 的属性timestampB 的属性windowStart 的最大子集进行比较,以限定A 事件。如果B 的那个子集是一个空集,我假设没有windowStart,因此需要0 的值。任何timestamp 大于windowStartA 都符合条件。

考虑以下伪代码进行阐述:

long findMaxWindowStartOrZero(int bID)
{
    Set bSubset = getAllBWithID(bID);
    if(bSubset is empty) return 0;
    return max(bSubset, B::windowStart);
}

如果没有这样的查询,则需要复制与此比较有关的每条规则的 LHS,一次是为了说明没有任何 B,一次是为了在子集非空时找到实际的最大值。

使用上面的伪代码这样的查询使这项任务变得更加容易,并且不需要分支 LHS。有可能这样做吗?这样做而不是对上述规则的 LHS 进行分支有什么缺点或好处吗?

【问题讨论】:

    标签: drools drools-fusion drools-kie-server


    【解决方案1】:
    rule qualify_A
    when
        accumulate( B( id == "bID", $ws: windowStart ); $mws: max( $ws ) )
        $a: A( timestamp > $mws )
    then
        ...process $a...
    end
    

    我认为如果没有匹配的 B,则不会触发此规则。要解决此问题,请插入一个 B 并将 windowStart 设置为 0。此虚拟对象还可用于定义匹配 B 的 id 的值:

    rule qualify_A
    when
        B( $id: id, windowStart == 0 )   // the dummy, defines id 
        accumulate( B( id == $id, $ws: windowStart ); $mws: max( $ws ) )
        $a: A( timestamp > $mws )
    then
        ...process $a...
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-11-26
      • 1970-01-01
      • 2020-09-02
      • 1970-01-01
      • 1970-01-01
      • 2013-12-07
      • 2022-10-24
      相关资源
      最近更新 更多