1.template
drools6开始提供模板的概念;
模板能为我们提供简单的规则替换;做到简单的规则动态加载;
本例子的demo基于最新稳定版drools6.4
2.项目结构
3.pom依赖
<!--drools--> <dependency> <groupId>org.drools</groupId> <artifactId>drools-core</artifactId> <version>6.4.0.Final</version> </dependency> <dependency> <groupId>org.drools</groupId> <artifactId>drools-decisiontables</artifactId> <version>6.4.0.Final</version> </dependency>
4.代码
Message.java
package com.caicongyang.drools.templates;
import java.io.Serializable;
/**
* @author caicongyang1
* @version id: Message, v 0.1 16/9/29 下午3:06 caicongyang1 Exp $$
*/
public class Message implements Serializable {
private static final long serialVersionUID = -3168739008574463191L;
public static final int HELLO = 0;
public static final int GOODBYE = 1;
private String message;
private int status;
public String getMessage() {
return this.message;
}
public void setMessage(String message) {
this.message = message;
}
public int getStatus() {
return this.status;
}
public void setStatus(int status) {
this.status = status;
}
}
MyDataProvider.java
package com.caicongyang.drools.templates;
import java.util.Iterator;
import java.util.List;
import org.drools.template.DataProvider;
/**
* @author caicongyang1
* @version id: Message, v 0.1 16/9/29 下午3:06 caicongyang1 Exp $$
*/
public class MyDataProvider implements DataProvider {
private Iterator<String[]> iterator;
public MyDataProvider(List<String[]> rows) {
this.iterator = rows.iterator();
}
public boolean hasNext() {
return iterator.hasNext();
}
public String[] next() {
return iterator.next();
}
}
DataProviderCompilerTest.java
package com.caicongyang.drools.templates;
import java.util.ArrayList;
import org.drools.template.DataProviderCompiler;
import org.kie.api.KieServices;
import org.kie.api.builder.KieBuilder;
import org.kie.api.builder.KieFileSystem;
import org.kie.api.builder.Message.Level;
import org.kie.api.builder.model.KieBaseModel;
import org.kie.api.builder.model.KieModuleModel;
import org.kie.api.io.KieResources;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
/**
*
* Drools模板实例应用
*
* @author caicongyang1
* @version id: Message, v 0.1 16/9/29 下午3:06 caicongyang1 Exp $$
*/
public class DataProviderCompilerTest {
public static void main(String[] args) {
ArrayList<String[]> rows = new ArrayList<String[]>();
rows.add(new String[] { "1", "status == 0" });
MyDataProvider tdp = new MyDataProvider(rows);
DataProviderCompiler converter = new DataProviderCompiler();
String drl = converter.compile(tdp, "/rules/rule_template_1.drl");
System.out.println(drl);
KieServices kieServices = KieServices.Factory.get();
KieResources resources = kieServices.getResources();
KieModuleModel kieModuleModel = kieServices.newKieModuleModel();//1
KieBaseModel baseModel = kieModuleModel.newKieBaseModel("FileSystemKBase").addPackage("rules");//2
baseModel.newKieSessionModel("FileSystemKSession");//3
KieFileSystem fileSystem = kieServices.newKieFileSystem();
String xml = kieModuleModel.toXML();
System.out.println(xml);//4
fileSystem.writeKModuleXML(xml);//5
String path = DataProviderCompilerTest.class.getClass().getResource("/").getPath();
fileSystem.write("src/main/resources/rules/rule1.drl", drl);
KieBuilder kb = kieServices.newKieBuilder(fileSystem);
kb.buildAll();//7
if (kb.getResults().hasMessages(Level.ERROR)) {
throw new RuntimeException("Build Errors:\n" + kb.getResults().toString());
}
KieContainer kContainer = kieServices.newKieContainer(kieServices.getRepository().getDefaultReleaseId());
KieSession kSession = kContainer.newKieSession("FileSystemKSession");
Message message = new Message();
message.setMessage("Hello World");
message.setStatus(Message.HELLO);
kSession.insert(message);
kSession.fireAllRules();
kSession.dispose();
}
}
rule_template_1.dl
template header
RULE_ID
RULE_KEY1
package com.caicongyang.drools.templates;
import com.caicongyang.drools.templates.Message;
template "RULE"
rule "RULE_@{RULE_ID}"
when
m: Message(@{RULE_KEY1})
then
System.out.println(m.getMessage());
end
end template
更多精彩内容请继续关注我的博客:http://blog.csdn.net/caicongyang
记录与分享,你我共成长 -from
caicongyang