本文源碼:GitHub·點這里 || GitEE·點這里
網(wǎng)站的建設(shè)創(chuàng)新互聯(lián)建站專注網(wǎng)站定制,經(jīng)驗豐富,不做模板,主營網(wǎng)站定制開發(fā).小程序定制開發(fā),H5頁面制作!給你煥然一新的設(shè)計體驗!已為成都銅雕雕塑等企業(yè)提供專業(yè)服務。Drools是一個基于java的規(guī)則引擎,開源的,可以將復雜多變的規(guī)則從硬編碼中解放出來,以規(guī)則腳本的形式存放在文件中,使得規(guī)則的變更不需要修正代碼重啟機器就可以立即在線上環(huán)境生效。具有易于訪問企業(yè)策略、易于調(diào)整以及易于管理的特點,作為開源業(yè)務規(guī)則引擎,符合業(yè)內(nèi)標準,速度快、效率高。
(1)、演示drl文件格式
package droolRule ;
import org.slf4j.Logger
import org.slf4j.LoggerFactory ;
dialect "java"
rule "paramcheck1"
when
then
final Logger LOGGER = LoggerFactory.getLogger("param-check-one 規(guī)則引擎") ;
LOGGER.info("參數(shù)");
end
(2)、語法說明
· 文件格式
可以 .drl、xml文件,也可以Java代碼塊硬編碼;
· package
規(guī)則文件中,package是必須定義的,必須放在規(guī)則文件第一行;
· import
規(guī)則文件使用到的外部變量,可以是一個類,也可以是類中的可訪問的靜態(tài)方法;
· rule
定義一個規(guī)則。paramcheck1規(guī)則名。規(guī)則通常包含三個部分:屬性、條件、結(jié)果;
org.drools
drools-core
7.6.0.Final
org.drools
drools-compiler
7.6.0.Final
org.drools
drools-templates
7.6.0.Final
org.kie
kie-api
7.6.0.Final
org.kie
kie-spring
7.6.0.Final
@Configuration
public class RuleEngineConfig {
private static final Logger LOGGER = LoggerFactory.getLogger(RuleEngineConfig.class) ;
private static final String RULES_PATH = "droolRule/";
private final KieServices kieServices = KieServices.Factory.get();
@Bean
public KieFileSystem kieFileSystem() throws IOException {
KieFileSystem kieFileSystem = kieServices.newKieFileSystem();
ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
Resource[] files = resourcePatternResolver.getResources("classpath*:" + RULES_PATH + "*.*");
String path = null;
for (Resource file : files) {
path = RULES_PATH + file.getFilename();
LOGGER.info("path="+path);
kieFileSystem.write(ResourceFactory.newClassPathResource(path, "UTF-8"));
}
return kieFileSystem;
}
@Bean
public KieContainer kieContainer() throws IOException {
KieRepository kieRepository = kieServices.getRepository();
kieRepository.addKieModule(kieRepository::getDefaultReleaseId);
KieBuilder kieBuilder = kieServices.newKieBuilder(kieFileSystem());
kieBuilder.buildAll();
return kieServices.newKieContainer(kieRepository.getDefaultReleaseId());
}
@Bean
public KieBase kieBase() throws IOException {
return kieContainer().getKieBase();
}
@Bean
public KieSession kieSession() throws IOException {
return kieContainer().newKieSession();
}
@Bean
public KModuleBeanFactoryPostProcessor kiePostProcessor() {
return new KModuleBeanFactoryPostProcessor();
}
}
這樣環(huán)境整合就完成了。
dialect "java"
rule "paramcheck1"
salience 99
when queryParam : QueryParam(paramId != null && paramSign.equals("+"))
resultParam : RuleResult()
then
final Logger LOGGER = LoggerFactory.getLogger("param-check-one 規(guī)則引擎") ;
LOGGER.info("參數(shù):getParamId="+queryParam.getParamId()+";getParamSign="+queryParam.getParamSign());
RuleEngineServiceImpl ruleEngineService = new RuleEngineServiceImpl() ;
ruleEngineService.executeAddRule(queryParam);
resultParam.setPostCodeResult(true);
end
dialect "java"
rule "paramcheck2"
salience 88
when queryParam : QueryParam(paramId != null && paramSign.equals("-"))
resultParam : RuleResult()
then
final Logger LOGGER = LoggerFactory.getLogger("param-check-two 規(guī)則引擎") ;
LOGGER.info("參數(shù):getParamId="+queryParam.getParamId()+";getParamSign="+queryParam.getParamSign());
RuleEngineServiceImpl ruleEngineService = new RuleEngineServiceImpl() ;
ruleEngineService.executeRemoveRule(queryParam);
resultParam.setPostCodeResult(true);
end
規(guī)則說明:
A、salience 的值越大,越優(yōu)先執(zhí)行;
B、規(guī)則流程:如果paramId不為null,參數(shù)標識是+號,執(zhí)行添加規(guī)則,-號,執(zhí)行移除規(guī)則操作。
@Service
public class RuleEngineServiceImpl implements RuleEngineService {
private static final Logger LOGGER = LoggerFactory.getLogger(RuleEngineServiceImpl.class) ;
@Override
public void executeAddRule(QueryParam param) {
LOGGER.info("參數(shù)數(shù)據(jù):"+param.getParamId()+";"+param.getParamSign());
ParamInfo paramInfo = new ParamInfo() ;
paramInfo.setId(param.getParamId());
paramInfo.setParamSign(param.getParamSign());
paramInfo.setCreateTime(new Date());
paramInfo.setUpdateTime(new Date());
ParamInfoService paramInfoService = (ParamInfoService)SpringContextUtil.getBean("paramInfoService") ;
paramInfoService.insertParam(paramInfo);
}
@Override
public void executeRemoveRule(QueryParam param) {
LOGGER.info("參數(shù)數(shù)據(jù):"+param.getParamId()+";"+param.getParamSign());
ParamInfoService paramInfoService = (ParamInfoService)SpringContextUtil.getBean("paramInfoService") ;
ParamInfo paramInfo = paramInfoService.selectById(param.getParamId());
if (paramInfo != null){
paramInfoService.removeById(param.getParamId()) ;
}
}
}
@RestController
@RequestMapping("/rule")
public class RuleController {
@Resource
private KieSession kieSession;
@Resource
private RuleEngineService ruleEngineService ;
@RequestMapping("/param")
public void param (){
QueryParam queryParam1 = new QueryParam() ;
queryParam1.setParamId("1");
queryParam1.setParamSign("+");
QueryParam queryParam2 = new QueryParam() ;
queryParam2.setParamId("2");
queryParam2.setParamSign("-");
// 入?yún)? kieSession.insert(queryParam1) ;
kieSession.insert(queryParam2) ;
kieSession.insert(this.ruleEngineService) ;
// 返參
RuleResult resultParam = new RuleResult() ;
kieSession.insert(resultParam) ;
kieSession.fireAllRules() ;
}
}
這樣,完整的案例就結(jié)束了。
GitHub·地址
https://github.com/cicadasmile/middle-ware-parent
GitEE·地址
https://gitee.com/cicadasmile/middle-ware-parent
另外有需要云服務器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。