Drools DSL - How to use parentheses 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 bracket, then it works, but with a parenthesis, the DRL file is not generated correctly. Therefore, only the R2 rule works, but my goal is the R1 rule.

Any idea?

+4
source share
2 answers

I think I found a possible solution:

DSL file (replace it with 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 restrictions starting on 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")
(...)

The use of this new DSL definition bracket is supported and works as expected. What do you think @laune?

+3
source

DSL:

[condition][]There is a Person with=Person()
[condition][]- :{field:\w*}  {operator}  {value:\d*}=
            {field}  {operator}  {value}
[condition][]:{field:\w*}  {operator}  {value:\d*}=
             {field} {operator} {value}
# operators as in the question

DSLR:

rule R1
when
There is a Person with
- :call_count is less than 10 or :name is equal to "Joe" 
- :points is greater than 5
then
    ...
end 

rule R1
when
Person(call_count  <  10 || name  ==  "Joe", points  >  5)
then
 ...
end 

DSL 6.3.0 . 5.5, "" DSL. JIRA Drools. ( , Drools DSL, Step Child, 6.x.

0

Source: https://habr.com/ru/post/1620410/


All Articles