前言:
Drools是一款基于Java的开源规则引擎
实现了将业务决策从应用程序中分离出来。
优点:
1、简化系统架构,优化应用
2、提高系统的可维护性和维护成本
3、方便系统的整合
4、减少编写“硬代码”业务规则的成本和风险
不多说,直接low代码,此例是使用springboot项目为基础:
1.需添加maven依赖 使用7.x:
<dependency>
<groupId>org.drools</groupId>
<artifactId>drools-core</artifactId>
<version>7.4.1.Final</version>
</dependency>
<dependency>
<groupId>org.drools</groupId>
<artifactId>drools-compiler</artifactId>
<version>7.4.1.Final</version>
</dependency>复制代码
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<KiePackage> 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<String,KieContainer> kieContainerMap = new ConcurrentHashMap<String,KieContainer>();
private static Map<String,KieSession> kieSessionMap= new ConcurrentHashMap<String,KieSession>();
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);
}
}复制代码
最终执行的结果: