前言:
Drools是一款基於Java的開源規則引擎
實現了將業務決策從應用程序中分離出來。
優勢:
一、簡化系統架構,優化應用
二、提升系統的可維護性和維護成本
三、方便系統的整合
四、減小編寫「硬代碼」業務規則的成本和風險java
很少說,直接low代碼,此例是使用springboot項目爲基礎:spring
1.需添加maven依賴 使用7.x:springboot
<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.執行的實體類:app
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:maven
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);
}
}複製代碼
最終執行的結果:測試