4

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?

Leonel
  • 2,796
  • 5
  • 25
  • 36
  • What is the DSL definition for "There is an object with"? - Strange. I would have thought that neither R1 nor R2 work. Did you check the generated DRL code? – laune Dec 16 '15 at 20:11
  • I change the question with the Person condition on the DSL file to answer your question. Do you have any clue regarding the parenthesis? – Leonel Dec 17 '15 at 09:55
  • The DRL for rule R1 is *definitely not* a result of an expansion using the given DSL and DSLR (and I don't mean because of the typo "chidldrens"). – laune Dec 17 '15 at 17:26
  • I have mentioned in my answer that I think that there are bugs in the DSL expansion of 6.3.0. – laune Dec 17 '15 at 17:27
  • I update the DRL for R1. I modify the "and" operator to "&&" instead of "," and now it's exactly as it is generated. Agree? – Leonel Dec 18 '15 at 09:44
  • R1 in DRL (from R1) isn't a correct transformation of R1 in DSLR. – laune Dec 18 '15 at 09:58
  • Yes, of course ... I swap R1 with R2. R1 has not parenthesis. Now please check the R1 in DRL (from R1). – Leonel Dec 18 '15 at 10:04

2 Answers2

4

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?

Leonel
  • 2,796
  • 5
  • 25
  • 36
  • I think that the DSL is different from the one in your question. Possibly there are other working alternatives. – laune Dec 17 '15 at 16:34
  • Yes, the DSL is different. I replace 2 conditions and I have now all constraints in a single line that's why it's working with parenthesis. – Leonel Dec 18 '15 at 09:42
0

The following DSL definitions should be used:

[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

And the 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 

This results in

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

There are at least two bugs in the 6.3.0 DSL expander. This used to be working in 5.5, but some "improvement" in DSL parsing has been made. You should raise a JIRA on the Drools bug reporting site. (But I don't think the Drools team will spend any time or effort on DSL, which has been demoted to a Step Child when work on 6.x began.

laune
  • 31,114
  • 3
  • 29
  • 42
  • Thanks for your comment. But I don't understand the difference between `[condition][]- :{field:\w*} {operator} {value:\d*}= {field} {operator} {value}` and `[condition][]:{field:\w*} {operator} {value:\d*}= {field} {operator} {value}`. And regarding the parenthesis any idea? – Leonel Dec 17 '15 at 09:57
  • Neither do I. Bugs are apt to create strange effects. It would appear that the marker hyphen ("put this into the parentheses of the preceding pattern") is retained in the generated code. - Sorry, but I'm not going to analyze this bug; I've been improving the DSL expander code in 5.2 or 5.3, but I'm not going to fix it, again, for 6.x. – laune Dec 17 '15 at 13:11
  • please check my answer. – Leonel Dec 17 '15 at 14:13