用好Drools 中的表達式是你的規則引擎可否強大的必要條件html
下面列出了幾個我的認爲比較重要的點: express
AND app
Drools 默認的並列表達式就是 前置的ANDide
(and Cheese( cheeseType : type ) Person( favouriteCheese == cheeseType ) ) when Cheese( cheeseType : type ) Person( favouriteCheese == cheeseType ) then ...
OR函數
OR 實際上並非按照||的短路邏輯進行處理的, 而是將一個規則寫爲兩個子規則, 分別匹配和執行ui
(or Person( sex == "f", age > 60 ) Person( sex == "m", age > 65 ) pensioner : ( Person( sex == "f", age > 60 ) or Person( sex == "m", age > 65 ) ) (or pensioner : Person( sex == "f", age > 60 ) pensioner : Person( sex == "m", age > 65 ) )
NOTthis
there must be none of...spa
至關於work memory必須沒有所示表達式的對象, 建議寫()code
// Brackets are optional: not Bus(color == "red") // Brackets are optional: not ( Bus(color == "red", number == 42) ) // "not" with nested infix and - two patterns, // brackets are requires: not ( Bus(color == "red") and Bus(color == "blue") )
EXIST htm
there is at least one..
至關於work memory至少有一個所示的對象
exists Bus(color == "red") // brackets are optional: exists ( Bus(color == "red", number == 42) ) // "exists" with nested infix and, // brackets are required: exists ( Bus(color == "red") and Bus(color == "blue") )
FORALL
WorkMemory中全部facts都須要符合表達式, 能夠支持多項匹配
單項匹配
rule "All Buses are Red"
when
forall( Bus( color == 'red' ) ) then // all Bus facts are red end
多項順序匹配, 全部英文的bus都是紅色的
rule "All English buses are red" when forall( $bus : Bus( type == 'english') Bus( this == $bus, color = 'red' ) ) then // all English buses are red end
更復雜的狀況, 全部employee都有health和dental care
rule "all employees have health and dental care programs" when forall( $emp : Employee() HealthCare( employee == $emp ) DentalCare( employee == $emp ) ) then // all employees have health and dental care end
FROM
從fact中拿出匹配表達式的field, 支持 基本類型 和 集合類, 若是是集合類且有多個符合條件的field, rule會fire屢次
rule "apply 10% discount to all items over US$ 100,00 in an order" when $order : Order() $item : OrderItem( value > 100 ) from $order.items then // apply discount to $item end
只要value>100就會被fire一次, 至關於 遍歷items, 判斷並執行 for item in items: if value > 100 then ....
rule "Assign people in North Carolina (NC) to sales region 1" ruleflow-group "test" lock-on-active true when $p : Person( ) $a : Address( state == "NC") from $p.address then modify ($p) {} // Assign person to sales region 1 in a modify block end rule "Apply a discount to people in the city of Raleigh" ruleflow-group "test" lock-on-active true when $p : Person( ) $a : Address( city == "Raleigh") from $p.address then modify ($p) {} // Apply discount to person in a modify block end
若是person在Raleign, NC生活, 應該兩個rule都被Fire, 實際上, 只有第二個rule被fire, 緣由是lock-on-active 當一系列fact變化時, 阻止rule進行新的activition, 因此第一個rule沒有被再次active, 若是沒有fact發生變化, 則一切正常
注意點:
Avoid the use of from
when you can assert all facts into working memory or use nested object references in your constraint expressions (shown below).
Place the variable assigned used in the modify block as the last sentence in your condition (LHS).
Avoid the use of lock-on-active
when you can explicitly manage how rules within the same rule-flow group place activations on one another (explained below).
Accumulate
將WorkMemory中的每一個fact中的值進行累加操做, 內嵌支持min max avg count sum collectList collectSet, 也能夠本身實現org.drools.core.runtime.rule.TypedAccumulateFunction
接口, 從而寫出本身的accumulate 函數
rule "Raise alarm" when $s : Sensor() accumulate( Reading( sensor == $s, $temp : temperature ); $min : min( $temp ), $max : max( $temp ), $avg : average( $temp ); $min < 20, $avg > 70 ) then // raise the alarm end
其餘注意點:
ruleflow-group
default value: N/A
type: String
Ruleflow is a Drools feature that lets you exercise control over the firing of rules. Rules that are assembled by the same ruleflow-group identifier fire only when their group is active.
規則流是一個Drools的功能,讓你在規則的fire進行控制。即用相同的規則流組只有當他們的group active時纔會被fire。
agenda-group
default value: MAIN
type: String
Agenda groups allow the user to partition the Agenda providing more execution control. Only rules in the agenda group that has acquired the focus are allowed to fire.
Agenda 容許用戶進行分區並提供更多的執行控制。只有在 agenda group 被focus的時候才能夠被fire
auto-focus
default value: false
type: Boolean
When a rule is activated where the auto-focus value is true and the rule's agenda group does not have focus yet, then it is given focus, allowing the rule to potentially fire.
可讓沒有被focus的 agenda group得到 focus, 容許rule去執行
activation-group
default value: N/A
type: String
Rules that belong to the same activation-group, identified by this attribute's string value, will only fire exclusively. More precisely, the first rule in an activation-group to fire will cancel all pending activations of all rules in the group, i.e., stop them from firing.
Note: This used to be called Xor group, but technically it's not quite an Xor. You may still hear people mention Xor group; just swap that term in your mind with activation-group.
屬於activation-group的rules將進行 獨佔式的觸發, 更準確地說,一個已被fire的規則能夠阻止全部的未執行規則
date-effective
default value: N/A
type: String, containing a date and time definition
A rule can only activate if the current date and time is after date-effective attribute.
一個rule只能在當前時間或者當前時間以後被觸發
date-expires
default value: N/A
type: String, containing a date and time definition
A rule cannot activate if the current date and time is after the date-expires attribute.
一個rule不能在當前時間或者當前時間以後被觸發
duration
default value: no default value
type: long
The duration dictates that the rule will fire after a specified duration, if it is still true.
rule將在duration時間以後被fire