0
votes

I am looking for a way to categorize rules in my drl files and determine which category/ categories of rules fired.

I see that the Drools Guvnor documentation has something about categorization http://docs.jboss.org/drools/release/5.2.0.Final/drools-guvnor-docs/html/ch03.html#d0e228. However, it is unclear to me if this is metadata maintained by Guvnor or if it is part of the drl file/ rules execution. I have not been able to find a sample DRL file with category mentioned in it. And, a way to determine the category/ categories of rules that got fired.

I am aware of activation-group but that doesn't quite fit my need since once a rule in the activation-group fires the other rules in the group are not evaluated.

Thanks

1
How are you authoring your rules? Guvnor? Editor?laune
I don't use Guvnor. Eclipse/editorshikarishambu

1 Answers

1
votes

The best way to classify rules is to do it via metadata. A metadata entry is added to the rule:

rule metademo
  @meta2( 123 )
  @meta3( foo  )
  @meta4( "foo" )
when...then...end

You access the metadata via a Rule, obtained from a package or some event:

for( Rule rule: kPackage.getRules() ){
    Map<String,Object> key2meta = rule.getMetaData();
    for( Map.Entry<String,Object> entry: key2meta.entrySet() ){
        System.out.print( "   @" + entry.getKey() + "( " );
        Object value = entry.getValue();
        System.out.print( "[" + value.getClass().getSimpleName() + "] "
                          + value.toString() );
        System.out.println( " ) " );
    }
}

Output:

@meta2( [Integer] 123 ) 
@meta4( [String] foo ) 
@meta3( [String] foo ) 

There was a plan to permit Maps as value so you might use

@meta7( foo = "foo", bar = "bar" )

This didn't work in 5.5, and I never tried it again.