Drools DSL - 如何在规则中使用括号

时间:2015-12-16 17:48:41

标签: java jboss drools dsl drools-flow

Drools版本:6.3.0.Final

POJO的:

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

DSL文件:

[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文件:

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(来自R1):

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

DRL(来自R2):未生成规则。

如果我删除括号,它正在工作,但是用括号表示DRL文件没有正确生成。所以只有R2规则正在运行,但我的目标是R1规则。

有什么想法吗?

2 个答案:

答案 0 :(得分:3)

我想我找到了一个可能的解决方案:

DSL文件(替换为这个新条件):

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

DSRL(定义从第一行开始的约束):

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

DRL(已生成):

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

支持使用这个新的DSL定义括号,它按预期工作。你怎么看@laune?

答案 1 :(得分:0)

应使用以下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

和数码单反相机:

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 

6.3.0 DSL扩展器中至少有两个错误。这曾经在5.5中工作,但在DSL解析中已经做了一些“改进”。您应该在Drools错误报告网站上提出JIRA。 (但我不认为Drools团队会花费任何时间或精力在DSL上,当6.x上的工作开始时,已经降级为Step Child。