zoukankan      html  css  js  c++  java
  • drools入门示例

      Drools是一个基于java的规则引擎,开源的,可以将复杂多变的规则从硬编码中解放出来,以规则脚本的形式存放在文件中,使得规则的变更不需要修正代码重启机器就可以立即在线上环境生效。随着互联网金融的兴起,个人信用资质的审核等业务如果采用硬编码的方式,规则一旦变了,那么编码也会改变,采用硬编码的方式就不能适应规则的快速变化。

      下面是我学习的springboot搭建的第一个规则引擎的例子,从网上学习的,感觉很好。链接《Drools7.0.0.Final规则引擎教程》之Springboot集成,编辑好的例子已经放到本人的GitHub上,可以下载运行。

    测试类:TestController

    @RequestMapping("/test")
    @Controller
    public class TestController {
    
    	@Resource
    	private KieSession kieSession;
    
    	@ResponseBody
    	@RequestMapping("/address")
    	public void test() {
    		Address address = new Address();
    		address.setPostcode("99425");
    
    		AddressCheckResult result = new AddressCheckResult();
    		FactHandle f = kieSession.insert(address);
    		FactHandle f1 = kieSession.insert(result);
    		int ruleFiredCount = kieSession.fireAllRules();
    		System.out.println("触发了" + ruleFiredCount + "条规则");
    		if (result.isPostCodeResult()) {
    			System.out.println("规则校验通过");
    		}
    //		kieSession.delete(f);
    //		kieSession.delete(f1);
    		// 这里添加过滤器
    		 Address address1 = new Address();
    		 address1.setPostcode("2017");
    		 AddressCheckResult result1 = new AddressCheckResult();
    		 kieSession.insert(address1);
    		 kieSession.insert(result1);
    		 AddressFilter addressFilter = new AddressFilter(address1);
    		 int ruleFiredCount1 = kieSession.fireAllRules(addressFilter);
    		 System.out.println("触发了" + ruleFiredCount1 + "条规则");
    	}
    }
    

     

    springboot运行类:

    @SpringBootApplication
    @EnableAutoConfiguration(exclude = { DataSourceAutoConfiguration.class })
    public class SpringbootDrools {
    	public static void main(String[] args) {
    		SpringApplication.run(SpringbootDrools.class, args);
    
    	}
    }
    

    实体类:

    public class Address {
    	private String postcode;
    
    	private String street;
    
    	private String state;
    }
    
    public class AddressCheckResult {
    	private boolean postCodeResult = false; // true:通过校验;false:未通过校验
    
    	public boolean isPostCodeResult() {
    		return postCodeResult;
    	}
    
    	public void setPostCodeResult(boolean postCodeResult) {
    		this.postCodeResult = postCodeResult;
    	}
    }
    

    springboot自动配置的类,这个是与spring不同的地方,后面在springboot专题中专门讲如何将spring的配置文件转换到springboot

    @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();
    	}
    
    	@Bean
    	@ConditionalOnMissingBean(KModuleBeanFactoryPostProcessor.class)
    	public KModuleBeanFactoryPostProcessor kiePostProcessor() {
    		return new KModuleBeanFactoryPostProcessor();
    	}
    }  

     

    过滤器:自定义实现过滤某些规则。

    public class AddressFilter implements AgendaFilter {
    
    	// 传进来的是Address对象,把postcode以2017年开头的执行规则,否则不执行
    	private final Address address;
    
    	public AddressFilter(Address address) {
    		this.address = address;
    	}
    
    	@Override
    	public boolean accept(Match match) {
    		return match.getRule().getName().contains(address.getPostcode());
    	}
    
    }
    

    运行结果:

    规则中打印日志:校验通过!
    规则中打印日志:校验通过2017!
    触发了2条规则
    规则校验通过
    规则中打印日志:校验通过2017!
    触发了1条规则
    

      

  • 相关阅读:
    TTTTTTTTTTTTTTTTTTT UVA 2045 Richness of words
    hdu 5723 Abandoned country 最小生成树+子节点统计
    hdu 5792 World is Exploding 树状数组+离散化+容斥
    MySQL字符集详解
    MySQL的sql语言分类DML、DQL、DDL、DCL、
    MySQL5.6的4个自带库详解
    Win10下安装MySQL5.6
    {MySQL数据库初识}一 数据库概述 二 MySQL介绍 三 MySQL的下载安装、简单应用及目录介绍 四 root用户密码设置及忘记密码的解决方案 五 修改字符集编码 六 初识sql语句
    {python--GIL锁}一 介绍 二 GIL介绍 三 GIL与Lock 四 GIL与多线程 五 多线程性能测试
    Navicat安装及简单使用
  • 原文地址:https://www.cnblogs.com/boywwj/p/7883019.html
Copyright © 2011-2022 走看看