Dools语法

585 阅读1分钟

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