zoukankan      html  css  js  c++  java
  • Springboot中Drools的配置类

    package com.example.testdrools.config;
    
    import org.kie.api.KieBase;
    import org.kie.api.KieServices;
    import org.kie.api.builder.*;
    import org.kie.api.runtime.KieContainer;
    import org.kie.api.runtime.KieSession;
    import org.kie.internal.io.ResourceFactory;
    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;
    
    @Configuration
    public class DroolsAutoConfiguration {
        private static final String RULES_PATH = "rules/";
    
        @Bean
        @ConditionalOnMissingBean(KieFileSystem.class)
        public KieFileSystem kieFileSystem() throws IOException {
            KieFileSystem kieFileSystem = getKieServices().newKieFileSystem();
            for (Resource file : getRuleFiles()) {
                kieFileSystem.write(ResourceFactory.newClassPathResource(RULES_PATH + file.getFilename(), "UTF-8"));
            }
            return kieFileSystem;
        }
    
        private Resource[] getRuleFiles() throws IOException {
            ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
            return resourcePatternResolver.getResources("classpath*:" + RULES_PATH + "**/*.*");
        }
    
        @Bean
        @ConditionalOnMissingBean(KieContainer.class)
        public KieContainer kieContainer() throws IOException {
            final KieRepository kieRepository = getKieServices().getRepository();
    
            kieRepository.addKieModule(new KieModule() {
                public ReleaseId getReleaseId() {
                    return kieRepository.getDefaultReleaseId();
                }
            });
    
            KieBuilder kieBuilder = getKieServices().newKieBuilder(kieFileSystem());
            kieBuilder.buildAll();
    
            return getKieServices().newKieContainer(kieRepository.getDefaultReleaseId());
        }
    
        private KieServices getKieServices() {
            return KieServices.Factory.get();
        }
    
        @Bean
        @ConditionalOnMissingBean(KieBase.class)
        public KieBase kieBase() throws IOException {
            return kieContainer().getKieBase();
        }
    
        @Bean
        @ConditionalOnMissingBean(KieSession.class)
        public KieSession kieSession() throws IOException {
            return kieContainer().newKieSession();
        }
    
    
    }
    
    
  • 相关阅读:
    [转]Asp.NET MVC Widget开发
    [转]让Windows Server 2008 + IIS 7+ ASP.NET 支持10万并发请求
    [转]jquery Fancybox丰富的弹出层效果
    [转]响应式网页设计:rem、em设置网页字体大小自适应
    [转]jQuery Popup Login and Contact Form
    [转][ASP.NET MVC 小牛之路]12
    [转]jQuery插件实现模拟alert和confirm
    [转]Ionic 实现双击返回键退出功能
    [转]Ionic – Mobile UI Framework for PhoneGap/Cordova Developers
    [转]VS2015 cordova尝试-camera
  • 原文地址:https://www.cnblogs.com/lyd447113735/p/14818860.html
Copyright © 2011-2022 走看看