0

I have a requirement to get rule name in my custom function where I use it for some processing , Below code is how I'm trying to do. If this is not possible directly, is there an alternative . BTW currently we are using Drools 5.6

 import org.drools.spi.KnowledgeHelper;    
            function boolean printRuleName(KnowledgeHelper context ) {
                System.out.println(context.getRule().getName());
              return true;
             }

            // rule values at C15, header at C10

            rule "MY_RULE_15"
                salience 65521
                when

                    k:StatefulKnowledgeSession(true == "true")
                    //context: KnowledgeHelper(true=="true")
                    m:Map(true == "true")
                    Map((printRuleName(kcontext) == "true")

                then
                    System.out.println(kcontext.getRule().getName());
    //this works in action
        end
        //Map((printRuleName(kcontext) == "true") this is causing null pointer exception, kcontext is not getting injected 
Praneeth
  • 559
  • 9
  • 19

1 Answers1

0

There is no rule context on the left hand side of a rule, i.e., during condition evaluation.

If you really need the rule name on the left hand side (which isn't going to be useful anyway) you'll have to write string literal containing the rule's name as a parameter.

I suggest that the reason for having this requirement is reviewed.

Note that printRuleName(kcontext) == "true" is never going to be true as it compares a boolean with a String. Also, comparing true == "true" doesn't make any sense.

laune
  • 31,114
  • 3
  • 29
  • 42
  • Thank you for the response!! can you please elaborate on "you'll have to write string literal containing the rule's name as a parameter". How we can do that please. – Praneeth May 10 '17 at 17:12
  • A method call like this: `x("y")` – laune May 11 '17 at 04:31