SpringBoot 3.2.0
JDK 17
Drools 9.44.0.Final
pom.xml 相关依赖
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>3.2.0</version> <relativePath/> <!-- lookup parent from repository --> </parent><properties> <java.version>17</java.version> <drools-version>9.44.0.Final</drools-version> </properties><dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>commons-lang</groupId> <artifactId>commons-lang</artifactId> <version>2.6</version> </dependency> <!--drools 规则引擎--> <dependency> <groupId>org.drools</groupId> <artifactId>drools-core</artifactId> <version>${drools-version}</version> </dependency> <dependency> <groupId>org.drools</groupId> <artifactId>drools-compiler</artifactId> <version>${drools-version}</version> </dependency> <dependency> <groupId>org.drools</groupId> <artifactId>drools-templates</artifactId> <version>${drools-version}</version> </dependency> <dependency> <groupId>org.kie</groupId> <artifactId>kie-api</artifactId> <version>${drools-version}</version> </dependency> <dependency> <groupId>org.drools</groupId> <artifactId>drools-decisiontables</artifactId> <version>${drools-version}</version> </dependency> <dependency> <groupId>org.drools</groupId> <artifactId>drools-xml-support</artifactId> <version>${drools-version}</version> </dependency> <dependency> <groupId>org.kie</groupId> <artifactId>kie-ci</artifactId> <version>${drools-version}</version> </dependency> <!--lombok插件--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> </dependencies>
配置文件
?
@Configuration public class DroolsConfig { // 指定规则文件存放的目录 private static final String RULES_PATH = "rules/"; private final KieServices kieServices = KieServices.Factory.get(); @Bean @ConditionalOnMissingBean public KieFileSystem kieFileSystem() throws IOException { System.setProperty("drools.dateformat", "yyyy-MM-dd"); 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(); kieFileSystem.write(ResourceFactory.newClassPathResource(path, "UTF-8")); } return kieFileSystem; } @Bean @ConditionalOnMissingBean 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 @ConditionalOnMissingBean public KieBase kieBase() throws IOException { return kieContainer().getKieBase(); } }
规则文件 calculation.drl? ?文件路径: resources/rules/calculation.drl?
?
//package calculation import com.example.calculation.entity.Calculation rule "个人所得税:计算应纳税所得额" enabled true salience 3 no-loop true date-effective "2011-09-01" //生效日期 when $cal : Calculation(wage>0) then $cal.setWagemore($cal.getWage()-3500); update($cal); end rule "个人所得税:设置税率-->>应纳税所得额<=1500" salience 2 no-loop true activation-group "SETCess_Group" when $cal : Calculation(wagemore <= 1500) then $cal.setCess(0.03); $cal.setPreminus(0); update($cal); end rule "个人所得税:设置税率-->>应纳税所得额在1500至4500之间" salience 2 no-loop true activation-group "SETCess_Group" when $cal : Calculation(wagemore > 1500 && wagemore <= 4500) then $cal.setCess(0.1); $cal.setPreminus(105); update($cal); end rule "个人所得税:设置税率-->>应纳税所得额在4500志9000之间" salience 2 no-loop true activation-group "SETCess_Group" when $cal : Calculation(wagemore > 4500 && wagemore <= 9000) then $cal.setCess(0.2); $cal.setPreminus(555); update($cal); end rule "个人所得税:设置税率-->>应纳税所得额在9000志35000之间" salience 2 no-loop true activation-group "SETCess_Group" when $cal : Calculation(wagemore > 9000 && wagemore <= 35000) then $cal.setCess(0.25); $cal.setPreminus(1005); update($cal); end rule "个人所得税:设置税率-->>应纳税所得额在35000至55000之间" salience 2 no-loop true activation-group "SETCess_Group" when $cal : Calculation(wagemore > 35000 && wagemore <= 55000) then $cal.setCess(0.3); $cal.setPreminus(2755); update($cal); end rule "个人所得税:设置税率-->>应纳税所得额在55000至80000之间" salience 2 no-loop true activation-group "SETCess_Group" when $cal : Calculation(wagemore > 55000 && wagemore <= 80000) then $cal.setCess(0.35); $cal.setPreminus(5505); update($cal); end rule "个人所得税:设置税率-->>应纳税所得额在80000以上" salience 2 no-loop true activation-group "SETCess_Group" when $cal : Calculation(wagemore > 80000) then $cal.setCess(0.45); $cal.setPreminus(13505); update($cal); end rule "个人所得税:计算税后工资" salience 1 when $cal : Calculation(wage > 0 && wagemore > 0 && wagemore > 0 && cess > 0) then $cal.setWageminus($cal.getWagemore()*$cal.getCess()-$cal.getPreminus()); $cal.setActualwage($cal.getWage()-$cal.getWageminus()); System.out.println("-----税前工资:"+$cal.getWage()); System.out.println("-----应纳税所得额:"+$cal.getWagemore()); System.out.println("-----税率:" + $cal.getCess()); System.out.println("-----速算扣除数:" + $cal.getPreminus()); System.out.println("-----扣税额:" + $cal.getWageminus()); System.out.println("-----税后工资:" + $cal.getActualwage()); end
?
使用
?
@Service public class RuleService { @Autowired private KieBase kieBase; public Calculation calculate(Calculation calculation) { KieSession kieSession = kieBase.newKieSession(); kieSession.insert(calculation); kieSession.fireAllRules(); kieSession.dispose(); return calculation; } }
验证可以使用?