Search code examples
globaldrools

Updating the global integer variable


I am modifying the global integer variable with some value in the Rule 1. I need to get this modified value in some other rule say Rule 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

I am not able to access the updated deviceCapacity value in Rule 2,as i want to use the deviceCapacity value in place of "4" and want to do this until the deviceCapacity becomes 0 (deviceCapacity--). Please help!


Solution

  • You can change the value of the object reference stored in a DRL global only by using the API:

    then
    //...
    kcontext.getKieRuntime().setGlobal( "deviceCapacity",
                                        $snrData.getDeviceCapacity() );
    end
    

    Clearly, this is ugly and not checked at compile time. You might consider writing a wrapper

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

    Then you could do

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

    Or make int value public...