在实际生产过程当中,有不少关于集合的处理场景,好比一个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系列优惠套餐》教程