This article source code: GitHub. Click here || GitEE. Click here
Introduction to Drools Engine
1. Basic Introduction
Drools is a rule engine based on java. It can liberate complex and changeable rules from hard code and store it in the form of rule script, making it possible to change the rules without rewriting the code to restart the machine. As an open source business rule engine, it meets the industry standards and has high speed and efficiency.
2. Rule grammar
(1) Demonstration drl file format
package droolRule ; import org.slf4j.Logger import org.slf4j.LoggerFactory ; dialect "java" rule "paramcheck1" when then final Logger LOGGER = LoggerFactory.getLogger("param-check-one Rule engine") ; LOGGER.info("parameter"); end
(2) Grammatical Explanation
· File format It can be.drl, xml file or hard block of Java code block. · package In a rule file, the package must be defined and placed in the first line of the rule file. · import The external variables used in the rule file can be either a class or an accessible static method in the class. · rule Define a rule. paramcheck1 rule name. Rules usually include three parts: attributes, conditions and results.
Integrating SpringBook Framework
1. Project structure
2. Core Dependence
<!--drools Rule engine--> <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>
3. Configuration file
@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(); } }
This completes environmental integration.
3. Demonstration cases
1. Rule Documents
- Rule 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 Rule engine") ; LOGGER.info("parameter:getParamId="+queryParam.getParamId()+";getParamSign="+queryParam.getParamSign()); RuleEngineServiceImpl ruleEngineService = new RuleEngineServiceImpl() ; ruleEngineService.executeAddRule(queryParam); resultParam.setPostCodeResult(true); end
- Rule two
dialect "java" rule "paramcheck2" salience 88 when queryParam : QueryParam(paramId != null && paramSign.equals("-")) resultParam : RuleResult() then final Logger LOGGER = LoggerFactory.getLogger("param-check-two Rule engine") ; LOGGER.info("parameter:getParamId="+queryParam.getParamId()+";getParamSign="+queryParam.getParamSign()); RuleEngineServiceImpl ruleEngineService = new RuleEngineServiceImpl() ; ruleEngineService.executeRemoveRule(queryParam); resultParam.setPostCodeResult(true); end
Rule Description:
A. The bigger the value of salience, the higher the priority of execution.
B. Rule flow: If paramId is not null, the parameter identifier is + number, add rules, - number, remove rules.
2. Rule Execution Code
@Service public class RuleEngineServiceImpl implements RuleEngineService { private static final Logger LOGGER = LoggerFactory.getLogger(RuleEngineServiceImpl.class) ; @Override public void executeAddRule(QueryParam param) { LOGGER.info("Parameter data:"+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("Parameter data:"+param.getParamId()+";"+param.getParamSign()); ParamInfoService paramInfoService = (ParamInfoService)SpringContextUtil.getBean("paramInfoService") ; ParamInfo paramInfo = paramInfoService.selectById(param.getParamId()); if (paramInfo != null){ paramInfoService.removeById(param.getParamId()) ; } } }
3. Rule Call Interface
@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("-"); // Participation kieSession.insert(queryParam1) ; kieSession.insert(queryParam2) ; kieSession.insert(this.ruleEngineService) ; // Return parameter RuleResult resultParam = new RuleResult() ; kieSession.insert(resultParam) ; kieSession.fireAllRules() ; } }
In this way, the complete case is over.
4. Source code address
GitHub·address https://github.com/cicadasmile/middle-ware-parent GitEE·address https://gitee.com/cicadasmile/middle-ware-parent