文章目录
pom文件配置一.使用@Configuration 配置,在springboot项目启动时加载
1.配置类2.在service中注入kiession使用3.规则文件内容 二.不使用DRL文件,写在字符串中加载规则
1.使用KieHelper加载规则内容2.根据业务规则,动态生成规则内容字符串。
参考项目
https://github.com/leifchen/hello-drools
pom文件配置
7.57.0.Final org.drools drools-bom ${drools.version} pom import org.kie kie-api org.kie kie-spring org.drools drools-compiler runtime
两种使用方式
一.使用@Configuration 配置,在springboot项目启动时加载 1.配置类
package com.newland.edc.pub.metainfo.config;import lombok.extern.slf4j.Slf4j;import org.kie.api.Kiebase;import org.kie.api.KieServices;import org.kie.api.builder.KieBuilder;import org.kie.api.builder.KieFileSystem;import org.kie.api.builder.KieRepository;import org.kie.api.runtime.KieContainer;import org.kie.api.runtime.KieSession;import org.kie.internal.io.ResourceFactory;import org.kie.spring.KModuleBeanFactoryPostProcessor;import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.core.io.Resource;import org.springframework.core.io.support.PathMatchingResourcePatternResolver;import org.springframework.core.io.support.ResourcePatternResolver;import java.io.IOException;@Slf4j@Configurationpublic class DroolsConfiguration { private static final String RULES_PATH = "rules/"; @Bean @ConditionalOnMissingBean(KieFileSystem.class) public KieFileSystem kieFileSystem() { KieFileSystem kieFileSystem = KieServices.Factory.get().newKieFileSystem(); for (Resource ruleFile : getRuleFiles()) { kieFileSystem.write(ResourceFactory.newClassPathResource(RULES_PATH + ruleFile.getFilename(), "UTF-8")); } return kieFileSystem; } @Bean @ConditionalOnMissingBean(KieContainer.class) public KieContainer kieContainer() { System.setProperty("drools.dateformat","yyyy-MM-dd"); KieRepository kieRepository = KieServices.Factory.get().getRepository(); kieRepository.addKieModule(kieRepository::getDefaultReleaseId); KieBuilder kieBuilder = KieServices.Factory.get().newKieBuilder(kieFileSystem()); kieBuilder.buildAll(); return KieServices.Factory.get().newKieContainer(kieRepository.getDefaultReleaseId()); } @Bean @ConditionalOnMissingBean(Kiebase.class) public Kiebase kiebase() { return kieContainer().getKiebase(); } @Bean @ConditionalOnMissingBean(KieSession.class) public KieSession kieSession() { return kieContainer().newKieSession(); } @Bean @ConditionalOnMissingBean(KModuleBeanFactoryPostProcessor.class) public KModuleBeanFactoryPostProcessor kModuleBeanFactoryPostProcessor(){ return new KModuleBeanFactoryPostProcessor(); } private Resource[] getRuleFiles() { Resource[] resources = new Resource[0]; ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver(); try { resources = resourcePatternResolver.getResources("classpath*:" + RULES_PATH + "** private String generateRuleString(StandardRuleBean ruleBean) { StandardRuleBean standardRule = this.getStandardRule(ruleBean.getRuleId()); //获取获取启停配置详情 List actionStrategyDetail = this.getActionStrategyDetail(ruleBean.getRuleId()); //查询规则操作符 List typeIdList = new ArrayList<>(); typeIdList.add("8002"); List operatorValueList = this.mdiDicCommonDao.getDicValueList(typeIdList); StringBuilder ruleSB = new StringBuilder(); //根据设计规则详情,生成正则表达式提取数据标准值进行匹配 rulevalueType 1 的是自定义 StringBuilder pattern = new StringBuilder(); pattern.append("^"); //限制开头 List keywordList = new ArrayList<>(); String tableNameInstance = ""; //表名示例 for (StandardRuleDetailBean bean : standardRule.getRulevalueList() ) { if (1 == bean.getRulevalueType()) { pattern.append(bean.getRulevalue()); tableNameInstance = tableNameInstance + bean.getRulevalue(); } else { pattern.append("([A-Za-z0-9]+)"); keywordList.add(bean.getRulevalue()); if ("A_CLASS_COMMENT".equalsIgnoreCase(bean.getRulevalue())) { tableNameInstance += "分层名称"; } else if ("A_PATH_COMMENT".equalsIgnoreCase(bean.getRulevalue())) { tableNameInstance += "分域名称"; }else if ("A_LEXICON".equalsIgnoreCase(bean.getRulevalue())) { tableNameInstance += "词汇库"; }else if ("A_2".equalsIgnoreCase(bean.getRulevalue())) { tableNameInstance += "数据周期"; } } } pattern.append("$");//限制结尾 // 用于匹配的数据标准列表 String keywordStr = "String[] keywordList = {""; String join = String.join("","", keywordList); keywordStr += join; keywordStr += ""}"; ruleSB.append("package rulesn" + "n" + "n" + "import com.newland.edc.pub.metainfo.mdi.entity.model.*;n" + "import com.newland.edc.pub.metainfo.standard.code.model.*;n" + "import java.util.Listn" + "import java.util.Mapn" + "import java.util.regex.Matchern" + "import java.util.regex.Patternn" + "import java.lang.Stringn" + "import java.util.HashMapn" + "import org.apache.commons.lang3.StringUtilsn" + "import java.util.Arraysn" + "import com.newland.edc.pub.metainfo.util.metaConstn" + "import java.util.ArrayListn" + "n" + "global List