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);
}
}
最終執行的結果:
總結
以上是生活随笔為你收集整理的java规则引擎_Drools规则引擎(Java)的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: Discuz! 防御CC攻击的设置办法
- 下一篇: linux如何过滤字符串,在linux系
