I am new to drools and am familiar with using the extends keyword to inherit a rule. Question is there a way to inherit multiple rules? This would be similar to using multiple interfaces on a java class. Here's an example of how I would expect it to work but I get an error on 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
Well, the answer is we can't call a Rule from another Rule. Drools matches the Rules with incoming data/facts, and if the data satisfies the Rule condition, it stores the data in an Agenda. It might be possible for the same data or facts to be matched by different rules, so it stores matching facts in an Agenda.
Rule Definition − It consists of the Rule Name, the condition, and the Consequence. Drools keywords are rule, when, then, and end. In the above example, the rule names are “Hello World” and “GoodBye”. The when part is the condition in both the rules and the then part is the consequence.
Drools is a Business Rule Management System (BRMS) solution. It provides a rule engine which processes facts and produces output as a result of rules and facts processing. Centralization of business logic makes it possible to introduce changes fast and cheap.
A DRL file can contain one or more rules that define at minimum the rule conditions ( when ) and actions ( then ). The DRL designer in Decision Central provides syntax highlighting for Java, DRL, and XML. All data objects related to a DRL rule must be in the same project package as the DRL rule in Decision Central.
It isn't well documented yet, but single inheritance does exist in drools. It allows you to create a rule that extends another rule. The sub rule will fire if and only if both of the conditions for the super rule AND the sub rule are true. (See my notes at the bottom).
In the example below, "Flags" is a simple Java class with 2 booleans: isSuperTrue and isSubTrue. The magic phrase is extends "super" as part of the "sub" rule definition. The names of the rules (sub and super) are illustrative and can be changed to any legal rule name.
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: There is an issue in 5.5.0.Final that requires the super rule to be placed before the sub rule in the rule definition file. I've confirmed that the bug is fixed for 5.6.0.CR1.
Note 2: This functionality is indirectly documented in the release notes for 5.5.0.Final in section 4.1.1.3. The core topic is "Conditional named consequences," but it leverages rule inheritance.
I know that this thread is old but it's possible to 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 chained rule from super rules inheriting their declared variables.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With