当前位置: 首页 > 知识库问答 >
问题:

drools dsl向最后一个模式添加表达式时“-”不起作用

郎星汉
2023-03-14
[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"

和为模板的when and then部分指定的操作:

"Action" "Update ShoppingCart"

下面是在将其传递给DRLParser之前编译好的drl:


    rule "Test1"
      dialect "mvel"
      when
         "There is a ShoppingCart that total price is greater than 1000"
      then
        "Update ShoppingCart"
    end
package com.sample.test

rule "Test1"
  dialect "mvel"
  when
     $shoppingcart : $Shoppingcart() total price is greater than 1000
  then
    System.out.println("ShoppingCart" + " test")
end
DrlParser parser = new DrlParser();
        DefaultExpanderResolver resolver = new DefaultExpanderResolver(new StringReader(dsl));
        String expandedDrl = parser.getExpandedDRL(dslr, resolver);
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"  ....

共有1个答案

越文康
2023-03-14

你能试试条件吗

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

... consequences I advise to wrap in java util class each as a static method to write java code in java code
[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
- not complex condition

如果您想在其他条件中重用条件,您希望它们是原子的。用大括号拥抱RHS的好规则()不要打破记录的原子性

测试

@DroolsSession({ "classpath:/test.rdslr", "classpath:/business.dsl", "classpath:/keywords.dsl" })
public class PlaygroundTest {

    @Rule
    public DroolsAssert drools = new DroolsAssert();

    @Test
    public void testIt() {
        drools.insertAndFire(new ShoppingCart(BigDecimal.valueOf(1000.5)));
        drools.insertAndFire(new ShoppingCart(BigDecimal.valueOf(999)));
    }
}

public class ShoppingCart {
    public BigDecimal totalPrice;

    public ShoppingCart(BigDecimal totalPrice) {
        this.totalPrice = totalPrice; 
    }
}
rule Test1
    when
        There is a ShoppingCart that 
        - total price is greater than 1000
    then
        print eligible price
end

rule Test2
    when
        There is a ShoppingCart that 
        - worth a discount
    then
        print eligible price
end
[when]worth a discount = 
    (total price is not less than 500
    and not over limit
    or total price is greater than 5000)
// stupid condition just for demonstration
[when]over limit = ((total price + total price * 0.05) > total price + 50)
[when]total price = totalPrice

[then]print eligible price = System.out.println($ShoppingCart.totalPrice);
[when]There (is( an?)?|are) {entityType}s?( that)? = ${entityType}: {entityType}()
[when](is )?not within\s*\({tail}?= not in ({tail}
[when](is )?within\s*\({tail}?= in ({tail}
[when](is )?not one of\s*\({tail}?= not in ({tail}
[when](is )?one of\s*\({tail}?= in ({tail}
[when]ignore case '{tail}?= '(?i){tail}
[when]{param:[$\w\.!'\[\]]+} as {param2:[$\w\.!'\[\]]+}=(({param2}) {param})
[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*- = 
00:00:00 --> inserted: ShoppingCart[totalPrice=1000.5]
00:00:00 --> fireAllRules
00:00:00 <-- 'Test1' has been activated by the tuple [ShoppingCart]
1000.5
00:00:00 --> inserted: ShoppingCart[totalPrice=999]
00:00:00 --> fireAllRules
00:00:00 <-- 'Test2' has been activated by the tuple [ShoppingCart]
999
 类似资料:
  • 我想安排一个任务在每个月的最后一天上午10:10运行。cron表达式是

  • 如果输入的文本与我的模式不匹配: 如果在输入的文本中找到\n字符,那么文本将不会被验证,但我想添加最小和最大长度到所有整个字符串。我希望如果文本不匹配的模式和字符串长度低于3和超过10,那么它将不会被验证,我知道下面的模式是不正确的,但至少我试图这样修改它: 对于示例: 有什么更正或建议吗?

  • 我有一个模式,我想从字符串链接捕获这个: 我想捕获文本的地方,。由于我所要做的就是 ** 和 ,我尝试了一个正则表达式,但它从第一个 一直返回匹配项,而不是在我的模式之前返回匹配项请参阅下面的正则表达式 101: https://regex101.com/r/kUBxQf/1

  • 这似乎不起作用。 我还尝试在代码生成器使用的xml中添加架构名称,如 http://www.jooq.org/doc/3.5/manual/code-generation/codegen-advanced/

  • 我想测试用户是否在模式 ##-#- 中输入了任何字符或数字#### 我的表达式是 ^(\w){4}-(\w){2}-(\w){3}-(\w){4}$,似乎在联机测试人员中有效,但在我的验证表达式中不起作用! 有什么想法吗? 这是我的代码(getComponentValye是我自己获取值的函数)…

  • 我在angular中有一个表单,其中输入(姓名、电子邮件、电话号码等)是使用angular指令生成的: }); 当我引用regex表达式来验证输入时:data ng pattern=“{{campo.validacion}}”验证有效,但抛出一个错误: 数据库中的表达式: ^(([^ 我已经看到,对于ng模式,您可能需要在表达式的每一端添加一个“/”以使其工作。在表达式的每一端添加“/”时: 角度