目的
官方的Drools范例大都是基于纯Java项目或Maven项目,而基于Spring Boot项目的很少。
本文介绍如何在Spring Boot项目上加上Drools规则引擎。
POM依赖
POM文件如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
|
< 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.galaxyyao</ groupId > < artifactId >springbootdroolstest1</ artifactId > < version >1.0.0</ version > < packaging >jar</ packaging > < name >springbootdroolstest1</ name > < url >http://maven.apache.org</ url > < properties > < project.build.sourceEncoding >UTF-8</ project.build.sourceEncoding > < java.version >1.8</ java.version > </ properties > < parent > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-starter-parent</ artifactId > < version >1.5.2.RELEASE</ version > </ parent > < dependencies > < dependency > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-starter-web</ artifactId > </ dependency > < dependency > < groupId >org.drools</ groupId > < artifactId >drools-core</ artifactId > < version >7.0.0.Final</ version > </ dependency > < dependency > < groupId >org.drools</ groupId > < artifactId >drools-compiler</ artifactId > < version >7.0.0.Final</ version > </ dependency > < dependency > < groupId >org.drools</ groupId > < artifactId >drools-decisiontables</ artifactId > < version >7.0.0.Final</ version > </ dependency > < dependency > < groupId >org.drools</ groupId > < artifactId >drools-templates</ artifactId > < version >7.0.0.Final</ version > </ dependency > < dependency > < groupId >org.kie</ groupId > < artifactId >kie-api</ artifactId > < version >7.0.0.Final</ version > </ dependency > </ dependencies > < build > < plugins > < plugin > < artifactId >maven-compiler-plugin</ artifactId > </ plugin > < plugin > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-maven-plugin</ artifactId > </ plugin > < plugin > < groupId >org.codehaus.mojo</ groupId > < artifactId >exec-maven-plugin</ artifactId > < configuration > < executable >java</ executable > < arguments > < argument >com.galaxyyao.springbootdroolstest1.SpringBootDroolsTest1Application</ argument > </ arguments > </ configuration > </ plugin > </ plugins > </ build > </ project > |
其中比较需要注意的是使用了exec-maven-plugin插件,不然无法启动
引用的Drools包的版本可以自行调整。
Resources
在src/main/resources下添加两个目录:
META-INF和rules
META-INF下添加kmodule.xml,内容如下:
1
2
3
4
5
|
< kmodule xmlns = "http://jboss.org/kie/6.0.0/kmodule" > < kbase name = "rules" packages = "rules" > < ksession name = "ksession-rules" /> </ kbase > </ kmodule > |
rules目录下添加一个简单的Hello World规则
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
package com.galaxyyao.springbootdroolstest1 import com.galaxyyao.springbootdroolstest1.domain.Message dialect "mvel" rule "Hello World" dialect "mvel" when m : Message(status.equals(Message.HELLO), message : message ) then System.out.println( message); modify ( m ) { message = "Goodbye cruel world",status = Message.GOODBYE }; end rule "Good Bye" dialect "java" when Message( status == Message.GOODBYE, message : message ) then System.out.println( message ); end |
Domain层
Domain层定义一个Drools中需要使用到的Model:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
package com.galaxyyao.springbootdroolstest1.domain; public class Message { public static final Integer HELLO = 0 ; public static final Integer GOODBYE = 1 ; private String message; private Integer status; public String getMessage() { return this .message; } public void setMessage(String message) { this .message = message; } public Integer getStatus() { return this .status; } public void setStatus(Integer status) { this .status = status; } } |
Service层
Service层负责实际运行规则:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
package com.galaxyyao.springbootdroolstest1.service; import org.kie.api.KieServices; import org.kie.api.runtime.KieContainer; import org.kie.api.runtime.KieSession; import org.springframework.stereotype.Service; import com.galaxyyao.springbootdroolstest1.domain.Message; @Service public class DroolsService { public String fireRule() { // load up the knowledge base KieServices ks = KieServices.Factory.get(); KieContainer kContainer = ks.getKieClasspathContainer(); KieSession kSession = kContainer.newKieSession( "ksession-rules" ); // go ! Message message = new Message(); message.setMessage( "Hello World" ); message.setStatus(Message.HELLO); kSession.insert(message); //插入 kSession.fireAllRules(); //执行规则 kSession.dispose(); return message.getMessage(); } } |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:http://www.cnblogs.com/galaxyyao/p/7119930.html