1

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!

abhijeet mane
  • 121
  • 1
  • 7

1 Answers1

2

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...

laune
  • 31,114
  • 3
  • 29
  • 42
  • Thanks laune. I have changed my approach now. I am not taking any global integer variable , its added to the object as a fact which i can utilize in the DRL files.This make the logic more simple. – abhijeet mane Mar 10 '15 at 05:35
  • This is frequently the better approach. – laune Mar 10 '15 at 05:44