时间:2021-07-01 10:21:17 帮助过:11人阅读
CREATE TABLE `drools_rule` ( `rule_id` int(11) NOT NULL AUTO_INCREMENT COMMENT ‘主键‘, `rule_uuid` varchar(64) DEFAULT NULL COMMENT ‘业务主键‘, `rule_content` longtext COMMENT ‘规则‘, PRIMARY KEY (`rule_id`) ) ENGINE=InnoDB AUTO_INCREMENT=22 DEFAULT CHARSET=utf8;
插入一条规则
INSERT INTO `drools_rule` (`rule_id`, `rule_uuid`, `rule_content`) VALUES (‘1‘, ‘repayment‘, ‘package rules\r\n\r\nimport com.utonw.finance.vo.FinanceRepaymentVo\r\nimport java.math.BigDecimal\r\n\r\nrule \"FirstInterest\"\r\n no-loop true\r\n lock-on-active true\r\n salience 1\r\n when\r\n repaymentVo : FinanceRepaymentVo(repaymentPlanVoList != null && repaymentPlanVoList.size() >= periods)\r\n then\r\n repaymentVo.addFee(\"repayInterest\",\"FirstInsterest_TotalInterestFee\");\r\n repaymentVo.addFee(\"repayManage\",\"FirstInsterest_TotalManagerFee\");\r\n repaymentVo.addFee(\"repayTotal\",\"FirstInsterest_TotalAllFee\");\r\n update(repaymentVo);\r\nend‘);
获取Drools的StatefulKnowledgeSession,然后加载规则
public class FinanceDroolsRuleServiceImpl implements FinanceDroolsRuleService { @Autowired FinanceDroolsRuleMapper droolsRuleMapper; @Override public List<FinanceDroolsRule> listRules() { return new FinanceDroolsRule().selectList(new EntityWrapper()); } @Override public KnowledgeBase getKnowledgeBase() { try { KnowledgeBuilder kb = KnowledgeBuilderFactory.newKnowledgeBuilder(); //装入规则,可以装入多个 List<FinanceDroolsRule> financeDroolsRuleList = listRules(); for (FinanceDroolsRule financeDroolsRule : financeDroolsRuleList) { kb.add(ResourceFactory.newByteArrayResource(financeDroolsRule.getRuleContent().getBytes("utf-8")), ResourceType.DRL); } KnowledgeBase kBase = KnowledgeBaseFactory.newKnowledgeBase(); kBase.addKnowledgePackages(kb.getKnowledgePackages()); return kBase; } catch (Exception e) { e.printStackTrace(); } return null; } @Override public StatefulKnowledgeSession getStatefulKnowledgeSession() { StatefulKnowledgeSession statefulKnowledgeSession = getKnowledgeBase().newStatefulKnowledgeSession(); return statefulKnowledgeSession; } }
使用规则
public class FinanceRepaymentServiceImpl implements FinanceRepaymentService { @Autowired private FinanceDroolsRuleService financeDroolsRuleService; @Override public FinanceRepaymentVo getRepayment() {try { StatefulKnowledgeSession statefulKnowledgeSession = financeDroolsRuleService.getStatefulKnowledgeSession(); statefulKnowledgeSession.insert(); statefulKnowledgeSession.fireAllRules(); statefulKnowledgeSession.dispose(); return repaymentVo; } catch (Exception e) { e.printStackTrace(); } return null; } }
规则怎么写其他博客中很多,就不写了,自行查找吧,代码已上传github
https://github.com/lwc398555797/springboot-drools.git
SpringBoot+Drools使用数据库
标签:group builder fas resource pen engine 数据 方式 区别