java规则引擎_Drools规则引擎(Java)

前言:

Drools是一款基于Java的开源规则引擎

实现了将业务决策从应用程序中分离出来。

优点:

1、简化系统架构,优化应用

2、提高系统的可维护性和维护成本

3、方便系统的整合

4、减少编写“硬代码”业务规则的成本和风险

不多说,直接low代码,此例是使用springboot项目为基础:

1.需添加maven依赖 使用7.x:

org.drools

drools-core

7.4.1.Final

org.drools

drools-compiler

7.4.1.Final

2.main方法测试

//执行的主类

public class Application {

public static void test1() throws Exception {

KieSession kieSession = check(getRule());

IrssetDroolsVo drools = new IrssetDroolsVo();

drools.setSurpDayCnt(2);

kieSession.insert(drools);

int i = kieSession.fireAllRules();

System.out.println("命中: " + i +"返回结果:" + drools.getMsg());

}

/**

* 不进行检查

* @param rule

* @return

*/

public static KieSession getSession(String rule) {

KieSession kieSession = null;

try {

KnowledgeBuilder builder = KnowledgeBuilderFactory.newKnowledgeBuilder();

builder.add(ResourceFactory.newByteArrayResource(rule.getBytes("UTF-8")),ResourceType.DRL);

InternalKnowledgeBase knowledgeBase = KnowledgeBaseFactory.newKnowledgeBase();

Collection packages = builder.getKnowledgePackages();

knowledgeBase.addPackages(packages);

kieSession = knowledgeBase.newKieSession();

} catch (Exception e) {

e.printStackTrace();

}

return kieSession;

}

/**

* 检查

* @param sq

* @return

* @throws Exception

*/

private static KieSession check(String sq) throws Exception {

KieSessionRepo kieSession = new KieSessionRepo();

KieServices kieServices = KieServices.Factory.get();

KieFileSystem kfs = kieServices.newKieFileSystem();

kfs.write("src/main/resources/test.drl", sq );

KieBuilder kieBuilder = kieServices.newKieBuilder(kfs).buildAll();

Results results = kieBuilder.getResults();

if (results.hasMessages(Message.Level.ERROR)) {

for (Message msg : results.getMessages()) {

System.out.println("drools script error info : " + msg.getText());

}

throw new Exception("drools script error");

}

kieSession.setKieContainer("test", kieServices.newKieContainer(KieServices.Factory.get().getRepository().getDefaultReleaseId()));

return kieSession.getKieSession("test");

}

public static String getRule() {

StringBuffer ruleSb = new StringBuffer();

ruleSb.append(" package rule_10001;\n");

ruleSb.append("import com.learn.rule.model.IrssetDroolsVo\n");

ruleSb.append("rule rule_10001 \n");

ruleSb.append("when \n");

ruleSb.append("$riskDroolsVo : IrssetDroolsVo(surpDayCnt>=2 && surpDayCnt<=10); \n");

ruleSb.append("then \n");

ruleSb.append("$riskDroolsVo.setMsg(\"命中了\"); \n");

ruleSb.append("end");

System.out.println(ruleSb.toString());

return ruleSb.toString();

}

public static void main(String[] args) throws Exception {

test1();

}

3.执行的实体类:

public class IrssetDroolsVo implements Serializable {

private Integer surpDayCnt = null;

private boolean mBlack = false;

private String msg;

public String getMsg() {

return msg;

}

public void setMsg(String msg) {

this.msg = msg;

}

public IrssetDroolsVo() {

}

private static final long serialVersionUID = 1L;

public Integer getSurpDayCnt() {

return surpDayCnt;

}

public void setSurpDayCnt(Integer surpDayCnt) {

this.surpDayCnt = surpDayCnt;

}

public boolean ismBlack() {

return mBlack;

}

public void setmBlack(boolean mBlack) {

this.mBlack = mBlack;

}

}

4. KieSeesionRepo:

public class KieSessionRepo {

private static Map kieContainerMap = new ConcurrentHashMap();

private static Map kieSessionMap= new ConcurrentHashMap();

public static void setKieContainer(String key,KieContainer kieContainer) {

KieSession newKieSession = kieContainer.newKieSession();

kieContainerMap.put(key, kieContainer);

kieSessionMap.put(key,newKieSession);

}

public KieSession getKieSession(String key) {

return kieSessionMap.get(key);

}

}

最终执行的结果:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值