在實際生產過程當中,有不少關於集合的處理場景,好比一個Fact對象中包含有一個集合,而須要判斷該集合是否包含某個值。而Drools規則引擎也提供了多種處理方式,好比經過from、contains、exists等進行操做,比較。java
固然也能夠經過function函數來作相應的比較,在個在其餘章節講到過,就不在此贅述。下面重點以幾個實例才進行講解,在具體實踐中根據具體狀況來進行運用。函數
省略掉基本的配置,直接看調用代碼和規則代碼。區塊鏈
測試調用代碼:測試
public class ContainsDemo extends BaseDemo { public static void main(String[] args) { KieSession kieSession = getKieSession("containsVar"); Corporation corporation = new Corporation(); Set<Scope> scopes = new HashSet<>(); scopes.add(new Scope("P2P")); scopes.add(new Scope("金融")); scopes.add(new Scope("區塊鏈")); corporation.setScopes(scopes); Scope scope = new Scope("區塊鏈"); kieSession.insert(corporation); kieSession.insert(scope); kieSession.fireAllRules(); } }
相關實體類:this
@Data public class Corporation { private Set<Scope> scopes; }
@Data public class Scope { public Scope(String scope){ this.scope = scope; } private String scope; }
而後看一下規則處理:.net
package com.containsVar import com.secbro2.drools.entity.Corporation import com.secbro2.drools.entity.Scope import java.util.List rule "containsVar1" when $c: Corporation($scopes:scopes); $s: Scope(scope == "P2P") from $scopes; then System.out.println("containsVar1行業類型爲:P2P"); end rule "containsVar2" when $c: Corporation($scopes:scopes); exists (Scope(scope == "P2P") from $scopes); then System.out.println("containsVar2行業類型爲:P2P"); end rule "containsVar3" when $s: Scope(scope == "區塊鏈") $c: Corporation(scopes contains $s); then System.out.println("containsVar3行業類型爲:區塊鏈"); end rule "containsVar4" when $s: Scope(scope == "區塊鏈") exists (Corporation(scopes contains $s)); then System.out.println("containsVar4行業類型爲:區塊鏈"); end
在上述實例中列舉了4中使用方法:code
CSDN學院:《Drools7規則引擎進階教程》視頻
CSDN學院:《Drools7規則引擎入門教程》對象
CSDN學院:《Drools7系列優惠套餐》教程