Dools语法

本文深入探讨Drools规则引擎的基本语法与高级用法,包括条件判断、集合操作、计算累积等,通过实例解析如何在业务场景中灵活运用Drools进行复杂逻辑处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Drools执行

Drools的基本语法是 when then ,可以加全局变量,优先级,执行顺序随机,执行一遍结束,要想再执行,除非有增删改对于对象的操作,而且即使再执行,同一个FACT也不会再触发。

语法

  1. 基本判断,修改值
global OutputDisplay showResults;

rule "Credit rule"
when
           $cash :CashFlow( $aDate : mvtDate, $no : accountNo ,type == CashFlow.CREDIT )
           $acc : Account(accountNo ==$no  )
           $period : AccountingPeriod(  startDate <= $aDate && endDate >= $aDate)
       then
           $acc.setBalance($acc.getBalance()+$cash.getAmount());
           showResults.showText("Account no "+$acc.getAccountNo()+ " has now a balance of "+$acc.getBalance());
end`
2.  in 判断

`$cash :CashFlow(type in ( CashFlow.DEBIT,CashFlow.CREDIT) )`

3. 元素中仍有元素

`$cash :PrivateAccount( owner.name =="Héron" )`

4. and/or

`( $c1 : Customer ( country=="GB") and  PrivateAccount(  owner==$c1))
            or
       ( $c1 : Customer (country=="US") and PrivateAccount(  owner==$c1))`

5. not

  not Customer(  )

6. exists

 exists Account(  )

7. forall

` "ForAll"
    when
        forall (   Account( $no : accountNo  )
                    CashFlow( accountNo  == $no)
                   )
    then
        showResult.showText("All cashflows are related to an Account ");
end

8. from

global CustomerService serviceCustomer;

rule "FromCondition"
    when
        $c : Customer()
        $cc : Customer(name ==$c.name,surname==$c.surname,country !=$c.country) from serviceCustomer.getListCustomer();
    then
        showResult.showText("Found same customer in 2 countries");
end

9. collect

rule "More then 2 CashFlow Line"
    when
        $c : Account( $acc : accountno )
        $p : AccountingPeriod ($sDate : startDate ,$eDate : endDate )
        $number : ArrayList(size >= 2 )
              from collect( CashFlow( mvtDate >= $sDate && mvtDate  <= $eDate,accountNo == $acc ) )

    then
        showResult.showText("Found more than 2 CashFlow Lines");
        showResult.showText("<<<<<<<<<<");
        for (Object ff : $number){
            showResult.showText(ff.toString());
        }
        showResult.showText(">>>>>>>>>>>>>>>>");
end

rule "Numbers of  CashFlow Line"
    when
        $c : Account( $acc : accountno )
        $p : AccountingPeriod ($sDate : startDate ,$eDate : endDate )
        $number : ArrayList( )
              from collect( CashFlow( mvtDate >= $sDate && mvtDate  <= $eDate,accountNo == $acc ) )

    then
        showResult.showText("Found "+$number+" more than 2 CashFlow Lines");
end

10. 计算

rule "Credit and Debit Rule"
    when
        $c : Account( $acc : accountno )
        $p : AccountingPeriod ($sDate : startDate ,$eDate : endDate )
        $totalCredit : Number( doubleValue > 100 )
             from accumulate( CashFlow( type ==CashFlow.CREDIT,$value : amount, mvtDate >= $sDate && mvtDate  <= $eDate,accountNo == $acc ),
                              init( double total = 0; ),
                              action( total += $value; ),
                              reverse( total -= $value; ),
                              result( total ) )
        $totalDebit : Number( doubleValue > 100 )
             from accumulate( CashFlow( type ==CashFlow.DEBIT,$value : amount, mvtDate >= $sDate && mvtDate  <= $eDate,accountNo == $acc ),
                              init( double total = 0; ),
                              action( total += $value; ),
                              reverse( total -= $value; ),
                              result( total ) )

    then
        showResult.showText(" Found "+$totalCredit+" as a credit");
        showResult.showText(" Found "+$totalDebit+" as a debit");
end
复制代码

转载于:https://juejin.im/post/5c6f8e81f265da2dd53fc777

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值