6
votes

Platform that I use requires Drools Rule Language (DRL), but for me more convenient thing is to create rules in Java API.

Is there any way to create rules in Java API, and then convert/transfer it to DRL?

Thx for answer.

1
No. Learn Drools and figure out how to write rules. There's no other way. - duffymo
Ok. Sow maybe there is the way to write custom 'when' clause and add to DRL rule? For example: ' rule "Hello World" dialect "mvel" when <CUSTOM_WHEN> then System.out.println( message ); modify ( m ) { message = "Goodbyte cruel world", status = Message.GOODBYE }; end ' - BartoBarto
Actually there is such an API. It's even a "fluent api". Ask on the drools mailing list - not sure if it's documented. IIRC it's part of drools-compiler, not kie-api. - Geoffrey De Smet
It should be noted in this context that there's also DSL which could be the basis for a very simple API if you have rules that follow an overall pattern (as many applications do). Moreover, templating is also a workaround if the API turns out to be too complicated. - laune

1 Answers

5
votes

Drools does have a fluent API for building rules as Geoffrey mentioned, but it is not in the public API. You can use and it has been stable for several years now, but you need to be aware that there might be changes over time due to features being added to the engine.

The API is used by the Drools parser itself. Here are some unit tests that you can use as examples:

https://github.com/droolsjbpm/drools/blob/6.3.x/drools-compiler/src/test/java/org/drools/compiler/lang/api/DescrBuilderTest.java#L451-L465