Search code examples
unit-testingvariablesdroolsdurationdrools-fusion

Drools : How to use a variable in the duration specification?


I have a set of rules that fire each 15seconds.

As I want to unit-test these rules, I want to set dynamically this duration value. More, I want to put it in my app's configuration. This value might be used in more than 20 rules...).

How can I do this ? Is it possible ?

Here is my rule :

rule "my rule Name"
duration 15000
when
    //match something
then
    //do something
end 

I would like to have something like :

gobal String timeDuration;

rule "my rule Name"
duration timeDuration
when
    //match something
then
    //do something
end 

I tried : - putting a long global variable set from my unit test - putting a String global variable set from my unit test containing "15s" - Import a class containing a static field and put


import my.temp.package.RemoveThisUglyClass;
rule "my rule Name"
duration RemoveThisUglyClass.timeDuration
when
    //match something
then
    //do something
end 

Seems that there is no way to do it. A I right ? Any suggestion ?

Thanx !


Solution

  • The duration rule attribute is deprecated for quite some time. You can use timer:

    rule "tock"
    timer( expr: $d )
    when
        A($d: duration )
    then
        //...
    end
    

    This fires after the number of milliseconds in duration, but you can also use String fields containing times like "3s". See the documentation.