In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
In this article Xiaobian for you to introduce in detail "how to integrate Drools rules engine in SpringBoot2", the content is detailed, the steps are clear, and the details are handled properly. I hope this article "how to integrate Drools rules engine in SpringBoot2" can help you solve your doubts.
A brief introduction to Drools engine
1. Introduction to the basics
Drools is a java-based rule engine, open source, which can liberate complex and changeable rules from hard coding and store them in files in the form of rule scripts, so that rule changes can take effect immediately on the online environment without restarting the code. It is easy to access enterprise policy, easy to adjust and easy to manage, as an open source business rules engine, in line with industry standards, high speed and high efficiency.
2. Rule grammar
(1) demo drl file format
Package droolRule; import org.slf4j.Loggerimport org.slf4j.LoggerFactory; dialect "java" rule "paramcheck1" when then final Logger LOGGER = LoggerFactory.getLogger ("param-check-one rules engine"); LOGGER.info ("parameters"); end
(2) grammatical description
File format
Can be .drl, xml files, or Java code blocks hard-coded
Package
In the rule file, package must be defined and must be placed on the first line of the rule file
Import
The external variable used in the rule file can be a class or an accessible static method in the class
Rule
Define a rule. Paramcheck1 rule name. A rule usually consists of three parts: attribute, condition, and result.
Second, integrate the SpringBoot framework
1. Project structure
SpringBoot2 integrates Drools rules engine to achieve efficient business rules
2. Core dependence
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
3. Configuration file
@ Configurationpublic 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 ();}}
In this way, the environmental integration is completed.
III. Demonstration case
1. Rule file
Rule number one
Dialect "java" rule "paramcheck1" salience 99when queryParam: QueryParam (paramId! = null & & paramSign.equals ("+")) resultParam: RuleResult () then final Logger LOGGER = LoggerFactory.getLogger ("param-check-one rule engine"); LOGGER.info ("parameters: getParamId=" + queryParam.getParamId () + "; getParamSign=" + queryParam.getParamSign ()); RuleEngineServiceImpl ruleEngineService = new RuleEngineServiceImpl (); ruleEngineService.executeAddRule (queryParam); resultParam.setPostCodeResult (true); end
Rule two
Dialect "java" rule "paramcheck2" salience 88when queryParam: QueryParam (paramId! = null & & paramSign.equals ("-")) resultParam: RuleResult () then final Logger LOGGER = LoggerFactory.getLogger ("param-check-two rule engine"); LOGGER.info ("parameters: getParamId=" + queryParam.getParamId () + "; getParamSign=" + queryParam.getParamSign ()); RuleEngineServiceImpl ruleEngineService = new RuleEngineServiceImpl (); ruleEngineService.executeRemoveRule (queryParam); resultParam.setPostCodeResult (true); end
Rule description:
The higher the value of An and salience, the higher the priority.
B, rule flow: if the paramId is not null, the parameter identification is +, add the rule,-sign, and remove the rule.
2. Rule execution code
@ Servicepublic 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 API
@ 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 ("-"); / / input parameter kieSession.insert (queryParam1) KieSession.insert (queryParam2); kieSession.insert (this.ruleEngineService); / / return parameter RuleResult resultParam = new RuleResult (); kieSession.insert (resultParam); kieSession.fireAllRules () }} here, the article "how to integrate Drools rules engine in SpringBoot2" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself. If you want to know more about related articles, please follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.