Using Single Inheritance for Rules in Drools 5.5

Using Single Inheritance for Rules in Drools 5.5

Single inheritance in Drools for rules isn’t very well documented; however, it is possible to create a rule that extends another rule in Drools.

NOTE: This example is known to work for Drools 5.3.0.Final and subsequent releases, and may work in prior 5.X releases. This example was built using Drools 5.5.0.Final; and was tested in Drools 5.3.0.Final.

The following example shows a simple case of Drools single inheritance for a rule. The ‘Object’ is a Java class that has two defined boolean fields, condition1 and condition2. The parent rule “Base” is defined first, and the child rule “ExtensionOfBase” is defined after the rule “Base”. The keywords extends is used to identify the extension of the parent rule “Base” within the child rule “ExtensionOfBase”.

NOTE: The child rule will fire if, and only if, the conditions for the parent rule and child rule are evaluated as true. Also, if the child rule “ExtensionOfBase” is evaluated as true, both the parent rule “Base” and child rule “ExtensionOfBase” semantic blocks are executed.

rule "Base"
    when
        Object ( condition1 == true )
    then
        System.out.println("Base will fire if Object.condition1 evaluates to true");
end

rule "ExtensionOfBase" extends "Base"
    when
        Object ( condition2 == true )
    then
        System.out.println("Base will fire if Object.condition1 and Object.condition2 evaluates to true");
end

Author: daharveyjr

I’m a solution architect responsible for the design, development, implementation, testing, and maintenance of e-commerce operations and applications using the Hybris and WebSphere Commerce product suites and other web technologies such as Java, J2EE/JEE, Spring, PHP, WordPress and more. Twitter | Facebook | LinkedIn

Leave a Reply

Your email address will not be published. Required fields are marked *