3

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

Tin Ng
  • 937
  • 2
  • 11
  • 25
  • 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 Feb 06 '14 at 11:45

2 Answers2

1

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

}
Arun Gowda
  • 2,721
  • 5
  • 29
  • 50
  • How would you create the kieContainer here for the Business Central using Drools 7.49 – m b Apr 12 '21 at 12:10
  • How could I get the kieContainer object for the Business Central from KieServicesClient where I have defined the rules as I have KieServicesClient object with KieServicesConfiguration configured with Business Central url "kie-server/services/rest/server" – m b Apr 13 '21 at 07:26
  • @m b It's hard to interpret what exactly you are asking for without some code examples. It would be better if you can create a new question with sample code. – Arun Gowda Apr 13 '21 at 07:55
  • I want to use the above code only but how to create kieContainer object that you have used in the line KieBase kieBase = kieContainer.getKieBase(); I want the kieContainer() object of the Business Central – m b Apr 13 '21 at 10:40
  • does this answer your question? https://stackoverflow.com/a/66568708/8283737 – Arun Gowda Apr 13 '21 at 14:35
  • Have posted a question with code snippet here. https://stackoverflow.com/questions/67057246/how-to-get-rule-data-from-business-central-drools – m b Apr 14 '21 at 06:58
0

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

Steve
  • 9,270
  • 5
  • 47
  • 61
  • Thank you for your answer Steve. I would like to ask another follow up question. We currently have lots and lots of rules generated via external sources (database, excel etc..). Is there a way to import these rules in guvnor automatically so they don't get out of sync? – Tin Ng Feb 07 '14 at 00:39
  • You can also use the REST API to push rules into Guvnor. However there's nothing out of the box of which I am aware that you could use to help you with it. Possibly the new 6.0 functionality might help, as you could create a build to generate a knowledge package from your generated rules and define that as a dependency. – Steve Feb 07 '14 at 09:40