2
votes

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

2
There's no such thing as the "rule content" to which you might have a link. Perhaps you modify the Q to describe what you really want to do (and I don't think it's poring over some object code).laune

2 Answers

1
votes

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();

}
0
votes

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