Hi,
What is the best way to detect duplicate rule names in drl files? In my
application a duplicate rule name is an error.
There are multiple drl files loaded into one package. If the rule name is
duplicated across the files only the last rule is used. This behavior is clearly
documented.
The code below silently eats duplicated rules.
Reader sourceDrl = new InputStreamReader( new FileInputStream("r1.drl"));
Reader sourceDsl = new InputStreamReader( new FileInputStream("r.dsl"));
Reader sourceDrlTwo = new InputStreamReader( new FileInputStream("r2.drl"));
Reader sourceDslTwo = new InputStreamReader( new FileInputStream("r.dsl"));
PackageBuilder builder = new PackageBuilder();
builder.addPackageFromDrl( sourceDrl, sourceDsl );
builder.addPackageFromDrl( sourceDrlTwo, sourceDslTwo );
System.out.println(builder.hasErrors()); // returns false
// A duplicate rule is not an error
Package pkg = builder.getPackage();
Is there a better alternative than loading each drl into a separate
PackageBuilder to track the individual rule counts in each drl file?
eg
PackageBuilder builder = new PackageBuilder();
builder.addPackageFromDrl( sourceDrl, sourceDsl );
int ruleCountOne = builder.getPackage().getRules().length;
PackageBuilder builderTwo = new PackageBuilder();
builderTwo.addPackageFromDrl( sourceDrlTwo, sourceDslTwo );
int ruleCountTwo = builderTwo.getPackage().getRules().length;
// now re-compile file (reset readers as well...)
builder.addPackageFromDrl( sourceDrlTwo, sourceDslTwo );
if (builder.getPackage().getRules().length != (ruleCountOne + ruleCountTwo)) {
System.out.println("A duplicate exists - No clue what the name is");
// grep -i rule *.drl | sort | uniq -c
}
This does not detect a duplicate rule name in the same file - that would be even
better.
Regards,
Jared Davis