更新全局整型变量

Updating the global integer variable

我正在用规则 1 中的某个值修改全局整数变量。 我需要在其他一些规则中获得这个修改后的值,比如规则 2。

global Integer deviceCapacity; 

rule "Rule 1"
dialect "java"
no-loop true
when
    $snrData : SensorDataVO(getWeightOffset().size()>0,
    $initOffset:getInitialOffset());
then
    for(Integer iOff:$snrData.getWeightOffset()){
        $snrData.getOffsetChngesInterval().add(iOff-$initOffset);
        insert (new NotificationVO(iOff-$initOffset));
    }
    deviceCapacity=$snrData.getDeviceCapacity();
end


rule "Rule 2"
dialect "java"
no-loop true
when
    $mstData : MasterDataVO();
    $notification:NotificationVO((getOffsetWeight()/4).
    equals($mstData.getRodentWeight()));
then
    System.out.println("Abhijeet--"+deviceCapacity);
end

我无法访问规则 2 中更新的 deviceCapacity 值,因为我想使用 deviceCapacity 值代替“4”,并希望在 deviceCapacity 变为 0 (deviceCapacity--) 之前执行此操作。 请帮忙!

您只能使用 API:

更改存储在 DRL 全局中的 对象引用 的值
then
//...
kcontext.getKieRuntime().setGlobal( "deviceCapacity",
                                    $snrData.getDeviceCapacity() );
end

很明显,这很丑陋并且在编译时没有检查。你可以考虑写一个包装器

class IntWrapper {
    private int value;
    // getter, setter
}

global IntWrapper deviceCapacity; 

那你可以

deviceCapacity.setValue( $snrData.getDeviceCapacity() );

int valuepublic...