drools 的一个小demo
直接上代码:
第一步,maven引入相关包
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>demo</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>demo</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.1.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <drools.version>6.1.0.Final</drools.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-webflux</artifactId> </dependency> <dependency> <groupId>org.kie</groupId> <artifactId>kie-spring</artifactId> <version>${drools.version}</version> </dependency> <dependency> <groupId>org.drools</groupId> <artifactId>drools-compiler</artifactId> <version>${drools.version}</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
第二步,编写一个规则文件:
//包名 package test import com.example.demo.drools.test.Stu; //rule 就是三大块中的规则体,以rule开头,以end 结尾,每个规则文件可以包含多个rule。规则体分为三个部分,LHS RHS 属性 三大部分 //LHS:条件部分又被称之为 LeftHand Side,简称为 LHS,在一个规则当中 when 与 then 中间的部分就是 LHS 部分。 //在 LHS 当中,可以包含 0~n 个条件,如果 LHS 部分没空的话,那么引擎会自动添加一个eval(true)的条件,由于该条件总是返回 true,所以 LHS 为空的规则总是返回 true。 //RHS:结果部分又被称之为 RightHand Side,简称为 RHS,在一个规则当中 then后面部分就是 RHS,只有在 LHS 的所有条件都满足时 RHS 部分才会执行。 rule "rule1" salience 1 when $stu:Stu(age>30)//这里就是LHS then System.out.println("age>30岁的人:"+$stu.getName());//RHS end rule "rule2" salience 2 when $stu:Stu(gender=="male",age>25) then System.out.println("性别为男,age>25岁的人:"+$stu.getName()); end rule "rule3" salience 3 when $stu:Stu(getName()=="张三") then System.out.println("名字为:"+$stu.getName()); end
第三步:测试代码
package com.example.demo.drools.test; import org.drools.core.io.impl.ClassPathResource; import org.kie.api.io.ResourceType; import org.kie.internal.KnowledgeBase; import org.kie.internal.KnowledgeBaseFactory; import org.kie.internal.builder.KnowledgeBuilder; import org.kie.internal.builder.KnowledgeBuilderFactory; import org.kie.internal.definition.KnowledgePackage; import org.kie.internal.runtime.StatefulKnowledgeSession; import java.util.ArrayList; import java.util.Collection; import java.util.List; /** * Created by ming on 2018/5/24. */ public class Test { public static void main(String[] args) { KnowledgeBuilder kb = KnowledgeBuilderFactory.newKnowledgeBuilder(); kb.add(new ClassPathResource("test.drl"), ResourceType.DRL); Collection<KnowledgePackage> collection = kb.getKnowledgePackages(); KnowledgeBase knowledgeBase = KnowledgeBaseFactory.newKnowledgeBase(); knowledgeBase.addKnowledgePackages(collection); StatefulKnowledgeSession statefulSession = knowledgeBase.newStatefulKnowledgeSession(); List<Stu> list = getStus(); for (Stu stu : list) { statefulSession.insert(stu); } statefulSession.fireAllRules(); statefulSession.dispose(); System.out.println("end...."); } public static List<Stu> getStus() { List<Stu> stus = new ArrayList<>(); stus.add(new Stu("张三", 16, "male")); stus.add(new Stu("huhu", 18, "male")); stus.add(new Stu("王五", 32, "male")); stus.add(new Stu("张红", 23, "female")); stus.add(new Stu("李四", 35, "male")); stus.add(new Stu("张小雅", 31, "female")); return stus; } }
package com.example.demo.drools.test; /** * Created by Administrator on 2018/5/24. */ public class Stu { private String name; private int age; private String gender; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Stu(String name,int age,String gender){ this.name=name; this.age=age; this.gender=gender; } }
然后看见规则文件生效了....