Drools - Ability to execute rules for random action result?

renaisy Source

Is it possible to define rules which action result can be determined randomly?

For example:

rule "X"

dialect "mvel"

when

     check : tRiskCheckData( riskValue <= 5 , Money <= 50000 )

then

     %%  this action is executed to accept the request

     check.setReturnCode( 0 );  

But, it is possible to define the rule that can accept the request randomly?

Such as:

if  rand < 0.5
       check.setReturnCode( 0 ); %%accept the request
else 
       check.setReturnCode( 1 ); %%refuse the request

Thanks!

droolsrule-engine

Answers

answered 2 years ago laune #1

You should define a simple class Oracle containing a java.util.Random object and use its nextDouble method in a method you might call

boolean accept( double prob );

Then you make this object visible as a global:

global Oracle pythia;

Set it before you call fireAllRules:

kieSession.setGlobal( "pythia", new Oracle() );

and use it on the right hand side:

if( pythia.accept( 0.5 ) ){
    check.setReturnCode( 0 ); // accept the request
} else { 
   check.setReturnCode( 1 );  // refuse the request
}

You should probably have one Random object per such decision since subsequences of pseudo-random numbers may exhibit certain regularities.

comments powered by Disqus