javajbossdroolsdsldrools-flow

Drools DSL - How to use parenthesis in rules


Drools version: 6.3.0.Final

Pojo:

public class Person {
    private Integer age;
    private Integer childrens;
    private String name;
    private String address;
    (...) 
}

DSL file:

[condition][]and=&&
[condition][]or=||
[condition][]is less than or equal to=<=
[condition][]is less than=<
[condition][]is greater than or equal to=>=
[condition][]is greater than=>
[condition][]is equal to===
[condition][]There is a [Pp]erson with=$person:Person()
[condition][].{field:\w*}  {operator}  {value:\d*}={field}  {operator}  {value}
(...)

DSRL file:

package <package>;

import <import class>.*

global org.slf4j.Logger logger;

expander <class>.dsl;

rule "R1"
    when
        There is a person with
        .age is greater than 10 or .chidldrens is less than 2 and .name is  equal to "<name>"
    then
        (...)
end 

rule "R2"
    when
        There is a person with
        (.age is greater than 10 or .childrens is less than 2) and .name is equal to "<name>"
    then
        (...)
end 

DRL (from R1):

(...)
rule "R1"
        when
            $person:Person(age > 10 || childrens < 2 && name = "<name>")
        then
            (...)
    end 
(...)

DRL (from R2): the rule is not generated.

If I remove the parenthesis it's working but with parenthesis the DRL file is not correctly generated. So only the R2 rule is working but my goal is the R1 rule.

Any idea?


Solution

  • I think I found a possible solution witch is:

    DSL file (replace with this new conditions):

    [condition][]There is a [Pp]erson that {constraints}=$person:Person({constraints})
    [condition][]{field:\w*}\s+{operator}\s+{value:\s*}={field}  {operator}  {value}
    

    DSRL (define the constraints starting from the first line):

    (...)
    There is a person that ((age is greater than 10 or chidldrens is less than 2) and name is equal to "<name>")
    (...)
    

    DRL (generated):

    (...)
    $person:Person((age > 10 || childrens < 2) && name == "name")
    (...)
    

    Using this new DSL definition parenthesis are supported and it's working as expected. What do you think @laune?