What is the new accepted way to programmatically create new drool rules in Drools 6?

In short, I want to create, edit, and delete rules from the rule repository at runtime. It's hard for me to figure out how to do this in saliva 6+.

I know in a previous version of drools (<= 5.6) that there was an XML representation of the .drl file and an API for working with it: https://docs.jboss.org/drools/release/5.6.0.Final/drools-expert -docs / html / ch04.html # d0e8052 .

The drools documentation on 5.6 indicates that this is deprecated, and it seems to be completely removed at 6. I donโ€™t want to use an API that is known to have no direct update path.

Displaying Guvnor or Workbench user interfaces for rule editing users is also not well suited due to workflow requirements and due to the complexity of user interfaces. I want to create and manage rules from Java code.

I need a better method than a string template for a .drl file to create new rules and change rules. What exists for programmatically creating new rules with Java? I did a lot of searching but cannot find a set of Java API calls for this.

+7
java rules drools drools-guvnor
source share
3 answers

I donโ€™t know if this is the โ€œacceptedโ€ way, but with the following code I add .drl files with programmatically created rules in Drools 6.

 public KieContainer build(KieServices kieServices) { KieFileSystem kieFileSystem = kieServices.newKieFileSystem(); ReleaseId rid = kieServices.newReleaseId("com.example.rulesengine", "model-test", "1.0-SNAPSHOT"); kieFileSystem.generateAndWritePomXML(rid); kieFileSystem.write("src/main/resources/rules.drl", getResource(kieServices, "rules.drl")); addRule(kieFileSystem); KieBuilder kieBuilder = kieServices.newKieBuilder(kieFileSystem); kieBuilder.buildAll(); if (kieBuilder.getResults().hasMessages(Message.Level.ERROR)) { throw new RuntimeException("Build Errors:\n" + kieBuilder.getResults().toString()); } return kieServices.newKieContainer(rid); } private void addRule(KieFileSystem kieFileSystem) { PackageDescrBuilder packageDescrBuilder = DescrFactory.newPackage(); packageDescrBuilder .name("com.example.model") .newRule() .name("Is of valid age") .lhs() .pattern("Person").constraint("age < 18").end() .pattern().id("$a", false).type("Action").end() .end() .rhs("$a.showBanner( false );") .end(); String rules = new DrlDumper().dump(packageDescrBuilder.getDescr()); kieFileSystem.write("src/main/resources/rule-1.drl", rules); } private Resource getResource(KieServices kieServices, String resourcePath) { try { InputStream is = Resources.getResource(resourcePath).openStream(); //guava return kieServices.getResources() .newInputStreamResource(is) .setResourceType(ResourceType.DRL); } catch (IOException e) { throw new RuntimeException("Failed to load drools resource file.", e); } } 

I am using the Guava class.

+7
source share

There is no stable API for building rules from Java code. Of course, there is an API for the DRL compiler parser, but it is not stable and complex, as is the syntax of the DRL rule, which is significant.

XML was an option where at least the syntax of the left side was simple enough, which ended with 5.2 (IIRC). You can now use the full Java expression syntax and much more, combined with many different ways to write CE.

If your rules are exceptionally simple, you can find a model for rules that can be controlled using the managed API. Otherwise, a text editor (or, of course, Kie Workbench) would be the best option.

+1
source share

The built-in method for creating rules programmatically is based on the free Descr API, which directly manipulates the AST compiler bypassing the parser.

see class org.drools.compiler.lang.api.DescrFactory

and class org.drools.compiler.lang.DrlDumper to get the DRL approximation from AST.

+1
source share

All Articles