1. Pengenalan kepada enjin Drools
1. Pengenalan asas
Drools ialah enjin peraturan berasaskan java yang merupakan sumber terbuka dan boleh menjadi Peraturan yang rumit dan boleh diubah dibebaskan daripada pengekodan keras dan disimpan dalam fail dalam bentuk skrip peraturan, supaya perubahan peraturan boleh berkuat kuasa serta-merta dalam persekitaran dalam talian tanpa mengubah suai kod dan memulakan semula mesin. Ia mempunyai ciri-ciri akses mudah kepada dasar perusahaan, pelarasan mudah dan pengurusan mudah Sebagai enjin peraturan perniagaan sumber terbuka, ia mematuhi piawaian industri, pantas dan cekap.
2. Sintaks peraturan
(1), format fail drl demonstrasi
package droolRule ; import org.slf4j.Logger import org.slf4j.LoggerFactory ; dialect "java" rule "paramcheck1" when then final Logger LOGGER = LoggerFactory.getLogger("param-check-one 规则引擎") ; LOGGER.info("参数"); end
(2), penerangan sintaks
· Format fail
Boleh .drl, fail xml atau blok kod Java berkod keras;
· pakej
Dalam fail peraturan, pakej diperlukan Ditakrifkan, mesti diletakkan di baris pertama fail peraturan; kelas Kaedah statik yang boleh diakses;
· peraturan
mentakrifkan peraturan. nama peraturan paramcheck1. Peraturan biasanya mengandungi tiga bahagian: atribut, syarat, keputusan; 🎜>
<!--drools规则引擎--> <dependency> <groupid>org.drools</groupid> <artifactid>drools-core</artifactid> <version>7.6.0.Final</version> </dependency> <dependency> <groupid>org.drools</groupid> <artifactid>drools-compiler</artifactid> <version>7.6.0.Final</version> </dependency> <dependency> <groupid>org.drools</groupid> <artifactid>drools-templates</artifactid> <version>7.6.0.Final</version> </dependency> <dependency> <groupid>org.kie</groupid> <artifactid>kie-api</artifactid> <version>7.6.0.Final</version> </dependency> <dependency> <groupid>org.kie</groupid> <artifactid>kie-spring</artifactid> <version>7.6.0.Final</version> </dependency>
@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(); } }
3. Kes tunjuk cara
1. Fail peraturan
Peraturan 1
dialect "java" rule "paramcheck1" salience 99 when queryParam : QueryParam(paramId != null && paramSign.equals("+")) resultParam : RuleResult() then final Logger LOGGER = LoggerFactory.getLogger("param-check-one 规则引擎") ; LOGGER.info("参数:getParamId="+queryParam.getParamId()+";getParamSign="+queryParam.getParamSign()); RuleEngineServiceImpl ruleEngineService = new RuleEngineServiceImpl() ; ruleEngineService.executeAddRule(queryParam); resultParam.setPostCodeResult(true); end
Peraturan 2
dialect "java" rule "paramcheck2" salience 88 when queryParam : QueryParam(paramId != null && paramSign.equals("-")) resultParam : RuleResult() then final Logger LOGGER = LoggerFactory.getLogger("param-check-two 规则引擎") ; LOGGER.info("参数:getParamId="+queryParam.getParamId()+";getParamSign="+queryParam.getParamSign()); RuleEngineServiceImpl ruleEngineService = new RuleEngineServiceImpl() ; ruleEngineService.executeRemoveRule(queryParam); resultParam.setPostCodeResult(true); end
A Semakin besar nilai kepentingan, semakin tinggi keutamaan
2. Kod pelaksanaan peraturan
@Service public class RuleEngineServiceImpl implements RuleEngineService { private static final Logger LOGGER = LoggerFactory.getLogger(RuleEngineServiceImpl.class) ; @Override public void executeAddRule(QueryParam param) { LOGGER.info("参数数据:"+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("参数数据:"+param.getParamId()+";"+param.getParamSign()); ParamInfoService paramInfoService = (ParamInfoService)SpringContextUtil.getBean("paramInfoService") ; ParamInfo paramInfo = paramInfoService.selectById(param.getParamId()); if (paramInfo != null){ paramInfoService.removeById(param.getParamId()) ; } } }
Atas ialah kandungan terperinci Bagaimana untuk mengintegrasikan enjin peraturan Drools dalam SpringBoot2. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!