Inheritance of drooling rules

I am new to drooling and familiar with using the extends keyword to inherit the rule. Question: Is there a way to inherit multiple rules? It will be like using multiple interfaces in a java class. Here is an example of how I expect it to work, but I get an error in rule 3:

rule "rule 1" when //person name == "John" then //print "John" end rule "rule 2" when //person last name == "Smith" then //print "Smith" end rule "rule 3" extends "rule 1", "rule 2" when //person age > 20 then //print John Smith is older than 20 end 

Thanks dawn

+4
source share
2 answers

This is still not well documented, but one single inheritance exists in drools. It allows you to create a rule that extends another rule. A sub rule will be triggered if and only if both conditions for the super rule and subfield are true. (See My notes below).

In the example below, Flags is a simple Java class with 2 Booleans: isSuperTrue and isSubTrue. The magic phrase extends "super" as part of the definition of the sub rule. The names of the rules (sub and super) are illustrative and can be changed to any name of a legal rule.

 rule "super" @description("Fires when isSuperTrue is true regardless of the state of isSubTrue") when $flag : Flags(isSuperTrue == true) then System.out.println("super rule should fire anytime super is true and ignore sub"); end rule "sub" extends "super" @description("Fires only when both isSubTrue and isSuperTrue are true") when Flags(isSubTrue == true) then System.out.println("sub rule should fire when both isSubTrue and isSuperTrue are true"); end 

Note 1: In the file 5.5.0. <value> nofollow "> issue is

Note 2: this functionality is indirectly documented in for 5.5.0.Final in section 4.1.1.3. The main topic is Conditional Named Consequences, but it uses rule inheritance.

+3
source

I know this thread is old, but you can do the following:

 rule "first name is John rule" when $p : Person( name == 'John' ) then end rule "last name is Smith rule" extends "first name rule" when eval( $p.getLastName() == "Smith" ) then end rule "age older than 20 rule" extends "last name rule" when eval ( $p.getAge() > 20 ) then System.out.println($p.getFirstName() + " " + $p.getLastName() + " is older than 20"); end rule "age younger than 20 rule" extends "last name rule" when eval ( $p.getAge() < 20 ) then System.out.println($p.getFirstName() + " " + $p.getLastName() + " is younger than 20"); end 

As you can see, you can create a chain rule from super rules that inherit declared variables.

+1
source

All Articles