续drools6 基本使用1

http://blog.csdn.net/cloud_ll/article/details/26979355

8. 创建src/main/test folder。把droolsTest.java移动到test folder。把droolsTest.java改装成junit test case,执行mvn test。确保rules执行没有问题

9. 运行mvn install 把rules打包成jar文件上传到本地库

Note: 假设rule文件中面有中文,记得要把文件保存成utf-8格式。然后測试的时候保证maven的surefire plugin加上了使用utf-8的执行參数,否则会乱码出错

10. 在另外一个项目中使用drools,主要是借助kie-ci框架,实现启动load rules,动态改动rules和运行规则

maven文件修改,增加例如以下依赖:

<dependency>

<groupId>org.kie</groupId>

<artifactId>kie-ci</artifactId>

</dependency

java代码:创建一个ruleservice接口和实现类。实现下面方法:启动load rules(initService),动态改动(refreshService), 运行规则(fireService)

	public void initService(String groupId, String artifactId, String version)  throws NotExistsException {
ks = KieServices.Factory.get();
try{
kContainer = ks.newKieContainer(ks.newReleaseId(groupId, artifactId, version));
} catch (Exception e){
StringBuilder sb = new StringBuilder("fail to get maven rules kmodule from groupId: [");
sb.append(groupId).append("] artifactId: [").append(artifactId).append("] version: [").append(version).append("]");
throw new NotExistsException(sb.toString());
}
if(null != kContainer)
kSession = kContainer.newStatelessKieSession("ksession-rules");
} @Override
public void refreshService(String groupId, String artifactId, String version) throws NotExistsException {
try{
kContainer.updateToVersion(ks.newReleaseId(groupId, artifactId, version));
} catch (Exception e){
StringBuilder sb = new StringBuilder("fail to get maven rules kmodule from groupId: [");
sb.append(groupId).append("] artifactId: [").append(artifactId).append("] version: [").append(version).append("]");
throw new NotExistsException(sb.toString());
}
if(null != kContainer)
kSession = kContainer.newStatelessKieSession("ksession-rules");
} @Override
public void fireService(Message info) {
kSession.execute(info);
}

11. 基本測试。对于使用drools和直接写java代码,显然使用drools能够实现动态部署,更具优势,只是有人操心性能受到影响,这里我做了一下測试,

我这里的測试是基于一个自己写的rule规则,对对象的某些字段做业务逻辑处理。选择使用java代码和使用drools,看耗时区别。当然这个測试非常简陋。也没有对drools使用做优化,也没有看CPU和内存使用情况

业务逻辑代码:

public void setMatchThresholdsInfo(CustomerInfo info) {
String temp = info.getProfessionCode();
// boolean flag = false;
if (null == temp || 0 == (temp.trim()).length() || "其他".equals(temp) || "其他".equals(temp) || "无业".equals(temp) || "未知".equals(temp)) {
info.setRiskValue(info.getRiskValue() + 20);
info.setRiskType(info.getRiskType() + " Invalid professionCode value: " + info.getProfessionCode());
}
if (info.getCertificateEndDate() == null
|| info.getCertificateEndDate().before(new Date())) {
info.setRiskValue(info.getRiskValue() + 80);
info.setRiskType(info.getRiskType() + " Invalid Certification End date value: " + info.getCertificateEndDate());
}
if (info.getCustomerName() == null || info.getCustomerName().trim().length() == 0) {
info.setRiskValue(info.getRiskValue() + 80);
info.setRiskType(info.getRiskType() + " Customer Name is null");
}
if (info.getCertificateId() == null || 0 == (info.getCertificateId().trim()).length()) {
info.setRiskValue(info.getRiskValue() + 80);
info.setRiskType(info.getRiskType() + " Invalid Certification ID: " + info.getCertificateId());
}
if (null == info.isForeignFlag() ||info.isForeignFlag()) {
info.setRiskValue(info.getRiskValue() + 20);
info.setRiskType(info.getRiskType() + " 境外标志: " + info.getForeignFlag());
} info.setRiskType(info.getRiskType().trim());
}

rule规则:

//created on: May 19, 2014
package com.elulian.CustomerSecurityManagementSystem.service.impl //list any import classes here.
import com.elulian.CustomerSecurityManagementSystem.vo.CustomerInfo
import java.util.Date //declare any global variables here rule "Profession Rule"
//include attributes such as "salience" here... when
//conditions
info : CustomerInfo (null == professionCode || 0 == (info.getProfessionCode().trim()).length() || "其他".equals(professionCode) || "其他".equals(professionCode) || "无业".equals(professionCode) || "未知".equals(professionCode))
then
//actions
info.setRiskValue(info.getRiskValue() + 20);
info.setRiskType(info.getRiskType() + " Invalid professionCode value: " + info.getProfessionCode());
end rule "Certification End date Rule" when
//conditions
info : CustomerInfo (null == certificateEndDate || certificateEndDate.before(new Date()))
then
//actions
info.setRiskValue(info.getRiskValue() + 80);
info.setRiskType(info.getRiskType() + " Invalid Certification End date value: " + info.getCertificateEndDate()); end rule "Customer Name Rule" when
//conditions
info : CustomerInfo (null == customerName || 0 == (info.getCustomerName().trim()).length())
then
//actions
info.setRiskValue(info.getRiskValue() + 80);
info.setRiskType(info.getRiskType() + " Customer Name is null"); end rule "Certification ID Rule" when
//conditions
info : CustomerInfo (null == certificateId || 0 == (info.getCertificateId().trim()).length())
then
//actions
info.setRiskValue(info.getRiskValue() + 80);
info.setRiskType(info.getRiskType() + " Invalid Certification ID: " + info.getCertificateId());
end /*
rule "Certification ID Rule 2" when
//use regx here to check digital and length later
info : CustomerInfo ("身份证" == certificateType && (15 != (info.getCertificateId().trim()) || 18 != (info.getCertificateId().trim())))
then
info.setRiskValue(info.getRiskValue() + 80);
info.setRiskType(info.getRiskType() + " Invalid Certification ID: " + info.getCertificateId());
end
*/ rule "Foregin Flag Rule" when
info : CustomerInfo (null == foreignFlag || true == foreignFlag)
then
//actions
info.setRiskValue(info.getRiskValue() + 20);
info.setRiskType(info.getRiskType() + " 境外标志: " + info.getForeignFlag());
end //execute after all other rules are fired
rule "Trim Risk Type Rule"
salience -9999
when
info : CustomerInfo (true)
then
info.setRiskType(info.getRiskType().trim());
end

測试代码:

@Test
public void testExeuctionTime(){
long recordsNumber = 100000;
long start = System.currentTimeMillis();
long used = 0;
for(int i = 0; i < recordsNumber ; i++){
CustomerInfo info = new CustomerInfo();
info.setRiskType("");
info.setRiskValue(0);
thresholdService.setCustomerThresholdsInfo(info);
}
used = System.currentTimeMillis() - start;
System.out.println(used); start = System.currentTimeMillis();
used = 0;
for(int i = 0; i < recordsNumber; i++){
CustomerInfo info = new CustomerInfo();
info.setRiskType("");
info.setRiskValue(0);
thresholdService.setMatchThresholdsInfo(info);
}
used = System.currentTimeMillis() - start;
System.out.println(used);
}

測试结果:

recordsNumber = 10

54

424

recordsNumber = 100

23

713

[Thread-3] WARN org.drools.core.rule.constraint.MvelConstraint - Exception jitting: null == certificateEndDate || certificateEndDate.before(

new Date()) This is NOT an error and NOT prevent the correct execution since the constraint will be evaluated in intrepreted mode

[Thread-4] WARN org.drools.core.rule.constraint.MvelConstraint - Exception jitting: null == customerName  ||  0 == (info.getCustomerName().t

rim()).length() This is NOT an error and NOT prevent the correct execution since the constraint will be evaluated in intrepreted mode

[Thread-2] WARN org.drools.core.rule.constraint.MvelConstraint - Exception jitting: null == professionCode || 0 == (info.getProfessionCode()

.trim()).length() || "鍏朵粬".equals(professionCode) || "鍏跺畠".equals(professionCode) || "鏃犱笟".equals(professionCode) || "鏈煡".equal

s(professionCode) This is NOT an error and NOT prevent the correct execution since the constraint will be evaluated in intrepreted mode

[Thread-5] WARN org.drools.core.rule.constraint.MvelConstraint - Exception jitting: null == certificateId  ||  0 == (info.getCertificateId()

.trim()).length() This is NOT an error and NOT prevent the correct execution since the constraint will be evaluated in intrepreted mode

recordsNumber = 10000

406

70937

很多其它rules warning。

。。。。有机会和时间要研究下怎样调优

加几句。一不小心用了spring tx adv,慢的更离谱了

INFO - init threshold service before test

83813

[Thread-4] WARN org.drools.core.rule.constraint.MvelConstraint - Exception jitting: null == certificateEndDate || certificateEndDate.before(new Date()) This is NOT an error and NOT prevent the cor

[Thread-3] WARN org.drools.core.rule.constraint.MvelConstraint - Exception jitting: null == professionCode || 0 == (info.getProfessionCode().trim()).length() || "鍏朵粬".equals(professionCode) ||

[Thread-5] WARN org.drools.core.rule.constraint.MvelConstraint - Exception jitting: null == customerName  ||  0 == (info.getCustomerName().trim()).length() This is NOT an error and NOT prevent the

[Thread-6] WARN org.drools.core.rule.constraint.MvelConstraint - Exception jitting: null == certificateId  ||  0 == (info.getCertificateId().trim()).length() This is NOT an error and NOT prevent t

146309

Tests run: 3, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 343.06 sec - in com.elulian.CustomerSecurityManagementSystem.service.ThresholdServiceTest

Running com.elulian.CustomerSecurityManagementSystem.service.UserInfoServiceTest

drools6 基本使用 -- 2的更多相关文章

  1. Eclipse Mars 2安装Drools6.4插件(Drools and jBPM tools)时无法安装JBoss Runtime Drools Detector

    在eclipse上本地安装Drools6.4Final的时候出现两个组件无法正常安装的情况,具体组件如下: 具体的提示信息为: Cannot complete the install because ...

  2. eclipse中配置drools6.5环境

    1.去官网下载两个压缩包 2.解压两个压缩包,依次进入droolsjbpm-tools-distribution-6.5.0.Final\droolsjbpm-tools-distribution-6 ...

  3. 【java规则引擎】drools6.5.0中kie的概论

    什么是KIE? KIE是jBoss里面一些相关项目的统称,下图就是KIE代表的一些项目,其中我们比较熟悉的就有jBPM和Drools. 这些项目都有一定的关联关系,并且存在一些通用的API,比如说涉及 ...

  4. 【java规则引擎】drools6.5.0版本中kmodule.xml解析

    kmodule.xml文件存放在src/main/resources/META-INF/文件夹下. <?xml version="1.0" encoding="UT ...

  5. 【java规则引擎】drools6.5.0版本api简介

    在有些术语使用的时候,我有时候会用KIE项目.KIE引擎或者Drools项目.Drools引擎,大家应该理解KIE是Drools等项目的一个统称,所以在大多数情况下KIE或者特指Drools都是差不多 ...

  6. drools6

    <dependency> <groupId>org.drools</groupId> <artifactId>drools-core</artif ...

  7. Drools6:规则互斥

    在Drools中,有的时候会出现一种情况,一个事实Fact,满足了两个规则. 比如Fact的POJO是下面的 public class Message { public static final in ...

  8. 【转】外部应用和drools-wb6.1集成解决方案

    一.手把手教你集成外部应用和drools workbench6.1 1.         首先按照官方文档安装workbench ,我用的是最完整版的jbpm6-console的平台系统,里面既包含j ...

  9. 安装DRools开发环境

    1.下载相关安装包和开发插件 网站:http://www.jboss.org/drools/downloads.html 1.1 drools-distribution-6.3.0.Final.zip ...

随机推荐

  1. nginx &amp; flup &amp; django &amp; python3.x @ window7配置备忘录

    最近考虑原Prism建筑(非职业.半专业人士认为C/S建筑)至B/S迁移,主要是由于部署问题,包括两个因素:已经做,虽然一键安装和部署的一个因素,心存顾虑,虽然我一再声明这是一个绿色软件.还有一个因素 ...

  2. POJ 2778 AC自己主动机+矩阵幂 不错的题

    http://poj.org/problem?id=2778 有空再又一次做下,对状态图的理解非常重要 题解: http://blog.csdn.net/morgan_xww/article/deta ...

  3. 用VLC搭建流媒体server

    VLC开元项目相当强大,我们既能够将其作为播放核心用于二次开发,又能够将其作为高性能的流媒体server.今篇博客主要讲用VLC搭建流媒体server. VLC搭建流媒体server步骤非常easy: ...

  4. VS快捷键教程

    删除或剪切一行(Ctrl + X) 不须要选择不论什么文本, 用来剪切一行, 最经常使用来删除一行.  删除一行也能用 Shift+Delete 格式化整个文档(Ctrl + K, Ctrl + D) ...

  5. 流动python - 字符串KMP匹配

    首先我们看一下简单的字符串匹配. 你可以把文本字符串s固定,模式字符串p从s对齐的左边缘,作为承担部分完全一致,匹配成功,失败将是模式字符串p整体向右1地点,继续检查对齐部分,重复. #朴素匹配 de ...

  6. ios 6 横竖屏转换

    xcode 版本4.5     模拟器:6.0 项目需求:刚进去界面横屏,从这个界面进去的界面全是竖屏. 程序的根控制器用了UINavigationController.下面是代码: 1.在appde ...

  7. Computational Network Toolkit (CNTK) 是微软出品的开源深度学习工具包

    Computational Network Toolkit (CNTK) 是微软出品的开源深度学习工具包 用 CNTK 搞深度学习 (一) 入门 Computational Network Toolk ...

  8. 手机定位原理 - GPS/GLONASS/北斗 + WIFI + 基站(转)

    卫星定位系统 - GPS/GLONASS/北斗: 关于GPS.GLONASS.北斗.伽利略系统的科普请自行谷歌. GPS是使用最广泛的全球定位网络,几乎是所有智能手机的标配.进几年,俄罗斯的GLONA ...

  9. hdu2569(递推dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2569 分析: f(n),n个珠子的合格数:a(n),n个珠子,最后2个相同的合格数:b(n),n个珠子 ...

  10. Hibernate实体对象继承策略

    Hibernate继承策略总共同拥有三种,一种是共用一张表:一种是每一个类一张表,表里面储存子类的信息和父类的信息:另一种是通过表连接的方式.每一个类都有一张表,可是子类相应的表仅仅保存自己的信息,父 ...