Reputation: 1047
We've got a web app that takes in lots of request params then invoke drools rules. I've got an event listener to capture what rule name is fired. However, I'd like to have a link to each of the rule that gets fired to point to the actual implementation of the rule. Is this possible?
Do I have to parse the .drl files and build a map of rule name to rule content? or pre-processing each rule name in a separate file name etc..? is there anything Drools API that given the name of the rule, it gives the content of the rule?
Any input is appreciated.
Thanks & regards voki
Upvotes: 3
Views: 2207
Reputation: 3530
For people from future, The following piece of code will get the rule content. Working on drools 7. Not sure about previous versions.
public String getRuleContent(String ruleName, String packageName) {
Rule rule = getRule(ruleName,packageName)
.orElseThrow(() -> new RuntimeException("Rule with name: " + ruleName + " does not exist in package: " + packageName));
return new String(((ByteArrayResource) ((RuleImpl) rule).getResource()).getBytes());
}
public Optional<Rule> getRule(String ruleName, String packageName) {
log.info("Getting rule by name: {} from {} package.",ruleName, packageName);
KieBase kieBase = kieContainer.getKieBase();
Collection<KiePackage> kiePackages = kieBase.getKiePackages();
return kiePackages.stream()
.filter(kiePackage -> kiePackage.getName().equals(packageName))
.flatMap(kiePackage -> kiePackage.getRules().stream())
.filter(r -> r.getName().equals(ruleName))
.findFirst();
}
Upvotes: 1
Reputation: 9490
The source code of the rules is not accessible at runtime.
However, if you have one rule per .drl file, then a file naming convention based on the rule would enable you to open the original .drl file and potentially display that in a web page.
Alternatively, if you are using Guvnor as your rule repository, you could use its REST API to find the source code:
http://{server}/guvnor/rest/packages/{packageName}/assets/{ruleName}/source
Docs on the REST API are here:
http://docs.jboss.org/drools/release/5.5.0.Final/drools-guvnor-docs/html/ch09.html#d0e3500
Upvotes: 0