首页 技术 正文
技术 2022年11月14日
0 收藏 693 点赞 5,005 浏览 1879 个字

规则引擎集合相关处理

在实际生产过程中,有很多关于集合的处理场景,比如一个Fact对象中包含有一个集合,而需要判断该集合是否包含某个值。而Drools规则引擎也提供了多种处理方式,比如通过from、contains、exists等进行操作,比较。

当然也可以通过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();}
}

相关实体类:

@Data
public class Corporation {private Set<Scope> scopes;}
@Data
public class Scope {public Scope(String scope){
this.scope = scope;
}private String scope;}

然后看一下规则处理:

package com.containsVarimport com.secbro2.drools.entity.Corporation
import com.secbro2.drools.entity.Scope
import java.util.Listrule "containsVar1"when $c: Corporation($scopes:scopes);
$s: Scope(scope == "P2P") from $scopes;then System.out.println("containsVar1行业类型为:P2P");
endrule "containsVar2"when $c: Corporation($scopes:scopes);
exists (Scope(scope == "P2P") from $scopes);
then System.out.println("containsVar2行业类型为:P2P");endrule "containsVar3"when
$s: Scope(scope == "区块链")
$c: Corporation(scopes contains $s);
then System.out.println("containsVar3行业类型为:区块链");endrule "containsVar4"when
$s: Scope(scope == "区块链")
exists (Corporation(scopes contains $s));
then System.out.println("containsVar4行业类型为:区块链");end

在上述实例中列举了4中使用方法:

  • 第一种,首先获取Fact对象Corporation,并重新定义了它的属性$scopes。然后,通过from关键字来遍历$scopes中的值,获得符合条件的。此时并不需要传入Scope对应的fact对象。
  • 第二种,前半部分同第一种方式,是不过没有获取筛选的结果,直接用exists来判断是否存在。
  • 第三种,先获得满足条件的Scope的Fact对象,然后再利用此fact对Corporation的fact对象进行筛选,只有满足条件才可以继续。
  • 第四种,与第三种效果相同,原理同第二种方式的exists使用。

相关技术视频

CSDN学院:《Drools7规则引擎进阶教程》

CSDN学院:《Drools7规则引擎入门教程》

CSDN学院:《Drools7系列优惠套餐》

原文链接: http://www.choupangxia.com/2019/07/31/drools规则引擎-如果判断某个对象中的集合是否包含指/

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,492
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,494
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,132
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,295