3

We are using JBoss Rules (a.k.a. Drools) and have several .drl files that each contain several rules. Is there a way to avoid duplication between files, so that we can define common rules that are available to more than one .drl file?

Unfortunately, there does not seem to be any kind of include or module facility.

TRiG
  • 10,148
  • 7
  • 57
  • 107
Peter Hilton
  • 17,211
  • 6
  • 50
  • 75

1 Answers1

2

There is no way of including rules from another .drl file from within a .drl file.

You can however add two .drl files to the same ruleBase and they will work as if they were in the same file.

PackageBuilder builder = new PackageBuilder();
builder.addPackageFromDrl( new InputStreamReader( getClass().getResourceAsStream( "common.drl" ) ) );
builder.addPackageFromDrl( new InputStreamReader( getClass().getResourceAsStream( "rules1.drl" ) ) );
RuleBase ruleBase  = RuleBaseFactory.newRuleBase();
ruleBase.addPackage( builder.getPackage()  );
Spike
  • 664
  • 5
  • 9
  • This answer was correct at the time it was written, but PackageBuilder was removed from Drools 6.1. See related question: [PackageBuilder not available in drools 6.1](https://stackoverflow.com/questions/27036793/packagebuilder-java-not-available-in-drools-6-1-0-final) – Gordon Hopper May 26 '20 at 16:13