Search code examples
drools

programmatical use of DSL in to change rule in Drools


I am currently experimenting with defining my own DSL on top of Drools as is described in http://docs.jboss.org/drools/release/6.2.0.Final/drools-docs/html/ch07.html#d0e10052

What I want to achieve is to use a normal DRL with an extension defined by me. To take a random example I want to be able to use the DSL definition

[when]Something is {colour}=Something(colour=="{colour}")

mixed with other lines, that are in normal DRL syntax already. My problem is now, that drools complains about not being able to expand those normal DRL lines. I am aware of the possibility of using > as a line prefix, but for my case I don't want the user later to be able to tell what is normal DRL and what is my DSL part.

What I do for testing is basically:

KieServices kieServices = KieServices.Factory.get();
KieFileSystem kfs = kieServices.newKieFileSystem();
int count = 0;
for (String rule : ruleBodies) {
    count++;
    kfs.write("src/main/resources/Rule" + count + ".dslr",
        kieServices.getResources().newReaderResource(new StringReader(rule)));
}
String dsl = "[when]Something is {colour}=Something(colour==\"{colour}\")";
kfs.write("src/main/resources/DSL.dsl",
    kieServices.getResources().newReaderResource(new StringReader(dsl)));
KieBuilder kieBuilder = kieServices.newKieBuilder(kfs).buildAll()

but what I get is drools complaining about not being able to expand those DRL lines.

Is there a way to define a part of the DSL that basically says to match everything and give it out again as is? I tried

[when]{enity:.*}={entity}

but since I am new to this part I surely totally misunderstood the documentation and the line probably makes no sense at all.

Would be nice if somebody told me if that approach of a DSL on top of a DRL is even the right way and if it can be done in the way I am trying to do. Best answer would be of course that if this is possible to tell me what the correct line is for my match all... or how to otherwise do this.


Solution

  • Drools DSL is a rather simplistic macro processor that lets you define macros as strings with embedded parameters, for single-line expansion.

    For DSLR, the rule-when-then-end mantra is retained, but the "flesh" of the LHS and RHS must come from expansions - or DRL lines flagged with '>'.

    Parsing arbitrary DSL macro invocations from anywhere within the (free-form) DRL is impossible.

    If it is important enough you might write a specific parser that can handle the DSL you have in mind even when it is embedded in DRL, but you'll have to make amends - your DSL would have to have some "tags" for identifying it.