1.新建一个Maven项目
项目结构如图:

安装依赖:
在pom.xml文件中添加
<dependencies>
<dependency>
<groupId>org.drools</groupId>
<artifactId>drools-core</artifactId>
<version>6.5.0.Final</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-nop</artifactId>
<version>1.7.2</version>
</dependency>
<dependency>
<groupId>org.drools</groupId>
<artifactId>drools-compiler</artifactId>
<version>6.5.0.Final</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.3.2</version>
</dependency>
</dependencies>
在resources下面新建文件夹META-INF用于注册规则,同时新建文件夹demoRules用于存放规则drl文件(这里文件名自己随意定),然后再src的java目录下,建立自己的项目包和java类,这个根据个人喜好。

2.编写简单的例子
创建一个Student类,作为规则要用到的fact
package com.daigua.models;
public class Student {
private String name;
private int age;
private String gender;
public Student(String name, int age, String gender) {
this.name = name;
this.age = age;
this.gender = gender;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public String getGender() {
return gender;
}
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
public void setGender(String gender) {
this.gender = gender;
}
}
编写一个简单的规则:如果学生名字是daigua
,就打印你好呀,呆瓜!
package demoRuls
import com.daigua.models.Student
rule "student_name"
when
$s : Student(name == "daigua")
then
System.out.println("你好呀,呆瓜!");
end
注册规则:
<?xml version="1.0" encoding="UTF-8"?>
<kmodule xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.drools.org/xsd/kmodule">
<!--注册规则-->
<kbase name="Demo1KB" packages="demoRules">
<ksession name="Demo1KS"/>
</kbase>
</kmodule>
使用drools的api进行简单规则测试:
package com.daigua.demos;
import com.daigua.models.Student;
import org.drools.KnowledgeBase;
import org.drools.KnowledgeBaseConfiguration;
import org.drools.KnowledgeBaseFactory;
import org.drools.builder.*;
import org.drools.definition.KnowledgePackage;
import org.drools.io.ResourceFactory;
import org.drools.runtime.StatefulKnowledgeSession;
import java.util.Collection;
public class MyDroolsDemo1 {
public static void main(String[] args) {
// 规则编译器
KnowledgeBuilder kb = KnowledgeBuilderFactory.newKnowledgeBuilder();
// 添加规则,并编译
kb.add(ResourceFactory.newClassPathResource("demoRules/Demo1.drl"), ResourceType.DRL);
// 检查规则是否有错误
if(kb.hasErrors()){
KnowledgeBuilderErrors errors = kb.getErrors();
for (KnowledgeBuilderError error : errors) {
System.out.println("error = " + error);
}
}
// 得到规则包
Collection<KnowledgePackage> kPackage = kb.getKnowledgePackages();
System.out.println("kPackage = " + kPackage);
// kPackage = [[Package name=demoRuls], [Package name=com.daigua.models]]
// 修改属性,创建替换文件
KnowledgeBaseConfiguration kbc = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
kbc.setProperty("org.drools.sequential", "true");
System.out.println("kbc = " + kbc);
// 创建base
KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(kbc);
// 加入包
kbase.addKnowledgePackages(kPackage);
// 得到session
StatefulKnowledgeSession knowledgeSession = kbase.newStatefulKnowledgeSession();
// 创建一个global
// knowledgeSession.setGlobal("testGlobal", new Object());
// 插入fact对象
knowledgeSession.insert(new Student("daigua", 18, "male"));
knowledgeSession.fireAllRules();
knowledgeSession.dispose();
}
}
运行后输出的结果会是:

3.使用kie接口
package com.daigua.demos;
import com.daigua.models.Student;
import org.kie.api.KieServices;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
// 使用kie的api
public class MyDroolsDemo2 {
public static void main(String[] args) {
KieServices ks = KieServices.Factory.get();
KieContainer kc = ks.getKieClasspathContainer();
KieSession kSession = kc.newKieSession("Demo1KS"); // 这里对应注册文件中的ksession标签
kSession.insert(new Student("daigua", 18, "female"));
kSession.fireAllRules();
kSession.fireAllRules();
}
}
可以看到代码变得简洁了很多