@Service
@Slf4j
public class KieServiceImpl implements KieService {
private StatelessKieSession kieSession;
private KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
@Resource
private RiskConfigService riskConfigService;
//商品规则处理
@Resource
private ProductRiskService productRiskService;
@Override
public void loadRules(String ruleCode) {
RiskConfig riskConfig = riskConfigService.queryRiskConfig(ruleCode);
if(riskConfig!=null){
loadRule(ruleCode,riskConfig.getRiskContent());
}
}
@Override
public void removeRule(String ruleName) {
if (kbase.getRule("rules", ruleName) != null) {
kbase.removeRule("rules", ruleName);
log.info("remove rule: rules." + ruleName);
kieSession = kbase.newStatelessKieSession();
printRules();
} else {
log.error("no rule: rules." + ruleName);
}
}
@Override
public void execute(Object object) {
this.kieSession.execute(object);
}
@PostConstruct
public void init() {
loadRulesAll();
}
private void loadRulesAll() {
RiskConfig config = new RiskConfig();
List<RiskConfig> riskConfigList = riskConfigService.queryRiskConfigs(config);
if(riskConfigList!=null){
for(RiskConfig riskConfig : riskConfigList){
loadRule(riskConfig.getRiskCode(),riskConfig.getRiskContent());
}
}
}
private void loadRule(String ruleCode,String ruleContent){
try {
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newByteArrayResource(ruleContent.getBytes()), ResourceType.DRL);
if(kbuilder.hasErrors()){
log.error("加载规则模板引擎异常{},{}",ruleCode,kbuilder.getErrors());
return;
}
kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
kieSession = kbase.newStatelessKieSession();
printRules();
setGlobal();
}catch (Exception e){
log.error("加载规则模板引擎异常",e);
}
}
private void setGlobal(){
kieSession.setGlobal("productRiskService", productRiskService);
}
private void printRules() {
log.info("print rule start: ---------");
kbase.getKnowledgePackages().forEach(knowledgePackage ->
knowledgePackage.getRules().forEach(rule ->
log.info("print rule: " + knowledgePackage.getName() + "." + rule.getName())));
log.info("print rule end: ---------");
}
}
- 1001EV晨报 | “宝宝巴士”极氪MIX今日上市;花旗预计比亚迪三季度盈利超百亿,11月销量有望突破50万辆
- 1002SD-WebUI插件推荐-提示词扩展
- 1003史上最全东丽碳纤维型号与参数
- 1004艾瑞银行营销报告:把握银行营销数字化底层需求,全面实现技术与业务的深度融合
- 1005清澈的爱 只为人民——大型话剧《张富清》恩施巡演侧记
- 1006抖音号实名认证怎么找回?抖音实名认证好处有哪些?
- 1007北京四日游最佳路线 北京4天时间怎么玩 北京四天三夜旅游攻略
- 1008哪些人需要粉妍片?玫琳凯怡日健粉妍片多少钱?成份作用有什么效果?2019年1月促销活动!
- 1009什么是淘宝关键词搜索规则?搞懂淘宝的搜索规则