The source code of this article: GitHub·click here|| GitEE·click here
1. Introduction to Drools engine
1. Basic introduction
< p>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 do not need to modify the code and restart the machine. The online environment takes effect immediately. It has the characteristics of easy access to corporate policies, easy adjustment and easy management. As an open source business rules engine, it meets industry standards, is fast and efficient.
2, rule syntax
(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("parameters");
end
(2), syntax description
· File The format
can be .drl, xml files, or Java code blocks can be hard-coded;
· package
In the rule file, package must be defined and must be placed in the first line of the rule file;< br />· import
The external variable used by the rule file can be a class or an accessible static method in the class;
· rule
define a rule. The name of the paramcheck1 rule. Rules usually consist of three parts: attributes, conditions, and results;
Two, integrate the SpringBoot framework
1, project structure
2, core dependencies
< !--drools rule engine-->
org.drools
drools-core
7.6.0.Final
org.drools
drools-compiler< /artifactId>
7.6.0.Final
org.drools
drools-templates
7.6.0.Final
org.kie
kie-api
7.6.0.Final
< dependency>
org.kie
kie-spring
7.6.0.Final
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();
ResourceResolver();
ResourceResolver();
Resource[] files = resourcePattern 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.get)DefaultReleaseId;
}
@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 the environment integration.
Three, demonstration case
1, rule file
- Rule 1
dialect "java"
rule "paramcheck1"< br />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());< br /> 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.getParamSig n());
RuleEngineServiceImpl ruleEngineService = new RuleEngineServiceImpl() ;
ruleEngineService.executeRemoveRule(queryParam);
resultParam.setPostCodeResult(true);
endli>
Rule description:
The larger the value of A, salience, the more priority the execution;
B. Rule flow: If paramId is not null, the parameter The identifier is + sign, execute adding rule,-sign, execute removing rule operation.
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());< br /> 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);< br /> }
@Override
public void executeRemoveRule(QueryParam param) {
LOGGER.info("Parameter data:"+param.getParamId()+";"+param.getParamSign( ));
ParamInfoService paramInf oService = (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("-");
// Enter parameters< br /> kieSession.insert(queryParam1) ;
kieSession.insert(queryParam2 ) ;
kieSession.insert(this.ruleEngineService) ;
// Return parameters
RuleResult resultParam = new RuleResult() ;
kieSession.insert(resultParam) ;
kieSession.fireAllRules() ;
}
}
In this way, the complete case is over.
Four. Source code address
GitHub·Address
https://github.com/cicadasmile/middle-ware-parent
GitEE·Address
https://gitee.com/cicadasmile/middle-ware-parent