Commit aaa5cc2dd5310fa47633879acf9f242339c5594c
1 parent
7605d4b2
update
Showing
5 changed files
with
210 additions
and
0 deletions
pom.xml
| @@ -98,7 +98,39 @@ | @@ -98,7 +98,39 @@ | ||
| 98 | <artifactId>guava</artifactId> | 98 | <artifactId>guava</artifactId> |
| 99 | <version>19.0</version> | 99 | <version>19.0</version> |
| 100 | </dependency> | 100 | </dependency> |
| 101 | + | ||
| 102 | + <!-- drools 6依赖 --> | ||
| 103 | + <dependency> | ||
| 104 | + <groupId>org.kie</groupId> | ||
| 105 | + <artifactId>kie-api</artifactId> | ||
| 106 | + </dependency> | ||
| 107 | + <dependency> | ||
| 108 | + <groupId>org.drools</groupId> | ||
| 109 | + <artifactId>drools-compiler</artifactId> | ||
| 110 | + </dependency> | ||
| 111 | + | ||
| 112 | + <!-- springboot测试 --> | ||
| 113 | + <dependency> | ||
| 114 | + <groupId>org.springframework.boot</groupId> | ||
| 115 | + <artifactId>spring-boot-starter-test</artifactId> | ||
| 116 | + <scope>test</scope> | ||
| 117 | + </dependency> | ||
| 118 | + | ||
| 101 | </dependencies> | 119 | </dependencies> |
| 120 | + | ||
| 121 | + <dependencyManagement> | ||
| 122 | + <dependencies> | ||
| 123 | + <!-- drools 6依赖 --> | ||
| 124 | + <dependency> | ||
| 125 | + <groupId>org.drools</groupId> | ||
| 126 | + <artifactId>drools-bom</artifactId> | ||
| 127 | + <type>pom</type> | ||
| 128 | + <version>6.2.0.Final</version> | ||
| 129 | + <scope>import</scope> | ||
| 130 | + </dependency> | ||
| 131 | + </dependencies> | ||
| 132 | + </dependencyManagement> | ||
| 133 | + | ||
| 102 | <build> | 134 | <build> |
| 103 | <plugins> | 135 | <plugins> |
| 104 | <plugin> | 136 | <plugin> |
src/main/java/com/bsth/service/schedule/rules/Message.java
0 → 100644
| 1 | +package com.bsth.service.schedule.rules; | ||
| 2 | + | ||
| 3 | +/** | ||
| 4 | + * Created by xu on 16/6/15. | ||
| 5 | + */ | ||
| 6 | +public class Message { | ||
| 7 | + public static final int HELLO = 0; | ||
| 8 | + public static final int GOODBYE = 1; | ||
| 9 | + | ||
| 10 | + private String message; | ||
| 11 | + private int status; | ||
| 12 | + public String getMessage() { | ||
| 13 | + return message; | ||
| 14 | + } | ||
| 15 | + public void setMessage(String message) { | ||
| 16 | + this.message = message; | ||
| 17 | + } | ||
| 18 | + public int getStatus() { | ||
| 19 | + return status; | ||
| 20 | + } | ||
| 21 | + public void setStatus(int status) { | ||
| 22 | + this.status = status; | ||
| 23 | + } | ||
| 24 | +} |
src/main/java/com/bsth/service/schedule/rules/MyDroolsConfiguration.java
0 → 100644
| 1 | +package com.bsth.service.schedule.rules; | ||
| 2 | + | ||
| 3 | +import org.kie.api.KieBase; | ||
| 4 | +import org.kie.api.KieBaseConfiguration; | ||
| 5 | +import org.kie.api.KieServices; | ||
| 6 | +import org.kie.api.builder.*; | ||
| 7 | +import org.kie.api.builder.Message; | ||
| 8 | +import org.kie.api.builder.model.KieBaseModel; | ||
| 9 | +import org.kie.api.builder.model.KieModuleModel; | ||
| 10 | +import org.kie.api.builder.model.KieSessionModel; | ||
| 11 | +import org.kie.api.conf.EqualityBehaviorOption; | ||
| 12 | +import org.kie.api.conf.EventProcessingOption; | ||
| 13 | +import org.kie.api.runtime.KieContainer; | ||
| 14 | +import org.kie.api.runtime.conf.ClockTypeOption; | ||
| 15 | +import org.springframework.context.annotation.Bean; | ||
| 16 | +import org.springframework.context.annotation.Configuration; | ||
| 17 | + | ||
| 18 | +/** | ||
| 19 | + * Drools 6配置类。 | ||
| 20 | + */ | ||
| 21 | +@Configuration | ||
| 22 | +public class MyDroolsConfiguration { | ||
| 23 | + /** | ||
| 24 | + * 返回一个kiebase知识库,直接冲文件系统读入drl规则文件, | ||
| 25 | + * TODO:以后需要从数据库读入规则文件,并重新创建kbase知识库。 | ||
| 26 | + */ | ||
| 27 | + @Bean | ||
| 28 | + public KieBase myKieBase() { | ||
| 29 | + // Drools 6开始引入kie统一接口(jboss的jbpm工作流也使用kie接口了),整个定义方式和5差别很大 | ||
| 30 | + // 这里使用全api方式创建知识库对象,不使用xml的方式,提供最大的灵活性 | ||
| 31 | + | ||
| 32 | + // 1、创建kieservices | ||
| 33 | + KieServices kieServices = KieServices.Factory.get(); | ||
| 34 | + // 2、创建KieModuleModel,默认是由kmodule.xml的方式创建,这里使用api方式闯将 | ||
| 35 | + KieModuleModel kieModuleModel = kieServices.newKieModuleModel(); | ||
| 36 | + // 2.1、创建KieBaseModel,类似kmodule.xml中的kbase标签 | ||
| 37 | + KieBaseModel kieBaseModel1 = kieModuleModel.newKieBaseModel("KBase1") | ||
| 38 | + .setDefault(true) | ||
| 39 | + .setEqualsBehavior(EqualityBehaviorOption.EQUALITY) | ||
| 40 | + .setEventProcessingMode(EventProcessingOption.STREAM); | ||
| 41 | + // 2.2、创建与kbase关联的KieSessionModel,类似kmodule.xml中的kbase内的ksession标签 | ||
| 42 | + kieBaseModel1.newKieSessionModel("KSession1") | ||
| 43 | + .setDefault(true) | ||
| 44 | + .setType(KieSessionModel.KieSessionType.STATEFUL) | ||
| 45 | + .setClockType(ClockTypeOption.get("realtime")); | ||
| 46 | + | ||
| 47 | + // 3、创建KieFileSystem,将模型xml,drl等写入,TODO:以后考虑从数据库中获取 | ||
| 48 | + KieFileSystem kfs = kieServices.newKieFileSystem(); | ||
| 49 | + // 3.1、写入KieBaseModel(内部包含了KieSessionModel的内容了,注意之前的KieSessionModel的创建方式) | ||
| 50 | + kfs.writeKModuleXML(kieModuleModel.toXML()); | ||
| 51 | + | ||
| 52 | + // 3.2、写入drl(写法超多,有点混乱) | ||
| 53 | + // 这里使用文件的形式写入,TODO:以后考虑从数据库中读drl写入 | ||
| 54 | + // 注意kfs写的时候如果指定path,强制为src/main/resources/加上文件名,还有就是文件名不要重复否则会覆盖的 | ||
| 55 | + kfs.write("src/main/resources/HelloWorld.drl", kieServices.getResources() | ||
| 56 | + .newInputStreamResource(this.getClass().getResourceAsStream( | ||
| 57 | + "/rules/HelloWorld.drl"), "UTF-8")); | ||
| 58 | + // TODO:还有其他drl.... | ||
| 59 | + | ||
| 60 | + // 4、创建KieBuilder,使用KieFileSystem构建 | ||
| 61 | + KieBuilder kieBuilder = kieServices.newKieBuilder(kfs).buildAll(); | ||
| 62 | + Results results = kieBuilder.getResults(); | ||
| 63 | + if (results.hasMessages(Message.Level.ERROR)) | ||
| 64 | + throw new IllegalStateException("构建drools6错误:" + results.getMessages()); | ||
| 65 | + | ||
| 66 | + // 5、获取KieContainer | ||
| 67 | + // TODO:ReleaseId用处很大,以后再议 | ||
| 68 | + ReleaseId releaseId = kieServices.getRepository().getDefaultReleaseId(); | ||
| 69 | + KieContainer kieContainer = kieServices.newKieContainer(releaseId); | ||
| 70 | + | ||
| 71 | + // 6、创建kbase | ||
| 72 | + KieBaseConfiguration kieBaseConfiguration = kieServices.newKieBaseConfiguration(); | ||
| 73 | + KieBase kieBase = kieContainer.newKieBase("KBase1", kieBaseConfiguration); | ||
| 74 | + | ||
| 75 | + return kieBase; | ||
| 76 | + } | ||
| 77 | +} |
src/main/resources/rules/HelloWorld.drl
0 → 100644
| 1 | +package com.bsth.service.schedule | ||
| 2 | + | ||
| 3 | +//list any import classes here. | ||
| 4 | + | ||
| 5 | +import com.bsth.service.schedule.rules.Message; | ||
| 6 | + | ||
| 7 | +//declare any global variables here | ||
| 8 | + | ||
| 9 | +global java.util.List list | ||
| 10 | + | ||
| 11 | + | ||
| 12 | +rule "Hello World" | ||
| 13 | + dialect "mvel" | ||
| 14 | + when | ||
| 15 | + $m : Message( status == Message.HELLO, $message : message ) | ||
| 16 | + then | ||
| 17 | + System.out.println($message); | ||
| 18 | + $m.status = com.bsth.service.schedule.rules.Message.GOODBYE; | ||
| 19 | + $m.message = "Goodbye cruel world"; | ||
| 20 | + update($m); | ||
| 21 | +end | ||
| 22 | + | ||
| 23 | +rule "Good bye" | ||
| 24 | + dialect "mvel" | ||
| 25 | + when | ||
| 26 | + $m : Message( status == Message.GOODBYE, $message : message ) | ||
| 27 | + then | ||
| 28 | + System.out.println($message); | ||
| 29 | + list.add("you come on!"); | ||
| 30 | +end |
src/test/java/com/bsth/service/schedule/rules/DroolsRulesTest.java
0 → 100644
| 1 | +package com.bsth.service.schedule.rules; | ||
| 2 | + | ||
| 3 | +import com.bsth.Application; | ||
| 4 | +import org.junit.Test; | ||
| 5 | +import org.junit.runner.RunWith; | ||
| 6 | +import org.kie.api.KieBase; | ||
| 7 | +import org.kie.api.runtime.KieSession; | ||
| 8 | +import org.springframework.beans.factory.annotation.Autowired; | ||
| 9 | +import org.springframework.boot.test.SpringApplicationConfiguration; | ||
| 10 | +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; | ||
| 11 | + | ||
| 12 | +import java.util.ArrayList; | ||
| 13 | +import java.util.List; | ||
| 14 | + | ||
| 15 | +@RunWith(SpringJUnit4ClassRunner.class) | ||
| 16 | +@SpringApplicationConfiguration(classes = {Application.class}) | ||
| 17 | +public class DroolsRulesTest { | ||
| 18 | + | ||
| 19 | + @Autowired | ||
| 20 | + private KieBase kieBase; | ||
| 21 | + | ||
| 22 | + @Test | ||
| 23 | + public void helloWorldDrlTest() throws Exception { | ||
| 24 | + // 1、创建session,内部配置的是stateful | ||
| 25 | + KieSession session = kieBase.newKieSession(); | ||
| 26 | + | ||
| 27 | + // 1.1 设置gloable对象,在drl中通过别名使用 | ||
| 28 | + List<String> gloableList = new ArrayList<String>(); | ||
| 29 | + session.setGlobal("list", gloableList); | ||
| 30 | + | ||
| 31 | + // 1.2 可以设置一些监听器,再议 | ||
| 32 | + | ||
| 33 | + // 2、创建fact对象 | ||
| 34 | + Message message = new Message(); | ||
| 35 | + message.setMessage("Hello World"); | ||
| 36 | + message.setStatus(Message.HELLO); | ||
| 37 | + session.insert(message); | ||
| 38 | + | ||
| 39 | + // 3、执行rule | ||
| 40 | + session.fireAllRules(); | ||
| 41 | + | ||
| 42 | + System.out.println(gloableList); | ||
| 43 | + | ||
| 44 | + // 4、执行完毕销毁,有日志的也要关闭 | ||
| 45 | + session.dispose(); | ||
| 46 | + } | ||
| 47 | +} |