我一直在使用Drools规则,并且最近才开始使用dsl,以使最终用户更容易编写规则。虽然我已经能够定义一个简单的dsl并按预期正确地编译为drl,但是我无法使“向前一个表达式添加约束”的dsl功能正常工作。我什至尝试了drools dsl指南中最简单的示例,并且这不会将我以'-'开头定义的条件编译到前面的表达式中。编译该规则时,在规则“ Rule1Sample_0”中始终收到“输入的价格不匹配”错误。如我所说,我的工作是针对简单的条件表达式和结果表达式。但是在文档后添加约束则根本无法工作。我正在使用Drools版本7.0.0.Final,直到更高版本才支持该功能吗?
在我正在测试的简单示例中,我的dsl文件仅包含:
[condition][]There is a {ShoppingCart} that=${ShoppingCart!lc} : ${ShoppingCart!ucfirst}()
[condition][]- total price is greater than 1000 =totalPrice > 1000
[consequence]Update {ShoppingCart}=System.out.println("{ShoppingCart}" + " test")
这里是条件
"There is a ShoppingCart that total price is greater than 1000"
以及我为模板的何时以及随后部分指定的动作:
"Action" "Update ShoppingCart"
这是当我使用DRLParser解析它时生成的drl:
(此处为代码段,省略了一些)
DrlParser parser = new DrlParser();
DefaultExpanderResolver resolver = new DefaultExpanderResolver(new StringReader(dsl));
String expandedDrl = parser.getExpandedDRL(dslr, resolver);
这是上面的代码段运行后,expandedDrl字符串包含的内容:
package com.sample.test
rule "Test1"
dialect "mvel"
when
$shoppingcart : $Shoppingcart() total price is greater than 1000
then
System.out.println("ShoppingCart" + " test")
end
以及我在控制台中看到的编译器错误:
[[13,43]: [ERR 102] Line 13:43 mismatched input 'price' in rule "Test1" ....
您可以尝试条件吗
There is a ShoppingCart that
- total price is greater than 1000
这些破折号从与流口水一起工作的一开始就使我烦恼,在处理结束时,我摆脱了它们。您可以尝试跟随dsl
... business definitions
[when]complex condition = simple condition or another condition
[when]simple condition = total price is not 0
[when]another condition = total price greater than 10
... field definitions
[when]total price = totalPrice
[consequence]Update {ShoppingCart}=System.out.println("{ShoppingCart}" + " test")
... tech dsl at the bottom
[when]There (is( an?)?|are) {entityType}s?( that)? = ${entityType}: {entityType}()
[when](is\s+)?not less than(\s+an?)? = >=
[when](is\s+)?less than(\s+an?)? = <
[when](is\s+)?not greater than(\s+an?)? = <=
[when](is\s+)?greater than(\s+an?)? = >
[when]((is|do(es)?)\s+)?not equals?(\s+to)? = !=
[when](is\s+)?equals?(\s+to)? = ==
[when]is not(\s+an?)? = !=
[when]is(\s+an?)? = ==
[when]like(\s+an?)? = matches
[when]{prefix}?\s*(?<![\w])and(?![\w])\s*{suffix}? = {prefix} && {suffix}
[when]{prefix}?\s*(?<![\w])or(?![\w])\s*{suffix}? = {prefix} || {suffix}
[when]{prefix}?\s*(?<![\w])not(?! (in|matches|contains|memberOf|soundslike|str))(\s+an?)?(?![\w])\s*\({suffix}? = {prefix} false == (true && {suffix}
[when]{prefix}?\s*(?<![\w])not(?! (in|matches|contains|memberOf|soundslike|str))(\s+an?)?(?![\w])\s*{suffix}? = {prefix} !{suffix}
[when](?<![^\(,])\s*- =
这应该能够处理类似规则
There is a ShoppingCart that
- total price is greater than 1000
- complex condition